View Javadoc
1 package net.sf.flock.util; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.io.StringReader; 6 import java.io.UnsupportedEncodingException; 7 import java.net.URL; 8 9 import net.sf.flock.FlockResourceException; 10 11 import org.apache.log4j.LogManager; 12 import org.apache.log4j.Logger; 13 import org.jdom.Document; 14 import org.jdom.JDOMException; 15 import org.jdom.input.SAXBuilder; 16 import org.xml.sax.EntityResolver; 17 import org.xml.sax.InputSource; 18 19 public class DocumentLoader { 20 21 private final static Logger LOGGER = LogManager.getLogger(DocumentLoader.class); 22 23 private final SAXBuilder saxBuilder; 24 25 public DocumentLoader() { 26 this.saxBuilder = new SAXBuilder(false); // no validation 27 this.saxBuilder.setEntityResolver(new NoOpEntityResolver()); // no DTD loading 28 } 29 30 public Document loadDocument(URL location) throws FlockResourceException { 31 URLLoader loader = null; 32 InputStream input = null; 33 try { 34 loader = new URLLoader(location); 35 loader.load(); 36 LOGGER.info("URL "+location+" encoding : "+loader.getEncoding()); 37 input = loader.getAsInputStream(); 38 } catch (IOException io) { 39 throw new FlockResourceException("Error in getting document at "+location, io); 40 } 41 42 Document doc; 43 try { 44 doc = this.saxBuilder.build(input); 45 } catch (JDOMException e) { 46 try { 47 LOGGER.info("Possible missing encoding declaration, try ISO-8859-1 ["+location+"]"); 48 doc = this.saxBuilder.build(loader.getAsReader("ISO-8859-1")); 49 LOGGER.info("... succeed"); 50 } catch (UnsupportedEncodingException ue) { 51 throw new FlockResourceException("Unsupported encoding", ue); 52 } catch (JDOMException ex) { 53 throw new FlockResourceException("Unable to parse document at "+location, ex); 54 } 55 } 56 return doc; 57 } 58 59 private static class NoOpEntityResolver implements EntityResolver { 60 61 public InputSource resolveEntity(String publicId, String systemId) { 62 return new InputSource(new StringReader("")); 63 } 64 65 } 66 }

This page was automatically generated by Maven