Files
@ f43d1a4680a9
Branch filter:
Location: hot67beta/libraries/domit/xml_domit_utilities.php
f43d1a4680a9
10.6 KiB
text/x-php
menubar 0 to 10
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 383 384 | <?php
/**
* DOMIT Utilities are a set of utilities for the DOMIT! parser
* @package domit-xmlparser
* @copyright (C) 2004 John Heinstein. All rights reserved
* @license http://www.gnu.org/copyleft/lesser.html LGPL License
* @author John Heinstein <johnkarl@nbnet.nb.ca>
* @link http://www.engageinteractive.com/domit/ DOMIT! Home Page
* DOMIT! is Free Software
**/
/**
*@global Array Translation table for predefined XML entities
*/
$GLOBALS['DOMIT_PREDEFINED_ENTITIES'] = array('&' => '&', '<' => '<', '>' => '>',
'"' => '"', "'" => ''');
/**
* A set of utilities for the DOMIT! parser
*
* These methods are intended to be called statically
*
* @package domit-xmlparser
* @author John Heinstein <johnkarl@nbnet.nb.ca>
*/
class DOMIT_Utilities {
/**
* Raises an error if an attempt to instantiate the class is made
*/
function DOMIT_Utilities() {
die("DOMIT_Utilities Error: this is a static class that should never be instantiated.\n" .
"Please use the following syntax to access methods of this class:\n" .
'DOMIT_Utilities::methodName(parameters)');
} //DOMIT_Utilities
/**
* Generates a normalized (formatted for readability) representation of the node and its children
* @param Object The root node of the narmalization
* @param boolean True if illegal xml characters in text nodes and attributes should be converted to entities
* @return string The formatted string representation
*/
function toNormalizedString (&$node, $subEntities=false, $definedEntities) {
$node_level = 0;
$response = '';
//$node is a DOMIT_Document
if ($node->nodeType == DOMIT_DOCUMENT_NODE) {
$total = $node->childCount;
for ($i = 0; $i < $total; $i++) {
$response .= DOMIT_Utilities::getNormalizedString($node->childNodes[$i],
$node_level, $subEntities, $definedEntities);
}
return $response;
}
else {
return ($response . DOMIT_Utilities::getNormalizedString($node,
$node_level, $subEntities, $definedEntities));
}
} //toNormalizedString
/**
* Converts illegal XML characters to their entity representations
* @param string The text to be formatted
* @param array User defined translation table for entities
* @return string The formatted text
*/
function convertEntities($text, $definedEntities) {
global $DOMIT_PREDEFINED_ENTITIES;
$result = strtr($text, $DOMIT_PREDEFINED_ENTITIES);
$result = strtr($result, $definedEntities);
return $result;
} //convertEntities
/**
* Gets a normalized (formatted for readability) representation of the current node
* @param Object The node to be normalized
* @param int The level in the DOM hierarchy where the node is located
* @param boolean True if illegal xml characters in text nodes and attributes should be converted to entities
* @param array User defined translation table for entities
* @return string The normalized string representation
*/
function getNormalizedString(&$node, $node_level, $subEntities=false, $definedEntities) {
$response = '';
switch ($node->nodeType) {
case DOMIT_ELEMENT_NODE:
$response .= DOMIT_Utilities::getNormalizedElementString($node, $response,
$node_level, $subEntities, $definedEntities);
break;
case DOMIT_TEXT_NODE:
if ($node->nextSibling == null) {
$node_level--;
}
$response .= ($subEntities ?
DOMIT_Utilities::convertEntities($node->nodeValue, $definedEntities) :
$node->nodeValue);
break;
case DOMIT_CDATA_SECTION_NODE:
if ($node->nextSibling == null) {
$node_level--;
}
$response .= '<![CDATA[' . $node->nodeValue . ']]>';
break;
case DOMIT_ATTRIBUTE_NODE:
$response .= $node->toString(false, $subEntities);
break;
case DOMIT_DOCUMENT_FRAGMENT_NODE:
$total = $node->childCount;
for ($i = 0; $i < $total; $i++) {
$response .= DOMIT_Utilities::getNormalizedString($node->childNodes[$i], $node_level,
$subEntities, $definedEntities);
}
break;
case DOMIT_COMMENT_NODE:
$response .= '<!--' . $node->nodeValue . '-->';
if ($node->nextSibling == null) {
$node_level--;
}
$response .= DOMIT_Utilities::getIndentation($node_level) ;
break;
case DOMIT_PROCESSING_INSTRUCTION_NODE:
$response .= '<' . '?' . $node->nodeName . ' ' . $node->nodeValue . '?' . '>';
if ($node->nextSibling == null) {
$node_level--;
}
$response .= DOMIT_Utilities::getIndentation($node_level) ;
break;
case DOMIT_DOCUMENT_TYPE_NODE:
$response .= $node->toString() . "\n";
break;
}
return $response;
} //getNormalizedString
/**
* Gets a normalized (formatted for readability) representation of the current element
* @param Object The node to be normalized
* @param string The current normalized text
* @param int The level in the DOM hierarchy where the node is located
* @param boolean True if illegal xml characters in text nodes and attributes should be converted to entities
* @param array User defined translation table for entities
* @return string The normalized string representation
*/
function getNormalizedElementString(&$node, $response, $node_level,
$subEntities, $definedEntities) {
$response .= '<' . $node->nodeName;
//get attributes text
if (is_object($node->attributes)) { //DOMIT
$response .= $node->attributes->toString(false, $subEntities);
/*
if ($node->ownerDocument->isNamespaceAware) {
foreach ($node->namespaceURIMap as $key => $value) {
$response .= ' xmlns:' . $value . '="' . $key . '"';
}
}
*/
}
else { //DOMIT_Lite
foreach ($node->attributes as $key => $value) {
$response .= ' ' . $key . '="';
$response .= ($subEntities ? DOMIT_Utilities::convertEntities($value,
$definedEntities) : $value);
$response .= '"';
}
}
$node_level++;
//if node is childless
if ($node->childCount == 0) {
if ($node->ownerDocument->doExpandEmptyElementTags) {
if (in_array($node->nodeName, $node->ownerDocument->expandEmptyElementExceptions)) {
$response .= ' />';
}
else {
$response .= '></' . $node->nodeName . '>';
}
}
else {
if (in_array($node->nodeName, $node->ownerDocument->expandEmptyElementExceptions)) {
$response .= '></' . $node->nodeName . '>';
}
else {
$response .= ' />';
}
}
}
else {
$response .= '>';
//get children
$myNodes =& $node->childNodes;
$total = $node->childCount;
//no indentation if first child is a text node
if (!DOMIT_Utilities::isTextNode($node->firstChild)) {
$response .= DOMIT_Utilities::getIndentation($node_level);
}
for ($i = 0; $i < $total; $i++) {
$child =& $myNodes[$i];
$response .= DOMIT_Utilities::getNormalizedString($child, $node_level,
$subEntities, $definedEntities);
}
$response .= '</' . $node->nodeName . '>';
}
$node_level--;
if ($node->nextSibling == null) {
$node_level--;
$response .= DOMIT_Utilities::getIndentation($node_level);
}
else {
//no indentation if next sibling is a text node
if (!DOMIT_Utilities::isTextNode($node->nextSibling)) {
$response .= DOMIT_Utilities::getIndentation($node_level);
}
}
return $response;
} //getNormalizedElementString
/**
* Determines whether the specified node is a Text node
* @param Object The node to be tested
* @return boolean True if the node is a Text node
*/
function isTextNode(&$node) {
$type = $node->nodeType;
return (($type == DOMIT_TEXT_NODE) || ($type == DOMIT_CDATA_SECTION_NODE));
} //isTextNode
/**
* Returns the indentation required for the specified node level
* @param int The current node level
* @return string The indentation required for the specified node level
*/
function getIndentation($node_level) {
$INDENT_LEN = ' ';
$indentation = "\n";
for ($i = 0; $i < $node_level; $i++) {
$indentation .= $INDENT_LEN;
}
return $indentation;
} //getIndentation
/**
* Removes the extension from the specified file name
* @param string The file name
* @return string The file name, stripped of its extension
*/
function removeExtension($fileName) {
$total = strlen($fileName);
$index = -1;
for ($i = ($total - 1); $i >= 0; $i--) {
if ($fileName{$i} == '.') {
$index = $i;
}
}
if ($index == -1) {
return $fileName;
}
return (substr($fileName, 0, $index));
} //removeExtension
/**
* Determines whether the XML string is valid (NOT FULLY IMPLEMENTED!)
* @param string The XML text
* @return boolean True if the XML text is valid
*/
function validateXML($xmlText) {
//this does only rudimentary validation
//at this point in time
$isValid = true;
if (is_string($xmlText)) {
$text = trim($xmlText);
switch ($text) {
case '':
$isValid = false;
break;
}
}
else {
$isValid = false;
}
return $isValid;
} //validateXML
/**
* Set the browser header to interpret data as UTF-8 formatted
* @param string The content type of the data
*/
function printUTF8Header($contentType = 'text/html') {
echo header('Content-type: ' . $contentType . '; charset=utf-8');
} //printUTF8Header
/**
* Formats a string for presentation as HTML
* @param string The string to be formatted
* @param boolean True if the string is to be sent directly to output
* @return string The HTML formatted string
*/
function forHTML($text, $doPrint = false) {
if ($doPrint) {
print ('<pre>' . htmlspecialchars($text) . '</pre>');
}
else {
return ('<pre>' . htmlspecialchars($text) . '</pre>');
}
} //forHTML
/**
* Generates a node tree from an array and appends it to the specified document or node
* @param object The document or node to which the child nodes should be appended
* @param array An associative multidimensional array of elements and values
*/
function fromArray (&$node, &$myArray) {
if ($node->nodeType == DOMIT_DOCUMENT_NODE) {
$docNode =& $node;
}
else {
$docNode =& $node->ownerDocument;
}
foreach ($myArray as $key => $value) {
if (is_array($value)) {
//check for numeric indices
$total = count($value);
if (($total > 0) && isset($value[0])){
for ($i = 0; $i < $total; $i++) {
$node->appendChild($docNode->createElement($key));
DOMIT_Utilities::fromArray($node->lastChild, $value[$i]);
}
}
else {
//generate child elements
$node->appendChild($docNode->createElement($key));
DOMIT_Utilities::fromArray($node->lastChild, $value);
}
}
else {
$node->appendChild($docNode->createElement($key));
$node->lastChild->appendChild($docNode->createTextNode($value));
}
}
} //fromArray
function parseAttributes() {
} //parseAttributes
} //DOMIT_Utilities
?>
|