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? }
/** * Write an element with character data content but no attributes. * * <p>This is a convenience method to write a complete element * with character data content, including the start tag * and end tag. This method provides an empty string * for the qname and an empty attribute list.</p> * * <p>This method invokes * {@link #startElement(String, String, String, Attributes)}, * followed by * {@link #characters(String)}, followed by * {@link #endElement(String, String, String)}.</p> * * @param uri The element's Namespace URI. * @param localName The element's local name. * @param content The character data content. * @exception org.xml.sax.SAXException If there is an error * writing the empty tag, or if a handler further down * the filter chain raises an exception. * @see #startElement(String, String, String, Attributes) * @see #characters(String) * @see #endElement(String, String, String) */ public void dataElement (String uri, String localName, String content) throws SAXException { dataElement(uri, localName, "", EMPTY_ATTS, content); }
public void endDocument() throws SAXException { try { write('\n'); } catch( IOException e ) { throw new SAXException(e); } super.endDocument(); }
throws SAXException startElement(uri, localName, qName, atts); characters(content); endElement(uri, localName, qName);
/** * Write a string of character data, with XML escaping. * * <p>This is a convenience method that takes an XML * String, converts it to a character array, then invokes * {@link #characters(char[], int, int)}.</p> * * @param data The character data. * @exception org.xml.sax.SAXException If there is an error * writing the string, or if a handler further down * the filter chain raises an exception. * @see #characters(char[], int, int) */ public void characters (String data) throws SAXException { try { if (!startTagIsClosed) { write('>'); startTagIsClosed = true; } char ch[] = data.toCharArray(); characters(ch, 0, ch.length); } catch( IOException e ) { throw new SAXException(e); } }
/** * 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(); }
state = SEEN_NOTHING; if (depth > 0) { super.characters("\n"); super.startElement(uri, localName, qName, atts); depth++;
/** * End an element without a Namespace URI or qname. * * <p>This method will supply an empty string for the qName * and an empty string for the Namespace URI. * It invokes {@link #endElement(String, String, String)} * directly.</p> * * @param localName The element's local name. * @exception org.xml.sax.SAXException If there is an error * writing the end tag, or if a handler further down * the filter chain raises an exception. * @see #endElement(String, String, String) */ public void endElement (String localName) throws SAXException { endElement("", localName, ""); }
/** * Start a new element without a qname, attributes or a Namespace URI. * * <p>This method will provide an empty string for the * Namespace URI, and empty string for the qualified name, * and a default empty attribute list. It invokes * #startElement(String, String, String, Attributes)} * directly.</p> * * @param localName The element's local name. * @exception org.xml.sax.SAXException If there is an error * writing the start tag, or if a handler further down * the filter chain raises an exception. * @see #startElement(String, String, String, Attributes) */ public void startElement (String localName) throws SAXException { startElement("", localName, "", EMPTY_ATTS); }
public XmlWriter(Writer writer, String encoding) { openedTags = new Stack<>(); xmlWriter = new XMLWriter(writer, encoding); try { xmlWriter.startDocument(); } catch (SAXException e) { throw new RuntimeException("Unable to start document", e); } }
/** * Create a new XML writer. * * <p>Write to the writer provided.</p> * * @param writer * The output destination, or null to use standard output. * @param encoding * If non-null string is specified, it is written as a part * of the XML declaration. */ public XMLWriter (Writer writer, String encoding, CharacterEscapeHandler _escapeHandler ) { init(writer,encoding); this.escapeHandler = _escapeHandler; }
/** * Write a sequence of characters. * * @param ch The characters to write. * @param start The starting position in the array. * @param length The number of characters to use. * @exception org.xml.sax.SAXException If there is an error * writing the characters, or if a filter further * down the chain raises an exception. * @see XMLWriter#characters(char[], int, int) */ public void characters (char ch[], int start, int length) throws SAXException { state = SEEN_DATA; super.characters(ch, start, length); }
/** * Write a newline at the end of the document. * * Pass the event on down the filter chain for further processing. * * @exception org.xml.sax.SAXException If there is an error * writing the newline, or if a handler further down * the filter chain raises an exception. * @see org.xml.sax.ContentHandler#endDocument() */ public void endDocument () throws SAXException { try { super.endDocument(); flush(); } catch( IOException e ) { throw new SAXException(e); } }
public XMLWriter createWriter( Writer w, String encoding ) throws JAXBException { CharacterEscapeHandler ceh = createEscapeHandler(encoding); if(isFormattedOutput()) { DataWriter d = new DataWriter(w,encoding,ceh); d.setIndentStep(indent); return d; } else return new XMLWriter(w,encoding,ceh); }
throws SAXException startElement(uri, localName, qName, atts); characters(content); endElement(uri, localName, qName);
/** * Write a string of character data, with XML escaping. * * <p>This is a convenience method that takes an XML * String, converts it to a character array, then invokes * {@link #characters(char[], int, int)}.</p> * * @param data The character data. * @exception org.xml.sax.SAXException If there is an error * writing the string, or if a handler further down * the filter chain raises an exception. * @see #characters(char[], int, int) */ public void characters (String data) throws SAXException { try { if (!startTagIsClosed) { write('>'); startTagIsClosed = true; } char ch[] = data.toCharArray(); characters(ch, 0, ch.length); } catch( IOException e ) { throw new SAXException(e); } }
/** * 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(); }
state = SEEN_NOTHING; if (depth > 0) { super.characters("\n"); super.startElement(uri, localName, qName, atts); depth++;
/** * End an element without a qname. * * <p>This method will supply an empty string for the qName. * It invokes {@link #endElement(String, String, String)} * directly.</p> * * @param uri The element's Namespace URI. * @param localName The element's local name. * @exception org.xml.sax.SAXException If there is an error * writing the end tag, or if a handler further down * the filter chain raises an exception. * @see #endElement(String, String, String) */ public void endElement (String uri, String localName) throws SAXException { endElement(uri, localName, ""); }
/** * Start a new element without a qname or attributes. * * <p>This method will provide a default empty attribute * list and an empty string for the qualified name. * It invokes {@link * #startElement(String, String, String, Attributes)} * directly.</p> * * @param uri The element's Namespace URI. * @param localName The element's local name. * @exception org.xml.sax.SAXException If there is an error * writing the start tag, or if a handler further down * the filter chain raises an exception. * @see #startElement(String, String, String, Attributes) */ public void startElement (String uri, String localName) throws SAXException { startElement(uri, localName, "", EMPTY_ATTS); }