001
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 import com.liferay.portal.xml.xpath.LiferayFunctionContext;
020 import com.liferay.portal.xml.xpath.LiferayNamespaceContext;
021
022 import java.util.List;
023 import java.util.Map;
024
025 import org.jaxen.FunctionContext;
026 import org.jaxen.NamespaceContext;
027
028
031 public class XPathImpl implements XPath {
032
033 public XPathImpl(
034 org.dom4j.XPath xPath, Map<String, String> namespaceContextMap) {
035
036 _xPath = xPath;
037
038 _xPath.setFunctionContext(_functionContext);
039
040 NamespaceContext namespaceContext = new LiferayNamespaceContext(
041 namespaceContextMap);
042
043 _xPath.setNamespaceContext(namespaceContext);
044 }
045
046 @Override
047 public boolean booleanValueOf(Object context) {
048 return _xPath.booleanValueOf(toOldContext(context));
049 }
050
051 @Override
052 public boolean equals(Object obj) {
053 if (this == obj) {
054 return true;
055 }
056
057 if (!(obj instanceof XPathImpl)) {
058 return false;
059 }
060
061 org.dom4j.XPath xPath = ((XPathImpl)obj).getWrappedXPath();
062
063 return _xPath.equals(xPath);
064 }
065
066 @Override
067 public Object evaluate(Object context) {
068 return toNewContext(_xPath.evaluate(toOldContext(context)));
069 }
070
071 @Override
072 public String getText() {
073 return _xPath.getText();
074 }
075
076 public org.dom4j.XPath getWrappedXPath() {
077 return _xPath;
078 }
079
080 @Override
081 public int hashCode() {
082 return _xPath.hashCode();
083 }
084
085 @Override
086 public boolean matches(Node node) {
087 NodeImpl nodeImpl = (NodeImpl)node;
088
089 return _xPath.matches(nodeImpl.getWrappedNode());
090 }
091
092 @Override
093 public Number numberValueOf(Object context) {
094 return _xPath.numberValueOf(toOldContext(context));
095 }
096
097 @Override
098 public List<Node> selectNodes(Object context) {
099 return SAXReaderImpl.toNewNodes(
100 _xPath.selectNodes(toOldContext(context)));
101 }
102
103 @Override
104 public List<Node> selectNodes(Object context, XPath sortXPath) {
105 XPathImpl sortXPathImpl = (XPathImpl)sortXPath;
106
107 return SAXReaderImpl.toNewNodes(
108 _xPath.selectNodes(
109 toOldContext(context), sortXPathImpl.getWrappedXPath()));
110 }
111
112 @Override
113 public List<Node> selectNodes(
114 Object context, XPath sortXPath, boolean distinct) {
115
116 XPathImpl sortXPathImpl = (XPathImpl)sortXPath;
117
118 return SAXReaderImpl.toNewNodes(
119 _xPath.selectNodes(
120 toOldContext(context), sortXPathImpl.getWrappedXPath(),
121 distinct));
122 }
123
124 @Override
125 public Node selectSingleNode(Object context) {
126 org.dom4j.Node node = _xPath.selectSingleNode(toOldContext(context));
127
128 if (node == null) {
129 return null;
130 }
131 else if (node instanceof org.dom4j.Element) {
132 return new ElementImpl((org.dom4j.Element)node);
133 }
134 else {
135 return new NodeImpl(node);
136 }
137 }
138
139 @Override
140 public void sort(List<Node> nodes) {
141 _xPath.sort(SAXReaderImpl.toOldNodes(nodes));
142 }
143
144 @Override
145 public void sort(List<Node> nodes, boolean distinct) {
146 _xPath.sort(SAXReaderImpl.toOldNodes(nodes), distinct);
147 }
148
149 @Override
150 public String toString() {
151 return _xPath.toString();
152 }
153
154 @Override
155 public String valueOf(Object context) {
156 return _xPath.valueOf(toOldContext(context));
157 }
158
159 protected Object toNewContext(Object context) {
160 if (context == null) {
161 return null;
162 }
163 else if (context instanceof org.dom4j.Document) {
164 org.dom4j.Document document = (org.dom4j.Document)context;
165
166 return new DocumentImpl(document);
167 }
168 else if (context instanceof org.dom4j.Node) {
169 org.dom4j.Node node = (org.dom4j.Node)context;
170
171 return new NodeImpl(node);
172 }
173 else if (context instanceof List<?>) {
174 return SAXReaderImpl.toNewNodes((List<org.dom4j.Node>)context);
175 }
176 else {
177 return context;
178 }
179 }
180
181 protected Object toOldContext(Object context) {
182 if (context == null) {
183 return null;
184 }
185 else if (context instanceof DocumentImpl) {
186 DocumentImpl documentImpl = (DocumentImpl)context;
187
188 return documentImpl.getWrappedDocument();
189 }
190 else if (context instanceof NodeImpl) {
191 NodeImpl nodeImpl = (NodeImpl)context;
192
193 return nodeImpl.getWrappedNode();
194 }
195 else if (context instanceof List<?>) {
196 return SAXReaderImpl.toOldNodes((List<Node>)context);
197 }
198 else {
199 return context;
200 }
201 }
202
203 private static FunctionContext _functionContext =
204 new LiferayFunctionContext();
205
206 private org.dom4j.XPath _xPath;
207
208 }