View Javadoc
1 /* 2 Copyright (C) 2003 Together 3 4 This library is free software; you can redistribute it and/or 5 modify it under the terms of the GNU Lesser General Public 6 License as published by the Free Software Foundation; either 7 version 2.1 of the License, or (at your option) any later version. 8 9 This library is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 Lesser General Public License for more details. 13 14 You should have received a copy of the GNU Lesser General Public 15 License along with this library; if not, write to the Free Software 16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 */ 18 19 package org.enhydra.xml; 20 21 import java.io.File; 22 import java.io.FileOutputStream; 23 import java.io.IOException; 24 import java.util.Properties; 25 26 //import org.apache.xerces.parsers.DOMParser; 27 import javax.xml.parsers.DocumentBuilderFactory; 28 import javax.xml.parsers.DocumentBuilder; 29 import org.xml.sax.ErrorHandler; 30 import org.xml.sax.SAXException; 31 import org.xml.sax.SAXParseException; 32 import javax.xml.parsers.ParserConfigurationException; 33 import org.w3c.dom.Document; 34 import org.w3c.dom.Node; 35 import org.xml.sax.SAXException; 36 37 38 39 /*** 40 * @author Tweety 41 * 42 * A class for manipulating the entire xml file (reading, writing...). 43 * 44 * @version 1.0 45 */ 46 public class XMLDocumentFactory { 47 48 private static String[] properties = { 49 "method", 50 "version", 51 "encoding", 52 "omit-xml-declaration", 53 "standalone", 54 "doctype-public", 55 "doctype-system", 56 "indent", 57 "media-type" 58 }; 59 60 61 private static int METHOD = 0; 62 private static int VERSION = 1; 63 private static int ENCODING = 2; 64 private static int OMIT_XML_DECLARATION = 3; 65 private static int STANDALONE = 4; 66 private static int DOCTYPE_PUBLIC = 5; 67 private static int DOCTYPE_SYSTEM = 6; 68 private static int CDATA_SECTION_ELEMENTS = 7; 69 private static int INDENT = 8; 70 private static int MEDIA_TYPE = 9; 71 72 /*** 73 * xml file name. 74 */ 75 private String fileName; 76 77 78 /*** 79 * Constructs an empty <code>XMLDocumentFactory</code> 80 */ 81 public XMLDocumentFactory() { 82 } 83 84 85 /*** 86 * Constructs a <code>XMLDocumentFactory</code> with the given 87 * @param fileName as <code>String</code> 88 */ 89 public XMLDocumentFactory(String fileName) { 90 this.fileName = fileName; 91 } 92 93 94 /*** 95 * Returns xml file name. 96 * 97 * @return xml file name. 98 */ 99 public String getFileName() { 100 return this.fileName; 101 } 102 103 104 /*** 105 * Parses xml file with the given name and creates <code>Document</code>. 106 * 107 * @param fileName xml file name. 108 * 109 * @return document. 110 */ 111 public static Document parse(String fileName) { 112 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 113 try { 114 DocumentBuilder builder = factory.newDocumentBuilder(); 115 builder.setErrorHandler(new UtilErrorHandler()); 116 Document doc = builder.parse(fileName); 117 return doc; 118 } catch (SAXParseException e) { 119 e.printStackTrace(); 120 } catch (ParserConfigurationException e) { 121 e.printStackTrace(); 122 } catch (IOException e) { 123 e.printStackTrace(); 124 } catch (SAXException e) { 125 e.printStackTrace(); 126 } 127 return null; 128 129 //OLD with apache xerces 130 // DOMParser parser = new DOMParser(); 131 // try { 132 // parser.parse(fileName); 133 // return parser.getDocument(); 134 // } catch (SAXException e) { 135 // e.printStackTrace(); 136 // } catch (IOException e) { 137 // e.printStackTrace(); 138 // } 139 // return null; 140 } 141 142 143 /*** 144 * Parses xml file and creates creates <code>Document</code>. 145 * @return object of document 146 */ 147 public Document parse() { 148 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 149 try { 150 DocumentBuilder builder = factory.newDocumentBuilder(); 151 builder.setErrorHandler(new UtilErrorHandler()); 152 Document doc = builder.parse(fileName); 153 return doc; 154 } catch (SAXParseException e) { 155 e.printStackTrace(); 156 } catch (ParserConfigurationException e) { 157 e.printStackTrace(); 158 } catch (IOException e) { 159 e.printStackTrace(); 160 } catch (SAXException e) { 161 e.printStackTrace(); 162 } 163 return null; 164 165 //OLD with apache xerces 166 // DOMParser parser = new DOMParser(); 167 // try { 168 // parser.parse(this.fileName); 169 // return parser.getDocument(); 170 // } catch (SAXException e) { 171 // System.err.println("SAXException - bad xml format"); 172 // } catch (IOException e) { 173 // } 174 // return null; 175 } 176 177 178 /*** 179 * Serializes node with all subnodes to the xml file with the given name, 180 * and with the <code>Properties</code> of the xml declaration. 181 * 182 * @param node root node of the document. 183 * @param fileName xml file name 184 * @param prop <code>Properties</code> of the xml declaration. 185 */ 186 public static void serialize(Node node, String fileName, Properties prop) { 187 String out = "<?xml version=\"1.0\"?>"; 188 File file = new File(fileName); 189 190 //serialize xml declaration 191 if (prop != null) { 192 out = "<?xml"; 193 String str = ""; 194 for (int i=0; i<properties.length; i++) { 195 str = (String)prop.get(properties[i]); 196 if (str != null) 197 out += " "+properties[i]+"=\""+str+"\""; 198 } 199 out += "?>"; 200 } 201 202 //serialize document 203 try { 204 FileOutputStream outStream = new FileOutputStream(file); 205 out += node.toString(); 206 outStream.write(out.getBytes()); 207 outStream.close(); 208 } catch(Exception e) { 209 System.err.println("Error serializing file"); 210 } 211 } 212 213 214 /*** 215 * Serializes node with all subnodes to the xml file 216 * with the default <code>Properties</code> of the xml declaration. 217 * 218 * @param node root node of the document. 219 */ 220 public void serialize(Node node) { 221 222 File file = new File(fileName); 223 try { 224 FileOutputStream outStream = new FileOutputStream(file); 225 outStream.write(node.toString().getBytes()); 226 outStream.close(); 227 } catch(Exception e) { 228 System.err.println("Error serializing file"); 229 } 230 } 231 232 static class UtilErrorHandler implements ErrorHandler 233 { 234 235 // throw SAXException for fatal errors 236 public void fatalError( SAXParseException exception ) throws SAXException 237 { 238 throw new SAXException(exception); 239 } 240 241 public void error( SAXParseException errorException ) throws SAXException 242 { 243 throw new SAXException(errorException); 244 } 245 246 // print any warnings 247 public void warning( SAXParseException warningError ) throws SAXException 248 { 249 System.err.println("[Validation : Warning] URI = " + warningError.getMessage()); 250 } 251 } 252 253 254 255 }

This page was automatically generated by Maven