Changeset 8652


Ignore:
Timestamp:
2004-11-24T15:00:44+13:00 (19 years ago)
Author:
mdewsnip
Message:

Removed more dead code, and moved most of the XML stuff out of Utility and into XMLTools.

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

Legend:

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

    r8627 r8652  
    626626        ///ystem.err.println("Writing.");
    627627        user_config_xml.getParentFile().mkdirs();
    628         Utility.export(general_config, user_config_xml.getAbsolutePath());
     628        XMLTools.export(general_config, user_config_xml.getAbsolutePath());
    629629    }
    630630    catch(Exception exception) {
     
    634634    if(user_config_xml == null || !user_config_xml.exists()) {
    635635        // But failing that we produce a general copy
    636         Utility.export(general_config, Utility.BASE_DIR + config_xml_name);
     636        XMLTools.export(general_config, Utility.BASE_DIR + config_xml_name);
    637637    }
    638638    }
  • trunk/gli/src/org/greenstone/gatherer/Dictionary.java

    r8591 r8652  
    8686    this.font = font;
    8787    this.locale = ((locale == null) ? Locale.getDefault() : locale);
    88     this.dictionary = ResourceBundle.getBundle(Utility.DICTIONARY, this.locale);
     88    this.dictionary = ResourceBundle.getBundle("dictionary", this.locale);
    8989    }
    9090
  • trunk/gli/src/org/greenstone/gatherer/cdm/CollectionConfiguration.java

    r8474 r8652  
    329329    if(collect_config_name.equals(COLLECTCONFIGURATION_XML)) {
    330330        // Parse with Utility but don't use class loader
    331         document = Utility.parse(collect_config_file, false);
     331        document = XMLTools.parse(collect_config_file, false);
    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         Utility.export(document, new File(collect_config_file.getParentFile(), COLLECTCONFIGURATION_XML));
     472        XMLTools.export(document, new File(collect_config_file.getParentFile(), COLLECTCONFIGURATION_XML));
    473473    }
    474474    else {
  • trunk/gli/src/org/greenstone/gatherer/collection/BuildOptions.java

    r8236 r8652  
    421421        }
    422422
    423         Document document = Utility.parse(input_stream,false);
    424 
    425         //DOMParser parser = new DOMParser();
    426         //parser.parse(source);
    427         //Document document = parser.getDocument();
     423        Document document = XMLTools.parse(input_stream,false);
    428424        arguments_element = document.getDocumentElement();
    429425    }
  • trunk/gli/src/org/greenstone/gatherer/collection/Collection.java

    r8586 r8652  
    8888    this.file = collection_xml;
    8989    // Try to load this collections details.
    90     document = Utility.parse(collection_xml, false);
     90    document = XMLTools.parse(collection_xml, false);
    9191    // If that fails load the default settings for a collection.
    9292    if(document == null) {
     
    187187    /** Save this xml document to the given file. */
    188188    public void save() {
    189     Utility.export(document, file);
     189    XMLTools.export(document, file);
    190190    saved = true;
    191191    }
  • trunk/gli/src/org/greenstone/gatherer/collection/CollectionManager.java

    r8651 r8652  
    521521        date_element = null;
    522522        date_time = null;
    523         Utility.export(default_lockfile, destination);
     523        XMLTools.export(default_lockfile, destination);
    524524    }
    525525    catch (Exception error) {
     
    553553     * @see org.greenstone.gatherer.util.Utility
    554554     */
    555     public String getCollectionArchive() {
     555    public String getCollectionArchives() {
    556556    if (Gatherer.GS3) {
    557         return Utility.getArchiveDir(Utility.getCollectionDir(Configuration.gsdl3_path, Configuration.site_name, collection.getName()));
     557        return Utility.getCollectionDir(Configuration.gsdl3_path, Configuration.site_name, collection.getName()) + "archives" + File.separator;
    558558    } else {
    559         return Utility.getArchiveDir(Utility.getCollectionDir(Configuration.gsdl_path, collection.getName()));
     559        return Utility.getCollectionDir(Configuration.gsdl_path, collection.getName()) + "archives" + File.separator;
    560560    }
    561561    }
     
    613613    public String getCollectionEtc() {
    614614    if (Gatherer.GS3) {
    615         return Utility.getEtcDir(Utility.getCollectionDir(Configuration.gsdl3_path, Configuration.site_name, collection.getName()));
     615        return Utility.getCollectionDir(Configuration.gsdl3_path, Configuration.site_name, collection.getName()) + "etc" + File.separator;
    616616    } else {
    617         return Utility.getEtcDir(Utility.getCollectionDir(Configuration.gsdl_path, collection.getName()));
     617        return Utility.getCollectionDir(Configuration.gsdl_path, collection.getName()) + "etc" + File.separator;
    618618    }
    619619    }
     
    669669    public String getCollectionIndex() {
    670670    if (Gatherer.GS3) {
    671         return Utility.getIndexDir(Utility.getCollectionDir(Configuration.gsdl3_path, Configuration.site_name, collection.getName()));
     671        return Utility.getCollectionDir(Configuration.gsdl3_path, Configuration.site_name, collection.getName()) + "index" + File.separator;
    672672    } else {
    673         return Utility.getIndexDir(Utility.getCollectionDir(Configuration.gsdl_path, collection.getName()));
     673        return Utility.getCollectionDir(Configuration.gsdl_path, collection.getName()) + "index" + File.separator;
    674674    }
    675675    }
     
    697697    public String getCollectionMetadata() {
    698698    if (Gatherer.GS3) {
    699         return Utility.getMetadataDir(Utility.getCollectionDir(Configuration.gsdl3_path, Configuration.site_name, collection.getName()));
     699        return Utility.getCollectionDir(Configuration.gsdl3_path, Configuration.site_name, collection.getName()) + "metadata" + File.separator;
    700700    } else {
    701         return Utility.getMetadataDir(Utility.getCollectionDir(Configuration.gsdl_path, collection.getName()));
    702     }
    703     }
    704 
    705 
    706     static public String getCollectionMetadataDirectory()
    707     {
    708     if (Gatherer.GS3) {
    709         return Utility.getMetadataDir(Utility.getCollectionDir(Configuration.gsdl3_path, Configuration.site_name, collection.getName()));
    710     } else {
    711         return Utility.getMetadataDir(Utility.getCollectionDir(Configuration.gsdl_path, collection.getName()));
    712     }
    713     }
    714    
     701        return Utility.getCollectionDir(Configuration.gsdl_path, collection.getName()) + "metadata" + File.separator;
     702    }
     703    }
     704
    715705
    716706    /** Retrieve the record set (tree model) associated with the current collection. */
     
    974964
    975965        // Read through the doc.xml files in the archives directory
    976         File collection_archives_directory = new File(collection_directory, Utility.ARCHIVES_DIR);
     966        File collection_archives_directory = new File(getCollectionArchives());
    977967        DocXMLFileManager.clearDocXMLFiles();
    978968        DocXMLFileManager.loadDocXMLFiles(collection_archives_directory);
  • trunk/gli/src/org/greenstone/gatherer/file/FileQueue.java

    r8600 r8652  
    4141import org.greenstone.gatherer.metadata.MetadataXMLFileManager;
    4242import org.greenstone.gatherer.util.DragComponent;
     43import org.greenstone.gatherer.util.StaticStrings;
    4344import org.greenstone.gatherer.util.SynchronizedTreeModelTools;
    4445import org.greenstone.gatherer.util.Utility;
     
    444445
    445446                // If it is a metadata.xml file, we must unload it
    446                 if (source_file.getName().equals(Utility.METADATA_XML)) {
     447                if (source_file.getName().equals(StaticStrings.METADATA_XML)) {
    447448                MetadataXMLFileManager.unloadMetadataXMLFile(source_file);
    448449                }
  • trunk/gli/src/org/greenstone/gatherer/gems/GValueModel.java

    r8270 r8652  
    149149    ///ystem.err.println("Retrieve the value for: " + index_str);
    150150    GValueNode result = null;
    151     if(isHierarchy() && Utility.isIndex(index_str)) {
     151    if(isHierarchy() && isIndex(index_str)) {
    152152        // StringTokenize the index
    153153        StringTokenizer tokenizer = new StringTokenizer(index_str, ".");
     
    180180    }
    181181
     182
     183    /** A string is a valid hierarchy index if it matches '[0-9](\.[0-9])*' */
     184    public boolean isIndex(String raw) {
     185    boolean result = true;
     186    for(int i = 0; result && i < raw.length(); i++) {
     187        char c = raw.charAt(i);
     188        if(Character.isDigit(c) || (c == '.' && (i != 0 || i != raw.length() - 1))) {
     189        // Valid index
     190        }
     191        else {
     192        result = false;
     193        }
     194    }
     195    return result;
     196    }
     197
    182198    public boolean isHierarchy() {
    183199    boolean result = false;
  • trunk/gli/src/org/greenstone/gatherer/gems/MetadataSet.java

    r8581 r8652  
    4646import org.greenstone.gatherer.util.StaticStrings;
    4747import org.greenstone.gatherer.util.Utility;
     48import org.greenstone.gatherer.util.XMLTools;
    4849import org.w3c.dom.*;
    4950/** An semi-data class to hold details about a loaded metadata set, it also provides some methods to manipulating the data within.
     
    9697    this.file = file;
    9798    this.value_trees = new Hashtable();
    98     this.document = Utility.parse(file, false);
     99    this.document = XMLTools.parse(file, false);
    99100
    100101    init(false); // don't use class loader
     
    502503    Element name_element = null;
    503504    Element metadataset_element = document.getDocumentElement();
    504     NodeList name_elements = metadataset_element.getElementsByTagName(Utility.NAME_ELEMENT);
     505    NodeList name_elements = metadataset_element.getElementsByTagName(StaticStrings.NAME_ELEMENT);
    505506    for(int i = 0; i < name_elements.getLength(); i++) {
    506507        Element possible_name_element = (Element) name_elements.item(i);
    507         if(possible_name_element.getAttribute(Utility.LANGUAGE_ATTRIBUTE).equals(Utility.ENGLISH_VALUE)) {
     508        if(possible_name_element.getAttribute(StaticStrings.LANGUAGE_ATTRIBUTE).equals(Utility.ENGLISH_VALUE)) {
    508509        // Found it.
    509510        name_element = possible_name_element;
     
    512513    // If there is none add one. Note that we can only add english metadata sets. Although others can edit them to add further names as necessary.
    513514    if(name_element == null) {
    514         name_element = document.createElement(Utility.NAME_ELEMENT);
    515         name_element.setAttribute(Utility.LANGUAGE_ATTRIBUTE, Utility.ENGLISH_VALUE);
     515        name_element = document.createElement(StaticStrings.NAME_ELEMENT);
     516        name_element.setAttribute(StaticStrings.LANGUAGE_ATTRIBUTE, Utility.ENGLISH_VALUE);
    516517        metadataset_element.insertBefore(name_element, metadataset_element.getFirstChild());
    517518    }
     
    627628            }
    628629            else {
    629             value_document = Utility.parse(value_file, false);
     630            value_document = XMLTools.parse(value_file, false);
    630631            }
    631632            if(value_document != null) {
  • trunk/gli/src/org/greenstone/gatherer/gems/MetadataSetManager.java

    r8581 r8652  
    4848import org.greenstone.gatherer.collection.CollectionManager;
    4949import org.greenstone.gatherer.file.*;
     50import org.greenstone.gatherer.util.XMLTools;
    5051import org.apache.xerces.parsers.*;
    5152import org.apache.xml.serialize.*;
     
    503504               
    504505            // System.out.println("::: " + mds_file.toString());
    505          Utility.export(set.getDocument(), mds_file);
     506         XMLTools.export(set.getDocument(), mds_file);
    506507             
    507508             set.setSetChanged(false);
     
    518519                //File value_file = new File(file, value_element.getName() + ".mdv");
    519520                ///ystem.err.println("Saving value file: " + value_file.toString());
    520                 //Utility.export(value_tree.getDocument(), value_file);
     521                //XMLTools.export(value_tree.getDocument(), value_file);
    521522                // If this is a hierarchy element, write hierarchy file.
    522523                //if(value_element.getNamespace().equals(MetadataSetManager.HIDDEN) || value_tree.isHierarchy()) {
  • trunk/gli/src/org/greenstone/gatherer/gui/LockFileDialog.java

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

    r8313 r8652  
    533533
    534534
    535             Utility.delete(Gatherer.c_man.getCollectionArchive()); // remove current archives
     535            Utility.delete(Gatherer.c_man.getCollectionArchives()); // remove current archives
    536536            GathererApplet.download_url_zip(col_name,"archives");
    537537            Utility.unzip(col_name);
     
    545545        fireMessage(type, typeAsString(type) + "> " + Dictionary.get("GShell.Parsing_Metadata_Start"), status, null);
    546546        DocXMLFileManager.clearDocXMLFiles();
    547         DocXMLFileManager.loadDocXMLFiles(new File(Gatherer.c_man.getCollectionArchive()));
     547        DocXMLFileManager.loadDocXMLFiles(new File(Gatherer.c_man.getCollectionArchives()));
    548548        fireMessage(type, typeAsString(type) + "> " + Dictionary.get("GShell.Parsing_Metadata_Complete"), status, null);
    549549        }
  • trunk/gli/src/org/greenstone/gatherer/util/Utility.java

    r8651 r8652  
    6868    /** The size of the io buffer, calculated as FACTOR * 1024. */
    6969    static final public int BUFFER_SIZE = FACTOR * 1024;
    70     /** Definition of an important directory name, in this case the archives directory of the collection. */
    71     static final public String ARCHIVES_DIR = "archives" + File.separator;
    7270    /** Definition of an important directory name, in this case the base dir, or the working directory of the Gatherer. */
    7371    static public String BASE_DIR  = System.getProperty("user.dir") + File.separator;
     
    8987    /** Definition of an important directory name, in this case the file the collection configuration is expect to be in. */
    9088    static final public String CONFIG_FILE = "etc" + File.separator + "collect.cfg";
    91     /** The default file name for the urls missing any file. */
    92     static final public String DEFAULT_FILE = "index.html";
    93     /** The default protocol header for those urls missing any protocol. */
    94     static final public String DEFAULT_PROTOCOL = "http://";
    95     /** The default dictionary to load. */
    96     static final public String DICTIONARY = "dictionary";
    9789    static final public String ENCODING = "UTF-8";
    9890    static final public String ENGLISH_VALUE = "en";
    99     /** Definition of an important directory name, in this case the etc (or extra information) directory for the collection. */
    100     static final public String ETC_DIR = "etc" + File.separator;
    10191    static final public String EXTRACTED_METADATA_NAMESPACE = "ex";
    10292    /** The location of the default greenstone metadata file. */
     
    110100    /** Definition of an important directory name, in this case the backup import directory for the collection. */
    111101    static final public String IMPORT_BAK_DIR = "import.bak" + File.separator;
    112     /** Definition of an important directory name, in this case the index directory for the collection. */
    113     static final public String INDEX_DIR = "index" + File.separator;
    114     static final public String LANGUAGE_ATTRIBUTE = "language";
    115102    /** Definition of an important directory name, in this case the log directory for the collection. */
    116103    static final public String LOG_DIR = "log" + File.separator;
     
    126113    static final public String METADATA_SET_TEMPLATE = "xml/template.mds";
    127114    static final public String METADATA_VALUE_TEMPLATE = "xml/template.mdv";
    128     static final public String METADATA_XML = "metadata.xml";
    129     static final public String NAME_ELEMENT = "Name";
    130115    /** The default name of the perl executable under unix. */
    131116    static final public String PERL_EXECUTABLE_UNIX = "perl";
     
    151136    static public ImageIcon OFF_ICON = null;
    152137   
    153     /** Decodes a string of text so its safe to use in a Greenstone configuration file. Esentially replaces "\n" with a newline.
    154      * @param raw The <strong>String</strong> before decoding, read from the configuration file..
    155      * @return A <strong>String</strong> ready to be placed in a component.
    156      */
    157     static public String decodeGreenstone(String raw) {
    158     raw = raw.replaceAll("&apos;", "\'");
    159     raw = raw.replaceAll("&gt;", ">");
    160     raw = raw.replaceAll("&lt;", "<");
    161     raw = raw.replaceAll("&quot;", "\"");
    162     raw = raw.replaceAll("&#39;", "\'");
    163     raw = raw.replaceAll("\\\\n", "\n");
    164     return raw;
    165     }
    166 
    167138
    168139    /** It turns out that in Java you have to make sure a directory is empty before you delete it (much like unix I suppose), and so just like unix I'll have to set up a recursive delete function.
     
    190161    }
    191162
    192 
    193     /** 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. */
    194     static public boolean export(Document document, String filename) {
    195     return export(document, new File(filename));
    196     }
    197 
    198     static public boolean export(Document document, File file) {
    199     try {
    200         OutputStream os = new FileOutputStream(file);
    201         // Create an output format for our document.
    202         OutputFormat f = new OutputFormat(document);
    203         f.setEncoding(ENCODING);
    204         f.setIndenting(true);
    205         f.setLineWidth(0); // Why isn't this working!
    206         f.setPreserveSpace(false);
    207         // Create the necessary writer stream for serialization.
    208         OutputStreamWriter osw = new OutputStreamWriter(os, ENCODING);
    209         Writer w               = new BufferedWriter(osw);
    210         // Generate a new serializer from the above.
    211         XMLSerializer s        = new XMLSerializer(w, f);
    212         s.asDOMSerializer();
    213         // Finally serialize the document to file.
    214         s.serialize(document);
    215         // And close.
    216         os.close();
    217         return true;
    218     }
    219     // 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.
    220     catch (Exception exception) {
    221         if(!file.getName().endsWith(METADATA_XML)) {
    222         DebugStream.printStackTrace(exception);
    223         return false;
    224         }
    225         return true;
    226     }
    227     }
    228163
    229164    /** Convert a long, detailing the length of a file in bytes, into a nice human readable string using b, kb, Mb and Gb. */
     
    406341    }
    407342
    408     /** Method which constructs the archive directory given a certain collection.
    409      * @param gsdl_path The location of the collection directory as a <strong>String</strong>.
    410      * @return The location of the given collections archive directory, also as a <strong>String</strong>.
    411      */
    412     static public String getArchiveDir(String col_dir) {
    413     return col_dir + ARCHIVES_DIR;
    414     }
    415343
    416344    /** Builds the cache dir by appending the user path and 'cache'.
     
    492420
    493421
    494     /** Method which constructs the etc directory given a certain collection.
    495      * @param col_dir The location of the collection directory as a <strong>String</strong>.
    496      * @return The location of the given collections etc directory, also as a <strong>String</strong>.
    497      */
    498     public static String getEtcDir(String col_dir) {
    499     return col_dir + ETC_DIR;
    500     }
    501 
    502 
    503422    static final private String APPLICATION_DATA_FOLDER = "Application Data";
    504423    static final private String UNIX_GLI_CONFIG_FOLDER = ".gli";
     
    583502    return col_dir + IMPORT_DIR;
    584503    }
    585     /** Method which constructs the index directory given a certain collection.
    586      * @param col_dir The location of the collection directory as a <strong>String</strong>.
    587      * @return The location of the given collections index directory, also as a <strong>String</strong>.
    588      */
    589     static public String getIndexDir(String col_dir) {
    590     return col_dir + INDEX_DIR;
    591     }
     504
    592505    /** Method which constructs the log directory given a certain collection.
    593506     * @param col_dir The location of the collection directory as a <strong>String</strong>.
     
    613526    return "Unknown Machine";
    614527    }
    615     /** Method which constructs the metadata directory given a certain collection.
    616      * @param col_dir The location of the collection directory as a <strong>String</strong>.
    617      * @return The location of the given collections metadata directory, also as a <strong>String</strong>.
    618      */
    619     static public String getMetadataDir(String col_dir) {
    620     return col_dir + META_DIR;
    621     }
     528
    622529
    623530    static public void extractFromJar(String filename, String dst_dir, boolean must_be_present)
     
    945852
    946853
    947     /** A string is a valid hierarchy index if it matches '[0-9](\.[0-9])*' */
    948     static public boolean isIndex(String raw) {
    949     boolean result = true;
    950     for(int i = 0; result && i < raw.length(); i++) {
    951         char c = raw.charAt(i);
    952         if(Character.isDigit(c) || (c == '.' && (i != 0 || i != raw.length() - 1))) {
    953         // Valid index
    954         }
    955         else {
    956         result = false;
    957         }
    958     }
    959     return result;
    960     }
    961 
    962854    /** Method to determine if the host system is MacOS based.
    963855     * @return a boolean which is true if the platform is MacOS, false otherwise
     
    972864    }
    973865
    974    /** Determines if the given file is a descendant of the folder described.
    975     * @param pos_parent the possible parent folder as a String
    976     * @param child the child File which we are checking
    977     * @return true if the child is a descendant of the pos_parent, false otherwise
    978     */
    979    static public boolean isParentFolderOf(String pos_parent, File child) {
    980       while(child != null) {
    981      if(pos_parent.equals(child.getAbsolutePath())) {
    982         return true;
    983      }
    984      // Just keep looping checking each of childs parent folders. Not a brilliantly efficient algorithm, but it should still be pretty fast compared to the algorithm for merging elements.
    985      child = child.getParentFile();
    986       }
    987       return false;
    988    }
    989866   
    990867    /** Method to determine if the host system is Microsoft Windows based.
     
    1037914        InputStream is = base.getResourceAsStream("/" + filename);
    1038915        if (is != null) {
    1039         return parse(is, true);
     916        return XMLTools.parse(is, true);
    1040917        }
    1041918    }
    1042919
    1043920    // Try the file outside the classes directory
    1044     return parse(new File(filename), true);
    1045     }
    1046 
    1047     /** Parse in a xml document from a given file. */
    1048     static public Document parse(File file) {
    1049     return parse(file, true);
    1050     }
    1051 
    1052     /** Parse in a xml document from a given file. */
    1053     static public Document parse(File file, boolean noisey)
    1054     {
    1055     Document document = null;
    1056     try {
    1057         if (file.exists()) {
    1058         DebugStream.println("Parsing XML file: " + file);
    1059         FileInputStream fis = new FileInputStream(file);
    1060         document = parse(fis, noisey);
    1061         }
    1062     }
    1063     catch (Exception error) {
    1064         if(noisey) {
    1065         error.printStackTrace();
    1066         DebugStream.println("Exception in Utility.parse() - Unexpected");
    1067         }
    1068         else {
    1069         DebugStream.println("Exception in Utility.parse() - Expected");
    1070         DebugStream.printStackTrace(error);
    1071         }
    1072     }
    1073 
    1074     return document;
    1075     }
    1076 
    1077     /** Parse in a xml document from a given URL. */
    1078     static public Document parse(URL url, boolean noisey)
    1079     {
    1080     Document document = null;
    1081     try {
    1082        
    1083         URLConnection connection = url.openConnection();
    1084         InputStream is = connection.getInputStream();
    1085         document = parse(is,noisey);
    1086     }
    1087     catch (Exception error) {
    1088         if(noisey) {
    1089         error.printStackTrace();
    1090         DebugStream.println("Exception in Utility.parse() - Unexpected");
    1091         }
    1092         else {
    1093         DebugStream.println("Exception in Utility.parse() - Expected");
    1094         DebugStream.printStackTrace(error);
    1095         }
    1096     }
    1097 
    1098     return document;
    1099     }
    1100 
    1101     /** Parse in a xml document from a given file. */
    1102     static public Document parse(InputStream is, boolean noisey) {
    1103     Document document = null;
    1104     try {
    1105         InputStreamReader isr = new InputStreamReader(is, ENCODING);
    1106         Reader r              = new BufferedReader(isr);
    1107         InputSource isc       = new InputSource(r);
    1108         DOMParser parser      = new DOMParser();
    1109         parser.setFeature("http://xml.org/sax/features/validation", false);
    1110         parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    1111                 // May or may not be ignored, the documentation for Xerces is contradictory. If it works then parsing -should- be faster.
    1112         parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", true);
    1113         parser.setFeature("http://apache.org/xml/features/dom/include-ignorable-whitespace", false);
    1114         parser.parse(isc);
    1115         document = parser.getDocument();
    1116         isr.close();
    1117         is.close();
    1118         parser = null;
    1119         isc = null;
    1120         r = null;
    1121         isr = null;
    1122         is = null;
    1123     }
    1124     catch (Exception error) {
    1125         if(noisey) {
    1126         error.printStackTrace();
    1127         DebugStream.println("Exception in Utility.parse() - Unexpected");
    1128         }
    1129         else {
    1130         DebugStream.println("Exception in Utility.parse() - Expected");
    1131         }
    1132         DebugStream.printStackTrace(error);
    1133     }
    1134     return document;
     921    return XMLTools.parse(new File(filename), true);
    1135922    }
    1136923
  • trunk/gli/src/org/greenstone/gatherer/util/XMLTools.java

    r8023 r8652  
    33
    44import java.io.*;
     5import java.net.*;
    56import java.util.*;
    67import org.apache.xerces.parsers.*;
    78import org.apache.xml.serialize.*;
     9import org.greenstone.gatherer.DebugStream;
    810import org.w3c.dom.*;
    911import org.xml.sax.*;
     
    167169    }
    168170    }
     171
     172
     173    /** ------------ OLD FUNCTIONS FROM UTILITY ---------------
     174    /** 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. */
     175    static public boolean export(Document document, String filename) {
     176    return export(document, new File(filename));
     177    }
     178
     179    static public boolean export(Document document, File file) {
     180    try {
     181        OutputStream os = new FileOutputStream(file);
     182        // Create an output format for our document.
     183        OutputFormat f = new OutputFormat(document);
     184        f.setEncoding("UTF-8");
     185        f.setIndenting(true);
     186        f.setLineWidth(0); // Why isn't this working!
     187        f.setPreserveSpace(false);
     188        // Create the necessary writer stream for serialization.
     189        OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
     190        Writer w               = new BufferedWriter(osw);
     191        // Generate a new serializer from the above.
     192        XMLSerializer s        = new XMLSerializer(w, f);
     193        s.asDOMSerializer();
     194        // Finally serialize the document to file.
     195        s.serialize(document);
     196        // And close.
     197        os.close();
     198        return true;
     199    }
     200    // 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.
     201    catch (Exception exception) {
     202        if(!file.getName().endsWith(StaticStrings.METADATA_XML)) {
     203        DebugStream.printStackTrace(exception);
     204        return false;
     205        }
     206        return true;
     207    }
     208    }
     209
     210    /** Parse in a xml document from a given file. */
     211    static public Document parse(File file) {
     212    return parse(file, true);
     213    }
     214
     215    /** Parse in a xml document from a given file. */
     216    static public Document parse(File file, boolean noisey)
     217    {
     218    Document document = null;
     219    try {
     220        if (file.exists()) {
     221        DebugStream.println("Parsing XML file: " + file);
     222        FileInputStream fis = new FileInputStream(file);
     223        document = parse(fis, noisey);
     224        }
     225    }
     226    catch (Exception error) {
     227        if(noisey) {
     228        error.printStackTrace();
     229        DebugStream.println("Exception in Utility.parse() - Unexpected");
     230        }
     231        else {
     232        DebugStream.println("Exception in Utility.parse() - Expected");
     233        DebugStream.printStackTrace(error);
     234        }
     235    }
     236
     237    return document;
     238    }
     239
     240    /** Parse in a xml document from a given URL. */
     241    static public Document parse(URL url, boolean noisey)
     242    {
     243    Document document = null;
     244    try {
     245       
     246        URLConnection connection = url.openConnection();
     247        InputStream is = connection.getInputStream();
     248        document = parse(is,noisey);
     249    }
     250    catch (Exception error) {
     251        if(noisey) {
     252        error.printStackTrace();
     253        DebugStream.println("Exception in Utility.parse() - Unexpected");
     254        }
     255        else {
     256        DebugStream.println("Exception in Utility.parse() - Expected");
     257        DebugStream.printStackTrace(error);
     258        }
     259    }
     260
     261    return document;
     262    }
     263
     264    /** Parse in a xml document from a given file. */
     265    static public Document parse(InputStream is, boolean noisey) {
     266    Document document = null;
     267    try {
     268        InputStreamReader isr = new InputStreamReader(is, "UTF-8");
     269        Reader r              = new BufferedReader(isr);
     270        InputSource isc       = new InputSource(r);
     271        DOMParser parser      = new DOMParser();
     272        parser.setFeature("http://xml.org/sax/features/validation", false);
     273        parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
     274                // May or may not be ignored, the documentation for Xerces is contradictory. If it works then parsing -should- be faster.
     275        parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", true);
     276        parser.setFeature("http://apache.org/xml/features/dom/include-ignorable-whitespace", false);
     277        parser.parse(isc);
     278        document = parser.getDocument();
     279        isr.close();
     280        is.close();
     281        parser = null;
     282        isc = null;
     283        r = null;
     284        isr = null;
     285        is = null;
     286    }
     287    catch (Exception error) {
     288        if(noisey) {
     289        error.printStackTrace();
     290        DebugStream.println("Exception in Utility.parse() - Unexpected");
     291        }
     292        else {
     293        DebugStream.println("Exception in Utility.parse() - Expected");
     294        }
     295        DebugStream.printStackTrace(error);
     296    }
     297    return document;
     298    }
    169299}
Note: See TracChangeset for help on using the changeset viewer.