Spaces:
Sleeping
Sleeping
File size: 12,012 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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 | <?php
/**
* class.xmlDocument.php
*
* @package gulliver.system
*
* ProcessMaker Open Source Edition
* Copyright (C) 2004 - 2011 Colosa Inc.
*
* 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 Xml_Node
*
* @author David S. Callizaya S. <davidsantos@colosa.com>
* @package gulliver.system
* @access public
*/
class Xml_Node
{
public $name = '';
public $type = '';
public $value = ''; //maybe not necesary
public $attributes = array ();
public $children = array ();
/**
* Function Xml_Node
*
* @author David S. Callizaya S. <davidsantos@colosa.com>
* @access public
* @param eter string name
* @param eter string type
* @param eter string value
* @param eter string attributes
* @return string
*/
public function Xml_Node ($name, $type, $value, $attributes = array())
{
$this->name = $name;
$this->type = $type;
$this->value = $value;
$this->attributes = $attributes;
}
/**
* Function addAttribute
*
* @author David S. Callizaya S. <davidsantos@colosa.com>
* @access public
* @param eter string name
* @param eter string value
* @return string
*/
public function addAttribute ($name, $value)
{
$this->attributes[$name] = $value;
return true;
}
/**
* Function addChildNode
*
* @author David S. Callizaya S. <davidsantos@colosa.com>
* @access public
* @param eter string childNode
* @return string
*/
public function addChildNode ($childNode)
{
if (is_object( $childNode ) && strcasecmp( get_class( $childNode ), 'Xml_Node' ) == 0) {
$this->type = 'open';
$childNode->parent = &$this;
$this->children[] = &$childNode;
return true;
} else {
return false;
}
}
/**
* Function toTree
*
* @author David S. Callizaya S. <davidsantos@colosa.com>
* @access public
* @return string
*/
public function toTree ()
{
$arr = new Xml_Node( $this->name, $this->type, $this->value, $this->attributes );
unset( $arr->parent );
foreach ($this->children as $k => $v) {
unset( $v->parent );
$arr->children[$k] = $v->toTree();
}
return $arr;
}
public function toArray ($obj = null)
{
$arr = array ();
if (! isset( $obj )) {
$obj = $this->toTree();
}
foreach ($obj as $att => $val) {
if (is_array( $val ) || is_object( $val )) {
$arr[$att] = Xml_Node::toArray( $val );
} else {
$arr[$att] = $val;
}
}
return $arr;
}
/**
* Function &findNode
*
* @author David S. Callizaya S. <davidsantos@colosa.com>
* @access public
* @param eter string xpath
* @return string
*/
public function &findNode ($xpath)
{
$n = null;
$p = explode( '/', $xpath );
if ($p[0] === '') {
return $this;
} elseif (substr( $p[0], 0, 1 ) === '@') {
$p[0] = substr( $p[0], 1 );
if (isset( $this->attributes[$p[0]] )) {
return $this->attributes[$p[0]];
}
} elseif ($p[0] === '..') {
array_shift( $p );
$n = & $this->parent->findNode( implode( '/', $p ) );
if (isset( $n )) {
return $n;
}
} else {
foreach ($this->children as $k => $v) {
if (($v->type !== 'cdata') && ($v->name === $p[0])) {
if (sizeof( $p ) > 1) {
array_shift( $p );
$n = & $this->children[$k]->findNode( implode( '/', $p ) );
if (isset( $n )) {
return $n;
}
} else {
return $this->children[$k];
}
}
}
}
return $n;
}
/**
* Function getXML
* Returns a string of the node in XML notation.
*
* @author David S. Callizaya S. <davidsantos@colosa.com>
* @access public
* @param eter string xpath
* @return string
*/
public function getXML ()
{
switch ($this->type) {
case 'open':
$xml = '<' . $this->name;
foreach ($this->attributes as $attib => $value) {
$value = htmlspecialchars( $value, ENT_QUOTES, 'utf-8' );
/*if(($attib == "hint")||($attib == "defaultvalue")){
$value = str_replace("&", "&", $value);
$value = str_replace("'", "\'", $value);
$value = str_replace(">", ">", $value);
$value = str_replace("<", "<", $value);
}
//else{
$value = htmlentities( $value, ENT_QUOTES, 'utf-8' );
//}
*/
$xml .= ' ' . $attib . '="' . $value . '"';
//check if the htmlentities result value is the euro symbol and
//replaced by their numeric character representation
if (strpos( $xml, '€' ) !== false) {
$xml = str_replace( '€', '€', $xml );
}
}
$xml .= '>' . $this->getCDATAValue();
foreach ($this->children as $child) {
$xml .= $child->getXML();
}
$xml .= '</' . $this->name . '>';
break;
case 'close':
$xml = '</' . $this->name . '>';
break;
case 'cdata':
$xml = $this->getCDATAValue();
break;
case 'complete':
$xml = '<' . $this->name;
foreach ($this->attributes as $attib => $value) {
$xml .= ' ' . $attib . '="' . htmlentities( $value, ENT_QUOTES, 'utf-8' ) . '"';
//check if the htmlentities result value is the euro symbol and
//replaced by their numeric character representation
if (strpos( $xml, '€' ) !== false) {
$xml = str_replace( '€', '€', $xml );
}
}
if ($this->value !== '') {
$xml .= '>' . $this->getCDATAValue();
$xml .= '</' . $this->name . '>';
} else {
$xml .= '/>';
}
break;
}
return $xml;
}
public function getCDATAValue ()
{
$cdata = htmlentities( $this->value, ENT_QUOTES, 'utf-8' );
if ($this->value === $cdata) {
return $this->value;
} else {
return '<![CDATA[' . $this->value . ']]>';
}
}
}
/**
* Class Xml_document
*
* @author David S. Callizaya S. <davidsantos@colosa.com>
* @package gulliver.system
* @access public
*/
class Xml_Document extends Xml_Node
{
public $currentNode;
/**
* Function Xml_document
*
* @author David S. Callizaya S. <davidsantos@colosa.com>
* @access public
* @return string
*/
public function Xml_Document ()
{
$this->currentNode = &$this;
}
/**
* Function parseXmlFile
*
* @author David S. Callizaya S. <davidsantos@colosa.com>
* @access public
* @param eter string filename
* @param eter string content
* @return string
*/
public function parseXmlFile ($filename, $content = "")
{ //$content is a new variable, if it has any value then use it instead of the file content.
if ($content == "") {
if (! file_exists( $filename )) {
throw (new Exception( "failed to open Xmlform File : No such file or directory in ".htmlspecialchars($filename) ));
}
$data = implode( '', file( $filename ) );
} else {
$data = $content;
}
$parser = xml_parser_create( 'utf-8' );
xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, 0 );
xml_parser_set_option( $parser, XML_OPTION_SKIP_WHITE, 0 );
xml_parse_into_struct( $parser, $data, $values, $tags );
if (xml_get_error_code( $parser ) !== 0) {
$msg = sprintf( "XML error in <b>%s</b>: %s at line %d", $filename, xml_error_string( xml_get_error_code( $parser ) ), xml_get_current_line_number( $parser ) );
trigger_error( $msg );
}
xml_parser_free( $parser );
$this->name = '#document';
$this->type = 'open';
$this->currentNode = &$this;
$m = &$this;
foreach ($values as $k => $v) {
switch ($v['type']) {
case 'open':
$this->currentNode->addChildNode( new Xml_Node( $v['tag'], $v['type'], isset( $v['value'] ) ? $v['value'] : '', isset( $v['attributes'] ) ? $v['attributes'] : array () ) );
$this->currentNode = &$this->findNode( $v['tag'] );
break;
case 'close':
$this->currentNode = & $this->findNode( '..' );
break;
case 'cdata':
$this->currentNode->addChildNode( new Xml_Node( '', $v['type'], isset( $v['value'] ) ? $v['value'] : '' ) );
break;
case 'complete':
$this->currentNode->addChildNode( new Xml_Node( $v['tag'], $v['type'], isset( $v['value'] ) ? $v['value'] : '', isset( $v['attributes'] ) ? $v['attributes'] : array () ) );
break;
}
}
return true;
}
/**
* Function &findNode
*
* @author David S. Callizaya S. <davidsantos@colosa.com>
* @access public
* @param eter string xpath
* @return string
*/
public function &findNode ($xpath)
{
if (substr( $xpath, 0, 1 ) == '/') {
return parent::findNode( substr( $xpath, 1 ) );
} else {
if (isset( $this->currentNode )) {
if ($this->currentNode->name === $this->name) {
return parent::findNode( $xpath );
} else {
return $this->currentNode->findNode( $xpath );
}
} else {
return $null;
}
}
} //function findNode
/**
* Function getXML
*
* @access public
* @return string $xml
*/
public function getXML ()
{
$xml = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
$xml .= $this->children[0]->getXML();
return $xml;
}
/**
* Function save
*
* @access public
* @return void
*/
public function save ($filename)
{
$xml = $this->getXML();
$fp = fopen( $filename, 'w' );
fwrite( $fp, $xml );
fclose( $fp );
}
}
|