| 
     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
 | 
   
       0
  
 | 
   
   
   
           public DocumentLoader() {
   
   
    | 
 
 
   | 
     26
 | 
   
       0
  
 | 
   
   
   
               this.saxBuilder = new SAXBuilder(false);    // no validation 
   
   
    | 
 
 
   | 
     27
 | 
   
       0
  
 | 
   
   
   
               this.saxBuilder.setEntityResolver(new NoOpEntityResolver());    // no DTD loading 
   
   
    | 
 
 
   | 
     28
 | 
   
        
 | 
   
           } 
    | 
 
 
   | 
     29
 | 
   
        
 | 
   
        
    | 
 
 
   | 
     30
 | 
   
       0
  
 | 
   
   
   
           public Document loadDocument(URL location) throws FlockResourceException {
   
   
    | 
 
 
   | 
     31
 | 
   
       0
  
 | 
   
   
   
               URLLoader loader = null; 
   
   
    | 
 
 
   | 
     32
 | 
   
       0
  
 | 
   
   
   
               InputStream input = null; 
   
   
    | 
 
 
   | 
     33
 | 
   
       0
  
 | 
   
   
   
               try {
   
   
    | 
 
 
   | 
     34
 | 
   
       0
  
 | 
   
   
   
                   loader = new URLLoader(location); 
   
   
    | 
 
 
   | 
     35
 | 
   
       0
  
 | 
   
   
   
                   loader.load(); 
   
   
    | 
 
 
   | 
     36
 | 
   
       0
  
 | 
   
   
   
                   LOGGER.info("URL "+location+" encoding : "+loader.getEncoding());
   
   
    | 
 
 
   | 
     37
 | 
   
       0
  
 | 
   
   
   
                   input = loader.getAsInputStream(); 
   
   
    | 
 
 
   | 
     38
 | 
   
        
 | 
   
               } catch (IOException io) {
    | 
 
 
   | 
     39
 | 
   
       0
  
 | 
   
   
   
                   throw new FlockResourceException("Error in getting document at "+location, io);
   
   
    | 
 
 
   | 
     40
 | 
   
        
 | 
   
               } 
    | 
 
 
   | 
     41
 | 
   
        
 | 
   
                
    | 
 
 
   | 
     42
 | 
   
       0
  
 | 
   
   
   
               Document doc; 
   
   
    | 
 
 
   | 
     43
 | 
   
       0
  
 | 
   
   
   
               try {
   
   
    | 
 
 
   | 
     44
 | 
   
       0
  
 | 
   
   
   
                   doc = this.saxBuilder.build(input); 
   
   
    | 
 
 
   | 
     45
 | 
   
        
 | 
   
               } catch (JDOMException e) {
    | 
 
 
   | 
     46
 | 
   
       0
  
 | 
   
   
   
                   try {
   
   
    | 
 
 
   | 
     47
 | 
   
       0
  
 | 
   
   
   
                       LOGGER.info("Possible missing encoding declaration, try ISO-8859-1 ["+location+"]");
   
   
    | 
 
 
   | 
     48
 | 
   
       0
  
 | 
   
   
   
                       doc = this.saxBuilder.build(loader.getAsReader("ISO-8859-1"));
   
   
    | 
 
 
   | 
     49
 | 
   
       0
  
 | 
   
   
   
                       LOGGER.info("... succeed");
   
   
    | 
 
 
   | 
     50
 | 
   
        
 | 
   
                   } catch (UnsupportedEncodingException ue) {
    | 
 
 
   | 
     51
 | 
   
       0
  
 | 
   
   
   
                       throw new FlockResourceException("Unsupported encoding", ue);
   
   
    | 
 
 
   | 
     52
 | 
   
        
 | 
   
                   } catch (JDOMException ex) {
    | 
 
 
   | 
     53
 | 
   
       0
  
 | 
   
   
   
                       throw new FlockResourceException("Unable to parse document at "+location, ex);
   
   
    | 
 
 
   | 
     54
 | 
   
        
 | 
   
                   } 
    | 
 
 
   | 
     55
 | 
   
        
 | 
   
               } 
    | 
 
 
   | 
     56
 | 
   
       0
  
 | 
   
   
   
               return doc; 
   
   
    | 
 
 
   | 
     57
 | 
   
        
 | 
   
           } 
    | 
 
 
   | 
     58
 | 
   
        
 | 
   
        
    | 
 
 
   | 
   
     59
   
 | 
   
        
 | 
   
           private static class NoOpEntityResolver implements EntityResolver {
    | 
 
 
   | 
     60
 | 
   
        
 | 
   
        
    | 
 
 
   | 
     61
 | 
   
       0
  
 | 
   
   
   
               public InputSource resolveEntity(String publicId, String systemId) {
   
   
    | 
 
 
   | 
     62
 | 
   
       0
  
 | 
   
   
   
                   return new InputSource(new StringReader(""));
   
   
    | 
 
 
   | 
     63
 | 
   
        
 | 
   
               } 
    | 
 
 
   | 
     64
 | 
   
        
 | 
   
        
    | 
 
 
   | 
     65
 | 
   
        
 | 
   
           } 
    | 
 
 
   | 
     66
 | 
   
        
 | 
   
       } 
    | 
 
 
   | 
     67
 | 
   
        
 | 
   
        
    |