1 22 package org.jboss.xb.binding; 23 24 import java.io.IOException ; 25 import java.io.Writer ; 26 import org.xml.sax.Attributes ; 27 import org.xml.sax.ContentHandler ; 28 import org.xml.sax.Locator ; 29 import org.xml.sax.SAXException ; 30 31 38 public class ContentWriter 39 implements ContentHandler 40 { 41 final boolean useIndent; 42 private String indent = " "; 43 private int depth = 0; 44 private boolean started = false; 45 46 private final Writer writer; 47 48 public ContentWriter(Writer writer, boolean indent) 49 { 50 this.writer = writer; 51 this.useIndent = indent; 52 } 53 54 public void setDocumentLocator(Locator locator) 55 { 56 throw new UnsupportedOperationException (); 57 } 58 59 public void startDocument() 60 throws SAXException 61 { 62 } 63 64 public void endDocument() 65 throws SAXException 66 { 67 } 68 69 public void startPrefixMapping(String prefix, String uri) 70 throws SAXException 71 { 72 throw new UnsupportedOperationException (); 73 } 74 75 public void endPrefixMapping(String prefix) 76 throws SAXException 77 { 78 throw new UnsupportedOperationException (); 79 } 80 81 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) 82 throws SAXException 83 { 84 if(useIndent) 85 { 86 write(writer, '\n'); 87 for(int j = 0; j < depth; ++j) 88 { 89 write(writer, indent); 90 } 91 } 92 93 if(!started) 94 { 95 started = true; 96 } 97 98 ++depth; 99 100 write(writer, '<'); 101 write(writer, qName); 102 103 if(atts != null && atts.getLength() > 0) 104 { 105 for(int i = 0; i < atts.getLength(); ++i) 106 { 107 write(writer, ' '); 108 write(writer, atts.getQName(i)); 109 write(writer, "=\""); 110 writeNormalized(writer, atts.getValue(i)); 111 write(writer, '\"'); 112 } 113 } 114 115 150 151 write(writer, '>'); 152 } 153 154 public void endElement(String namespaceURI, String localName, 155 String qName) 156 throws SAXException 157 { 158 --depth; 159 if(!started) 160 { 161 if(useIndent) 162 { 163 write(writer, '\n'); 164 for(int j = 0; j < depth; ++j) 165 { 166 write(writer, indent); 167 } 168 } 169 } 170 else 171 { 172 started = false; 173 } 174 175 write(writer, "</"); 176 write(writer, qName); 177 write(writer, '>'); 178 } 179 180 public void characters(char ch[], int start, int length) 181 throws SAXException 182 { 183 writeNormalized(writer, ch, start, length); 184 } 185 186 public void ignorableWhitespace(char ch[], int start, int length) 187 throws SAXException 188 { 189 throw new UnsupportedOperationException (); 190 } 191 192 public void processingInstruction(String target, String data) 193 throws SAXException 194 { 195 throw new UnsupportedOperationException (); 196 } 197 198 public void skippedEntity(String name) 199 throws SAXException 200 { 201 throw new UnsupportedOperationException (); 202 } 203 204 206 private static void write(Writer writer, String str) throws SAXException 207 { 208 try 209 { 210 writer.write(str); 211 } 212 catch(IOException e) 213 { 214 throw new SAXException ("Writting failed: " + e.getMessage(), e); 215 } 216 } 217 218 private static void writeNormalized(Writer writer, String str) throws SAXException 219 { 220 writeNormalized(writer, str.toCharArray(), 0, str.length()); 221 } 222 223 private static void write(Writer writer, int ch) throws SAXException 224 { 225 try 226 { 227 writer.write(ch); 228 } 229 catch(IOException e) 230 { 231 throw new SAXException ("Writting failed: " + e.getMessage(), e); 232 } 233 } 234 235 private static void writeNormalized(Writer writer, char[] ch, int start, int length) throws SAXException 236 { 237 try 238 { 239 int left = start; 240 int i = start; 241 while(i < start + length) 242 { 243 char c = ch[i++]; 244 if(c == '<') 245 { 246 writer.write(ch, left, i - left - 1); 247 writer.write("<"); 248 left = i; 249 } 250 else if(c == '>') 251 { 252 writer.write(ch, left, i - left - 1); 253 writer.write(">"); 254 left = i; 255 } 256 else if(c == '&') 257 { 258 writer.write(ch, left, i - left - 1); 259 writer.write("&"); 260 left = i; 261 } 262 else if(c == '\'') 263 { 264 writer.write(ch, left, i - left - 1); 265 writer.write("'"); 266 left = i; 267 } 268 else if(c == '\"') 269 { 270 writer.write(ch, left, i - left - 1); 271 writer.write("""); 272 left = i; 273 } 274 } 275 276 if(left < i) 277 { 278 writer.write(ch, left, i - left); 279 } 280 } 281 catch(IOException e) 282 { 283 throw new SAXException ("Writting failed: " + e.getMessage(), e); 284 } 285 } 286 } 287 | Popular Tags |