1 7 package org.jboss.xml.binding; 8 9 import org.xml.sax.ContentHandler ; 10 import org.xml.sax.Locator ; 11 import org.xml.sax.SAXException ; 12 import org.xml.sax.Attributes ; 13 14 import java.io.Writer ; 15 import java.io.IOException ; 16 17 24 public class ContentWriter 25 implements ContentHandler 26 { 27 final boolean useIndent; 28 private String indent = " "; 29 private int depth = 0; 30 private boolean started = false; 31 32 private final Writer writer; 33 34 public ContentWriter(Writer writer, boolean indent) 35 { 36 this.writer = writer; 37 this.useIndent = indent; 38 } 39 40 public void setDocumentLocator(Locator locator) 41 { 42 throw new UnsupportedOperationException (); 43 } 44 45 public void startDocument() 46 throws SAXException 47 { 48 } 49 50 public void endDocument() 51 throws SAXException 52 { 53 } 54 55 public void startPrefixMapping(String prefix, String uri) 56 throws SAXException 57 { 58 throw new UnsupportedOperationException (); 59 } 60 61 public void endPrefixMapping(String prefix) 62 throws SAXException 63 { 64 throw new UnsupportedOperationException (); 65 } 66 67 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) 68 throws SAXException 69 { 70 if(useIndent) 71 { 72 write(writer, '\n'); 73 for(int j = 0; j < depth; ++j) 74 { 75 write(writer, indent); 76 } 77 } 78 79 if(!started) 80 { 81 started = true; 82 } 83 84 ++depth; 85 86 write(writer, '<'); 87 write(writer, qName); 88 89 if(atts != null && atts.getLength() > 0) 90 { 91 for(int i = 0; i < atts.getLength(); ++i) 92 { 93 write(writer, ' '); 94 write(writer, atts.getQName(i)); 95 write(writer, "=\""); 96 write(writer, atts.getValue(i)); 97 write(writer, '\"'); 98 } 99 } 100 101 if(namespaceURI != null && namespaceURI.length() > 1) 102 { 103 int colon = qName.indexOf(':'); 104 112 113 if(colon >= 0) 114 { 115 String prefix = qName.substring(0, colon); 116 if(useIndent) 117 { 118 write(writer, '\n'); 119 for(int i = 0; i < depth + 1; ++i) 120 { 121 write(writer, indent); 122 } 123 } 124 else 125 { 126 write(writer, ' '); 127 } 128 129 write(writer, "xmlns:"); 130 write(writer, prefix); 131 write(writer, "=\""); 132 write(writer, namespaceURI); 133 write(writer, "\""); 134 } 135 } 136 137 write(writer, '>'); 138 } 139 140 public void endElement(String namespaceURI, String localName, 141 String qName) 142 throws SAXException 143 { 144 --depth; 145 if(!started) 146 { 147 if(useIndent) 148 { 149 write(writer, '\n'); 150 for(int j = 0; j < depth; ++j) 151 { 152 write(writer, indent); 153 } 154 } 155 } 156 else 157 { 158 started = false; 159 } 160 161 write(writer, "</"); 162 write(writer, qName); 163 write(writer, '>'); 164 } 165 166 public void characters(char ch[], int start, int length) 167 throws SAXException 168 { 169 write(writer, ch, start, length); 170 } 171 172 public void ignorableWhitespace(char ch[], int start, int length) 173 throws SAXException 174 { 175 throw new UnsupportedOperationException (); 176 } 177 178 public void processingInstruction(String target, String data) 179 throws SAXException 180 { 181 throw new UnsupportedOperationException (); 182 } 183 184 public void skippedEntity(String name) 185 throws SAXException 186 { 187 throw new UnsupportedOperationException (); 188 } 189 190 192 private static void write(Writer writer, String str) throws SAXException 193 { 194 try 195 { 196 writer.write(str); 197 } 198 catch(IOException e) 199 { 200 throw new SAXException ("Writting failed: " + e.getMessage(), e); 201 } 202 } 203 204 private static void write(Writer writer, int ch) throws SAXException 205 { 206 try 207 { 208 writer.write(ch); 209 } 210 catch(IOException e) 211 { 212 throw new SAXException ("Writting failed: " + e.getMessage(), e); 213 } 214 } 215 216 private static void write(Writer writer, char[] ch, int start, int length) throws SAXException 217 { 218 try 219 { 220 writer.write(ch, start, length); 221 } 222 catch(IOException e) 223 { 224 throw new SAXException ("Writting failed: " + e.getMessage(), e); 225 } 226 } 227 } 228 | Popular Tags |