1 package org.columba.core.xml; 17 18 import java.io.BufferedWriter ; 19 import java.io.CharArrayWriter ; 20 import java.io.FileOutputStream ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.OutputStream ; 24 import java.io.OutputStreamWriter ; 25 import java.io.Writer ; 26 import java.net.URL ; 27 import java.util.Enumeration ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.logging.Logger ; 31 32 import javax.swing.JOptionPane ; 33 import javax.xml.parsers.SAXParser ; 34 import javax.xml.parsers.SAXParserFactory ; 35 36 import org.columba.core.logging.Logging; 37 import org.xml.sax.Attributes ; 38 import org.xml.sax.SAXException ; 39 import org.xml.sax.helpers.DefaultHandler ; 40 41 public class XmlIO extends DefaultHandler { 42 43 private static final Logger LOG = Logger.getLogger("org.columba.core.xml"); 44 45 private static final String ROOT_XML_ELEMENT_NAME = "__COLUMBA_XML_TREE_TOP__"; 46 47 private XmlElement rootElement; 49 50 private XmlElement currentElement; 52 53 private int writeIndent = 2; 56 57 private int maxOneLineData = 20; 59 60 private CharArrayWriter contents = new CharArrayWriter (); 63 64 private URL url = null; 65 66 69 74 public XmlIO(URL url) { 75 super(); 76 this.url = url; 77 } 78 79 public XmlIO() { 81 currentElement = null; 82 } 83 84 86 92 public XmlIO(XmlElement element) { 93 rootElement = new XmlElement(ROOT_XML_ELEMENT_NAME); 94 rootElement.addElement(element); 95 } 96 97 public void setURL(URL url) { 98 this.url = url; 99 } 100 101 public boolean load() { 102 return load(url); 104 } 105 106 108 114 public boolean load(InputStream input) { 115 116 rootElement = new XmlElement(ROOT_XML_ELEMENT_NAME); 117 currentElement = rootElement; 118 119 try { 120 SAXParserFactory factory = SAXParserFactory.newInstance(); 123 124 SAXParser saxParser = factory.newSAXParser(); 127 128 saxParser.parse(input, this); 129 } catch (javax.xml.parsers.ParserConfigurationException ex) { 130 LOG 131 .severe("XML config error while attempting to read from the input stream \n'" 132 + input + "'"); 133 LOG.severe(ex.toString()); 134 ex.printStackTrace(); 135 136 return (false); 137 } catch (SAXException ex) { 138 LOG 140 .severe("XML parse error while attempting to read from the input stream \n'" 141 + input + "'"); 142 LOG.severe(ex.toString()); 143 ex.printStackTrace(); 144 145 return (false); 146 } catch (IOException ex) { 147 LOG 148 .severe("I/O error while attempting to read from the input stream \n'" 149 + input + "'"); 150 LOG.severe(ex.toString()); 151 ex.printStackTrace(); 152 153 return (false); 154 } 155 156 return (true); 158 } 159 160 166 public boolean load(URL inputURL) { 167 168 rootElement = new XmlElement(ROOT_XML_ELEMENT_NAME); 169 currentElement = rootElement; 170 171 try { 172 SAXParserFactory factory = SAXParserFactory.newInstance(); 175 176 SAXParser saxParser = factory.newSAXParser(); 179 180 saxParser.parse(inputURL.toString(), this); 181 } catch (javax.xml.parsers.ParserConfigurationException ex) { 182 LOG.severe("XML config error while attempting to read XML file \n'" 183 + inputURL + "'"); 184 LOG.severe(ex.toString()); 185 if ( Logging.DEBUG) 186 ex.printStackTrace(); 187 188 return (false); 189 } catch (SAXException ex) { 190 LOG.severe("XML parse error while attempting to read XML file \n'" 192 + inputURL + "'"); 193 LOG.severe(ex.toString()); 194 if ( Logging.DEBUG) 195 ex.printStackTrace(); 196 197 return (false); 198 } catch (IOException ex) { 199 LOG.severe("I/O error while attempting to read XML file \n'" 200 + inputURL + "'"); 201 LOG.severe(ex.toString()); 202 if ( Logging.DEBUG) 203 ex.printStackTrace(); 204 205 return (false); 206 } 207 208 return (true); 210 } 211 212 public void startElement(String namespaceURI, String localName, 215 String qName, Attributes attrs) throws SAXException { 216 try { 225 contents.reset(); 226 227 String name = localName; 229 if (name.equals("")) { 230 name = qName; } 232 233 XmlElement p = currentElement; 234 235 currentElement = currentElement.addSubElement(name); 236 currentElement.setParent(p); 237 238 if (attrs != null) { 239 for (int i = 0; i < attrs.getLength(); i++) { 240 String aName = attrs.getLocalName(i); 242 if (aName.equals("")) { 243 aName = attrs.getQName(i); 244 } 245 246 currentElement.addAttribute(aName, attrs.getValue(i)); 247 } 248 } 249 } catch (java.lang.NullPointerException ex) { 250 LOG.severe("Null!!!"); 251 LOG.severe(ex.toString()); 252 ex.printStackTrace(); 253 } 254 } 255 256 public void endElement(String namespaceURI, String localName, String qName) 257 throws SAXException { 258 currentElement.setData(contents.toString().trim()); 259 contents.reset(); 260 261 currentElement = currentElement.getParent(); 262 } 263 264 public void characters(char[] ch, int start, int length) 265 throws SAXException { 266 contents.write(ch, start, length); 268 } 269 270 280 public XmlElement getRoot() { 281 return (rootElement); 282 } 283 284 public void errorDialog(String Msg) { 285 JOptionPane.showMessageDialog(null, "Error: " + Msg); 286 } 287 288 public void warningDialog(String Msg) { 289 JOptionPane.showMessageDialog(null, "Warning: " + Msg); 290 } 291 292 public void infoDialog(String Msg) { 293 JOptionPane.showMessageDialog(null, "Info: " + Msg); 294 } 295 296 public void save() throws Exception { 297 write(new FileOutputStream (url.getPath())); 298 } 299 300 public void write(OutputStream out) throws IOException { 304 BufferedWriter PW = new BufferedWriter (new OutputStreamWriter (out, 305 "UTF-8")); 306 PW.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); 307 308 if (rootElement.subElements.size() > 0) { 309 for (int i = 0; i < rootElement.subElements.size(); i++) { 310 _writeSubNode(PW, (XmlElement) rootElement.subElements.get(i), 311 0); 312 } 313 } 314 315 PW.flush(); 316 } 317 318 private void _writeSubNode(Writer out, XmlElement element, int indent) 319 throws IOException { 320 _writeSpace(out, indent); 321 out.write("<"); 322 out.write(element.getName()); 323 324 for (Enumeration e = element.getAttributeNames(); e.hasMoreElements();) { 325 String K = (String ) e.nextElement(); 326 out.write(" " + K + "=\"" 327 + TextUtils.escapeText(element.getAttribute(K)) + "\""); 328 } 329 330 out.write(">"); 331 332 String data = element.getData(); 333 334 if ((data != null) && !data.equals("")) { 335 if (data.length() > maxOneLineData) { 336 out.write("\n"); 337 _writeSpace(out, indent + writeIndent); 338 } 339 340 out.write(TextUtils.escapeText(data)); 341 } 342 343 List subElements = element.getElements(); 344 345 if (subElements.size() > 0) { 346 out.write("\n"); 347 348 for (Iterator it = subElements.iterator(); it.hasNext();) { 349 _writeSubNode(out, (XmlElement) it.next(), indent + writeIndent); 350 351 } 357 358 _writeSpace(out, indent); 359 } 360 361 if (data.length() > maxOneLineData) { 362 out.write("\n"); 363 _writeSpace(out, indent); 364 } 365 366 out.write("</" + TextUtils.escapeText(element.getName()) + ">\n"); 367 } 368 369 private void _writeSpace(Writer out, int numSpaces) throws IOException { 370 for (int i = 0; i < numSpaces; i++) { 371 out.write(" "); 372 } 373 } 374 } 375 376 | Popular Tags |