1: <?php
2:
3: /**
4: * This file contains the XML reader 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 reader class
19: *
20: * @package Core
21: * @subpackage XML
22: */
23: class cXmlReader extends cXmlBase {
24:
25: /**
26: * Loads a XML document from file and initializes a corresponding DOMXPath
27: * instance.
28: *
29: * @param string $filename
30: * path to the XML document
31: * @throws cException
32: * if file could not be loaded
33: * @return bool
34: * load state (true = successfully loaded, false = not found or loaded)
35: */
36: public function load($filename) {
37:
38: if (cFileHandler::exists($filename) === false) {
39: return false;
40: }
41:
42: // Load document via object method to avoid warning in PHP strict mode.
43: $doc = new DOMDocument();
44: if (false === $doc->load($filename)) {
45: throw new cException('Could not load file "' . $filename . '"');
46: }
47:
48:
49: $this->setDomDocument($doc);
50:
51: return $this->_dom instanceof DOMDocument;
52: }
53:
54: /**
55: * Loads a XML document from file and initializes a corresponding DOMXPath
56: * instance.
57: *
58: * @param string $sFilename
59: * path to the XML document
60: * @throws cException
61: * if XML could not be loaded
62: * @return bool
63: * load state (true = successfully loaded, false = not found or loaded)
64: */
65: public function loadXML($sXml) {
66: // Load document via object method to avoid warning in PHP strict mode.
67: $oDoc = new DOMDocument();
68: if (false === $oDoc->loadXML($sXml)) {
69: throw new cException('could not load XML');
70: }
71:
72: $this->_dom = $oDoc;
73: $this->_initXpathInstance();
74:
75: return $this->_dom instanceof DOMDocument;
76: }
77:
78: /**
79: * Returns a DOMNodeList for a given XPath expression.
80: *
81: * @param string $path
82: * xpath string
83: * @throws cException if there is no xpath
84: * @return DOMNodeList
85: */
86: public function getXpathNodeList($path) {
87: if ($this->_xpath === NULL) {
88: throw new cException('Can not execute XPath string: DOMXpath instance not found.');
89: }
90:
91: return $this->_xpath->query(parent::resolvePath($path));
92: }
93:
94: /**
95: * Returns the element of an DOMNodeList read out by a xpath string.
96: *
97: * @param string $path
98: * xpath string
99: * @param int $nodeKey [optional, default: 0]
100: * node key
101: *
102: * @return DOMNode
103: * @throws cException
104: */
105: public function getXpathNode($path, $nodeKey = 0) {
106: $path = parent::getLevelXpath($path, $nodeKey);
107:
108: $domNodeList = $this->getXpathNodeList($path);
109: return $domNodeList->item(0);
110: }
111:
112: /**
113: * Returns the value of an DOMNode read out by a xpath string.
114: *
115: * @param string $path
116: * xpath string
117: * @param int $nodeKey [optional, default: 0]
118: * node key
119: *
120: * @return string
121: * value of DOMNode
122: * @throws cException
123: */
124: public function getXpathValue($path, $nodeKey = 0) {
125:
126: $domNode = $this->getXpathNode($path, $nodeKey);
127: return $this->_decode($domNode->nodeValue);
128: }
129:
130: /**
131: * Returns the amount of nodes in a given XPath string.
132: *
133: * @param string $path
134: * XPath string
135: * @return int
136: * amount of nodes in node list
137: * @throws cException
138: */
139: public function countXpathNodes($path) {
140: $domNodeList = $this->getXpathNodeList($path);
141:
142: if (isset($domNodeList->length)) {
143: $length = (int) $domNodeList->length;
144: } else {
145: $length = 0;
146: }
147:
148: return $length;
149: }
150:
151: /**
152: * Decodes the value if XML document has not UTF-8 encoding.
153: *
154: * @param string $value
155: * value to decode
156: * @return string
157: * decoded value
158: * @throws cException
159: */
160: protected function _decode($value) {
161:
162: if ($this->getEncoding() != 'UTF-8') {
163: $value = utf8_decode($value);
164: }
165:
166: return $value;
167: }
168:
169: }
170: