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