1 package org.enhydra.xml; 2 3 import java.io.File ; 4 import java.io.FileOutputStream ; 5 import java.io.IOException ; 6 import java.util.Properties ; 7 8 import javax.xml.parsers.DocumentBuilder ; 9 import javax.xml.parsers.DocumentBuilderFactory ; 10 import javax.xml.parsers.ParserConfigurationException ; 11 12 import org.w3c.dom.Document ; 13 import org.w3c.dom.Node ; 14 import org.xml.sax.ErrorHandler ; 15 import org.xml.sax.SAXException ; 16 import org.xml.sax.SAXParseException ; 17 18 19 20 27 public class XMLDocumentFactory { 28 29 private static String [] properties = { 30 "method", 31 "version", 32 "encoding", 33 "omit-xml-declaration", 34 "standalone", 35 "doctype-public", 36 "doctype-system", 37 "indent", 38 "media-type" 39 }; 40 41 42 private static int METHOD = 0; 43 private static int VERSION = 1; 44 private static int ENCODING = 2; 45 private static int OMIT_XML_DECLARATION = 3; 46 private static int STANDALONE = 4; 47 private static int DOCTYPE_PUBLIC = 5; 48 private static int DOCTYPE_SYSTEM = 6; 49 private static int CDATA_SECTION_ELEMENTS = 7; 50 private static int INDENT = 8; 51 private static int MEDIA_TYPE = 9; 52 53 56 private String fileName; 57 58 59 62 public XMLDocumentFactory() { 63 } 64 65 66 70 public XMLDocumentFactory(String fileName) { 71 this.fileName = fileName; 72 } 73 74 75 80 public String getFileName() { 81 return this.fileName; 82 } 83 84 85 92 public static Document parse(String fileName) { 93 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 94 try { 95 DocumentBuilder builder = factory.newDocumentBuilder(); 96 builder.setErrorHandler(new UtilErrorHandler()); 97 Document doc = builder.parse(fileName); 98 return doc; 99 } catch (SAXParseException e) { 100 e.printStackTrace(); 101 } catch (ParserConfigurationException e) { 102 e.printStackTrace(); 103 } catch (IOException e) { 104 e.printStackTrace(); 105 } catch (SAXException e) { 106 e.printStackTrace(); 107 } 108 return null; 109 110 } 122 123 124 127 public Document parse() { 128 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 129 try { 130 DocumentBuilder builder = factory.newDocumentBuilder(); 131 builder.setErrorHandler(new UtilErrorHandler()); 132 Document doc = builder.parse(fileName); 133 return doc; 134 } catch (SAXParseException e) { 135 e.printStackTrace(); 136 } catch (ParserConfigurationException e) { 137 e.printStackTrace(); 138 } catch (IOException e) { 139 e.printStackTrace(); 140 } catch (SAXException e) { 141 e.printStackTrace(); 142 } 143 return null; 144 145 } 156 157 158 166 public static void serialize(Node node, String fileName, Properties prop) { 167 String out = "<?xml version=\"1.0\"?>"; 168 File file = new File (fileName); 169 170 if (prop != null) { 172 out = "<?xml"; 173 String str = ""; 174 for (int i=0; i<properties.length; i++) { 175 str = (String )prop.get(properties[i]); 176 if (str != null) 177 out += " "+properties[i]+"=\""+str+"\""; 178 } 179 out += "?>"; 180 } 181 182 try { 184 FileOutputStream outStream = new FileOutputStream (file); 185 out += node.toString(); 186 outStream.write(out.getBytes()); 187 outStream.close(); 188 } catch(Exception e) { 189 System.err.println("Error serializing file"); 190 } 191 } 192 193 194 200 public void serialize(Node node) { 201 202 204 File file = new File (fileName); 205 try { 206 FileOutputStream outStream = new FileOutputStream (file); 207 outStream.write(node.toString().getBytes()); 208 outStream.close(); 209 } catch(Exception e) { 210 System.err.println("Error serializing file"); 211 } 212 } 213 214 static class UtilErrorHandler implements ErrorHandler 215 { 216 217 public void fatalError( SAXParseException exception ) throws SAXException 219 { 220 throw new SAXException (exception); 221 } 222 223 public void error( SAXParseException errorException ) throws SAXException 224 { 225 throw new SAXException (errorException); 226 } 227 228 public void warning( SAXParseException warningError ) throws SAXException 230 { 231 System.err.println("[Validation : Warning] URI = " + warningError.getMessage()); 232 } 233 } 234 235 236 237 } 238 | Popular Tags |