View Javadoc

1   /*
2    * Copyright 2010 Capgemini
3    * Licensed under the Apache License, Version 2.0 (the "License"); 
4    * you may not use this file except in compliance with the License. 
5    * You may obtain a copy of the License at 
6    * 
7    * http://www.apache.org/licenses/LICENSE-2.0 
8    * 
9    * Unless required by applicable law or agreed to in writing, software 
10   * distributed under the License is distributed on an "AS IS" BASIS, 
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
12   * See the License for the specific language governing permissions and 
13   * limitations under the License. 
14   * 
15   */
16  package org.xmlfield.core.impl.dom;
17  
18  import java.util.ArrayList;
19  import java.util.Iterator;
20  import java.util.List;
21  import java.util.Map;
22  
23  import javax.xml.namespace.NamespaceContext;
24  import javax.xml.xpath.XPath;
25  import javax.xml.xpath.XPathConstants;
26  import javax.xml.xpath.XPathExpressionException;
27  import javax.xml.xpath.XPathFactory;
28  
29  import org.w3c.dom.Node;
30  import org.w3c.dom.NodeList;
31  import org.xmlfield.core.api.XmlFieldNode;
32  import org.xmlfield.core.api.XmlFieldNodeList;
33  import org.xmlfield.core.api.XmlFieldSelector;
34  import org.xmlfield.core.exception.XmlFieldXPathException;
35  import org.xmlfield.core.internal.NamespaceMap;
36  
37  /**
38   * Default xml field selector implementation. Use the jaxp implementation.
39   * 
40   * @author Guillaume Mary <guillaume.mary@capgemini.com>
41   * 
42   */
43  @Deprecated
44  public class DomXalanSelector implements XmlFieldSelector {
45  	private static final ThreadLocal<XPathFactory> xPathFactory = new ThreadLocal<XPathFactory>() {
46  
47  		@Override
48  		protected XPathFactory initialValue() {
49  			return XPathFactory.newInstance();
50  		}
51  
52  	};
53  
54  	public static XPath getXPath(final NamespaceMap namespaces) {
55  
56  		final XPath xpath = getXPathFactory().newXPath();
57  
58  		if (namespaces != null) {
59  
60  			final Map<String, String> prefixesURIs = namespaces
61  					.getPrefixesURIs();
62  
63  			final NamespaceContext ns = new NamespaceContext() {
64  
65  				@Override
66  				public String getNamespaceURI(final String prefix) {
67  
68  					if (prefix == null) {
69  
70  						return null;
71  					}
72  
73  					final String nsURI = prefixesURIs.get(prefix);
74  
75  					return nsURI;
76  				}
77  
78  				@Override
79  				public String getPrefix(final String namespaceURI) {
80  
81  					if (namespaceURI == null) {
82  
83  						return null;
84  					}
85  
86  					if (prefixesURIs.containsValue(namespaceURI)) {
87  
88  						for (final Map.Entry<String, String> e : prefixesURIs
89  								.entrySet()) {
90  
91  							if (namespaceURI.equals(e.getValue())) {
92  
93  								return e.getKey();
94  							}
95  						}
96  					}
97  
98  					return null;
99  				}
100 
101 				@Override
102 				public Iterator<?> getPrefixes(final String namespaceURI) {
103 
104 					return prefixesURIs.keySet().iterator();
105 				}
106 			};
107 
108 			xpath.setNamespaceContext(ns);
109 		}
110 
111 		return xpath;
112 	}
113 
114 	private static XPathFactory getXPathFactory() {
115 		return xPathFactory.get();
116 	}
117 
118 	private void checkXPathNotNull(String xpath) throws XmlFieldXPathException {
119 		if (xpath == null) {
120 			throw new XmlFieldXPathException("The requested xpath is null");
121 		}
122 	}
123 
124 	@Override
125 	public Boolean selectXPathToBoolean(NamespaceMap namespaces, String xpath,
126 			XmlFieldNode node) throws XmlFieldXPathException {
127 		checkXPathNotNull(xpath);
128 		final XPath xp = getXPath(namespaces);
129 		final Boolean value;
130 		try {
131 			value = (Boolean) xp.evaluate(xpath, node.getNode(),
132 					XPathConstants.BOOLEAN);
133 		} catch (XPathExpressionException e) {
134 			throw new XmlFieldXPathException(e);
135 		}
136 		return value;
137 	}
138 
139 	@Override
140 	public XmlFieldNode selectXPathToNode(NamespaceMap namespaces,
141 			String xpath, XmlFieldNode node) throws XmlFieldXPathException {
142 		checkXPathNotNull(xpath);
143 		final XPath xp = getXPath(namespaces);
144 		final Node selectedNode;
145 		try {
146 			selectedNode = (Node) xp.evaluate(xpath, node.getNode(),
147 					XPathConstants.NODE);
148 		} catch (XPathExpressionException e) {
149 			throw new XmlFieldXPathException(e);
150 		}
151 		if (selectedNode == null) {
152 			return null;
153 		}
154 		return new DomNode(selectedNode);
155 	}
156 
157 	@Override
158 	public XmlFieldNodeList selectXPathToNodeList(NamespaceMap namespaces,
159 			String xpath, XmlFieldNode node) throws XmlFieldXPathException {
160 		checkXPathNotNull(xpath);
161 		final XPath xp = getXPath(namespaces);
162 
163 		final NodeList nodeList;
164 		try {
165 			nodeList = (NodeList) xp.evaluate(xpath, node.getNode(),
166 					XPathConstants.NODESET);
167 		} catch (XPathExpressionException e) {
168 			throw new XmlFieldXPathException(e);
169 		}
170 
171 		final int nodeCount = nodeList.getLength();
172 
173 		final List<XmlFieldNode> list = new ArrayList<XmlFieldNode>();
174 
175 		for (int i = 0; i < nodeCount; ++i) {
176 
177 			final XmlFieldNode subNode = new DomNode(nodeList.item(i));
178 
179 			list.add(subNode);
180 		}
181 		return new DomNodeList(list);
182 	}
183 
184 	@Override
185 	public Double selectXPathToNumber(NamespaceMap namespaces, String xpath,
186 			XmlFieldNode node) throws XmlFieldXPathException {
187 		checkXPathNotNull(xpath);
188 		final XPath xp = getXPath(namespaces);
189 		final Double value;
190 		try {
191 			value = (Double) xp.evaluate(xpath, node.getNode(),
192 					XPathConstants.NUMBER);
193 		} catch (XPathExpressionException e) {
194 			throw new XmlFieldXPathException(e);
195 		}
196 		return value;
197 	}
198 
199 	@Override
200 	public String selectXPathToString(NamespaceMap namespaces, String xpath,
201 			XmlFieldNode node) throws XmlFieldXPathException {
202 		checkXPathNotNull(xpath);
203 		final XPath xp = getXPath(namespaces);
204 		final String value;
205 		try {
206 			value = (String) xp.evaluate(xpath, node.getNode(),
207 					XPathConstants.STRING);
208 		} catch (XPathExpressionException e) {
209 			throw new XmlFieldXPathException(e);
210 		}
211 		return value;
212 	}
213 
214 }