File size: 5,983 Bytes
07c3cdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
/**
 * inc.dynaForms.php
 *  
 * ProcessMaker Open Source Edition
 * Copyright (C) 2004 - 2008 Colosa Inc.23
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * For more information, contact Colosa Inc, 2566 Le Jeune Rd., 
 * Coral Gables, FL, 33134, USA, or email info@colosa.com.
 * 
 */

class Node {
  var $name = "";
  var $attribute = NULL;
  var $value = NULL;
  var $children = NULL;
}

$header = "";
$tree = NULL;

function read_config_file(){
  global $home;
  $directory = PATH_DYNAFORM;
  $car = substr ($directory, strlen ($directory)-1, 1);
  if ($car=='/' ) $directory = substr( $directory, 0, strlen($directory)- 1 );
  $home = $directory;
  
  global $showlang;
  $showlang = "es,en";
}

function parseNodo1 ( $token ) {
  //parse de  <name atr=1  attr=2 ... > en un nuevo nodo.
  $nodo = new Node;
  $nodo->children = NULL;
  $tok = ""; $n = 0;
  for ($i=0; $i<strlen ($token); $i++) {
    $car = $token[$i];
    if ($car == ' ' || $car == "=") {
      if (strlen($tok) > 0 ) { $tokens [$n++] = $tok; $tok = ""; }
      if ($car == '='      ) { $tokens [$n++] = $car; }
    } 
    else{
      $tok .= $car;
    }
  }
  if (strlen($tok) > 0 ) { $tokens [$n++] = $tok; $tok = ""; }
 
  //asignar los valores para el nombre del nodo y sus attributos   
  $nodo->name = trim($tokens[0]);
  for ($i=1; $i< $n; $i++ ) {
    $aux = $tokens[$i+2];
    if ($aux[0] == $aux[strlen($aux)-1] || $aux[0] == '"') $aux = substr ($aux,1,strlen($aux)-2);
    $key = $tokens[$i]; $val = "true";
    if ($tokens[$i+1]== "=") {$val = $aux; $i+=2;}
//    $nodo->attribute = array_merge ( $nodo->attribute, array ( $key => $val ) );
    $nodo->attribute[$key] = $val;
  }
  return $nodo;
}

function getTag () {
  global $buffer;
  global $bufIndex;
  global $size;
  global $header;
  global $tree;
  $token = ""; 

  //PRIMER BUCLE buscar tag >
  while ($bufIndex <= $size && $car !='>') {
    $token .= $car;
    $car = $buffer [$bufIndex++];
  }
  if ($token[0] == "?") {
    $header = $token;
    return;
  }
  $nodo = parseNodo1 ($token); 

  //SEGUNDO BUCLE buscar VALUE
  $car = $buffer [$bufIndex++]; $token = "";
  while ($bufIndex <= $size && $car !='<') {
    $token .= $car;
    $car = $buffer [$bufIndex++];
  }
  $nodo->value = trim($token);


  //TERCER BUCLE buscar </tag>
  $res = 0;
  while ($res == 0) {
    if ($car != "<") while ( ($car = trim( $buffer [$bufIndex++] )) == "");
    if ($car=="<") {
      $res = 0;
      $car = $buffer [$bufIndex++];
      //ADDCHILD nodo recien creado
      if ($car != "/") { 
        $bufIndex --;
        $n = count($nodo->children);
        $nodo->children[$n] = getTag ();
      } else $res = 1;
    } 
    else die ("se esperaba '<' y se encontro $car");
  }

  //se supone que se ha leido "</" y continuamos con el nombre
  $token = "";
  $car = $buffer [$bufIndex++];
  while ($bufIndex <= $size && $car !='>') {
    $token .= $car;
    $car = $buffer [$bufIndex++];
  }
  if ( strcmp ($nodo->name, trim($token)) != 0) die ("no corresponde fin de tag &lt;/$token> con &lt;" . $nodo->name. ">"); 

  return $nodo;
}

function parseFile ($filename) {
  global $tree;
  global $buffer;
  global $bufIndex;
  global $size;
 
  if (strlen($filename) <= 0) die ("invalid filename");

  if ( ! file_exists ( $filename) ) die ("This file $filename does not exist");
  if ( ! is_file ( $filename) )     die ("$filename is not a file");

  $size = filesize ($filename);
  $fp = fopen ($filename, "rb");
  $i = 0;

  $aux = fread($fp, $size);
  $buffer = "";
  for ($i = 0; $i < $size; $i++) {
    if (!($aux[$i] == "\n" || $aux[$i] == "\r" || $aux[$i] == "\t")) $buffer .= $aux[$i];
  }
  fclose ($fp);
  $bufIndex = 0;
  
  $size = strlen ($buffer);

  while ( $bufIndex <= $size ) {
    $car = $buffer [$bufIndex++];
    if ($car == '<' ) 
      $tree = getTag();
  }
}



function saveXml(){
  global $tree;
  global $header;
  global $filename;
  global $curDir;
  global $onlyName;

  //crear el archivo Xml
//  print "$filename <br>";

  $fp = fopen ($filename, "w+");
  fputs ($fp,"<$header>\n");

  $aux = explode ( '/', $filename);
  $onlyName = $aux[count($aux)-1];
  $curDir = $_POST['curDir'];

  fputs ($fp,"<dynaForm name=\"$onlyName\" basedir=\"$curDir\">$tree->value\n");

  for ($i = 0; $i < count($tree->children); $i++) {
    $nodo = $tree->children[$i];

    fputs ($fp,"<$nodo->name ");
    if ( is_array ($nodo->attribute) )
      foreach ( $nodo->attribute as $attr=>$attrValue )
        fputs ($fp,"$attr=\"$attrValue\" ");
    fputs ($fp,">\n");

    //si es un dropdown-SQL la sentencia select va como valor del nodo
    if (strlen ($nodo->value) > 0) {
      fputs ($fp,"$nodo->value\n");
    }
      
    for ($j = 0; $j < count($nodo->children); $j++) {
      $lang = $nodo->children[$j];
      fputs ($fp, "  <$lang->name>$lang->value");
      
      //si tiene etiquetas del tipo option
      if (is_array($lang->children)) {
        fputs ($fp, "\n");
        for ($k = 0; $k < count($lang->children); $k++) {
          $option = $lang->children[$k];
          fputs ($fp, "<option name=\"" . $option->attribute['name'] . "\">$option->value</option>\n");
        }
      } //if tiene etiquetas del tipo opcion.
      fputs ($fp, "</$lang->name>\n");

    } //for j
    fputs ($fp,"</$nodo->name>\n");

  } //for i
  fputs ($fp,"</dynaForm>\n");
  fclose ($fp);
}

?>