1 2 package org.enhydra.tool.archive.xml; 3 4 import org.enhydra.tool.archive.ArchiveException; 6 import org.apache.xerces.parsers.DOMParser; 7 import org.apache.xml.serialize.XMLSerializer; 8 import org.apache.xml.serialize.OutputFormat; 9 import org.apache.xml.serialize.OutputFormat; 10 import org.w3c.dom.Document ; 11 import org.w3c.dom.Element ; 12 import org.w3c.dom.Node ; 13 import org.w3c.dom.NodeList ; 14 import org.xml.sax.SAXException ; 15 import org.xml.sax.InputSource ; 16 17 import java.io.FileReader ; 19 import java.io.FileNotFoundException ; 20 import java.io.IOException ; 21 import java.io.ByteArrayInputStream ; 22 import java.io.InputStream ; 23 import java.io.OutputStream ; 24 import java.io.BufferedWriter ; 25 import java.io.BufferedReader ; 26 import java.io.StringWriter ; 27 import java.io.StringReader ; 28 29 abstract public class AbstractDescriptorHandler implements DescriptorHandler { 31 32 private Document doc = null; 34 private String source = new String (); 35 private OutputStream outStream = null; 36 private StringWriter writer = null; 37 38 public void setOutStream(OutputStream s) { 40 outStream = s; 41 } 42 43 public OutputStream getOutputStream() { 44 return outStream; 45 } 46 47 public void setWriter(StringWriter w) { 48 writer = w; 49 } 50 51 public StringWriter getWriter() { 52 return writer; 53 } 54 55 public String getSource() { 56 return source; 57 } 58 59 public void setSource(String s) { 60 source = s; 61 } 62 63 public InputStream getInputStream() { 64 ByteArrayInputStream stream = null; 65 byte[] bytes = new byte[0]; 66 67 if (getWriter() != null) { 68 getWriter().flush(); 69 bytes = getWriter().toString().getBytes(); 70 stream = new ByteArrayInputStream (bytes); 71 } 72 return stream; 73 } 74 75 protected Document getDocument() throws ArchiveException { 76 DOMParser parser = null; 77 InputSource inputSource = null; 78 DocTypeFilterReader reader = null; 79 80 try { 81 reader = new DocTypeFilterReader( 82 new BufferedReader ( 83 new FileReader (getSource()))); 84 } catch (FileNotFoundException e) { 85 throw new ArchiveException(e, "Unable to prep: " + getSource()); 86 } 87 88 inputSource = new InputSource (reader); 89 inputSource.setPublicId(null); 90 inputSource.setSystemId(null); 91 parser = new DOMParser(); 92 93 try { 95 parser.parse(inputSource); 96 } catch (IOException e) { 97 e.printStackTrace(); 98 throw new ArchiveException(e, "Unable to parse: " + getSource()); 99 } catch (SAXException e) { 100 e.printStackTrace(); 101 throw new ArchiveException(e, "Unable to parse: " + getSource()); 102 } 103 return parser.getDocument(); 104 } 105 106 107 public void prep() throws ArchiveException { 108 OutputFormat format = null; 109 XMLSerializer serializer = null; 110 111 format = new OutputFormat(); 112 serializer = new XMLSerializer(); 113 114 doc = getDocument(); 115 prepElements(); 116 117 format.setDoctype(getPublicID(), getSystemID()); 119 format.setLineSeparator("\r\n"); 120 format.setLineWidth(8192); 121 format.setIndent(2); 122 format.setIndenting(true); 123 format.setPreserveSpace(false); 124 125 if (getWriter() != null) { 127 serializer.setOutputCharStream(getWriter()); 128 } else if (getOutputStream() != null) { 129 serializer.setOutputByteStream(getOutputStream()); 130 } else { 131 initWriter(); 132 serializer.setOutputCharStream(getWriter()); 133 } 134 serializer.setOutputFormat(format); 135 try { 136 serializer.serialize(doc.getDocumentElement()); 137 } catch (IOException e) { 138 throw new ArchiveException(e, 139 "Unable to serialize prep'd " 140 + getSource()); 141 } 142 } 143 144 abstract protected void prepElements(); 147 abstract protected String getPublicID(); 148 abstract protected String getSystemID(); 149 150 protected Document getDoc() { 153 return doc; 154 } 155 156 protected Element lookup(Element parent, String name) { 157 NodeList list = null; 158 Node cursor = null; 159 Element element = null; 160 int len = 0; 161 162 list = parent.getChildNodes(); 163 len = list.getLength(); 164 for (int i = 0; i < len; i++) { 165 cursor = list.item(i); 166 if (cursor.getNodeName().equals(name) 167 && cursor instanceof Element ) { 168 element = (Element ) cursor; 169 break; 170 } 171 } 172 return element; 173 } 174 175 private void initWriter() { 178 setWriter(new StringWriter ()); 179 } 180 181 } 182 | Popular Tags |