~chroot64bit/zivios/gentoo-experimental

« back to all changes in this revision

Viewing changes to application/library/Zend/Dom/Query.php

  • Committer: Mustafa A. Hashmi
  • Date: 2008-12-04 13:32:21 UTC
  • Revision ID: mhashmi@zivios.org-20081204133221-0nd1trunwevijj38
Inclusion of new installation framework with ties to zend layout and dojo layout

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Zend Framework
 
4
 *
 
5
 * LICENSE
 
6
 *
 
7
 * This source file is subject to the new BSD license that is bundled
 
8
 * with this package in the file LICENSE.txt.
 
9
 * It is also available through the world-wide-web at this URL:
 
10
 * http://framework.zend.com/license/new-bsd
 
11
 * If you did not receive a copy of the license and are unable to
 
12
 * obtain it through the world-wide-web, please send an email
 
13
 * to license@zend.com so we can send you a copy immediately.
 
14
 *
 
15
 * @category   Zend
 
16
 * @package    Zend_Dom
 
17
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
18
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 
19
 */
 
20
 
 
21
/**
 
22
 * @see Zend_Dom_Query_Css2Xpath
 
23
 */
 
24
require_once 'Zend/Dom/Query/Css2Xpath.php';
 
25
 
 
26
/**
 
27
 * @see Zend_Dom_Query_Result
 
28
 */
 
29
require_once 'Zend/Dom/Query/Result.php';
 
30
 
 
31
/**
 
32
 * Query DOM structures based on CSS selectors and/or XPath
 
33
 * 
 
34
 * @package    Zend_Dom
 
35
 * @subpackage Query
 
36
 * @copyright  Copyright (C) 2008 - Present, Zend Technologies, Inc.
 
37
 * @license    New BSD {@link http://framework.zend.com/license/new-bsd}
 
38
 */
 
39
class Zend_Dom_Query
 
