src/core/net/sf/basedb/util/XmlUtil2.java

Code
Comments
Other
Rev Date Author Line
6474 11 Jun 14 nicklas 1 /*
6474 11 Jun 14 nicklas 2   $Id: XMLUtil.java 6444 2014-04-09 12:21:20Z nicklas $
6474 11 Jun 14 nicklas 3
6474 11 Jun 14 nicklas 4   Copyright (C) 2005 Jari Häkkinen, Nicklas Nordborg
6474 11 Jun 14 nicklas 5   Copyright (C) 2006 Jari Häkkinen, Nicklas Nordborg, Martin Svensson
6474 11 Jun 14 nicklas 6   Copyright (C) 2007 Nicklas Nordborg
6474 11 Jun 14 nicklas 7
6474 11 Jun 14 nicklas 8   This file is part of BASE - BioArray Software Environment.
6474 11 Jun 14 nicklas 9   Available at http://base.thep.lu.se/
6474 11 Jun 14 nicklas 10
6474 11 Jun 14 nicklas 11   BASE is free software; you can redistribute it and/or
6474 11 Jun 14 nicklas 12   modify it under the terms of the GNU General Public License
6474 11 Jun 14 nicklas 13   as published by the Free Software Foundation; either version 3
6474 11 Jun 14 nicklas 14   of the License, or (at your option) any later version.
6474 11 Jun 14 nicklas 15
6474 11 Jun 14 nicklas 16   BASE is distributed in the hope that it will be useful,
6474 11 Jun 14 nicklas 17   but WITHOUT ANY WARRANTY; without even the implied warranty of
6474 11 Jun 14 nicklas 18   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6474 11 Jun 14 nicklas 19   GNU General Public License for more details.
6474 11 Jun 14 nicklas 20
6474 11 Jun 14 nicklas 21   You should have received a copy of the GNU General Public License
6474 11 Jun 14 nicklas 22   along with BASE. If not, see <http://www.gnu.org/licenses/>.
6474 11 Jun 14 nicklas 23 */
6474 11 Jun 14 nicklas 24 package net.sf.basedb.util;
6474 11 Jun 14 nicklas 25
6474 11 Jun 14 nicklas 26 import java.io.InputStream;
6474 11 Jun 14 nicklas 27 import java.io.IOException;
6474 11 Jun 14 nicklas 28 import java.io.InputStreamReader;
6474 11 Jun 14 nicklas 29 import java.io.StringReader;
6474 11 Jun 14 nicklas 30 import java.net.URL;
6474 11 Jun 14 nicklas 31 import java.nio.charset.Charset;
6474 11 Jun 14 nicklas 32
6474 11 Jun 14 nicklas 33 import org.jdom2.Document;
6474 11 Jun 14 nicklas 34 import org.jdom2.Element;
6474 11 Jun 14 nicklas 35 import org.jdom2.Attribute;
6474 11 Jun 14 nicklas 36 import org.jdom2.DocType;
6474 11 Jun 14 nicklas 37 import org.jdom2.JDOMException;
6474 11 Jun 14 nicklas 38 import org.jdom2.input.SAXBuilder;
6474 11 Jun 14 nicklas 39
6474 11 Jun 14 nicklas 40 import org.jdom2.input.sax.XMLReaderSAX2Factory;
6474 11 Jun 14 nicklas 41 import org.xml.sax.EntityResolver;
6474 11 Jun 14 nicklas 42 import org.xml.sax.ErrorHandler;
6474 11 Jun 14 nicklas 43 import org.xml.sax.SAXException;
6474 11 Jun 14 nicklas 44 import org.xml.sax.SAXParseException;
6474 11 Jun 14 nicklas 45 import org.xml.sax.InputSource;
6474 11 Jun 14 nicklas 46 import org.jdom2.output.Format;
6474 11 Jun 14 nicklas 47 import org.jdom2.output.XMLOutputter;
6474 11 Jun 14 nicklas 48
6474 11 Jun 14 nicklas 49 /**
6474 11 Jun 14 nicklas 50   This class contains some useful methods for handling
6474 11 Jun 14 nicklas 51   XML documents using JDOM2.
6474 11 Jun 14 nicklas 52
6474 11 Jun 14 nicklas 53   @author Nicklas
6474 11 Jun 14 nicklas 54   @since 3.4
6474 11 Jun 14 nicklas 55   @base.modified $Date$
6474 11 Jun 14 nicklas 56 */
6474 11 Jun 14 nicklas 57 public class XmlUtil2
6474 11 Jun 14 nicklas 58 {
6474 11 Jun 14 nicklas 59
6474 11 Jun 14 nicklas 60   /**
6474 11 Jun 14 nicklas 61     Log core events.
6474 11 Jun 14 nicklas 62   */
6474 11 Jun 14 nicklas 63   private static final org.slf4j.Logger log = 
6474 11 Jun 14 nicklas 64     org.slf4j.LoggerFactory.getLogger("net.sf.basedb.core");
6474 11 Jun 14 nicklas 65
6474 11 Jun 14 nicklas 66   /**
6474 11 Jun 14 nicklas 67     The parser class we are using.
6474 11 Jun 14 nicklas 68   */
6474 11 Jun 14 nicklas 69   private static final String xmlParserClass = "org.apache.xerces.parsers.SAXParser";
6474 11 Jun 14 nicklas 70
6474 11 Jun 14 nicklas 71   /**
6474 11 Jun 14 nicklas 72     Convert a <code>Document</code> to xml. The encoding is set to UTF-8.
6474 11 Jun 14 nicklas 73     @param dom The document
6474 11 Jun 14 nicklas 74     @return The XML
6474 11 Jun 14 nicklas 75   */
6474 11 Jun 14 nicklas 76   public static String toXml(Document dom)
6474 11 Jun 14 nicklas 77   {
6474 11 Jun 14 nicklas 78     XMLOutputter out = new XMLOutputter(Format.getCompactFormat().setEncoding("UTF-8"));
6474 11 Jun 14 nicklas 79     return out.outputString(dom);
6474 11 Jun 14 nicklas 80   }
6474 11 Jun 14 nicklas 81   
6474 11 Jun 14 nicklas 82   /**
6474 11 Jun 14 nicklas 83     Create a new <code>Document</code> with root element and System id
6474 11 Jun 14 nicklas 84     declaration.
6474 11 Jun 14 nicklas 85     @param rootElement The name of the root element tag
6474 11 Jun 14 nicklas 86     @param systemId The system id in the DOCTYPE section
6880 21 Apr 15 nicklas 87      @return a {@link org.jdom2.Document} object  
6474 11 Jun 14 nicklas 88   */
6474 11 Jun 14 nicklas 89   public static Document createDom(String rootElement, String systemId)
6474 11 Jun 14 nicklas 90   {
6474 11 Jun 14 nicklas 91     return new Document(new Element(rootElement), new DocType(rootElement, systemId));
6474 11 Jun 14 nicklas 92   }
6474 11 Jun 14 nicklas 93
6474 11 Jun 14 nicklas 94   /**
6474 11 Jun 14 nicklas 95     Load and validate an XML file against a DTD, and return it as a
6474 11 Jun 14 nicklas 96     <code>Document</code> object. The path to the files should
6474 11 Jun 14 nicklas 97     be given as a URL, not a filesystem path. For a file on the classpath
6474 11 Jun 14 nicklas 98     it is easiest done by the {@link Class#getResource(String)} method, ie.
6474 11 Jun 14 nicklas 99     <code>XMLUtil.class.getResource("/net/sf/basedb/core/the-xml-file.xml")</code>.
6474 11 Jun 14 nicklas 100     
6474 11 Jun 14 nicklas 101     @param xmlFile The URL to the XML file
6474 11 Jun 14 nicklas 102     @param dtdFile The URL to the DTD used for validation, the DTD must be encoded in ISO-8859-1
6474 11 Jun 14 nicklas 103     @return A <code>Document</code> object
6474 11 Jun 14 nicklas 104      @throws IOException If reading the input stream fails.
6474 11 Jun 14 nicklas 105   */
6474 11 Jun 14 nicklas 106   public static Document getValidatedXml(URL xmlFile, URL dtdFile)  
6474 11 Jun 14 nicklas 107     throws IOException
6474 11 Jun 14 nicklas 108   {    
6474 11 Jun 14 nicklas 109     InputStream is = xmlFile.openStream();    
6474 11 Jun 14 nicklas 110     Document xmlDoc = getValidatedXML(is, dtdFile, xmlFile.toString()); 
6474 11 Jun 14 nicklas 111     return xmlDoc;
6474 11 Jun 14 nicklas 112   }
6474 11 Jun 14 nicklas 113   
6474 11 Jun 14 nicklas 114   /**
6474 11 Jun 14 nicklas 115     @see #getXml(String, URL, boolean)
6474 11 Jun 14 nicklas 116   */
6474 11 Jun 14 nicklas 117   public static Document getValidatedXml(String xml, URL dtdFile)
6474 11 Jun 14 nicklas 118     throws IOException
6474 11 Jun 14 nicklas 119   {
6474 11 Jun 14 nicklas 120     return getXml(xml, dtdFile, true);
6474 11 Jun 14 nicklas 121   }
6474 11 Jun 14 nicklas 122
6474 11 Jun 14 nicklas 123   /**
6474 11 Jun 14 nicklas 124     Parse a string containing XML, optionally validating it against a DTD, 
6474 11 Jun 14 nicklas 125     and return it as a <code>Document</code> object. The path to the DTD should
6474 11 Jun 14 nicklas 126     be given as a URL, not a filesystem path. For a file on the classpath
6474 11 Jun 14 nicklas 127     it is easiest done by the {@link Class#getResource(String)} method, ie.
6474 11 Jun 14 nicklas 128     <code>XMLUtil.class.getResource("/net/sf/basedb/core/the-dtd-file.dtd")</code>.
6474 11 Jun 14 nicklas 129     
6474 11 Jun 14 nicklas 130     @param xml The string containing the XML
6474 11 Jun 14 nicklas 131     @param dtdFile The URL to the DTD used for validation, the DTD must be encoded in ISO-8859-1
6474 11 Jun 14 nicklas 132     @param validate TRUE to validate the xml data
6474 11 Jun 14 nicklas 133     @return A <code>Document</code> object
6474 11 Jun 14 nicklas 134      @throws IOException If reading the string fails.
6474 11 Jun 14 nicklas 135   */
6474 11 Jun 14 nicklas 136   public static Document getXml(String xml, URL dtdFile, boolean validate)
6474 11 Jun 14 nicklas 137     throws IOException
6474 11 Jun 14 nicklas 138   {
6474 11 Jun 14 nicklas 139     try
6474 11 Jun 14 nicklas 140     {
6474 11 Jun 14 nicklas 141       SAXBuilder sax = new SAXBuilder(new XMLReaderSAX2Factory(validate, xmlParserClass));
6474 11 Jun 14 nicklas 142       Validator validator = new Validator(null, dtdFile);
6474 11 Jun 14 nicklas 143       // The entity resolver lets us find the correct DTD for validating
6474 11 Jun 14 nicklas 144       sax.setEntityResolver(validator);
6474 11 Jun 14 nicklas 145       // So we can throw our own exception messages if there is a parse error
6474 11 Jun 14 nicklas 146       sax.setErrorHandler(validator);
6474 11 Jun 14 nicklas 147       StringReader sr = new StringReader(xml);
6474 11 Jun 14 nicklas 148       Document dom = sax.build(sr);
6474 11 Jun 14 nicklas 149       sr.close();
6474 11 Jun 14 nicklas 150       return dom;
6474 11 Jun 14 nicklas 151     }
6474 11 Jun 14 nicklas 152     catch (JDOMException ex)
6474 11 Jun 14 nicklas 153     {
6474 11 Jun 14 nicklas 154       throw new RuntimeException(ex);
6474 11 Jun 14 nicklas 155     }
6474 11 Jun 14 nicklas 156   }
6474 11 Jun 14 nicklas 157   
6474 11 Jun 14 nicklas 158   /**
6474 11 Jun 14 nicklas 159      Validate an <code>InputStream</code> against a DTD file 
6474 11 Jun 14 nicklas 160      and return it as a <code>Document</code>
6474 11 Jun 14 nicklas 161      
6474 11 Jun 14 nicklas 162      @param is The inputstream to the XML
6474 11 Jun 14 nicklas 163     @param dtdFile The URL to the DTD used for validation or null to not valdiate
6474 11 Jun 14 nicklas 164       against a DTD. The DTD must be encoded in ISO-8859-1
6474 11 Jun 14 nicklas 165      @param filename The filename of the original XML data, used for error reporting only;
6474 11 Jun 14 nicklas 166        use null if the filename is not known
6474 11 Jun 14 nicklas 167      @return A <code>Document</code> object
6474 11 Jun 14 nicklas 168      @throws IOException If reading the input stream fails.
6474 11 Jun 14 nicklas 169   */
6474 11 Jun 14 nicklas 170   public static Document getValidatedXML(InputStream is, URL dtdFile, String filename)
6474 11 Jun 14 nicklas 171     throws IOException
6474 11 Jun 14 nicklas 172   {
6474 11 Jun 14 nicklas 173     try
6474 11 Jun 14 nicklas 174     {
6474 11 Jun 14 nicklas 175       boolean validate = dtdFile != null;
6474 11 Jun 14 nicklas 176       SAXBuilder sax = new SAXBuilder(new XMLReaderSAX2Factory(validate, xmlParserClass));
6474 11 Jun 14 nicklas 177       Validator validator = new Validator(filename, dtdFile);
6474 11 Jun 14 nicklas 178       // The entity resolver lets us find the correct DTD for validating
6474 11 Jun 14 nicklas 179       sax.setEntityResolver(validator);
6474 11 Jun 14 nicklas 180       // So we can throw our own exception messages if there is a parse error
6474 11 Jun 14 nicklas 181       sax.setErrorHandler(validator);
6474 11 Jun 14 nicklas 182       Document dom = sax.build(is);
6474 11 Jun 14 nicklas 183       is.close();
6474 11 Jun 14 nicklas 184       return dom;
6474 11 Jun 14 nicklas 185     }
6474 11 Jun 14 nicklas 186     catch (JDOMException ex)
6474 11 Jun 14 nicklas 187     {
6474 11 Jun 14 nicklas 188       throw new RuntimeException(ex);
6474 11 Jun 14 nicklas 189     }
6474 11 Jun 14 nicklas 190   }
6474 11 Jun 14 nicklas 191   
6474 11 Jun 14 nicklas 192   /**
6474 11 Jun 14 nicklas 193     Validate an XML file using XML schemas. 
6474 11 Jun 14 nicklas 194     
6474 11 Jun 14 nicklas 195     @param xmlFile The URL pointing to the XML file to load
6474 11 Jun 14 nicklas 196     @param schemaFiles An array of schema file declarations. The array must
6474 11 Jun 14 nicklas 197       contain pairs of elements, the first one is the namespace name, and the
6474 11 Jun 14 nicklas 198       second element is the URL to the schema definition file
6474 11 Jun 14 nicklas 199      @return A <code>Document</code> object
6474 11 Jun 14 nicklas 200      @throws IOException If reading the input stream fails
6474 11 Jun 14 nicklas 201   */
6474 11 Jun 14 nicklas 202   public static Document getSchemaValidatedXML(URL xmlFile, String... schemaFiles)  
6474 11 Jun 14 nicklas 203     throws IOException
6474 11 Jun 14 nicklas 204   {    
6474 11 Jun 14 nicklas 205     InputStream is = xmlFile.openStream();
6474 11 Jun 14 nicklas 206     Document xmlDoc = getSchemaValidatedXML(is, xmlFile.toString(), schemaFiles); 
6474 11 Jun 14 nicklas 207     return xmlDoc;
6474 11 Jun 14 nicklas 208   }
6474 11 Jun 14 nicklas 209   
6474 11 Jun 14 nicklas 210   /**
6474 11 Jun 14 nicklas 211     Validate an XML input stream using XML schemas. 
6474 11 Jun 14 nicklas 212     
6474 11 Jun 14 nicklas 213     @param in The inputstream to the XML
6474 11 Jun 14 nicklas 214     @param filename The filename of the original XML data, used for error reporting only;
6474 11 Jun 14 nicklas 215        use null if the filename is not known
6474 11 Jun 14 nicklas 216     @param schemaFiles An array of schema file declarations. The array must
6474 11 Jun 14 nicklas 217       contain pairs of elements, the first one is the namespace name, and the
6474 11 Jun 14 nicklas 218       second element is the URL to the schema definition file
6474 11 Jun 14 nicklas 219      @return A <code>Document</code> object
6474 11 Jun 14 nicklas 220      @throws IOException If reading the input stream fails
6474 11 Jun 14 nicklas 221   */
6474 11 Jun 14 nicklas 222   public static Document getSchemaValidatedXML(InputStream in, String filename, String... schemaFiles)
6474 11 Jun 14 nicklas 223     throws IOException
6474 11 Jun 14 nicklas 224   {
6474 11 Jun 14 nicklas 225     SAXBuilder sax = new SAXBuilder(new XMLReaderSAX2Factory(true, xmlParserClass));
6474 11 Jun 14 nicklas 226     Validator validator = new Validator(filename, null);
6474 11 Jun 14 nicklas 227     // So we can throw our own exception messages if there is a parse error
6474 11 Jun 14 nicklas 228     sax.setErrorHandler(validator);
6474 11 Jun 14 nicklas 229     
6474 11 Jun 14 nicklas 230     sax.setFeature("http://apache.org/xml/features/validation/schema", true);
6474 11 Jun 14 nicklas 231     sax.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
6474 11 Jun 14 nicklas 232     if (schemaFiles != null)
6474 11 Jun 14 nicklas 233     {
6474 11 Jun 14 nicklas 234       if (schemaFiles.length % 2 != 0)
6474 11 Jun 14 nicklas 235       {
6474 11 Jun 14 nicklas 236         throw new ArrayIndexOutOfBoundsException("schemeFiles must contain an even number of elements");
6474 11 Jun 14 nicklas 237       }
6474 11 Jun 14 nicklas 238       StringBuilder sb = new StringBuilder();
6474 11 Jun 14 nicklas 239       for (String schemaFile : schemaFiles)
6474 11 Jun 14 nicklas 240       {
6474 11 Jun 14 nicklas 241         sb.append(schemaFile).append(" ");
6474 11 Jun 14 nicklas 242       }
6474 11 Jun 14 nicklas 243       sax.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation", 
6474 11 Jun 14 nicklas 244           sb.toString());
6474 11 Jun 14 nicklas 245     }
6474 11 Jun 14 nicklas 246
6474 11 Jun 14 nicklas 247     try
6474 11 Jun 14 nicklas 248     {
6474 11 Jun 14 nicklas 249       Document dom = sax.build(in);
6474 11 Jun 14 nicklas 250       in.close();
6474 11 Jun 14 nicklas 251       return dom;
6474 11 Jun 14 nicklas 252     }
6474 11 Jun 14 nicklas 253     catch (JDOMException ex)
6474 11 Jun 14 nicklas 254     {
6474 11 Jun 14 nicklas 255       throw new RuntimeException(ex);
6474 11 Jun 14 nicklas 256     }
6474 11 Jun 14 nicklas 257
6474 11 Jun 14 nicklas 258   }
6474 11 Jun 14 nicklas 259   
6474 11 Jun 14 nicklas 260   /**
6474 11 Jun 14 nicklas 261     Get the value of an attribute as an integer. If the attribute is missing or
6474 11 Jun 14 nicklas 262     doesn't contain an integer, the default value is returned.
6474 11 Jun 14 nicklas 263     @param el The node element which contains the attribute
6474 11 Jun 14 nicklas 264     @param attribute The name of the attribute
6474 11 Jun 14 nicklas 265     @param defaultValue The default value to return
6474 11 Jun 14 nicklas 266      @return an integer. 
6474 11 Jun 14 nicklas 267   */
6474 11 Jun 14 nicklas 268   public static int getIntAttribute(Element el, String attribute, int defaultValue)
6474 11 Jun 14 nicklas 269   {
6474 11 Jun 14 nicklas 270     Attribute attr = el.getAttribute(attribute);
6474 11 Jun 14 nicklas 271     int value = defaultValue;
6474 11 Jun 14 nicklas 272     if (attr != null)
6474 11 Jun 14 nicklas 273     {
6474 11 Jun 14 nicklas 274       try
6474 11 Jun 14 nicklas 275       {
6474 11 Jun 14 nicklas 276         value = attr.getIntValue();
6474 11 Jun 14 nicklas 277       }
6474 11 Jun 14 nicklas 278       catch (Exception ex)
6474 11 Jun 14 nicklas 279       {}
6474 11 Jun 14 nicklas 280     }
6474 11 Jun 14 nicklas 281     return value;
6474 11 Jun 14 nicklas 282   }
6474 11 Jun 14 nicklas 283
6474 11 Jun 14 nicklas 284   /**
6474 11 Jun 14 nicklas 285     Get the value of an attribute as a boolean. If the attribute is missing or
6474 11 Jun 14 nicklas 286     doesn't contain an boolean value, the default value is returned.
6474 11 Jun 14 nicklas 287     @param el The node element which contains the attribute
6474 11 Jun 14 nicklas 288     @param attribute The name of the attribute
6474 11 Jun 14 nicklas 289     @param defaultValue The default value to return
6474 11 Jun 14 nicklas 290      @return TRUE if the attribute has a true value, FALSE if it has a false value
6474 11 Jun 14 nicklas 291   */
6474 11 Jun 14 nicklas 292   public static boolean getBooleanAttribute(Element el, String attribute, boolean defaultValue)
6474 11 Jun 14 nicklas 293   {
6474 11 Jun 14 nicklas 294     Attribute attr = el.getAttribute(attribute);
6474 11 Jun 14 nicklas 295     boolean value = defaultValue;
6474 11 Jun 14 nicklas 296     if (attr != null)
6474 11 Jun 14 nicklas 297     {
6474 11 Jun 14 nicklas 298       try
6474 11 Jun 14 nicklas 299       {
6474 11 Jun 14 nicklas 300         value = attr.getBooleanValue();
6474 11 Jun 14 nicklas 301       }
6474 11 Jun 14 nicklas 302       catch (Exception ex)
6474 11 Jun 14 nicklas 303       {}
6474 11 Jun 14 nicklas 304     }
6474 11 Jun 14 nicklas 305     return value;
6474 11 Jun 14 nicklas 306   }
6474 11 Jun 14 nicklas 307
6474 11 Jun 14 nicklas 308   /**
6474 11 Jun 14 nicklas 309     This class is used to handle callbacks from the XML parser.
6474 11 Jun 14 nicklas 310     We need this for throwing better error messages and to
6474 11 Jun 14 nicklas 311     find the DTD.
6474 11 Jun 14 nicklas 312   */
6474 11 Jun 14 nicklas 313   private static class Validator
6474 11 Jun 14 nicklas 314     implements ErrorHandler, EntityResolver
6474 11 Jun 14 nicklas 315   {
6474 11 Jun 14 nicklas 316
6474 11 Jun 14 nicklas 317     private final String parsedFile;
6474 11 Jun 14 nicklas 318     private final URL dtdFile;
6474 11 Jun 14 nicklas 319     
6474 11 Jun 14 nicklas 320     /**
6474 11 Jun 14 nicklas 321       Create a new instance of this class.
6474 11 Jun 14 nicklas 322     */
6474 11 Jun 14 nicklas 323     private Validator(String parsedFile, URL dtdFile)
6474 11 Jun 14 nicklas 324     {
6474 11 Jun 14 nicklas 325       this.parsedFile = parsedFile;
6474 11 Jun 14 nicklas 326       this.dtdFile = dtdFile;
6474 11 Jun 14 nicklas 327     }
6474 11 Jun 14 nicklas 328
6474 11 Jun 14 nicklas 329     /*
6474 11 Jun 14 nicklas 330       From the org.xml.sax.ErrorHandler class
6474 11 Jun 14 nicklas 331       -------------------------------------------
6474 11 Jun 14 nicklas 332     */
6474 11 Jun 14 nicklas 333     /**
6474 11 Jun 14 nicklas 334       Errors thrown with a detailed error message.
6474 11 Jun 14 nicklas 335     */
6474 11 Jun 14 nicklas 336     @Override
6474 11 Jun 14 nicklas 337     public void error(SAXParseException exception)
6474 11 Jun 14 nicklas 338       throws SAXException
6474 11 Jun 14 nicklas 339     {
6474 11 Jun 14 nicklas 340       fatalError(exception);
6474 11 Jun 14 nicklas 341     }
6474 11 Jun 14 nicklas 342     /**
6474 11 Jun 14 nicklas 343       Fatal errors thrown with a detailed error message.
6474 11 Jun 14 nicklas 344     */
6474 11 Jun 14 nicklas 345     @Override
6474 11 Jun 14 nicklas 346     public void fatalError(SAXParseException exception)
6474 11 Jun 14 nicklas 347       throws SAXException
6474 11 Jun 14 nicklas 348     {
6474 11 Jun 14 nicklas 349       throw new SAXException(exception.getMessage()+" at line "+
6474 11 Jun 14 nicklas 350         exception.getLineNumber()+" in file '"+parsedFile+"'");
6474 11 Jun 14 nicklas 351     }
6474 11 Jun 14 nicklas 352     /**
6474 11 Jun 14 nicklas 353       Ignore warnings
6474 11 Jun 14 nicklas 354     */
6474 11 Jun 14 nicklas 355     @Override
6474 11 Jun 14 nicklas 356     public void warning(SAXParseException exception)
6474 11 Jun 14 nicklas 357       throws SAXException
6474 11 Jun 14 nicklas 358     {}
6474 11 Jun 14 nicklas 359
6474 11 Jun 14 nicklas 360     /*
6474 11 Jun 14 nicklas 361       From the org.xml.sax.EntityResolver class
6474 11 Jun 14 nicklas 362       -------------------------------------------
6474 11 Jun 14 nicklas 363     */
6474 11 Jun 14 nicklas 364     /**
6474 11 Jun 14 nicklas 365       This method is called when the parser wants a reference to the
6474 11 Jun 14 nicklas 366       DTD file. If the DTD cannot be loaded null is returned.
6474 11 Jun 14 nicklas 367     */
6474 11 Jun 14 nicklas 368     @Override
6474 11 Jun 14 nicklas 369     public InputSource resolveEntity(String publicId, String systemId)
6474 11 Jun 14 nicklas 370       throws SAXException, IOException
6474 11 Jun 14 nicklas 371     {
6474 11 Jun 14 nicklas 372       InputStream is = null;
6474 11 Jun 14 nicklas 373       try
6474 11 Jun 14 nicklas 374       {
6474 11 Jun 14 nicklas 375         is = dtdFile.openStream();
6474 11 Jun 14 nicklas 376       }
6474 11 Jun 14 nicklas 377       catch (IOException ex)
6474 11 Jun 14 nicklas 378       {
6474 11 Jun 14 nicklas 379         log.info("Exception while resolving XML entity: publicId = '"+
6474 11 Jun 14 nicklas 380           publicId+"'; systemId = '"+systemId+"'; dtdFile = '"+dtdFile+"'");
6474 11 Jun 14 nicklas 381         throw ex;
6474 11 Jun 14 nicklas 382       }
6474 11 Jun 14 nicklas 383       return new InputSource(new InputStreamReader(is, Charset.forName("ISO-8859-1")));
6474 11 Jun 14 nicklas 384     }
6474 11 Jun 14 nicklas 385   }
6474 11 Jun 14 nicklas 386
6474 11 Jun 14 nicklas 387 }