1 16 package org.apache.ws.jaxme.impl; 17 18 import org.apache.ws.jaxme.XMLWriter; 19 import org.xml.sax.Attributes ; 20 import org.xml.sax.Locator ; 21 import org.xml.sax.SAXException ; 22 23 import java.io.IOException ; 24 import java.io.Writer ; 25 26 import javax.xml.XMLConstants ; 27 import javax.xml.bind.JAXBException; 28 29 30 34 public class XMLWriterImpl implements XMLWriter { 35 private JMMarshallerImpl m; 36 private Writer w; 37 private Locator l; 38 private java.util.Map delayedPrefixes; 39 int curIndent = 0; 40 41 private static final int STATE_OUTSIDE = 0; 42 private static final int STATE_IN_START_ELEMENT = 1; 43 private static final int STATE_IN_ELEMENT = 2; 44 private int state; 45 46 47 public XMLWriterImpl() {} 48 49 51 public void init(JMMarshallerImpl pMarshaller) throws JAXBException { 52 m = pMarshaller; 53 } 54 55 57 public JMMarshallerImpl getMarshaller() { 58 return m; 59 } 60 61 63 public void setWriter(Writer pWriter) throws JAXBException { 64 w = pWriter; 65 } 66 68 public Writer getWriter() { 69 return w; 70 } 71 72 77 public void setDocumentLocator(Locator pLocator) { l = pLocator; } 78 79 83 public Locator getDocumentLocator() { return l; } 84 85 92 public void startPrefixMapping(String prefix, String namespaceURI) 93 throws SAXException { 94 if (delayedPrefixes == null) { 95 delayedPrefixes = new java.util.HashMap (); 96 } 97 if ("".equals(prefix)) { 98 if (namespaceURI.equals(prefix)) { 99 return; 100 } 101 prefix = XMLConstants.XMLNS_ATTRIBUTE; 102 } else { 103 prefix = XMLConstants.XMLNS_ATTRIBUTE + ":" + prefix; 104 } 105 delayedPrefixes.put(prefix, namespaceURI); 106 } 107 108 113 public void endPrefixMapping(String prefix) throws SAXException { 114 if (delayedPrefixes != null) { 115 if ("".equals(prefix)) { 116 prefix = XMLConstants.XMLNS_ATTRIBUTE; 117 } else { 118 prefix = XMLConstants.XMLNS_ATTRIBUTE + ":" + prefix; 119 } 120 delayedPrefixes.remove(prefix); 121 } 122 } 123 124 127 public void startDocument() throws SAXException { 128 if (delayedPrefixes != null) { 129 delayedPrefixes.clear(); 130 } 131 state = STATE_OUTSIDE; 132 curIndent = 0; 133 } 134 135 141 public void endDocument() throws SAXException {} 142 143 149 public void ignorableWhitespace(char[] ch, int start, int length) 150 throws SAXException { 151 characters(ch, start, length); 152 } 153 154 private void stopTerminator() throws java.io.IOException { 155 if (state == STATE_IN_START_ELEMENT) { 156 if (w != null) { 157 w.write('>'); 158 } 159 state = STATE_IN_ELEMENT; 160 } 161 } 162 163 169 public void characters(char[] ch, int start, int length) throws SAXException { 170 try { 171 stopTerminator(); 172 if (w == null) return; 173 int end = start+length; 174 for (int i = start; i < end; i++) { 175 char c = ch[i]; 176 switch (c) { 177 case '&': w.write("&"); break; 178 case '<': w.write("<"); break; 179 case '>': w.write(">"); break; 180 case '\n': 181 case '\r': 182 case '\t': 183 w.write(c); break; 184 default: 185 if (canEncode(c)) { 186 w.write(c); 187 } else { 188 w.write("&#"); 189 w.write(Integer.toString(c)); 190 w.write(";"); 191 } 192 break; 193 } 194 } 195 } catch (IOException e) { 196 throw new SAXException (e); 197 } 198 } 199 200 public boolean canEncode(char c) { 201 return c > 31 && c < 127; 202 } 203 204 205 212 public void endElement(String namespaceURI, String localName, String qName) 213 throws SAXException { 214 if (m != null && m. getIndentation()) { 215 --curIndent; 216 } 217 if (w != null) { 218 try { 219 if (state == STATE_IN_START_ELEMENT) { 220 w.write("/>"); 221 state = STATE_OUTSIDE; 222 } else { 223 if (state == STATE_OUTSIDE) { 224 indentMe(); 225 } 226 w.write("</"); 227 w.write(qName); 228 w.write('>'); 229 } 230 state = STATE_OUTSIDE; 231 } catch (java.io.IOException e) { 232 throw new SAXException (e); 233 } 234 } 235 } 236 237 private void indentMe() throws java.io.IOException { 238 if (w != null) { 239 if (m != null && m.getIndentation()) { 240 String s = m.getIndentationSeparator(); 241 if (s != null) { 242 w.write(s); 243 } 244 s = m.getIndentationString(); 245 for (int i = 0; i < curIndent; i++) { 246 w.write(s); 247 } 248 } 249 } 250 } 251 252 private void writeCData(String v) throws java.io.IOException { 253 int len = v.length(); 254 for (int j = 0; j < len; j++) { 255 char c = v.charAt(j); 256 switch (c) { 257 case '&': w.write("&"); break; 258 case '<': w.write("<"); break; 259 case '>': w.write(">"); break; 260 case '\'': w.write("'"); break; 261 case '"': w.write("""); break; 262 default: 263 if (canEncode(c)) { 264 w.write(c); 265 } else { 266 w.write("&#"); 267 w.write(Integer.toString(c)); 268 w.write(';'); 269 } 270 break; 271 } 272 } 273 } 274 275 283 public void startElement(String namespaceURI, String localName, String qName, 284 Attributes attr) throws SAXException { 285 try { 286 stopTerminator(); 287 if (m != null && m.getIndentation()) { 288 if (curIndent > 0) { 289 indentMe(); 290 } 291 curIndent++; 292 } 293 294 if (w != null) { 295 w.write('<'); 296 w.write(qName); 297 if (attr != null) { 298 for (int i = attr.getLength(); i > 0;) { 299 w.write(' '); 300 String name = attr.getQName(--i); 301 w.write(name); 302 if (delayedPrefixes != null) { 303 delayedPrefixes.remove(name); 304 } 305 w.write("=\""); 306 writeCData(attr.getValue(i)); 307 w.write('"'); 308 } 309 } 310 if (delayedPrefixes != null && delayedPrefixes.size() > 0) { 311 for (java.util.Iterator iter = delayedPrefixes.entrySet().iterator(); 312 iter.hasNext(); ) { 313 java.util.Map.Entry entry = (java.util.Map.Entry) iter.next(); 314 w.write(' '); 315 w.write((String ) entry.getKey()); 316 w.write("=\""); 317 w.write((String ) entry.getValue()); 318 w.write('"'); 319 } 320 delayedPrefixes.clear(); 321 } 322 } 323 state = STATE_IN_START_ELEMENT; 324 } catch (java.io.IOException e) { 325 throw new SAXException (e); 326 } 327 } 328 329 334 public void skippedEntity(String ent) throws SAXException { 335 throw new SAXException ("Don't know how to skip entities"); 336 } 337 338 344 public void processingInstruction(String target, String data) 345 throws SAXException { 346 try { 347 stopTerminator(); 348 if (w != null) { 349 w.write("<?"); 350 w.write(target); 351 w.write(' '); 352 w.write(data); 353 w.write("?>"); 354 } 355 } catch (java.io.IOException e) { 356 throw new SAXException (e); 357 } 358 } 359 } 360 | Popular Tags |