001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Nam Nguyen
028: */
029:
030: package com.caucho.quercus.lib.dom;
031:
032: import java.util.HashMap;
033: import java.util.Iterator;
034: import java.util.LinkedHashSet;
035: import java.util.Map;
036:
037: import javax.xml.namespace.NamespaceContext;
038: import javax.xml.xpath.XPath;
039: import javax.xml.xpath.XPathConstants;
040: import javax.xml.xpath.XPathExpression;
041: import javax.xml.xpath.XPathExpressionException;
042: import javax.xml.xpath.XPathFactory;
043:
044: import org.w3c.dom.Node;
045: import org.w3c.dom.NodeList;
046:
047: import com.caucho.quercus.QuercusModuleException;
048: import com.caucho.quercus.annotation.Optional;
049: import com.caucho.quercus.env.Env;
050:
051: public class DOMXPath {
052: private XPath _xpath;
053: private XPathExpression _compiledXPath;
054: private String _expression;
055: private DOMNamespaceContext _context;
056:
057: private DOMDocument _document;
058:
059: public static DOMXPath __construct(Env env, DOMDocument document) {
060: return new DOMXPath(document);
061: }
062:
063: private DOMXPath(DOMDocument document) {
064: _document = document;
065:
066: _context = new DOMNamespaceContext();
067:
068: _xpath = XPathFactory.newInstance().newXPath();
069: _xpath.setNamespaceContext(_context);
070: }
071:
072: public Object evaluate(Env env, String expression) {
073: Node node = _document.getDelegate();
074:
075: NodeList nodeList = (NodeList) query(env, expression, node);
076:
077: if (nodeList.getLength() == 1) {
078: return _document.wrap(nodeList.item(0));
079: } else
080: return _document.wrap(nodeList);
081:
082: }
083:
084: public DOMNodeList query(Env env, String expression, @Optional
085: DOMNode<Node> contextNode) {
086: Node node;
087:
088: if (contextNode != null)
089: node = contextNode.getDelegate();
090: else
091: node = _document.getDelegate();
092:
093: NodeList nodeList = (NodeList) query(env, expression, node);
094:
095: return _document.wrap(nodeList);
096: }
097:
098: private NodeList query(Env env, String expression, Node node) {
099: try {
100: compile(expression);
101:
102: NodeList nodeList = (NodeList) _compiledXPath.evaluate(
103: node, XPathConstants.NODESET);
104:
105: return nodeList;
106: } catch (XPathExpressionException e) {
107: throw new QuercusModuleException(e);
108: }
109: }
110:
111: private void compile(String expression) {
112: try {
113: if (_compiledXPath == null
114: || !expression.equals(_expression)) {
115: _compiledXPath = _xpath.compile(expression);
116: _expression = expression;
117: }
118: } catch (XPathExpressionException e) {
119: throw new QuercusModuleException(e);
120: }
121: }
122:
123: public boolean registerNamespace(String prefix, String namespaceURI) {
124: _context.addNamespace(prefix, namespaceURI);
125:
126: return true;
127: }
128:
129: public class DOMNamespaceContext implements NamespaceContext {
130: private HashMap<String, LinkedHashSet<String>> _namespaceMap = new HashMap<String, LinkedHashSet<String>>();
131:
132: protected void addNamespace(String prefix, String namespaceURI) {
133: LinkedHashSet<String> list = _namespaceMap
134: .get(namespaceURI);
135:
136: if (list == null) {
137: list = new LinkedHashSet<String>();
138:
139: _namespaceMap.put(namespaceURI, list);
140: }
141:
142: list.add(prefix);
143: }
144:
145: public String getNamespaceURI(String prefix) {
146: for (Map.Entry<String, LinkedHashSet<String>> entry : _namespaceMap
147: .entrySet()) {
148: if (entry.getValue().contains(prefix))
149: return entry.getKey();
150: }
151:
152: return null;
153: }
154:
155: public String getPrefix(String namespaceURI) {
156: Iterator<String> iter = getPrefixes(namespaceURI);
157:
158: if (iter != null)
159: return iter.next();
160: else
161: return null;
162: }
163:
164: public Iterator<String> getPrefixes(String namespaceURI) {
165: LinkedHashSet<String> prefixList = _namespaceMap
166: .get(namespaceURI);
167:
168: if (prefixList != null)
169: return prefixList.iterator();
170: else
171: return null;
172: }
173: }
174: }
|