| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| XMLReader |
|
| 2.8666666666666667;2.867 |
| 1 | package org.jtheque.core.utils.file; | |
| 2 | ||
| 3 | /* | |
| 4 | * This file is part of JTheque. | |
| 5 | * | |
| 6 | * JTheque is free software: you can redistribute it and/or modify | |
| 7 | * it under the terms of the GNU General Public License as published by | |
| 8 | * the Free Software Foundation, either version 3 of the License. | |
| 9 | * | |
| 10 | * JTheque is distributed in the hope that it will be useful, | |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | * GNU General Public License for more details. | |
| 14 | * | |
| 15 | * You should have received a copy of the GNU General Public License | |
| 16 | * along with JTheque. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | */ | |
| 18 | ||
| 19 | import org.jdom.Document; | |
| 20 | import org.jdom.Element; | |
| 21 | import org.jdom.JDOMException; | |
| 22 | import org.jdom.input.SAXBuilder; | |
| 23 | import org.jdom.xpath.XPath; | |
| 24 | ||
| 25 | import java.io.Closeable; | |
| 26 | import java.io.File; | |
| 27 | import java.io.FileInputStream; | |
| 28 | import java.io.IOException; | |
| 29 | import java.io.InputStream; | |
| 30 | import java.net.MalformedURLException; | |
| 31 | import java.net.URL; | |
| 32 | import java.net.URLConnection; | |
| 33 | import java.util.Collection; | |
| 34 | ||
| 35 | /** | |
| 36 | * A reader for XML files. | |
| 37 | * | |
| 38 | * @author Baptiste Wicht | |
| 39 | */ | |
| 40 | 0 | public final class XMLReader implements Closeable { |
| 41 | private InputStream stream; | |
| 42 | private Document document; | |
| 43 | ||
| 44 | private static final String OPEN_ERROR = "Error opening the file"; | |
| 45 | private static final String READING_ERROR = "Error reading the file"; | |
| 46 | ||
| 47 | /** | |
| 48 | * Open the file a the URL. | |
| 49 | * | |
| 50 | * @param strUrl The URL of the XML file. | |
| 51 | * @throws XMLException If an error occurs during the open process. | |
| 52 | */ | |
| 53 | public void openURL(String strUrl) throws XMLException { | |
| 54 | URL url; | |
| 55 | try { | |
| 56 | 0 | url = new URL(strUrl); |
| 57 | 0 | } catch (MalformedURLException e) { |
| 58 | 0 | throw new XMLException("Invalid URL", e); |
| 59 | 0 | } |
| 60 | ||
| 61 | 0 | openURL(url); |
| 62 | 0 | } |
| 63 | ||
| 64 | /** | |
| 65 | * Open the file a the URL. | |
| 66 | * | |
| 67 | * @param url The URL of the XML file. | |
| 68 | * @throws XMLException If an error occurs during the open process. | |
| 69 | */ | |
| 70 | public void openURL(URL url) throws XMLException { | |
| 71 | try { | |
| 72 | 0 | URLConnection urlConnection = url.openConnection(); |
| 73 | 0 | urlConnection.setUseCaches(false); |
| 74 | ||
| 75 | 0 | urlConnection.connect(); |
| 76 | ||
| 77 | 0 | stream = urlConnection.getInputStream(); |
| 78 | ||
| 79 | 0 | SAXBuilder sxb = new SAXBuilder("org.apache.xerces.parsers.SAXParser"); |
| 80 | ||
| 81 | try { | |
| 82 | 0 | document = sxb.build(stream); |
| 83 | 0 | } catch (JDOMException e) { |
| 84 | 0 | throw new XMLException(OPEN_ERROR, e); |
| 85 | 0 | } |
| 86 | 0 | } catch (IOException e) { |
| 87 | 0 | throw new XMLException(OPEN_ERROR, e); |
| 88 | 0 | } |
| 89 | 0 | } |
| 90 | ||
| 91 | /** | |
| 92 | * Open the file. | |
| 93 | * | |
| 94 | * @param strFile The path to the file to open. | |
| 95 | * @throws XMLException If an error occurs during the open process. | |
| 96 | */ | |
| 97 | public void openFile(String strFile) throws XMLException { | |
| 98 | 0 | openFile(new File(strFile)); |
| 99 | 0 | } |
| 100 | ||
| 101 | /** | |
| 102 | * Open the file. | |
| 103 | * | |
| 104 | * @param file The file to open. | |
| 105 | * @throws XMLException If an error occurs during the open process. | |
| 106 | */ | |
| 107 | public void openFile(File file) throws XMLException { | |
| 108 | try { | |
| 109 | 0 | stream = new FileInputStream(file); |
| 110 | ||
| 111 | 0 | SAXBuilder sxb = new SAXBuilder(); |
| 112 | ||
| 113 | 0 | document = sxb.build(stream); |
| 114 | 0 | } catch (JDOMException e) { |
| 115 | 0 | throw new XMLException(OPEN_ERROR, e); |
| 116 | 0 | } catch (IOException e) { |
| 117 | 0 | throw new XMLException(OPEN_ERROR, e); |
| 118 | 0 | } |
| 119 | 0 | } |
| 120 | ||
| 121 | @Override | |
| 122 | public void close() throws IOException { | |
| 123 | 0 | if (stream != null) { |
| 124 | 0 | stream.close(); |
| 125 | } | |
| 126 | 0 | } |
| 127 | ||
| 128 | /** | |
| 129 | * Return the root element of the reader. | |
| 130 | * | |
| 131 | * @return the root element else null if the reader is not open. | |
| 132 | */ | |
| 133 | public Element getRootElement() { | |
| 134 | 0 | if (document != null) { |
| 135 | 0 | return document.getRootElement(); |
| 136 | } | |
| 137 | ||
| 138 | 0 | return null; |
| 139 | } | |
| 140 | ||
| 141 | /** | |
| 142 | * Return all the nodes corresponding to the XPath request on the specified node. | |
| 143 | * | |
| 144 | * @param path The XPath request. | |
| 145 | * @param node The node to request in. | |
| 146 | * @return A List containing all elements corresponding to the request. | |
| 147 | * @throws XMLException If an errors occurs during the reading process. | |
| 148 | */ | |
| 149 | public Collection<Element> getNodes(String path, Object node) throws XMLException { | |
| 150 | try { | |
| 151 | 0 | return XPath.newInstance(path).selectNodes(node); |
| 152 | 0 | } catch (JDOMException e) { |
| 153 | 0 | throw new XMLException("Error selecting nodes", e); |
| 154 | } | |
| 155 | } | |
| 156 | ||
| 157 | /** | |
| 158 | * Return the unique node corresponding to the XPath request on the specified node. | |
| 159 | * | |
| 160 | * @param path The XPath request. | |
| 161 | * @param node The node to request in. | |
| 162 | * @return The unique node corresponding to the request else null if there is no node corresponding to the request. | |
| 163 | * @throws XMLException If an errors occurs during the reading process. | |
| 164 | */ | |
| 165 | public Element getNode(String path, Object node) throws XMLException { | |
| 166 | Element n; | |
| 167 | ||
| 168 | try { | |
| 169 | 0 | n = (Element) XPath.newInstance(path).selectSingleNode(node); |
| 170 | 0 | } catch (JDOMException e) { |
| 171 | 0 | throw new XMLException("Error selecting nodes", e); |
| 172 | 0 | } |
| 173 | ||
| 174 | 0 | return n; |
| 175 | } | |
| 176 | ||
| 177 | /** | |
| 178 | * Indicate if a node exists with the specified path in the specified node. | |
| 179 | * | |
| 180 | * @param path The path to search for. | |
| 181 | * @param node The node to search from. | |
| 182 | * | |
| 183 | * @return true if the path exists from the node else false. | |
| 184 | * | |
| 185 | * @throws XMLException If an errors occurs during the reading process. | |
| 186 | */ | |
| 187 | public boolean existsNode(String path, Object node) throws XMLException { | |
| 188 | 0 | return getNode(path, node) != null; |
| 189 | } | |
| 190 | ||
| 191 | /** | |
| 192 | * Indicate if a value exists with the specified path in the specified node. | |
| 193 | * | |
| 194 | * @param path The path to search for. | |
| 195 | * @param node The node to search from. | |
| 196 | * | |
| 197 | * @return true if the path exists from the node else false. | |
| 198 | * | |
| 199 | * @throws XMLException If an errors occurs during the reading process. | |
| 200 | */ | |
| 201 | public boolean existsValue(String path, Object node) throws XMLException { | |
| 202 | 0 | return readString(path, node) != null; |
| 203 | } | |
| 204 | ||
| 205 | /** | |
| 206 | * Read a String value from the node. | |
| 207 | * | |
| 208 | * @param path The XPath request. | |
| 209 | * @param node The node. | |
| 210 | * @return The string value of the request. | |
| 211 | * @throws XMLException If an errors occurs during the reading process. | |
| 212 | */ | |
| 213 | public String readString(String path, Object node) throws XMLException { | |
| 214 | String value; | |
| 215 | ||
| 216 | try { | |
| 217 | 0 | value = XPath.newInstance(path).valueOf(node); |
| 218 | 0 | } catch (JDOMException e) { |
| 219 | 0 | throw new XMLException(READING_ERROR, e); |
| 220 | 0 | } |
| 221 | ||
| 222 | 0 | return value; |
| 223 | } | |
| 224 | ||
| 225 | /** | |
| 226 | * Read a int value from the node. | |
| 227 | * | |
| 228 | * @param path The XPath request. | |
| 229 | * @param node The node. | |
| 230 | * @return The int value of the request. | |
| 231 | * @throws XMLException If an errors occurs during the reading process. | |
| 232 | */ | |
| 233 | public int readInt(String path, Object node) throws XMLException { | |
| 234 | String value; | |
| 235 | ||
| 236 | try { | |
| 237 | 0 | value = XPath.newInstance(path).valueOf(node); |
| 238 | 0 | } catch (JDOMException e) { |
| 239 | 0 | throw new XMLException(READING_ERROR, e); |
| 240 | 0 | } |
| 241 | ||
| 242 | 0 | return Integer.parseInt(value); |
| 243 | } | |
| 244 | ||
| 245 | /** | |
| 246 | * Read a double value from the node. | |
| 247 | * | |
| 248 | * @param path The XPath request. | |
| 249 | * @param node The node. | |
| 250 | * @return The double value of the request. | |
| 251 | * @throws XMLException If an errors occurs during the reading process. | |
| 252 | */ | |
| 253 | public double readDouble(String path, Object node) throws XMLException { | |
| 254 | String value; | |
| 255 | ||
| 256 | try { | |
| 257 | 0 | value = XPath.newInstance(path).valueOf(node); |
| 258 | 0 | } catch (JDOMException e) { |
| 259 | 0 | throw new XMLException(READING_ERROR, e); |
| 260 | 0 | } |
| 261 | ||
| 262 | 0 | return Double.parseDouble(value); |
| 263 | } | |
| 264 | ||
| 265 | /** | |
| 266 | * Read a boolean value from the node. | |
| 267 | * | |
| 268 | * @param path The XPath request. | |
| 269 | * @param node The node. | |
| 270 | * @return The boolean value of the request. | |
| 271 | * @throws XMLException If an errors occurs during the reading process. | |
| 272 | */ | |
| 273 | public boolean readBoolean(String path, Object node) throws XMLException { | |
| 274 | String value; | |
| 275 | ||
| 276 | try { | |
| 277 | 0 | value = XPath.newInstance(path).valueOf(node); |
| 278 | 0 | } catch (JDOMException e) { |
| 279 | 0 | throw new XMLException(READING_ERROR, e); |
| 280 | 0 | } |
| 281 | ||
| 282 | 0 | return Boolean.parseBoolean(value); |
| 283 | } | |
| 284 | ||
| 285 | /** | |
| 286 | * Read a long value from the node. | |
| 287 | * | |
| 288 | * @param path The XPath request. | |
| 289 | * @param node The node. | |
| 290 | * @return The double value of the request. | |
| 291 | * @throws XMLException If an errors occurs during the reading process. | |
| 292 | */ | |
| 293 | public long readLong(String path, Object node) throws XMLException { | |
| 294 | String value; | |
| 295 | ||
| 296 | try { | |
| 297 | 0 | value = XPath.newInstance(path).valueOf(node); |
| 298 | 0 | } catch (JDOMException e) { |
| 299 | 0 | throw new XMLException(READING_ERROR, e); |
| 300 | 0 | } |
| 301 | ||
| 302 | 0 | return Long.parseLong(value); |
| 303 | } | |
| 304 | } |