1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package net.jot.xml; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Vector; /** * Does not deal with "entity" (ignored) * @author thibautc */ public class JOTXML extends JOTXMLElement { /** * The index in "items" of the XML root element */ private int rootIndex = -1; /** * Create the XML object from the given raw xml(text) * You can get this xml from a file using (JOTXML.readXML()) * @param xml * @throws net.jot.xml.JOTXMLException */ public JOTXML(StringBuffer xml) throws JOTXMLException { super("ROOT"); items=new Vector(); } /** * Create a new xml file with the given root element. */ public JOTXML(JOTXMLElement rootElement) { super("ROOT"); items=new Vector(); items.add(rootElement); rootIndex = 0; } public JOTXMLElement getRoot() { return (JOTXMLElement) items.get(rootIndex); } /** * Note: You are responsible for closing the stream * @param stream * @return * @throws java.io.IOException */ public static StringBuffer readXmlFrom(InputStream stream) throws IOException { int length=stream.available(); byte[] buff=new byte[length]; stream.read(buff); return new StringBuffer(new String(buff)); } public static StringBuffer readXmlFrom(File f) throws IOException { FileInputStream stream=null; StringBuffer xml=null; try { stream=new FileInputStream(f); xml=readXmlFrom(stream); } catch(IOException e) { throw(e); } finally { if(stream!=null) stream.close(); } return xml; } public void writeTo(OutputStream out) throws IOException { //TBD } public void writeTo(File out) throws IOException { FileOutputStream o =new FileOutputStream(out); try { writeTo(out); } catch(IOException e) { throw(e); } finally { o.close(); } } }