public XmlOutput createWriter( Writer w, String encoding ) { // XMLWriter doesn't do buffering, so do it here if it looks like a good idea if(!(w instanceof BufferedWriter)) w = new BufferedWriter(w); assert toBeFlushed==null; toBeFlushed = w; CharacterEscapeHandler ceh = createEscapeHandler(encoding); XMLWriter xw; if(isFormattedOutput()) { DataWriter d = new DataWriter(w,encoding,ceh); d.setIndentStep(indent); xw=d; } else xw = new XMLWriter(w,encoding,ceh); xw.setXmlDecl(!isFragment()); xw.setHeader(header); return new SAXOutput(xw); // TODO: don't we need a better writer? }
/** * Print indentation for the current level. * * @exception org.xml.sax.SAXException If there is an error * writing the indentation characters, or if a filter * further down the chain raises an exception. */ private void doIndent () throws SAXException { if (depth > 0) { char[] ch = indentStep.toCharArray(); for( int i=0; i<depth; i++ ) characters(ch, 0, ch.length); } }
/** * Write an end tag. * * <p>If the element has contained other elements, the tag * will appear indented on a new line; otherwise, it will * appear immediately following whatever came before.</p> * * <p>The newline and indentation will be passed on down * the filter chain through regular characters events.</p> * * @param uri The element's Namespace URI. * @param localName The element's local name. * @param qName The element's qualified (prefixed) name. * @exception org.xml.sax.SAXException If there is an error * writing the end tag, or if a filter further * down the chain raises an exception. * @see XMLWriter#endElement(String, String, String) */ public void endElement (String uri, String localName, String qName) throws SAXException { depth--; if (state == SEEN_ELEMENT) { super.characters("\n"); doIndent(); } super.endElement(uri, localName, qName); state = stateStack.pop(); }
/** * Set the current indent step. * * @param indentStep The new indent step (0 or less for no * indentation). * @see #getIndentStep() * * @deprecated * Should use the version that takes string. */ public void setIndentStep (int indentStep) { StringBuilder buf = new StringBuilder(); for( ; indentStep>0; indentStep-- ) buf.append(' '); setIndentStep(buf.toString()); }
DataWriter dataWriter = new DataWriter() { @Override void onWritingDone() { //do data read here } };
protected void writeXmlDecl(String decl) throws IOException { super.writeXmlDecl(decl); write('\n'); }
/** * Set the current indent step. * * @param indentStep The new indent step (0 or less for no * indentation). * @see #getIndentStep() * * @deprecated * Should use the version that takes string. */ public void setIndentStep (int indentStep) { StringBuilder buf = new StringBuilder(); for( ; indentStep>0; indentStep-- ) buf.append(' '); setIndentStep(buf.toString()); }
Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); StringWriter stringWriter = new StringWriter(); PrintWriter printWriter = new PrintWriter(stringWriter); DataWriter dataWriter = new DataWriter(printWriter, "UTF-8", new CharacterEscapeHandler() { @Override public void escape(char[] buf, int start, int len, boolean b, Writer out) throws IOException { out.write(buf, start, len); } }); marshaller.marshal(data, dataWriter); System.out.println(stringWriter.toString());
public void endDocument() throws SAXException { try { write('\n'); } catch( IOException e ) { throw new SAXException(e); } super.endDocument(); }
public XmlOutput createWriter( Writer w, String encoding ) { // XMLWriter doesn't do buffering, so do it here if it looks like a good idea if(!(w instanceof BufferedWriter)) w = new BufferedWriter(w); assert toBeFlushed==null; toBeFlushed = w; CharacterEscapeHandler ceh = createEscapeHandler(encoding); XMLWriter xw; if(isFormattedOutput()) { DataWriter d = new DataWriter(w,encoding,ceh); d.setIndentStep(indent); xw=d; } else xw = new XMLWriter(w,encoding,ceh); xw.setXmlDecl(!isFragment()); xw.setHeader(header); return new SAXOutput(xw); // TODO: don't we need a better writer? }
/** * Set the current indent step. * * @param indentStep The new indent step (0 or less for no * indentation). * @see #getIndentStep() * * @deprecated * Should use the version that takes string. */ public void setIndentStep (int indentStep) { StringBuilder buf = new StringBuilder(); for( ; indentStep>0; indentStep-- ) buf.append(' '); setIndentStep(buf.toString()); }
public class DataCreate { public void readData() { new DataReader().readData(); } public void writeData() { new DataWriter().writeData(); } }
public void endDocument() throws SAXException { try { write('\n'); } catch( IOException e ) { throw new SAXException(e); } super.endDocument(); }
super.characters("\n"); doIndent(); super.startElement(uri, localName, qName, atts); depth++;
/** * Print indentation for the current level. * * @exception org.xml.sax.SAXException If there is an error * writing the indentation characters, or if a filter * further down the chain raises an exception. */ private void doIndent () throws SAXException { if (depth > 0) { char[] ch = indentStep.toCharArray(); for( int i=0; i<depth; i++ ) characters(ch, 0, ch.length); } }
/** * Dumps the contents of the forest to the specified stream. * * This is a debug method. As such, error handling is sloppy. */ public void dump( OutputStream out ) throws IOException { try { // create identity transformer Transformer it = TransformerFactory.newInstance().newTransformer(); for (Map.Entry<String, Document> e : core.entrySet()) { out.write( ("---<< "+e.getKey()+'\n').getBytes() ); DataWriter dw = new DataWriter(new OutputStreamWriter(out),null); dw.setIndentStep(" "); it.transform( new DOMSource(e.getValue()), new SAXResult(dw)); out.write( "\n\n\n".getBytes() ); } } catch( TransformerException e ) { e.printStackTrace(); } } }
/** * Set the current indent step. * * @param indentStep The new indent step (0 or less for no * indentation). * @see #getIndentStep() * * @deprecated * Should use the version that takes string. */ public void setIndentStep (int indentStep) { StringBuilder buf = new StringBuilder(); for( ; indentStep>0; indentStep-- ) buf.append(' '); setIndentStep(buf.toString()); }
protected void writeXmlDecl(String decl) throws IOException { super.writeXmlDecl(decl); write('\n'); }
/** * Write an end tag. * * <p>If the element has contained other elements, the tag * will appear indented on a new line; otherwise, it will * appear immediately following whatever came before.</p> * * <p>The newline and indentation will be passed on down * the filter chain through regular characters events.</p> * * @param uri The element's Namespace URI. * @param localName The element's local name. * @param qName The element's qualified (prefixed) name. * @exception org.xml.sax.SAXException If there is an error * writing the end tag, or if a filter further * down the chain raises an exception. * @see XMLWriter#endElement(String, String, String) */ public void endElement (String uri, String localName, String qName) throws SAXException { depth--; if (state == SEEN_ELEMENT) { super.characters("\n"); doIndent(); } super.endElement(uri, localName, qName); state = stateStack.pop(); }
/** * Print indentation for the current level. * * @exception org.xml.sax.SAXException If there is an error * writing the indentation characters, or if a filter * further down the chain raises an exception. */ private void doIndent () throws SAXException { if (depth > 0) { char[] ch = indentStep.toCharArray(); for( int i=0; i<depth; i++ ) characters(ch, 0, ch.length); } }