40
{
 
41
    /**#@+
 
42
     * @const string Document types
 
43
     */
 
44
    const DOC_XML   = 'docXml';
 
45
    const DOC_HTML  = 'docHtml';
 
46
    const DOC_XHTML = 'docXhtml';
 
47
    /**#@-*/
 
48
 
 
49
    /**
 
50
     * @var string
 
51
     */
 
52
    protected $_document;
 
53
 
 
54
    /**
 
55
     * Document type
 
56
     * @var string
 
57
     */
 
58
    protected $_docType;
 
59
 
 
60
    /**
 
61
     * Constructor
 
62
     * 
 
63
     * @param  null|string $document 
 
64
     * @return void
 
65
     */
 
66
    public function __construct($document = null)
 
67
    {
 
68
        if (null !== $document) {
 
69
            $this->setDocument($document);
 
70
        }
 
71
    }
 
72
 
 
73
    /**
 
74
     * Set document to query
 
75
     * 
 
76
     * @param  string $document 
 
77
     * @return Zend_Dom_Query
 
78
     */
 
79
    public function setDocument($document)
 
80
    {
 
81
        if ('<?xml' == substr(trim($document), 0, 5)) {
 
82
            return $this->setDocumentXml($document);
 
83
        }
 
84
        if (strstr($document, 'DTD XHTML')) {
 
85
            return $this->setDocumentXhtml($document);
 
86
        }
 
87
        return $this->setDocumentHtml($document);
 
88
    }
 
89
 
 
90
    /**
 
91
     * Register HTML document 
 
92
     * 
 
93
     * @param  string $document 
 
94
     * @return Zend_Dom_Query
 
95
     */
 
96
    public function setDocumentHtml($document)
 
97
    {
 
98
        $this->_document = (string) $document;
 
99
        $this->_docType  = self::DOC_HTML;
 
100
        return $this;
 
101
    }
 
102
 
 
103
    /**
 
104
     * Register XHTML document
 
105
     * 
 
106
     * @param  string $document 
 
107
     * @return Zend_Dom_Query
 
108
     */
 
109
    public function setDocumentXhtml($document)
 
110
    {
 
111
        $this->_document = (string) $document;
 
112
        $this->_docType  = self::DOC_XHTML;
 
113
        return $this;
 
114
    }
 
115
 
 
116
    /**
 
117
     * Register XML document
 
118
     * 
 
119
     * @param  string $document 
 
120
     * @return Zend_Dom_Query
 
121
     */
 
122
    public function setDocumentXml($document)
 
123
    {
 
124
        $this->_document = (string) $document;
 
125
        $this->_docType  = self::DOC_XML;
 
126
        return $this;
 
127
    }
 
128
 
 
129
    /**
 
130
     * Retrieve current document
 
131
     * 
 
132
     * @return string
 
133
     */
 
134
    public function getDocument()
 
135
    {
 
136
        return $this->_document;
 
137
    }
 
138
 
 
139
    /**
 
140
     * Get document type
 
141
     * 
 
142
     * @return string
 
143
     */
 
144
    public function getDocumentType()
 
145
    {
 
146
        return $this->_docType;
 
147
    }
 
148
 
 
149
    /**
 
150
     * Perform a CSS selector query
 
151
     * 
 
152
     * @param  string $query 
 
153
     * @return Zend_Dom_Query_Result
 
154
     */
 
155
    public function query($query)
 
156
    {
 
157
        $xpathQuery = Zend_Dom_Query_Css2Xpath::transform($query);
 
158
        return $this->queryXpath($xpathQuery, $query);
 
159
    }
 
160
 
 
161
    /**
 
162
     * Perform an XPath query
 
163
     * 
 
164
     * @param  string $xpathQuery
 
165
     * @param  string $query CSS selector query
 
166
     * @return Zend_Dom_Query_Result
 
167
     */
 
168
    public function queryXpath($xpathQuery, $query = null)
 
169
    {
 
170
        if (null === ($document = $this->getDocument())) {
 
171
            require_once 'Zend/Dom/Exception.php';
 
172
            throw new Zend_Dom_Exception('Cannot query; no document registered');
 
173
        }
 
174
 
 
175
        $domDoc = new DOMDocument;
 
176
        $type   = $this->getDocumentType();
 
177
        switch ($type) {
 
178
            case self::DOC_XML:
 
179
                $success = @$domDoc->loadXML($document);
 
180
                break;
 
181
            case self::DOC_HTML:
 
182
            case self::DOC_XHTML:
 
183
            default:
 
184
                $success = @$domDoc->loadHTML($document);
 
185
                break;
 
186
        }
 
187
 
 
188
        if (!$success) {
 
189
            require_once 'Zend/Dom/Exception.php';
 
190
            throw new Zend_Dom_Exception(sprintf('Error parsing document (type == %s)', $type));
 
191
        }
 
192
 
 
193
        $nodeList   = $this->_getNodeList($domDoc, $xpathQuery);
 
194
        return new Zend_Dom_Query_Result($query, $xpathQuery, $domDoc, $nodeList);
 
195
    }
 
196
 
 
197
    /**
 
198
     * Prepare node list
 
199
     * 
 
200
     * @param  DOMDocument $document
 
201
     * @param  string|array $xpathQuery
 
202
     * @return array
 
203
     */
 
204
    protected function _getNodeList($document, $xpathQuery)
 
205
    {
 
206
        $xpath      = new DOMXPath($document);
 
207
        $xpathQuery = (string) $xpathQuery;
 
208
        if (preg_match_all('|\[contains\((@[a-z0-9_-]+),\s?\' |i', $xpathQuery, $matches)) {
 
209
            foreach ($matches[1] as $attribute) {
 
210
                $queryString = '//*[' . $attribute . ']';
 
211
                $attributeName = substr($attribute, 1);
 
212
                $nodes = $xpath->query($queryString);
 
213
                foreach ($nodes as $node) {
 
214
                    $attr = $node->attributes->getNamedItem($attributeName);
 
215
                    $attr->value = ' ' . $attr->value . ' ';
 
216
                }
 
217
            }
 
218
        }
 
219
        return $xpath->query($xpathQuery);
 
220
    }
 
221
}