baseline
[omp.pkp.sfu.ca.git] / lib / pkp / classes / xml / XMLCustomWriter.inc.php
blob52d4c4e4b000f36f3764bd76969fbb0d779b3bdb
1 <?php
3 /**
4 * @file classes/xml/XMLCustomWriter.inc.php
6 * Copyright (c) 2000-2009 John Willinsky
7 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
9 * @class XMLCustomWriter
10 * @ingroup xml
12 * @brief Wrapper class for writing XML documents using PHP 4.x or 5.x
15 // $Id: XMLCustomWriter.inc.php,v 1.5 2009/09/23 00:42:47 asmecher Exp $
18 import ('xml.XMLNode');
20 class XMLCustomWriter {
21 /**
22 * Create a new XML document.
23 * If $url is set, the DOCTYPE definition is treated as a PUBLIC
24 * definition; $dtd should contain the ID, and $url should contain the
25 * URL. Otherwise, $dtd should be the DTD name.
27 function &createDocument($type = null, $dtd = null, $url = null) {
28 $version = '1.0';
29 if (class_exists('DOMImplementation')) {
30 // Use the new (PHP 5.x) DOM
31 $impl = new DOMImplementation();
32 // only generate a DOCTYPE if type is non-empty
33 if ($type != '') {
34 $domdtd = $impl->createDocumentType($type, isset($url)?$dtd:'', isset($url)?$url:$dtd);
35 $doc = $impl->createDocument($version, '', $domdtd);
36 } else {
37 $doc = $impl->createDocument($version, '');
39 // ensure we are outputting UTF-8
40 $doc->encoding = 'UTF-8';
41 } else {
42 // Use the XMLNode class
43 $doc = new XMLNode();
44 $doc->setAttribute('version', $version);
45 if ($type !== null) $doc->setAttribute('type', $type);
46 if ($dtd !== null) $doc->setAttribute('dtd', $dtd);
47 if ($url !== null) $doc->setAttribute('url', $url);
49 return $doc;
52 function &createElement(&$doc, $name) {
53 if (is_callable(array($doc, 'createElement'))) $element =& $doc->createElement($name);
54 else $element = new XMLNode($name);
56 return $element;
59 function &createTextNode(&$doc, $value) {
61 $value = Core::cleanVar($value);
63 if (is_callable(array($doc, 'createTextNode'))) $element =& $doc->createTextNode($value);
64 else {
65 $element = new XMLNode();
66 $element->setValue($value);
69 return $element;
72 function &appendChild(&$parentNode, &$child) {
73 if (is_callable(array($parentNode, 'appendChild'))) $node =& $parentNode->appendChild($child);
74 else {
75 $parentNode->addChild($child);
76 $child->setParent($parentNode);
77 $node =& $child;
80 return $node;
83 function &getAttribute(&$node, $name) {
84 return $node->getAttribute($name);
87 function &hasAttribute(&$node, $name) {
88 if (is_callable(array($node, 'hasAttribute'))) $value =& $node->hasAttribute($name);
89 else {
90 $attribute =& XMLCustomWriter::getAttribute($node, $name);
91 $value = ($attribute !== null);
93 return $value;
96 function setAttribute(&$node, $name, $value, $appendIfEmpty = true) {
97 if (!$appendIfEmpty && $value == '') return;
98 return $node->setAttribute($name, $value);
101 function &getXML(&$doc) {
102 if (is_callable(array($doc, 'saveXML'))) $xml =& $doc->saveXML();
103 else {
104 $xml = $doc->toXml();
106 return $xml;
109 function printXML(&$doc) {
110 if (is_callable(array($doc, 'saveXML'))) echo $doc->saveXML();
111 else $doc->toXml(true);
114 function &createChildWithText(&$doc, &$node, $name, $value, $appendIfEmpty = true) {
115 $childNode = null;
116 if ($appendIfEmpty || $value != '') {
117 $childNode =& XMLCustomWriter::createElement($doc, $name);
118 $textNode =& XMLCustomWriter::createTextNode($doc, $value);
119 XMLCustomWriter::appendChild($childNode, $textNode);
120 XMLCustomWriter::appendChild($node, $childNode);
122 return $childNode;
125 function &createChildFromFile(&$doc, &$node, $name, $filename) {
126 $contents =& FileManager::readFile($filename);
127 if ($contents === false) return null;