1 /* 2 Copyright (C) 2000 - 2007 Grid Systems, S.A. 3 4 This program is free software; you can redistribute it and/or modify 5 it under the terms of the GNU General Public License, version 2, as 6 published by the Free Software Foundation. 7 8 This program is distributed in the hope that it will be useful, 9 but WITHOUT ANY WARRANTY; without even the implied warranty of 10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 GNU General Public License for more details. 12 13 You should have received a copy of the GNU General Public License 14 along with this program; if not, write to the Free Software 15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 16 */ 17 package com.gridsystems; 18 19 import java.io.File; 20 import java.io.IOException; 21 import java.io.InputStream; 22 import java.io.Reader; 23 24 import javax.xml.parsers.DocumentBuilder; 25 import javax.xml.parsers.DocumentBuilderFactory; 26 import javax.xml.parsers.ParserConfigurationException; 27 28 import org.w3c.dom.Document; 29 import org.xml.sax.InputSource; 30 import org.xml.sax.SAXException; 31 32 /** 33 * XmlUtils type. 34 * 35 * @author Rodrigo Ruiz 36 */ 37 public final class XmlUtils { 38 39 /** 40 * Hidden constructor. 41 */ 42 private XmlUtils() { } 43 44 /** 45 * Gets a document builder instance. 46 * 47 * @return An instance 48 * @throws ParserConfigurationException If an error occurs 49 */ 50 public static DocumentBuilder getBuilder() throws ParserConfigurationException { 51 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 52 return factory.newDocumentBuilder(); 53 } 54 55 /** 56 * Creates a new empty XML document. 57 * 58 * @return A Document instance 59 * @throws ParserConfigurationException If the builder cannot be instantiated 60 */ 61 public static Document createDocument() throws ParserConfigurationException { 62 return getBuilder().newDocument(); 63 } 64 65 /** 66 * Parses a document from the given source. 67 * 68 * @param src The source to parse 69 * @return The parsed document 70 * @throws ParserConfigurationException If the builder cannot be instantiated 71 * @throws SAXException If a syntax error is found 72 * @throws IOException If an error occurs reading the source 73 */ 74 public static Document parse(InputSource src) 75 throws ParserConfigurationException, SAXException, IOException { 76 return getBuilder().parse(src); 77 } 78 79 /** 80 * Parses a document from the given source. 81 * 82 * @param is The stream to parse 83 * @return The parsed document 84 * @throws ParserConfigurationException If the builder cannot be instantiated 85 * @throws SAXException If a syntax error is found 86 * @throws IOException If an error occurs reading the stream 87 */ 88 public static Document parse(InputStream is) 89 throws ParserConfigurationException, SAXException, IOException { 90 return getBuilder().parse(is); 91 } 92 93 /** 94 * Parses a document from the given source. 95 * 96 * @param f The file to parse 97 * @return The parsed document 98 * @throws ParserConfigurationException If the builder cannot be instantiated 99 * @throws SAXException If a syntax error is found 100 * @throws IOException If an error occurs reading the stream 101 */ 102 public static Document parse(File f) 103 throws ParserConfigurationException, SAXException, IOException { 104 return getBuilder().parse(f); 105 } 106 107 /** 108 * Parses a document from the given source. 109 * 110 * @param reader The reader to parse 111 * @return The parsed document 112 * @throws ParserConfigurationException If the builder cannot be instantiated 113 * @throws SAXException If a syntax error is found 114 * @throws IOException If an error occurs reading the stream 115 */ 116 public static Document parse(Reader reader) 117 throws ParserConfigurationException, SAXException, IOException { 118 return getBuilder().parse(new InputSource(reader)); 119 } 120 121 122 /** 123 * Gets a resource from the current context class-loader as a Document instance. 124 * 125 * @param resName The name of the resource to load 126 * @return A Document instance 127 * @throws ParserConfigurationException If the parser cannot be instantiated 128 * @throws SAXException If a syntax error is found in the parsed XML 129 * @throws IOException If an I/O error occurs while reading the resource 130 */ 131 public static Document getResourceAsDocument(String resName) 132 throws ParserConfigurationException, SAXException, IOException { 133 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 134 return getResourceAsDocument(cl, resName); 135 } 136 137 /** 138 * Gets a resource as a Document instance. 139 * 140 * @param cl The ClassLoader to use 141 * @param resName The name of the resource to load 142 * @return A Document instance 143 * @throws ParserConfigurationException If the parser cannot be instantiated 144 * @throws SAXException If a syntax error is found in the parsed XML 145 * @throws IOException If an I/O error occurs while reading the resource 146 */ 147 public static Document getResourceAsDocument(ClassLoader cl, String resName) 148 throws ParserConfigurationException, SAXException, IOException { 149 InputStream is = cl.getResourceAsStream(resName); 150 try { 151 return XmlUtils.parse(is); 152 } finally { 153 is.close(); 154 } 155 } 156 }