Changeset 28962

Show
Ignore:
Timestamp:
10.04.2014 14:27:43 (6 years ago)
Author:
kjdon
Message:

getDOM was already creating a new parser each time, so we don't need one in the constructor. now that we are basically doing nothing in the constructor, I have made all the methods static, and newDOM is always called statically. getDOM and others may now also be called statically. I might remove all this.converter and just use this statically, but haven't done that yet. have consolidated getDOM methods for maximum code reuse. Only the file + encoding version was actually using an EntityResolver? in the code, so that is the only one I have added that as a param too. Maybe in future need other getDOM methods with EntityResolver? param. nodeToElement moved to GSXML as it doesn't need to be here.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/XMLConverter.java

    r28849 r28962  
    3131import org.xml.sax.ErrorHandler; 
    3232import org.xml.sax.SAXParseException; 
     33import org.xml.sax.SAXNotRecognizedException; 
     34import org.xml.sax.SAXNotSupportedException; 
    3335import org.apache.xerces.parsers.DOMParser; 
    34 import org.apache.xerces.dom.*; // for new Documents 
     36import org.apache.xerces.dom.DocumentImpl; // for new Documents 
     37import org.apache.xerces.dom.DocumentTypeImpl;  
    3538 
    3639// other java classes 
     
    5861 * XMLConverter - utility class for greenstone 
    5962 *  
    60  * parses XML Strings into Documents, converts Nodes to Strings different 
    61  * parsers have different behaviour - can experiment in here now we only use 
    62  * xerces 
    63  *  
     63 * generates new Documents 
     64 * parses XML Strings into Documents, converts Nodes to Strings  
     65 * different parsers have different behaviour - can experiment in here  
     66 * at the moment we only use xerces 
     67 * all xerces specific code is in here 
    6468 */ 
    6569public class XMLConverter 
     
    6872    static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.XMLConverter.class.getName()); 
    6973 
    70     protected EntityResolver resolver = null; 
    71  
    72     /** xerces parser */ 
    73     protected DOMParser parser = null; 
     74  //protected EntityResolver resolver = null; 
    7475 
    7576    private static boolean outputEscaping = true; 
     
    7879    public XMLConverter() 
    7980    { 
    80         try 
    81         { 
    82             this.parser = new DOMParser(); 
    83             this.parser.setFeature("http://xml.org/sax/features/validation", false); 
    84             // don't try and load external DTD - no need if we are not validating, and may cause connection errors if a proxy is not set up. 
    85             this.parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); 
    86             // a performance test showed that having this on lead to increased  
    87             // memory use for small-medium docs, and not much gain for large  
    88             // docs. 
    89             // http://www.sosnoski.com/opensrc/xmlbench/conclusions.html 
    90             this.parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false); 
    91             // add an errorhandler to the parser which will store useful a error message on encountering fatal errors, errors and warnings when parsing 
    92             // this errormessage can then be converted to xhtml and displayed in a browser. 
    93             this.parser.setErrorHandler(new ParseErrorHandler()); 
    94         } 
    95         catch (Exception e) 
    96         { 
    97             logger.error(e.getMessage()); 
    98         } 
    99     } 
    100  
    101     /** sets the entity resolver. pass in null to unset it */ 
    102     public void setEntityResolver(EntityResolver er) 
    103     { 
    104         this.resolver = er; 
    105         this.parser.setEntityResolver(er); 
    106     } 
    107  
    108     /** 
    109      * Given a Node representing an Element or Document, will return the 
    110      * Element/docroot Element. Returns null if the Node was not an element. 
    111      */ 
    112     public static Element nodeToElement(Node node) 
    113     { 
    114         if (node == null) 
    115         { 
    116             return null; 
    117         } 
    118         short nodeType = node.getNodeType(); 
    119  
    120         if (nodeType == Node.DOCUMENT_NODE) 
    121         { 
    122             Document docNode = (Document) node; 
    123             return docNode.getDocumentElement(); 
    124         } 
    125         else if (nodeType == Node.ELEMENT_NODE) 
    126         { 
    127             return (Element) node; 
    128         } 
    129         else 
    130         { 
    131             String message = "Expecting Document or Element node type but got " + node.getNodeName() + "\nReturning null"; 
    132             System.err.println(message); 
    133             logger.warn(message); 
    134             return null; 
    135         } 
    136     } 
     81 
     82    } 
     83 
     84    // /** sets the entity resolver. pass in null to unset it */ 
     85    // public void setEntityResolver(EntityResolver er) 
     86    // { 
     87    //  this.resolver = er; 
     88    // } 
    13789 
    13890    /** returns a DOM Document */ 
    139     public Document getDOM(String in) 
     91  public static Document getDOM(String in) 
    14092    { 
    14193 
     
    14496            Reader reader = new StringReader(in); 
    14597            InputSource xml_source = new InputSource(reader); 
    146  
    147             DOMParser parser = new DOMParser(); 
    148             parser.setFeature("http://xml.org/sax/features/validation", false); 
    149             parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); 
    150             parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false); 
    151             if (resolver != null) 
    152             { 
    153                 parser.setEntityResolver(this.resolver); 
    154             } 
    155             parser.setErrorHandler(new ParseErrorHandler()); 
    156             parser.parse(xml_source); 
    157  
    158             Document doc = parser.getDocument(); 
    159  
     98            Document doc = getDOM(xml_source, null); 
    16099            reader.close(); 
    161100            return doc; 
     
    170109 
    171110    /** returns a DOM Document */ 
    172     public Document getDOM(String in, String encoding) 
     111  public static Document getDOM(String in, String encoding) 
    173112    { 
    174113        try 
     
    176115            InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(in.getBytes(encoding)), encoding); 
    177116            InputSource xml_source = new InputSource(reader); 
    178  
    179             DOMParser parser = new DOMParser(); 
    180             parser.setFeature("http://xml.org/sax/features/validation", false); 
    181             parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); 
    182             parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false); 
    183             if (resolver != null) 
    184             { 
    185                 parser.setEntityResolver(this.resolver); 
    186             } 
    187             parser.setErrorHandler(new ParseErrorHandler()); 
    188             parser.parse(xml_source); 
    189  
    190             Document doc = parser.getDocument(); 
    191  
     117            Document doc = getDOM(xml_source, null); 
    192118            reader.close(); 
    193119            return doc; 
     
    202128 
    203129    /** returns a DOM Document */ 
    204     public Document getDOM(File in) 
    205     { 
     130  public static Document getDOM(File in) { 
    206131        try 
    207132        { 
    208133            FileReader reader = new FileReader(in); 
    209134            InputSource xml_source = new InputSource(reader); 
    210  
    211             DOMParser parser = new DOMParser(); 
    212             parser.setFeature("http://xml.org/sax/features/validation", false); 
    213             parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); 
    214             parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false); 
    215             if (resolver != null) 
    216             { 
    217                 parser.setEntityResolver(this.resolver); 
    218             } 
    219             parser.setErrorHandler(new ParseErrorHandler()); 
    220             parser.parse(xml_source); 
    221  
    222             Document doc = parser.getDocument(); 
    223  
     135            Document doc = getDOM(xml_source, null); 
    224136            reader.close(); 
    225137            return doc; 
     
    234146    } 
    235147 
     148  public static Document getDOM(File in, String encoding) { 
     149    return getDOM(in, encoding, null); 
     150  } 
     151 
    236152    /** returns a DOM document */ 
    237     public Document getDOM(File in, String encoding) 
    238     { 
    239         try 
    240         { 
    241  
    242             InputStreamReader isr = new InputStreamReader(new FileInputStream(in), encoding); 
    243             InputSource xml_source = new InputSource(isr); 
    244  
    245             DOMParser parser = new DOMParser(); 
    246             parser.setFeature("http://xml.org/sax/features/validation", false); 
    247             parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); 
    248             parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false); 
    249             if (resolver != null) 
    250             { 
    251                 parser.setEntityResolver(this.resolver); 
    252             } 
    253             parser.setErrorHandler(new ParseErrorHandler()); 
    254             parser.parse(xml_source); 
    255  
    256             Document doc = parser.getDocument(); 
    257  
    258             isr.close(); 
    259             return doc; 
    260  
    261         } 
    262         catch (Exception e) 
    263         { 
    264             logger.error(e.getMessage()); 
    265         } 
    266         return null; 
    267     } 
     153  public static Document getDOM(File in, String encoding, EntityResolver er) { 
     154     
     155    try { 
     156       
     157       
     158      InputStreamReader isr = new InputStreamReader(new FileInputStream(in), encoding); 
     159      InputSource xml_source = new InputSource(isr); 
     160      Document doc = getDOM(xml_source, er); 
     161      isr.close(); 
     162      return doc; 
     163       
     164    } 
     165    catch (Exception e) 
     166      { 
     167    logger.error(e.getMessage()); 
     168      } 
     169    return null; 
     170    } 
     171 
     172  public static Document getDOM(InputSource source, EntityResolver er) { 
     173     
     174    try { 
     175    DOMParser parser = new DOMParser(); 
     176    parser.setFeature("http://xml.org/sax/features/validation", false); 
     177    // don't try and load external DTD - no need if we are not validating, and may cause connection errors if a proxy is not set up. 
     178    parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); 
     179    // a performance test showed that having this on lead to increased  
     180    // memory use for small-medium docs, and not much gain for large  
     181    // docs. 
     182    // http://www.sosnoski.com/opensrc/xmlbench/conclusions.html 
     183    parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false); 
     184    // add an errorhandler to the parser which will output messages on encountering fatal errors, errors and warnings when parsing 
     185    parser.setErrorHandler(new ParseErrorHandler()); 
     186    if (er != null) { 
     187      parser.setEntityResolver(er); 
     188    } 
     189    parser.parse(source); 
     190 
     191    Document doc = parser.getDocument(); 
     192    return doc; 
     193 
     194    } catch (Exception e) { 
     195       
     196      logger.error(e.getMessage()); 
     197    } 
     198    return null; 
     199     
     200  } 
    268201 
    269202    /** creates a new empty DOM Document */ 
    270     public Document newDOM() 
     203    public static Document newDOM() 
    271204    { 
    272205        Document doc = new DocumentImpl(); 
     
    563496 
    564497    // returns null if there no error occurred during parsing, or else returns the error message 
    565     public String getParseErrorMessage() 
    566     { 
    567         ParseErrorHandler errorHandler = (ParseErrorHandler) this.parser.getErrorHandler(); 
    568         return errorHandler.getErrorMessage(); 
    569     } 
     498 
     499    // public String getParseErrorMessage() 
     500    // { 
     501    //  ParseErrorHandler errorHandler = (ParseErrorHandler) this.parser.getErrorHandler(); 
     502    //  return errorHandler.getErrorMessage(); 
     503    // } 
    570504 
    571505    // Errorhandler for SAXParseExceptions that are errors, fatal errors or warnings. This class can be used to  
     
    626560    } 
    627561 
    628   public boolean writeDOM(Element elem, File file) { 
     562  public static boolean writeDOM(Element elem, File file) { 
    629563 
    630564    BufferedWriter writer = null;