1: <?php
2:
3: /**
4: * This file contains the XML writer class.
5: *
6: * @package Core
7: * @subpackage XML
8: * @author Dominik Ziegler
9: * @copyright four for business AG <www.4fb.de>
10: * @license http://www.contenido.org/license/LIZENZ.txt
11: * @link http://www.4fb.de
12: * @link http://www.contenido.org
13: */
14:
15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
16:
17: /**
18: * XML writer class
19: *
20: * @package Core
21: * @subpackage XML
22: */
23: class cXmlWriter extends cXmlBase {
24:
25: /**
26: * Constructor to create an instance of this class.
27: *
28: * Creates the XML document.
29: *
30: * @param string $version [optional, default: 1.0]
31: * version of XML document
32: * @param string $encoding [optional, default: UTF-8]
33: * encoding of XML document
34: */
35: public function __construct($version = '', $encoding = '') {
36: $this->_createDocument($version, $encoding);
37: }
38:
39: /**
40: * Adds a new element to the XML document.
41: * If no root element is given the element will be appended to the root
42: * node.
43: *
44: * @param string $name
45: * name of the element
46: * @param string $value [optional]
47: * value of the element
48: * @param DOMElement $rootElement [optional]
49: * root element
50: * @param array $attributes [optional]
51: * array of attributes added to this element
52: * @param bool $cdata [optional]
53: * whether the value is surround by CDATA blocks
54: * @return DOMElement
55: * created DOM element
56: */
57: public function addElement($name, $value = '', $rootElement = NULL, $attributes = array(), $cdata = false) {
58: if ($value == '' || ($value != '' && $cdata == true)) {
59: $element = $this->_dom->createElement($name);
60: if ($value != '' && $cdata == true) {
61: $element->appendChild($this->_dom->createCDATASection($value));
62: }
63: } else {
64: $element = $this->_dom->createElement($name, $value);
65: }
66:
67: $element = $this->_addElementAttributes($element, $attributes);
68:
69: if ($rootElement === NULL) {
70: $this->_dom->appendChild($element);
71: } else {
72: $rootElement->appendChild($element);
73: }
74:
75: return $element;
76: }
77:
78: /**
79: * Adds an array of attributes to a specific DOM element.
80: *
81: * @param DOMElement $element
82: * DOM element to add attributes
83: * @param array $attributes [optional]
84: * array of attributes
85: * @return DOMElement
86: * DOM element with assigned attributes
87: */
88: protected function _addElementAttributes(DOMElement $element, array $attributes = array()) {
89: if (count($attributes) == 0) {
90: return $element;
91: }
92:
93: foreach ($attributes as $attributeName => $attributeValue) {
94: $element->setAttribute($attributeName, $attributeValue);
95: }
96:
97: return $element;
98: }
99:
100: /**
101: * Returns the complete XML tree as string.
102: *
103: * @return string
104: * XML tree
105: */
106: public function saveToString() {
107: return $this->_dom->saveXML();
108: }
109:
110: /**
111: * Saves the XML tree into a file.
112: *
113: * @param string $directory
114: * path to destination directory
115: * @param string $fileName
116: * name of the written file
117: * @throws cException
118: * if the directory is not writable
119: * @return bool
120: * state of saving process (true if file was created, false otherwise)
121: */
122: public function saveToFile($directory, $fileName) {
123: if (is_writable($directory) === false) {
124: throw new cException('Can not write XML file: Directory is not writable.');
125: }
126:
127: if (substr($directory, 0, -1) != '/') {
128: $directory = $directory . '/';
129: }
130:
131: cFileHandler::write($directory . $fileName, $this->saveToString());
132:
133: return cFileHandler::exists($directory . $fileName);
134: }
135:
136: }
137: