Changeset 9036


Ignore:
Timestamp:
2005-02-15T11:25:50+13:00 (19 years ago)
Author:
mdewsnip
Message:

Removed all XML functions from XMLTools, and changed occurrences to use new functions.

Location:
trunk/gli/src/org/greenstone/gatherer
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/gli/src/org/greenstone/gatherer/Configuration.java

    r8974 r9036  
    618618        ///ystem.err.println("Writing.");
    619619        user_config_xml.getParentFile().mkdirs();
    620         XMLTools.export(general_config, user_config_xml.getAbsolutePath());
     620        XMLTools.writeXMLFile(new File(user_config_xml.getAbsolutePath()), general_config);
    621621    }
    622622    catch(Exception exception) {
     
    626626    if(user_config_xml == null || !user_config_xml.exists()) {
    627627        // But failing that we produce a general copy
    628         XMLTools.export(general_config, Utility.BASE_DIR + config_xml_name);
     628        XMLTools.writeXMLFile(new File(Utility.BASE_DIR + config_xml_name), general_config);
    629629    }
    630630    }
  • trunk/gli/src/org/greenstone/gatherer/cdm/CollectionConfiguration.java

    r8652 r9036  
    329329    if(collect_config_name.equals(COLLECTCONFIGURATION_XML)) {
    330330        // Parse with Utility but don't use class loader
    331         document = XMLTools.parse(collect_config_file, false);
     331        document = XMLTools.parseXMLFile(collect_config_file);
    332332    }
    333333    // Otherwise if this is a legacy collect.cfg file then read in the template and send to magic parser
     
    470470    if(force_xml || collect_config_file.getName().equals(COLLECTCONFIGURATION_XML)) {
    471471        ///ystem.err.println("Writing XML");
    472         XMLTools.export(document, new File(collect_config_file.getParentFile(), COLLECTCONFIGURATION_XML));
     472        XMLTools.writeXMLFile(new File(collect_config_file.getParentFile(), COLLECTCONFIGURATION_XML), document);
    473473    }
    474474    else {
  • trunk/gli/src/org/greenstone/gatherer/collection/BuildOptions.java

    r8652 r9036  
    421421        }
    422422
    423         Document document = XMLTools.parse(input_stream,false);
     423        Document document = XMLTools.parseXML(input_stream);
    424424        arguments_element = document.getDocumentElement();
    425425    }
  • trunk/gli/src/org/greenstone/gatherer/collection/Collection.java

    r8846 r9036  
    8787    this.file = collection_xml;
    8888    // Try to load this collections details.
    89     document = XMLTools.parse(collection_xml, false);
     89    document = XMLTools.parseXMLFile(collection_xml);
    9090    // If that fails load the default settings for a collection.
    9191    if(document == null) {
     
    186186    /** Save this xml document to the given file. */
    187187    public void save() {
    188     XMLTools.export(document, file);
     188    XMLTools.writeXMLFile(file, document);
    189189    saved = true;
    190190    }
  • trunk/gli/src/org/greenstone/gatherer/collection/CollectionManager.java

    r9019 r9036  
    488488        date_element = null;
    489489        date_time = null;
    490         XMLTools.export(default_lockfile, destination);
     490        XMLTools.writeXMLFile(destination, default_lockfile);
    491491    }
    492492    catch (Exception error) {
  • trunk/gli/src/org/greenstone/gatherer/gems/MetadataSet.java

    r8978 r9036  
    9797    this.file = file;
    9898    this.value_trees = new Hashtable();
    99     this.document = XMLTools.parse(file, false);
     99    this.document = XMLTools.parseXMLFile(file);
    100100
    101101    init(false); // don't use class loader
     
    694694            }
    695695            else {
    696             value_document = XMLTools.parse(value_file, false);
     696            value_document = XMLTools.parseXMLFile(value_file);
    697697            }
    698698            if(value_document != null) {
  • trunk/gli/src/org/greenstone/gatherer/gems/MetadataSetManager.java

    r8971 r9036  
    566566               
    567567            // System.out.println("::: " + mds_file.toString());
    568          XMLTools.export(set.getDocument(), mds_file);
     568         XMLTools.writeXMLFile(mds_file, set.getDocument());
    569569             
    570570             set.setSetChanged(false);
  • trunk/gli/src/org/greenstone/gatherer/gui/LockFileDialog.java

    r8652 r9036  
    7070    this.self = this;
    7171
    72     // Parse the lock file, but do so quietly so that if the XML is poorly formed it doesn't show exception.
    73     document = XMLTools.parse(lock_file, false);
     72    // Parse the lock file
     73    document = XMLTools.parseXMLFile(lock_file);
    7474
    7575    // Creation
  • trunk/gli/src/org/greenstone/gatherer/util/Utility.java

    r9021 r9036  
    906906        InputStream is = base.getResourceAsStream("/" + filename);
    907907        if (is != null) {
    908         return XMLTools.parse(is, true);
     908        return XMLTools.parseXML(is);
    909909        }
    910910    }
    911911
    912912    // Try the file outside the classes directory
    913     return XMLTools.parse(new File(filename), true);
     913    return XMLTools.parseXMLFile(new File(filename));
    914914    }
    915915
  • trunk/gli/src/org/greenstone/gatherer/util/XMLTools.java

    r8695 r9036  
    102102    static public Document parseXMLFile(File xml_file)
    103103    {
     104    try {
     105        return parseXML(new FileInputStream(xml_file));
     106    }
     107    catch (Exception exception) {
     108        DebugStream.printStackTrace(exception);
     109        return null;
     110    }
     111    }
     112
     113
     114    /** Parse an XML document from a given input stream */
     115    static public Document parseXML(InputStream xml_input_stream)
     116    {
    104117    Document document = null;
    105118
    106119    try {
    107         FileInputStream fis   = new FileInputStream(xml_file);
    108         InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
     120        InputStreamReader isr = new InputStreamReader(xml_input_stream, "UTF-8");
    109121        Reader r              = new BufferedReader(isr);
    110122        InputSource isc       = new InputSource(r);
     
    118130        document = parser.getDocument();
    119131        isr.close();
    120         fis.close();
     132        xml_input_stream.close();
    121133    }
    122134    catch (Exception exception) {
     
    189201    }
    190202    }
    191 
    192 
    193     /** ------------ OLD FUNCTIONS FROM UTILITY ---------------
    194     /** Using this method we can request that a certain document be written, as valid XML, to a certain output stream. This makes use of the Xerces Serialization suite, which should in no way be confused with the usual method of Serialization used by Java. */
    195     static public boolean export(Document document, String filename) {
    196     return export(document, new File(filename));
    197     }
    198 
    199     static public boolean export(Document document, File file) {
    200     try {
    201         OutputStream os = new FileOutputStream(file);
    202         // Create an output format for our document.
    203         OutputFormat f = new OutputFormat(document);
    204         f.setEncoding("UTF-8");
    205         f.setIndenting(true);
    206         f.setLineWidth(0); // Why isn't this working!
    207         f.setPreserveSpace(false);
    208         // Create the necessary writer stream for serialization.
    209         OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
    210         Writer w               = new BufferedWriter(osw);
    211         // Generate a new serializer from the above.
    212         XMLSerializer s        = new XMLSerializer(w, f);
    213         s.asDOMSerializer();
    214         // Finally serialize the document to file.
    215         s.serialize(document);
    216         // And close.
    217         os.close();
    218         return true;
    219     }
    220     // A file not found exception is most likely thrown because the directory the metadata.xml file is attempting to be written to no longer has any files in it. I'll add a test in MetadataXMLFile to test for this, but if it still happens ignore it (a non-existant directory can't really have metadata added to it any way.
    221     catch (Exception exception) {
    222         if(!file.getName().endsWith(StaticStrings.METADATA_XML)) {
    223         DebugStream.printStackTrace(exception);
    224         return false;
    225         }
    226         return true;
    227     }
    228     }
    229 
    230     /** Parse in a xml document from a given file. */
    231     static public Document parse(File file) {
    232     return parse(file, true);
    233     }
    234 
    235     /** Parse in a xml document from a given file. */
    236     static public Document parse(File file, boolean noisey)
    237     {
    238     Document document = null;
    239     try {
    240         if (file.exists()) {
    241         DebugStream.println("Parsing XML file: " + file);
    242         FileInputStream fis = new FileInputStream(file);
    243         document = parse(fis, noisey);
    244         }
    245     }
    246     catch (Exception error) {
    247         if(noisey) {
    248         error.printStackTrace();
    249         DebugStream.println("Exception in Utility.parse() - Unexpected");
    250         }
    251         else {
    252         DebugStream.println("Exception in Utility.parse() - Expected");
    253         DebugStream.printStackTrace(error);
    254         }
    255     }
    256 
    257     return document;
    258     }
    259 
    260     /** Parse in a xml document from a given URL. */
    261     static public Document parse(URL url, boolean noisey)
    262     {
    263     Document document = null;
    264     try {
    265        
    266         URLConnection connection = url.openConnection();
    267         InputStream is = connection.getInputStream();
    268         document = parse(is,noisey);
    269     }
    270     catch (Exception error) {
    271         if(noisey) {
    272         error.printStackTrace();
    273         DebugStream.println("Exception in Utility.parse() - Unexpected");
    274         }
    275         else {
    276         DebugStream.println("Exception in Utility.parse() - Expected");
    277         DebugStream.printStackTrace(error);
    278         }
    279     }
    280 
    281     return document;
    282     }
    283 
    284     /** Parse in a xml document from a given file. */
    285     static public Document parse(InputStream is, boolean noisey) {
    286     Document document = null;
    287     try {
    288         InputStreamReader isr = new InputStreamReader(is, "UTF-8");
    289         Reader r              = new BufferedReader(isr);
    290         InputSource isc       = new InputSource(r);
    291         DOMParser parser      = new DOMParser();
    292         parser.setFeature("http://xml.org/sax/features/validation", false);
    293         parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    294                 // May or may not be ignored, the documentation for Xerces is contradictory. If it works then parsing -should- be faster.
    295         parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", true);
    296         parser.setFeature("http://apache.org/xml/features/dom/include-ignorable-whitespace", false);
    297         parser.parse(isc);
    298         document = parser.getDocument();
    299         isr.close();
    300         is.close();
    301         parser = null;
    302         isc = null;
    303         r = null;
    304         isr = null;
    305         is = null;
    306     }
    307     catch (Exception error) {
    308         if(noisey) {
    309         error.printStackTrace();
    310         DebugStream.println("Exception in Utility.parse() - Unexpected");
    311         }
    312         else {
    313         DebugStream.println("Exception in Utility.parse() - Expected");
    314         }
    315         DebugStream.printStackTrace(error);
    316     }
    317     return document;
    318     }
    319203}
Note: See TracChangeset for help on using the changeset viewer.