Changeset 24862


Ignore:
Timestamp:
2011-12-06T12:55:33+13:00 (12 years ago)
Author:
sjm84
Message:

Reformatting this file ahead of some changes

File:
1 edited

Legend:

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

    r23791 r24862  
    2121// XML classes
    2222import org.w3c.dom.DOMImplementation;
    23 import org.w3c.dom.Document; 
    24 import org.w3c.dom.DocumentType; 
    25 import org.w3c.dom.Element; 
    26 import org.w3c.dom.Node; 
     23import org.w3c.dom.Document;
     24import org.w3c.dom.DocumentType;
     25import org.w3c.dom.Element;
     26import org.w3c.dom.Node;
    2727import org.w3c.dom.NodeList;
    2828import org.w3c.dom.NamedNodeMap;
     
    5151import java.lang.reflect.*;
    5252
    53 /** XMLConverter - utility class for greenstone
    54  *
    55  * parses XML Strings into Documents, converts Nodes to Strings
    56  * different parsers have different behaviour - can experiment in here
    57  * now we only use xerces
    58  *
     53/**
     54 * XMLConverter - utility class for greenstone
     55 *
     56 * parses XML Strings into Documents, converts Nodes to Strings different
     57 * parsers have different behaviour - can experiment in here now we only use
     58 * xerces
     59 *
    5960 */
    60 public class XMLConverter {
    61 
    62      static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.XMLConverter.class.getName());
    63 
    64     /** xerces parser */
    65     protected DOMParser parser = null;
    66 
    67     private static boolean outputEscaping = true;
    68 
    69      /** the no-args constructor */
    70     public XMLConverter() {
    71     try {
    72         this.parser = new DOMParser();
    73         this.parser.setFeature("http://xml.org/sax/features/validation", false);
    74         // 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.
    75         this.parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    76         // a performance test showed that having this on lead to increased
    77         // memory use for small-medium docs, and not much gain for large
    78         // docs.
    79         // http://www.sosnoski.com/opensrc/xmlbench/conclusions.html
    80         this.parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false);
    81         // add an errorhandler to the parser which will store useful a error message on encountering fatal errors, errors and warnings when parsing
    82         // this errormessage can then be converted to xhtml and displayed in a browser.
    83         this.parser.setErrorHandler(new ParseErrorHandler());
    84     } catch (Exception e) {
    85         logger.error(e.getMessage());
    86     }
    87     }
    88 
    89     /** sets the entity resolver. pass in null to unset it */
    90     public void setEntityResolver(EntityResolver er) {
    91     this.parser.setEntityResolver(er);
    92     }
    93 
    94     /** Given a Node representing an Element or Document, will return the
    95      * Element/docroot Element. Returns null if the Node was not an element. */
    96     public static Element nodeToElement(Node node)
    97     {
    98     if(node == null) {
    99         return null;
    100     }
    101     short nodeType = node.getNodeType();
    102 
    103     if (nodeType == Node.DOCUMENT_NODE) {
    104         Document docNode = (Document)node;
    105         return docNode.getDocumentElement() ;
    106     }
    107     else if (nodeType == Node.ELEMENT_NODE) {
    108         return (Element)node;
    109     }
    110     else {
    111         String message = "Expecting Document or Element node type but got "
    112         + node.getNodeName() + "\nReturning null";
    113         System.err.println(message);
    114         logger.warn(message);
    115         return null;
    116     }
    117     }
    118 
    119     /** returns a DOM Document */
    120     public Document getDOM(String in) {
    121        
    122     try {
    123         Reader reader = new StringReader(in);
    124         InputSource xml_source = new InputSource(reader);
    125        
    126         this.parser.parse(xml_source);
    127         Document doc = this.parser.getDocument();
    128        
    129         return doc;
    130        
    131     } catch (Exception e) {
    132         logger.error(e.getMessage());
    133     }
    134     return null;
    135     }
    136  
    137     /** returns a DOM Document */
    138     public Document getDOM(File in) {
    139     try {
    140         FileReader reader = new FileReader(in);
    141         InputSource xml_source = new InputSource(reader);
    142         this.parser.parse(xml_source);
    143         Document doc = this.parser.getDocument();
    144         return doc;
    145 
    146     } catch (Exception e) {
    147         logger.error(e.getMessage(), e);
    148        
    149     }
    150     return null;
    151     }
    152 
    153     /** returns a DOM document */
    154     public Document getDOM(File in, String encoding) {
    155     try {
    156        
    157         InputStreamReader isr = new InputStreamReader(new FileInputStream(in), encoding);
    158         InputSource xml_source = new InputSource(isr);
    159        
    160         this.parser.parse(xml_source);
    161         Document doc = this.parser.getDocument();
    162        
    163         return doc;
    164 
    165     } catch (Exception e) {
    166         logger.error(e.getMessage());
    167     }
    168     return null;
    169     }
    170    
    171 
    172     /** creates a new empty DOM Document */
    173     public static Document newDOM() {
    174     Document doc = new DocumentImpl();
    175     return doc;
    176     }
    177    
    178     /**
    179      * This method's parameters represent the parts of the Doctype of this
    180      * Document that is to be created.
    181      * For more info see
    182      * http://xerces.apache.org/xerces-j/apiDocs/org/apache/xerces/dom/DocumentTypeImpl.html#DocumentTypeImpl(org.apache.xerces.dom.CoreDocumentImpl,%20java.lang.String)
    183      *
    184      * */
    185     public static Document newDOM(String qualifiedName, String publicID, String systemID) {
    186         // create empty DOM document
    187         DocumentImpl docImpl = new DocumentImpl();
    188        
    189         // Need to use the document to create the docType for it
     61public class XMLConverter
     62{
     63
     64    static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.XMLConverter.class.getName());
     65
     66    /** xerces parser */
     67    protected DOMParser parser = null;
     68
     69    private static boolean outputEscaping = true;
     70
     71    /** the no-args constructor */
     72    public XMLConverter()
     73    {
     74        try
     75        {
     76            this.parser = new DOMParser();
     77            this.parser.setFeature("http://xml.org/sax/features/validation", false);
     78            // 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.
     79            this.parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
     80            // a performance test showed that having this on lead to increased
     81            // memory use for small-medium docs, and not much gain for large
     82            // docs.
     83            // http://www.sosnoski.com/opensrc/xmlbench/conclusions.html
     84            this.parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false);
     85            // add an errorhandler to the parser which will store useful a error message on encountering fatal errors, errors and warnings when parsing
     86            // this errormessage can then be converted to xhtml and displayed in a browser.
     87            this.parser.setErrorHandler(new ParseErrorHandler());
     88        }
     89        catch (Exception e)
     90        {
     91            logger.error(e.getMessage());
     92        }
     93    }
     94
     95    /** sets the entity resolver. pass in null to unset it */
     96    public void setEntityResolver(EntityResolver er)
     97    {
     98        this.parser.setEntityResolver(er);
     99    }
     100
     101    /**
     102     * Given a Node representing an Element or Document, will return the
     103     * Element/docroot Element. Returns null if the Node was not an element.
     104     */
     105    public static Element nodeToElement(Node node)
     106    {
     107        if (node == null)
     108        {
     109            return null;
     110        }
     111        short nodeType = node.getNodeType();
     112
     113        if (nodeType == Node.DOCUMENT_NODE)
     114        {
     115            Document docNode = (Document) node;
     116            return docNode.getDocumentElement();
     117        }
     118        else if (nodeType == Node.ELEMENT_NODE)
     119        {
     120            return (Element) node;
     121        }
     122        else
     123        {
     124            String message = "Expecting Document or Element node type but got " + node.getNodeName() + "\nReturning null";
     125            System.err.println(message);
     126            logger.warn(message);
     127            return null;
     128        }
     129    }
     130
     131    /** returns a DOM Document */
     132    public Document getDOM(String in)
     133    {
     134
     135        try
     136        {
     137            Reader reader = new StringReader(in);
     138            InputSource xml_source = new InputSource(reader);
     139
     140            this.parser.parse(xml_source);
     141            Document doc = this.parser.getDocument();
     142
     143            return doc;
     144
     145        }
     146        catch (Exception e)
     147        {
     148            logger.error(e.getMessage());
     149        }
     150        return null;
     151    }
     152
     153    /** returns a DOM Document */
     154    public Document getDOM(File in)
     155    {
     156        try
     157        {
     158            FileReader reader = new FileReader(in);
     159            InputSource xml_source = new InputSource(reader);
     160            this.parser.parse(xml_source);
     161            Document doc = this.parser.getDocument();
     162            return doc;
     163
     164        }
     165        catch (Exception e)
     166        {
     167            logger.error(e.getMessage(), e);
     168
     169        }
     170        return null;
     171    }
     172
     173    /** returns a DOM document */
     174    public Document getDOM(File in, String encoding)
     175    {
     176        try
     177        {
     178
     179            InputStreamReader isr = new InputStreamReader(new FileInputStream(in), encoding);
     180            InputSource xml_source = new InputSource(isr);
     181
     182            this.parser.parse(xml_source);
     183            Document doc = this.parser.getDocument();
     184
     185            return doc;
     186
     187        }
     188        catch (Exception e)
     189        {
     190            logger.error(e.getMessage());
     191        }
     192        return null;
     193    }
     194
     195    /** creates a new empty DOM Document */
     196    public static Document newDOM()
     197    {
     198        Document doc = new DocumentImpl();
     199        return doc;
     200    }
     201
     202    /**
     203     * This method's parameters represent the parts of the Doctype of this
     204     * Document that is to be created. For more info see
     205     * http://xerces.apache.org
     206     * /xerces-j/apiDocs/org/apache/xerces/dom/DocumentTypeImpl
     207     * .html#DocumentTypeImpl
     208     * (org.apache.xerces.dom.CoreDocumentImpl,%20java.lang.String)
     209     *
     210     * */
     211    public static Document newDOM(String qualifiedName, String publicID, String systemID)
     212    {
     213        // create empty DOM document
     214        DocumentImpl docImpl = new DocumentImpl();
     215
     216        // Need to use the document to create the docType for it
    190217        DocumentType myDocType = new DocumentTypeImpl(docImpl, qualifiedName, publicID, systemID);
    191        
     218
    192219        // Although we have created the docType using the document, we need to still
    193220        // put it into the empty document we just created
    194         try{
     221        try
     222        {
    195223            docImpl.appendChild(myDocType);
    196         } catch(Exception e) {
    197             System.out.println("Could not append docType because: " + e) ;     
    198         }
    199        
     224        }
     225        catch (Exception e)
     226        {
     227            System.out.println("Could not append docType because: " + e);
     228        }
     229
    200230        // return the document containing a DocType
    201         return docImpl;     
    202     }
    203 
    204     /** returns the Node as a String */
    205     public static String getString(Node xmlNode)
    206     {
    207     outputEscaping = true;
    208     StringBuffer xmlRepresentation = new StringBuffer();
    209     getString(xmlNode, xmlRepresentation, 0, false);
    210     return xmlRepresentation.toString();
    211     }
    212 
    213     /** returns the node as a nicely formatted String - this introduces extra
    214      * text nodes if the String is read back in as a DOM, so should only be
    215      * used for printing */
    216     public static String getPrettyString(Node xmlNode) {
    217    
    218     outputEscaping = true;
    219     StringBuffer xmlRepresentation = new StringBuffer();
    220     getString(xmlNode, xmlRepresentation, 0, true);
    221     return xmlRepresentation.toString();
    222     }
    223 
    224     /* For the purposes of logger.debug statements, where this is called and hence outputted,
    225     returns an empty string if debugging is not enabled */
    226     public static String getPrettyStringLogger(Node xmlNode, Logger log) {
    227 
    228     if(log.isDebugEnabled())
    229         return getPrettyString(xmlNode);
    230    
    231     return "";
    232 
    233     }
    234 
    235     private static void getString(Node xmlNode, StringBuffer xmlRepresentation,
    236                  int depth, boolean pretty)
    237     {
    238    
    239     if (xmlNode == null) {
    240         xmlRepresentation.append("<null>");
    241         return;
    242     }
    243 
    244     short nodeType = xmlNode.getNodeType();
    245     String nodeName = xmlNode.getNodeName();
    246 
    247     if (nodeType == Node.DOCUMENT_NODE) {
    248         Document xmlDocNode = (Document)xmlNode;
    249        
    250         //if (xmlDocNode.getDoctype() == null) {
    251         //System.err.println("Doctype is null.");
    252         //}
    253         //else {
    254         if (xmlDocNode.getDoctype() != null) {
    255         DocumentType dt = xmlDocNode.getDoctype();
    256        
    257         String name = dt.getName();
    258         String pid  = dt.getPublicId();
    259         String sid  = dt.getSystemId();
    260        
    261         // Use previously assigned name, not dt.getName() again
    262         String doctype_str = "<!DOCTYPE " + name + " PUBLIC \"" + pid + "\" \"" + sid + "\">\n";
    263        
    264         xmlRepresentation.append(doctype_str);
    265         }
    266         getString(xmlDocNode.getDocumentElement(), xmlRepresentation, depth, pretty);
    267         return;
    268     }
    269     // Handle Element nodes
    270     if (nodeType == Node.ELEMENT_NODE) {
    271         if (pretty) {
    272         xmlRepresentation.append("\n");
    273         for (int i = 0; i < depth; i++) {
    274             xmlRepresentation.append("  ");
    275         }
    276         }
    277 
    278         // Write opening tag
    279         xmlRepresentation.append("<");
    280         xmlRepresentation.append(nodeName);
    281        
    282         // Write the node attributes
    283         NamedNodeMap nodeAttributes = xmlNode.getAttributes();
    284         for (int i = 0; i < nodeAttributes.getLength(); i++) {
    285         Node attribute = nodeAttributes.item(i);
    286         xmlRepresentation.append(" ");
    287         xmlRepresentation.append(attribute.getNodeName());
    288         xmlRepresentation.append("=\"");
    289         xmlRepresentation.append(attribute.getNodeValue());
    290         xmlRepresentation.append("\"");
    291         }
    292 
    293         // If the node has no children, close the opening tag and return
    294         if (xmlNode.hasChildNodes() == false) {
    295         // This produces somewhat ugly output, but it is necessary to compensate
    296         // for display bugs in Netscape. Firstly, the space is needed before the
    297         // closing bracket otherwise Netscape will ignore some tags (<br/>, for
    298         // example). Also, a newline character would be expected after the tag,
    299         // but this causes problems with the display of links (the link text
    300         // will contain a newline character, which is displayed badly).
    301         xmlRepresentation.append(" />");
     231        return docImpl;
     232    }
     233
     234    /** returns the Node as a String */
     235    public static String getString(Node xmlNode)
     236    {
     237        outputEscaping = true;
     238        StringBuffer xmlRepresentation = new StringBuffer();
     239        getString(xmlNode, xmlRepresentation, 0, false);
     240        return xmlRepresentation.toString();
     241    }
     242
     243    /**
     244     * returns the node as a nicely formatted String - this introduces extra
     245     * text nodes if the String is read back in as a DOM, so should only be used
     246     * for printing
     247     */
     248    public static String getPrettyString(Node xmlNode)
     249    {
     250
     251        outputEscaping = true;
     252        StringBuffer xmlRepresentation = new StringBuffer();
     253        getString(xmlNode, xmlRepresentation, 0, true);
     254        return xmlRepresentation.toString();
     255    }
     256
     257    /*
     258     * For the purposes of logger.debug statements, where this is called and
     259     * hence outputted, returns an empty string if debugging is not enabled
     260     */
     261    public static String getPrettyStringLogger(Node xmlNode, Logger log)
     262    {
     263
     264        if (log.isDebugEnabled())
     265            return getPrettyString(xmlNode);
     266
     267        return "";
     268
     269    }
     270
     271    private static void getString(Node xmlNode, StringBuffer xmlRepresentation, int depth, boolean pretty)
     272    {
     273
     274        if (xmlNode == null)
     275        {
     276            xmlRepresentation.append("<null>");
     277            return;
     278        }
     279
     280        short nodeType = xmlNode.getNodeType();
     281        String nodeName = xmlNode.getNodeName();
     282
     283        if (nodeType == Node.DOCUMENT_NODE)
     284        {
     285            Document xmlDocNode = (Document) xmlNode;
     286
     287            //if (xmlDocNode.getDoctype() == null) {
     288            //System.err.println("Doctype is null.");
     289            //}
     290            //else {
     291            if (xmlDocNode.getDoctype() != null)
     292            {
     293                DocumentType dt = xmlDocNode.getDoctype();
     294
     295                String name = dt.getName();
     296                String pid = dt.getPublicId();
     297                String sid = dt.getSystemId();
     298
     299                // Use previously assigned name, not dt.getName() again
     300                String doctype_str = "<!DOCTYPE " + name + " PUBLIC \"" + pid + "\" \"" + sid + "\">\n";
     301
     302                xmlRepresentation.append(doctype_str);
     303            }
     304            getString(xmlDocNode.getDocumentElement(), xmlRepresentation, depth, pretty);
     305            return;
     306        }
     307        // Handle Element nodes
     308        if (nodeType == Node.ELEMENT_NODE)
     309        {
     310            if (pretty)
     311            {
     312                xmlRepresentation.append("\n");
     313                for (int i = 0; i < depth; i++)
     314                {
     315                    xmlRepresentation.append("  ");
     316                }
     317            }
     318
     319            // Write opening tag
     320            xmlRepresentation.append("<");
     321            xmlRepresentation.append(nodeName);
     322
     323            // Write the node attributes
     324            NamedNodeMap nodeAttributes = xmlNode.getAttributes();
     325            for (int i = 0; i < nodeAttributes.getLength(); i++)
     326            {
     327                Node attribute = nodeAttributes.item(i);
     328                xmlRepresentation.append(" ");
     329                xmlRepresentation.append(attribute.getNodeName());
     330                xmlRepresentation.append("=\"");
     331                xmlRepresentation.append(attribute.getNodeValue());
     332                xmlRepresentation.append("\"");
     333            }
     334
     335            // If the node has no children, close the opening tag and return
     336            if (xmlNode.hasChildNodes() == false)
     337            {
     338                // This produces somewhat ugly output, but it is necessary to compensate
     339                // for display bugs in Netscape. Firstly, the space is needed before the
     340                // closing bracket otherwise Netscape will ignore some tags (<br/>, for
     341                // example). Also, a newline character would be expected after the tag,
     342                // but this causes problems with the display of links (the link text
     343                // will contain a newline character, which is displayed badly).
     344                xmlRepresentation.append(" />");
     345                return;
     346            }
     347
     348            // Close the opening tag
     349            xmlRepresentation.append(">");
     350
     351            // Apply recursively to the children of this node
     352            // hack for nodes next to text nodes - dont make them pretty
     353            // this is needed for text inside a <pre> element - any new lines
     354            // or spaces around the span elements show up in the text
     355            NodeList children = xmlNode.getChildNodes();
     356            boolean do_pretty = pretty;
     357            for (int i = 0; i < children.getLength(); i++)
     358            {
     359                if (children.item(i).getNodeType() == Node.TEXT_NODE)
     360                {
     361                    do_pretty = false; // if there is a text node amongst the children, do teh following nodes in non-pretty mode - hope this doesn't stuff up something else
     362                }
     363                getString(children.item(i), xmlRepresentation, depth + 1, do_pretty);
     364            }
     365
     366            // Write closing tag
     367            if (pretty)
     368            {
     369                if (xmlRepresentation.charAt(xmlRepresentation.length() - 1) == '\n')
     370                {
     371                    for (int i = 0; i < depth; i++)
     372                        xmlRepresentation.append("  ");
     373                }
     374            }
     375            xmlRepresentation.append("</");
     376            xmlRepresentation.append(nodeName);
     377            xmlRepresentation.append(">");
     378            if (pretty)
     379            {
     380                xmlRepresentation.append("\n");
     381            }
     382        }
     383
     384        // Handle Text nodes
     385        else if (nodeType == Node.TEXT_NODE)
     386        {
     387            String text = xmlNode.getNodeValue();
     388
     389            // Perform output escaping, if required
     390            // Apache Commons replace method is far superior to String.replaceAll - very fast!
     391            if (outputEscaping)
     392            {
     393
     394                text = StringUtils.replace(text, "&", "&amp;");
     395                text = StringUtils.replace(text, "<", "&lt;");
     396                text = StringUtils.replace(text, ">", "&gt;");
     397                text = StringUtils.replace(text, "'", "&apos;");
     398                text = StringUtils.replace(text, "\"", "&quot;");
     399            }
     400
     401            // Remove any control-C characters
     402            text = StringUtils.replace(text, "" + (char) 3, "");
     403
     404            xmlRepresentation.append(text);
     405        }
     406
     407        // Handle Processing Instruction nodes
     408        else if (nodeType == Node.PROCESSING_INSTRUCTION_NODE)
     409        {
     410            if (nodeName.equals("javax.xml.transform.disable-output-escaping"))
     411            {
     412                outputEscaping = false;
     413            }
     414            else if (nodeName.equals("javax.xml.transform.enable-output-escaping"))
     415            {
     416                outputEscaping = true;
     417            }
     418            else
     419            {
     420                logger.warn("Unhandled processing instruction " + nodeName);
     421            }
     422        }
     423
     424        else if (nodeType == Node.COMMENT_NODE)
     425        {
     426            String text = xmlNode.getNodeValue();
     427            xmlRepresentation.append("<!-- ");
     428            xmlRepresentation.append(text);
     429            xmlRepresentation.append(" -->");
     430        }
     431
     432        // A type of node that is not handled yet
     433        else
     434        {
     435            logger.warn("Unknown node type: " + nodeType + " " + getNodeTypeString(nodeType));
     436        }
     437
    302438        return;
    303         }
    304        
    305         // Close the opening tag
    306         xmlRepresentation.append(">");
    307        
    308         // Apply recursively to the children of this node
    309         // hack for nodes next to text nodes - dont make them pretty
    310         // this is needed for text inside a <pre> element - any new lines
    311         // or spaces around the span elements show up in the text
    312         NodeList children = xmlNode.getChildNodes();
    313         boolean do_pretty = pretty;
    314         for (int i = 0; i < children.getLength(); i++) {
    315         if (children.item(i).getNodeType()==Node.TEXT_NODE) {
    316             do_pretty=false; // if there is a text node amongst the children, do teh following nodes in non-pretty mode - hope this doesn't stuff up something else
    317         }
    318         getString(children.item(i), xmlRepresentation, depth + 1, do_pretty);
    319         }
    320        
    321         // Write closing tag
    322         if (pretty) {
    323         if (xmlRepresentation.charAt(xmlRepresentation.length()-1) == '\n') {
    324             for (int i = 0; i < depth; i++)
    325             xmlRepresentation.append("  ");
    326         }
    327         }
    328         xmlRepresentation.append("</");
    329         xmlRepresentation.append(nodeName);
    330         xmlRepresentation.append(">");
    331         if (pretty) {
    332         xmlRepresentation.append("\n");
    333         }
    334     }
    335    
    336     // Handle Text nodes
    337     else if (nodeType == Node.TEXT_NODE) {
    338         String text = xmlNode.getNodeValue();
    339 
    340         // Perform output escaping, if required
    341         // Apache Commons replace method is far superior to String.replaceAll - very fast!
    342         if (outputEscaping) {
    343 
    344         text = StringUtils.replace(text, "&", "&amp;");
    345         text = StringUtils.replace(text, "<", "&lt;");
    346         text = StringUtils.replace(text, ">", "&gt;");
    347         text = StringUtils.replace(text, "'", "&apos;");
    348         text = StringUtils.replace(text, "\"", "&quot;");
    349         }
    350 
    351         // Remove any control-C characters
    352         text = StringUtils.replace(text, "" + (char)3, "");
    353 
    354         xmlRepresentation.append(text);
    355     }
    356 
    357     // Handle Processing Instruction nodes
    358     else if (nodeType == Node.PROCESSING_INSTRUCTION_NODE) {
    359         if (nodeName.equals("javax.xml.transform.disable-output-escaping")) {
    360         outputEscaping = false;
    361         }
    362         else if (nodeName.equals("javax.xml.transform.enable-output-escaping")) {
    363         outputEscaping = true;
    364         }
    365         else {
    366         logger.warn("Unhandled processing instruction " + nodeName);
    367         }
    368     }
    369    
    370     else if (nodeType == Node.COMMENT_NODE) {
    371         String text = xmlNode.getNodeValue();
    372         xmlRepresentation.append("<!-- ");
    373         xmlRepresentation.append(text);
    374         xmlRepresentation.append(" -->");
    375     }
    376        
    377        
    378    
    379     // A type of node that is not handled yet
    380     else {
    381         logger.warn("Unknown node type: " + nodeType+" "+getNodeTypeString(nodeType));
    382     }
    383 
    384     return;
    385     }
    386 
    387     protected static String getNodeTypeString(short node_type) {
    388 
    389     String type = "";
    390     switch(node_type) {
    391     case Node.ATTRIBUTE_NODE:
    392         type="ATTRIBUTE_NODE";
    393         break;
    394     case Node.CDATA_SECTION_NODE:
    395         type="CDATA_SECTION_NODE";
    396         break;
    397     case Node.COMMENT_NODE:
    398         type="COMMENT_NODE";
    399         break;
    400     case Node.DOCUMENT_FRAGMENT_NODE:
    401         type="DOCUMENT_FRAGMENT_NODE";
    402         break;
    403     case Node.DOCUMENT_NODE:
    404         type="DOCUMENT_NODE";
    405         break;
    406     case Node.DOCUMENT_TYPE_NODE:
    407         type="DOCUMENT_TYPE_NODE";
    408         break;
    409     case Node.ELEMENT_NODE:
    410         type="ELEMENT_NODE";
    411         break;
    412     case Node.ENTITY_NODE:
    413         type="ENTITY_NODE";
    414         break;
    415     case Node.ENTITY_REFERENCE_NODE:
    416         type="ENTITY_REFERENCE_NODE";
    417         break;
    418     case Node.NOTATION_NODE:
    419         type="NOTATION_NODE";
    420         break;
    421     case Node.PROCESSING_INSTRUCTION_NODE:
    422         type="PROCESSING_INSTRUCTION_NODE";
    423         break;
    424     case Node.TEXT_NODE:
    425         type="TEXT_NODE";
    426         break;
    427     default:
    428         type="UNKNOWN";
    429     }
    430 
    431     return type;
    432     }
    433    
     439    }
     440
     441    protected static String getNodeTypeString(short node_type)
     442    {
     443
     444        String type = "";
     445        switch (node_type)
     446        {
     447        case Node.ATTRIBUTE_NODE:
     448            type = "ATTRIBUTE_NODE";
     449            break;
     450        case Node.CDATA_SECTION_NODE:
     451            type = "CDATA_SECTION_NODE";
     452            break;
     453        case Node.COMMENT_NODE:
     454            type = "COMMENT_NODE";
     455            break;
     456        case Node.DOCUMENT_FRAGMENT_NODE:
     457            type = "DOCUMENT_FRAGMENT_NODE";
     458            break;
     459        case Node.DOCUMENT_NODE:
     460            type = "DOCUMENT_NODE";
     461            break;
     462        case Node.DOCUMENT_TYPE_NODE:
     463            type = "DOCUMENT_TYPE_NODE";
     464            break;
     465        case Node.ELEMENT_NODE:
     466            type = "ELEMENT_NODE";
     467            break;
     468        case Node.ENTITY_NODE:
     469            type = "ENTITY_NODE";
     470            break;
     471        case Node.ENTITY_REFERENCE_NODE:
     472            type = "ENTITY_REFERENCE_NODE";
     473            break;
     474        case Node.NOTATION_NODE:
     475            type = "NOTATION_NODE";
     476            break;
     477        case Node.PROCESSING_INSTRUCTION_NODE:
     478            type = "PROCESSING_INSTRUCTION_NODE";
     479            break;
     480        case Node.TEXT_NODE:
     481            type = "TEXT_NODE";
     482            break;
     483        default:
     484            type = "UNKNOWN";
     485        }
     486
     487        return type;
     488    }
     489
    434490    // returns null if there no error occurred during parsing, or else returns the error message
    435     public String getParseErrorMessage() {
    436         ParseErrorHandler errorHandler = (ParseErrorHandler)this.parser.getErrorHandler();
     491    public String getParseErrorMessage()
     492    {
     493        ParseErrorHandler errorHandler = (ParseErrorHandler) this.parser.getErrorHandler();
    437494        return errorHandler.getErrorMessage();
    438495    }
    439    
     496
    440497    // Errorhandler for SAXParseExceptions that are errors, fatal errors or warnings. This class can be used to
    441498    // register a handler for any fatal errors, errors and warnings that may occur when parsing an xml file. The
    442499    // errors are printed both to the greenstone.log and to the tomcat console (System.err), and the error message
    443500    // is stored in the errorMessage variable so that it can be retrieved and be used to generate an xhtml error page.
    444     static public class ParseErrorHandler implements ErrorHandler {
     501    static public class ParseErrorHandler implements ErrorHandler
     502    {
    445503        protected String errorMessage = null;
    446    
     504
    447505        //  Receive notification of a recoverable error.
    448         public void error(SAXParseException exception) {
     506        public void error(SAXParseException exception)
     507        {
    449508            handleError("Error:\n", exception);
    450509        }
    451         //   Receive notification of a non-recoverable error.
    452         public void fatalError(SAXParseException exception) {
     510
     511        //   Receive notification of a non-recoverable error.
     512        public void fatalError(SAXParseException exception)
     513        {
    453514            handleError("Fatal Error:\n", exception);
    454515        }
     516
    455517        // Receive notification of a warning.
    456         public void warning(SAXParseException exception) {
     518        public void warning(SAXParseException exception)
     519        {
    457520            handleError("Warning:\n", exception);
    458         }
    459        
    460         public String toString(SAXParseException e) {
     521        }
     522
     523        public String toString(SAXParseException e)
     524        {
    461525            String msg = e.getMessage();
    462526            msg += "\nOn line(column): " + e.getLineNumber() + "(" + e.getColumnNumber() + ")";
    463527            msg += (e.getPublicId() != null) ? ("\npublic ID: " + e.getPublicId()) : "\nNo public ID";
    464528            msg += (e.getSystemId() != null) ? ("\nsystem ID: " + e.getSystemId()) : "\nNo system ID";
    465                    
     529
    466530            return msg;
    467531        }
    468        
     532
    469533        // clears the errorPage variable after first call to this method
    470         public String getErrorMessage() {
     534        public String getErrorMessage()
     535        {
    471536            String errMsg = this.errorMessage;
    472             if(this.errorMessage != null) {
     537            if (this.errorMessage != null)
     538            {
    473539                this.errorMessage = null;
    474540            }
    475541            return errMsg;
    476542        }
    477        
     543
    478544        // sets the errorMessage member variable to the data stored in the exception
    479545        // and writes the errorMessage to the logger and tomcat's System.err
    480         protected void handleError(String errorType, SAXParseException exception) {
    481             this.errorMessage = errorType + toString(exception);
     546        protected void handleError(String errorType, SAXParseException exception)
     547        {
     548            this.errorMessage = errorType + toString(exception);
    482549            System.err.println("\n****Error parsing xml:\n" + this.errorMessage + "\n****\n");
    483550            logger.error(this.errorMessage);
Note: See TracChangeset for help on using the changeset viewer.