001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.xml;
016    
017    import com.liferay.portal.kernel.xml.Node;
018    import com.liferay.portal.kernel.xml.XPath;
019    
020    import java.util.List;
021    
022    /**
023     * @author Brian Wing Shun Chan
024     */
025    public class XPathImpl implements XPath {
026    
027            public XPathImpl(org.dom4j.XPath xPath) {
028                    _xPath = xPath;
029            }
030    
031            public boolean booleanValueOf(Object context) {
032                    return _xPath.booleanValueOf(toOldContext(context));
033            }
034    
035            public boolean equals(Object obj) {
036                    org.dom4j.XPath xPath = ((XPathImpl)obj).getWrappedXPath();
037    
038                    return _xPath.equals(xPath);
039            }
040    
041            public Object evaluate(Object context) {
042                    return toNewContext(_xPath.evaluate(toOldContext(context)));
043            }
044    
045            public String getText() {
046                    return _xPath.getText();
047            }
048    
049            public org.dom4j.XPath getWrappedXPath() {
050                    return _xPath;
051            }
052    
053            public int hashCode() {
054                    return _xPath.hashCode();
055            }
056    
057            public boolean matches(Node node) {
058                    NodeImpl nodeImpl = (NodeImpl)node;
059    
060                    return _xPath.matches(nodeImpl.getWrappedNode());
061            }
062    
063            public Number numberValueOf(Object context) {
064                    return _xPath.numberValueOf(toOldContext(context));
065            }
066    
067            public List<Node> selectNodes(Object context) {
068                    return SAXReaderImpl.toNewNodes(
069                            _xPath.selectNodes(toOldContext(context)));
070            }
071    
072            public List<Node> selectNodes(Object context, XPath sortXPath) {
073                    XPathImpl sortXPathImpl = (XPathImpl)sortXPath;
074    
075                    return SAXReaderImpl.toNewNodes(
076                            _xPath.selectNodes(
077                                    toOldContext(context), sortXPathImpl.getWrappedXPath()));
078            }
079    
080            public List<Node> selectNodes(
081                    Object context, XPath sortXPath, boolean distinct) {
082    
083                    XPathImpl sortXPathImpl = (XPathImpl)sortXPath;
084    
085                    return SAXReaderImpl.toNewNodes(
086                            _xPath.selectNodes(
087                                    toOldContext(context), sortXPathImpl.getWrappedXPath(),
088                                    distinct));
089            }
090    
091            public Node selectSingleNode(Object context) {
092                    org.dom4j.Node node = _xPath.selectSingleNode(toOldContext(context));
093    
094                    if (node == null) {
095                            return null;
096                    }
097                    else {
098                            return new NodeImpl(node);
099                    }
100            }
101    
102            public void sort(List<Node> nodes) {
103                    _xPath.sort(SAXReaderImpl.toOldNodes(nodes));
104            }
105    
106            public void sort(List<Node> nodes, boolean distinct) {
107                    _xPath.sort(SAXReaderImpl.toOldNodes(nodes), distinct);
108            }
109    
110            public String toString() {
111                    return _xPath.toString();
112            }
113    
114            public String valueOf(Object context) {
115                    return _xPath.valueOf(toOldContext(context));
116            }
117    
118            protected Object toNewContext(Object context) {
119                    if (context == null) {
120                            return null;
121                    }
122                    else if (context instanceof org.dom4j.Document) {
123                            org.dom4j.Document document = (org.dom4j.Document)context;
124    
125                            return new DocumentImpl(document);
126                    }
127                    else if (context instanceof org.dom4j.Node) {
128                            org.dom4j.Node node = (org.dom4j.Node)context;
129    
130                            return new NodeImpl(node);
131                    }
132                    else if (context instanceof List<?>) {
133                            return SAXReaderImpl.toNewNodes((List<org.dom4j.Node>)context);
134                    }
135                    else {
136                            return context;
137                    }
138            }
139    
140            protected Object toOldContext(Object context) {
141                    if (context == null) {
142                            return null;
143                    }
144                    else if (context instanceof DocumentImpl) {
145                            DocumentImpl documentImpl = (DocumentImpl)context;
146    
147                            return documentImpl.getWrappedDocument();
148                    }
149                    else if (context instanceof NodeImpl) {
150                            NodeImpl nodeImpl = (NodeImpl)context;
151    
152                            return nodeImpl.getWrappedNode();
153                    }
154                    else if (context instanceof List<?>) {
155                            return SAXReaderImpl.toOldNodes((List<Node>)context);
156                    }
157                    else {
158                            return context;
159                    }
160            }
161    
162            private org.dom4j.XPath _xPath;
163    
164    }