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 boolean $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: * @access protected
77: *
78: * @param DOMElement $element DOM element to add attributes
79: * @param array $attributes array of attributes
80: *
81: * @return DOMElement DOM element with assigned attributes
82: */
83: protected function _addElementAttributes(DOMElement $element, array $attributes = array()) {
84: if (count($attributes) == 0) {
85: return $element;
86: }
87:
88: foreach ($attributes as $attributeName => $attributeValue) {
89: $element->setAttribute($attributeName, $attributeValue);
90: }
91:
92: return $element;
93: }
94:
95: /**
96: * Returns the complete XML tree as string.
97: *
98: * @return string XML tree
99: */
100: public function saveToString() {
101: return $this->_dom->saveXML();
102: }
103:
104: /**
105: * Saves the XML tree into a file.
106: *
107: * @param string $directory path to destination directory
108: * @param string $fileName name of the written file
109: * @throws cException if the directory is not writable
110: * @return boolean state of saving process (true if file was created, false
111: * otherwise)
112: */
113: public function saveToFile($directory, $fileName) {
114: if (is_writable($directory) === false) {
115: throw new cException('Can not write XML file: Directory is not writable.');
116: }
117:
118: if (substr($directory, 0, -1) != '/') {
119: $directory = $directory . '/';
120: }
121:
122: cFileHandler::write($directory . $fileName, $this->saveToString());
123:
124: return cFileHandler::exists($directory . $fileName);
125: }
126:
127: }
128: