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