1 3 package org.jahia.services.importexport; 4 5 import java.io.Writer ; 6 import java.util.Stack ; 7 8 import org.xml.sax.Attributes ; 9 import org.xml.sax.SAXException ; 10 import org.xml.sax.XMLReader ; 11 12 13 71 public class DataWriter extends XMLWriter 72 { 73 74 75 76 77 81 82 85 public DataWriter () 86 { 87 super(); 88 } 89 90 91 98 public DataWriter (XMLReader xmlreader) 99 { 100 super(xmlreader); 101 } 102 103 104 110 public DataWriter (Writer writer) 111 { 112 super(writer); 113 } 114 115 116 124 public DataWriter (XMLReader xmlreader, Writer writer) 125 { 126 super(xmlreader, writer); 127 } 128 129 130 131 132 136 137 148 public int getIndentStep () 149 { 150 return indentStep; 151 } 152 153 154 161 public void setIndentStep (int indentStep) 162 { 163 this.indentStep = indentStep; 164 } 165 166 167 168 169 173 174 182 public void reset () 183 { 184 depth = 0; 185 state = SEEN_NOTHING; 186 stateStack = new Stack (); 187 super.reset(); 188 } 189 190 191 210 public void startElement (String uri, String localName, 211 String qName, Attributes atts) 212 throws SAXException 213 { 214 stateStack.push(SEEN_ELEMENT); 215 state = SEEN_NOTHING; 216 if (depth > 0) { 217 super.characters("\n"); 218 } 219 doIndent(); 220 super.startElement(uri, localName, qName, atts); 221 depth++; 222 } 223 224 225 243 public void endElement (String uri, String localName, String qName) 244 throws SAXException 245 { 246 depth--; 247 if (state == SEEN_ELEMENT) { 248 super.characters("\n"); 249 doIndent(); 250 } 251 super.endElement(uri, localName, qName); 252 state = stateStack.pop(); 253 } 254 255 256 275 public void emptyElement (String uri, String localName, 276 String qName, Attributes atts) 277 throws SAXException 278 { 279 state = SEEN_ELEMENT; 280 if (depth > 0) { 281 super.characters("\n"); 282 } 283 doIndent(); 284 super.emptyElement(uri, localName, qName, atts); 285 } 286 287 288 299 public void characters (char ch[], int start, int length) 300 throws SAXException 301 { 302 state = SEEN_DATA; 303 super.characters(ch, start, length); 304 } 305 306 307 308 309 313 314 321 private void doIndent () 322 throws SAXException 323 { 324 if (indentStep > 0 && depth > 0) { 325 int n = indentStep * depth; 326 char ch[] = new char[n]; 327 for (int i = 0; i < n; i++) { 328 ch[i] = ' '; 329 } 330 characters(ch, 0, n); 331 } 332 } 333 334 335 336 337 341 private final static Object SEEN_NOTHING = new Object (); 342 private final static Object SEEN_ELEMENT = new Object (); 343 private final static Object SEEN_DATA = new Object (); 344 345 346 347 348 352 private Object state = SEEN_NOTHING; 353 private Stack stateStack = new Stack (); 354 355 private int indentStep = 4; 356 private int depth = 0; 357 358 } 359 360 | Popular Tags |