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