- Timestamp:
- 2005-02-15T11:25:50+13:00 (19 years ago)
- Location:
- trunk/gli/src/org/greenstone/gatherer
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gli/src/org/greenstone/gatherer/Configuration.java
r8974 r9036 618 618 ///ystem.err.println("Writing."); 619 619 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); 621 621 } 622 622 catch(Exception exception) { … … 626 626 if(user_config_xml == null || !user_config_xml.exists()) { 627 627 // 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); 629 629 } 630 630 } -
trunk/gli/src/org/greenstone/gatherer/cdm/CollectionConfiguration.java
r8652 r9036 329 329 if(collect_config_name.equals(COLLECTCONFIGURATION_XML)) { 330 330 // Parse with Utility but don't use class loader 331 document = XMLTools.parse (collect_config_file, false);331 document = XMLTools.parseXMLFile(collect_config_file); 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 XMLTools. export(document, new File(collect_config_file.getParentFile(), COLLECTCONFIGURATION_XML));472 XMLTools.writeXMLFile(new File(collect_config_file.getParentFile(), COLLECTCONFIGURATION_XML), document); 473 473 } 474 474 else { -
trunk/gli/src/org/greenstone/gatherer/collection/BuildOptions.java
r8652 r9036 421 421 } 422 422 423 Document document = XMLTools.parse (input_stream,false);423 Document document = XMLTools.parseXML(input_stream); 424 424 arguments_element = document.getDocumentElement(); 425 425 } -
trunk/gli/src/org/greenstone/gatherer/collection/Collection.java
r8846 r9036 87 87 this.file = collection_xml; 88 88 // Try to load this collections details. 89 document = XMLTools.parse (collection_xml, false);89 document = XMLTools.parseXMLFile(collection_xml); 90 90 // If that fails load the default settings for a collection. 91 91 if(document == null) { … … 186 186 /** Save this xml document to the given file. */ 187 187 public void save() { 188 XMLTools. export(document, file);188 XMLTools.writeXMLFile(file, document); 189 189 saved = true; 190 190 } -
trunk/gli/src/org/greenstone/gatherer/collection/CollectionManager.java
r9019 r9036 488 488 date_element = null; 489 489 date_time = null; 490 XMLTools. export(default_lockfile, destination);490 XMLTools.writeXMLFile(destination, default_lockfile); 491 491 } 492 492 catch (Exception error) { -
trunk/gli/src/org/greenstone/gatherer/gems/MetadataSet.java
r8978 r9036 97 97 this.file = file; 98 98 this.value_trees = new Hashtable(); 99 this.document = XMLTools.parse (file, false);99 this.document = XMLTools.parseXMLFile(file); 100 100 101 101 init(false); // don't use class loader … … 694 694 } 695 695 else { 696 value_document = XMLTools.parse (value_file, false);696 value_document = XMLTools.parseXMLFile(value_file); 697 697 } 698 698 if(value_document != null) { -
trunk/gli/src/org/greenstone/gatherer/gems/MetadataSetManager.java
r8971 r9036 566 566 567 567 // System.out.println("::: " + mds_file.toString()); 568 XMLTools. export(set.getDocument(), mds_file);568 XMLTools.writeXMLFile(mds_file, set.getDocument()); 569 569 570 570 set.setSetChanged(false); -
trunk/gli/src/org/greenstone/gatherer/gui/LockFileDialog.java
r8652 r9036 70 70 this.self = this; 71 71 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); 74 74 75 75 // Creation -
trunk/gli/src/org/greenstone/gatherer/util/Utility.java
r9021 r9036 906 906 InputStream is = base.getResourceAsStream("/" + filename); 907 907 if (is != null) { 908 return XMLTools.parse (is, true);908 return XMLTools.parseXML(is); 909 909 } 910 910 } 911 911 912 912 // Try the file outside the classes directory 913 return XMLTools.parse (new File(filename), true);913 return XMLTools.parseXMLFile(new File(filename)); 914 914 } 915 915 -
trunk/gli/src/org/greenstone/gatherer/util/XMLTools.java
r8695 r9036 102 102 static public Document parseXMLFile(File xml_file) 103 103 { 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 { 104 117 Document document = null; 105 118 106 119 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"); 109 121 Reader r = new BufferedReader(isr); 110 122 InputSource isc = new InputSource(r); … … 118 130 document = parser.getDocument(); 119 131 isr.close(); 120 fis.close();132 xml_input_stream.close(); 121 133 } 122 134 catch (Exception exception) { … … 189 201 } 190 202 } 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 }319 203 }
Note:
See TracChangeset
for help on using the changeset viewer.