Changeset 8652
- Timestamp:
- 2004-11-24T15:00:44+13:00 (19 years ago)
- Location:
- trunk/gli/src/org/greenstone/gatherer
- Files:
-
- 14 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gli/src/org/greenstone/gatherer/Configuration.java
r8627 r8652 626 626 ///ystem.err.println("Writing."); 627 627 user_config_xml.getParentFile().mkdirs(); 628 Utility.export(general_config, user_config_xml.getAbsolutePath());628 XMLTools.export(general_config, user_config_xml.getAbsolutePath()); 629 629 } 630 630 catch(Exception exception) { … … 634 634 if(user_config_xml == null || !user_config_xml.exists()) { 635 635 // 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); 637 637 } 638 638 } -
trunk/gli/src/org/greenstone/gatherer/Dictionary.java
r8591 r8652 86 86 this.font = font; 87 87 this.locale = ((locale == null) ? Locale.getDefault() : locale); 88 this.dictionary = ResourceBundle.getBundle( Utility.DICTIONARY, this.locale);88 this.dictionary = ResourceBundle.getBundle("dictionary", this.locale); 89 89 } 90 90 -
trunk/gli/src/org/greenstone/gatherer/cdm/CollectionConfiguration.java
r8474 r8652 329 329 if(collect_config_name.equals(COLLECTCONFIGURATION_XML)) { 330 330 // 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); 332 332 } 333 333 // Otherwise if this is a legacy collect.cfg file then read in the template and send to magic parser … … 470 470 if(force_xml || collect_config_file.getName().equals(COLLECTCONFIGURATION_XML)) { 471 471 ///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)); 473 473 } 474 474 else { -
trunk/gli/src/org/greenstone/gatherer/collection/BuildOptions.java
r8236 r8652 421 421 } 422 422 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); 428 424 arguments_element = document.getDocumentElement(); 429 425 } -
trunk/gli/src/org/greenstone/gatherer/collection/Collection.java
r8586 r8652 88 88 this.file = collection_xml; 89 89 // Try to load this collections details. 90 document = Utility.parse(collection_xml, false);90 document = XMLTools.parse(collection_xml, false); 91 91 // If that fails load the default settings for a collection. 92 92 if(document == null) { … … 187 187 /** Save this xml document to the given file. */ 188 188 public void save() { 189 Utility.export(document, file);189 XMLTools.export(document, file); 190 190 saved = true; 191 191 } -
trunk/gli/src/org/greenstone/gatherer/collection/CollectionManager.java
r8651 r8652 521 521 date_element = null; 522 522 date_time = null; 523 Utility.export(default_lockfile, destination);523 XMLTools.export(default_lockfile, destination); 524 524 } 525 525 catch (Exception error) { … … 553 553 * @see org.greenstone.gatherer.util.Utility 554 554 */ 555 public String getCollectionArchive () {555 public String getCollectionArchives() { 556 556 if (Gatherer.GS3) { 557 return Utility.get ArchiveDir(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; 558 558 } else { 559 return Utility.get ArchiveDir(Utility.getCollectionDir(Configuration.gsdl_path, collection.getName()));559 return Utility.getCollectionDir(Configuration.gsdl_path, collection.getName()) + "archives" + File.separator; 560 560 } 561 561 } … … 613 613 public String getCollectionEtc() { 614 614 if (Gatherer.GS3) { 615 return Utility.get EtcDir(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; 616 616 } else { 617 return Utility.get EtcDir(Utility.getCollectionDir(Configuration.gsdl_path, collection.getName()));617 return Utility.getCollectionDir(Configuration.gsdl_path, collection.getName()) + "etc" + File.separator; 618 618 } 619 619 } … … 669 669 public String getCollectionIndex() { 670 670 if (Gatherer.GS3) { 671 return Utility.get IndexDir(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; 672 672 } else { 673 return Utility.get IndexDir(Utility.getCollectionDir(Configuration.gsdl_path, collection.getName()));673 return Utility.getCollectionDir(Configuration.gsdl_path, collection.getName()) + "index" + File.separator; 674 674 } 675 675 } … … 697 697 public String getCollectionMetadata() { 698 698 if (Gatherer.GS3) { 699 return Utility.get MetadataDir(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; 700 700 } 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 715 705 716 706 /** Retrieve the record set (tree model) associated with the current collection. */ … … 974 964 975 965 // 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()); 977 967 DocXMLFileManager.clearDocXMLFiles(); 978 968 DocXMLFileManager.loadDocXMLFiles(collection_archives_directory); -
trunk/gli/src/org/greenstone/gatherer/file/FileQueue.java
r8600 r8652 41 41 import org.greenstone.gatherer.metadata.MetadataXMLFileManager; 42 42 import org.greenstone.gatherer.util.DragComponent; 43 import org.greenstone.gatherer.util.StaticStrings; 43 44 import org.greenstone.gatherer.util.SynchronizedTreeModelTools; 44 45 import org.greenstone.gatherer.util.Utility; … … 444 445 445 446 // 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)) { 447 448 MetadataXMLFileManager.unloadMetadataXMLFile(source_file); 448 449 } -
trunk/gli/src/org/greenstone/gatherer/gems/GValueModel.java
r8270 r8652 149 149 ///ystem.err.println("Retrieve the value for: " + index_str); 150 150 GValueNode result = null; 151 if(isHierarchy() && Utility.isIndex(index_str)) {151 if(isHierarchy() && isIndex(index_str)) { 152 152 // StringTokenize the index 153 153 StringTokenizer tokenizer = new StringTokenizer(index_str, "."); … … 180 180 } 181 181 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 182 198 public boolean isHierarchy() { 183 199 boolean result = false; -
trunk/gli/src/org/greenstone/gatherer/gems/MetadataSet.java
r8581 r8652 46 46 import org.greenstone.gatherer.util.StaticStrings; 47 47 import org.greenstone.gatherer.util.Utility; 48 import org.greenstone.gatherer.util.XMLTools; 48 49 import org.w3c.dom.*; 49 50 /** An semi-data class to hold details about a loaded metadata set, it also provides some methods to manipulating the data within. … … 96 97 this.file = file; 97 98 this.value_trees = new Hashtable(); 98 this.document = Utility.parse(file, false);99 this.document = XMLTools.parse(file, false); 99 100 100 101 init(false); // don't use class loader … … 502 503 Element name_element = null; 503 504 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); 505 506 for(int i = 0; i < name_elements.getLength(); i++) { 506 507 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)) { 508 509 // Found it. 509 510 name_element = possible_name_element; … … 512 513 // 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. 513 514 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); 516 517 metadataset_element.insertBefore(name_element, metadataset_element.getFirstChild()); 517 518 } … … 627 628 } 628 629 else { 629 value_document = Utility.parse(value_file, false);630 value_document = XMLTools.parse(value_file, false); 630 631 } 631 632 if(value_document != null) { -
trunk/gli/src/org/greenstone/gatherer/gems/MetadataSetManager.java
r8581 r8652 48 48 import org.greenstone.gatherer.collection.CollectionManager; 49 49 import org.greenstone.gatherer.file.*; 50 import org.greenstone.gatherer.util.XMLTools; 50 51 import org.apache.xerces.parsers.*; 51 52 import org.apache.xml.serialize.*; … … 503 504 504 505 // System.out.println("::: " + mds_file.toString()); 505 Utility.export(set.getDocument(), mds_file);506 XMLTools.export(set.getDocument(), mds_file); 506 507 507 508 set.setSetChanged(false); … … 518 519 //File value_file = new File(file, value_element.getName() + ".mdv"); 519 520 ///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); 521 522 // If this is a hierarchy element, write hierarchy file. 522 523 //if(value_element.getNamespace().equals(MetadataSetManager.HIDDEN) || value_tree.isHierarchy()) { -
trunk/gli/src/org/greenstone/gatherer/gui/LockFileDialog.java
r8243 r8652 71 71 72 72 // 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); 74 74 75 75 // Creation -
trunk/gli/src/org/greenstone/gatherer/shell/GShell.java
r8313 r8652 533 533 534 534 535 Utility.delete(Gatherer.c_man.getCollectionArchive ()); // remove current archives535 Utility.delete(Gatherer.c_man.getCollectionArchives()); // remove current archives 536 536 GathererApplet.download_url_zip(col_name,"archives"); 537 537 Utility.unzip(col_name); … … 545 545 fireMessage(type, typeAsString(type) + "> " + Dictionary.get("GShell.Parsing_Metadata_Start"), status, null); 546 546 DocXMLFileManager.clearDocXMLFiles(); 547 DocXMLFileManager.loadDocXMLFiles(new File(Gatherer.c_man.getCollectionArchive ()));547 DocXMLFileManager.loadDocXMLFiles(new File(Gatherer.c_man.getCollectionArchives())); 548 548 fireMessage(type, typeAsString(type) + "> " + Dictionary.get("GShell.Parsing_Metadata_Complete"), status, null); 549 549 } -
trunk/gli/src/org/greenstone/gatherer/util/Utility.java
r8651 r8652 68 68 /** The size of the io buffer, calculated as FACTOR * 1024. */ 69 69 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;72 70 /** Definition of an important directory name, in this case the base dir, or the working directory of the Gatherer. */ 73 71 static public String BASE_DIR = System.getProperty("user.dir") + File.separator; … … 89 87 /** Definition of an important directory name, in this case the file the collection configuration is expect to be in. */ 90 88 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";97 89 static final public String ENCODING = "UTF-8"; 98 90 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;101 91 static final public String EXTRACTED_METADATA_NAMESPACE = "ex"; 102 92 /** The location of the default greenstone metadata file. */ … … 110 100 /** Definition of an important directory name, in this case the backup import directory for the collection. */ 111 101 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";115 102 /** Definition of an important directory name, in this case the log directory for the collection. */ 116 103 static final public String LOG_DIR = "log" + File.separator; … … 126 113 static final public String METADATA_SET_TEMPLATE = "xml/template.mds"; 127 114 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";130 115 /** The default name of the perl executable under unix. */ 131 116 static final public String PERL_EXECUTABLE_UNIX = "perl"; … … 151 136 static public ImageIcon OFF_ICON = null; 152 137 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("'", "\'");159 raw = raw.replaceAll(">", ">");160 raw = raw.replaceAll("<", "<");161 raw = raw.replaceAll(""", "\"");162 raw = raw.replaceAll("'", "\'");163 raw = raw.replaceAll("\\\\n", "\n");164 return raw;165 }166 167 138 168 139 /** 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. … … 190 161 } 191 162 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 }228 163 229 164 /** Convert a long, detailing the length of a file in bytes, into a nice human readable string using b, kb, Mb and Gb. */ … … 406 341 } 407 342 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 }415 343 416 344 /** Builds the cache dir by appending the user path and 'cache'. … … 492 420 493 421 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 503 422 static final private String APPLICATION_DATA_FOLDER = "Application Data"; 504 423 static final private String UNIX_GLI_CONFIG_FOLDER = ".gli"; … … 583 502 return col_dir + IMPORT_DIR; 584 503 } 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 592 505 /** Method which constructs the log directory given a certain collection. 593 506 * @param col_dir The location of the collection directory as a <strong>String</strong>. … … 613 526 return "Unknown Machine"; 614 527 } 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 622 529 623 530 static public void extractFromJar(String filename, String dst_dir, boolean must_be_present) … … 945 852 946 853 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 index954 }955 else {956 result = false;957 }958 }959 return result;960 }961 962 854 /** Method to determine if the host system is MacOS based. 963 855 * @return a boolean which is true if the platform is MacOS, false otherwise … … 972 864 } 973 865 974 /** Determines if the given file is a descendant of the folder described.975 * @param pos_parent the possible parent folder as a String976 * @param child the child File which we are checking977 * @return true if the child is a descendant of the pos_parent, false otherwise978 */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 }989 866 990 867 /** Method to determine if the host system is Microsoft Windows based. … … 1037 914 InputStream is = base.getResourceAsStream("/" + filename); 1038 915 if (is != null) { 1039 return parse(is, true);916 return XMLTools.parse(is, true); 1040 917 } 1041 918 } 1042 919 1043 920 // 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); 1135 922 } 1136 923 -
trunk/gli/src/org/greenstone/gatherer/util/XMLTools.java
r8023 r8652 3 3 4 4 import java.io.*; 5 import java.net.*; 5 6 import java.util.*; 6 7 import org.apache.xerces.parsers.*; 7 8 import org.apache.xml.serialize.*; 9 import org.greenstone.gatherer.DebugStream; 8 10 import org.w3c.dom.*; 9 11 import org.xml.sax.*; … … 167 169 } 168 170 } 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 } 169 299 }
Note:
See TracChangeset
for help on using the changeset viewer.