/** * Normalizes the given string using a shared buffer. * * @param s the string that should be XML-Encoded. * @param transformNewLine a flag controling whether to transform newlines into character-entities. * @return the transformed string. */ public String normalizeLocal( final String s, final boolean transformNewLine ) throws IOException { return normalize( s, transformNewLine ); }
/** * Creates a new XMLWriter instance. * * @param writer the character stream. * @param tagDescription the tags that are safe for line breaks. * @param indentString the indent string. */ public XmlWriter( final Writer writer, final TagDescription tagDescription, final String indentString ) { if ( writer == null ) { throw new NullPointerException( "Writer must not be null." ); } this.support = new XmlWriterSupport( tagDescription, indentString ); this.writer = writer; }
public void addImpliedNamespace( final String uri, final String prefix ) { support.addImpliedNamespace( uri, prefix ); }
indent( w ); setLineEmpty( false ); DeclaredNamespaces namespaces = computeNamespaces(); w.write( " " ); buildAttributeName( entry, namespaces, w ); w.write( "=\"" ); writeTextNormalized( w, entry.getValue(), true ); w.write( "\"" ); if ( isHtmlCompatiblityMode() ) { w.write( " />" ); } else { doEndOfLine( w ); } else { w.write( ">" ); doEndOfLine( w );
/** * Writes a comment into the generated xml file. * * @param writer the writer. * @param comment the comment text * @throws IOException if there is a problem writing to the character stream. */ public void writeComment( final Writer writer, final String comment ) throws IOException { if ( openTags.isEmpty() == false ) { final ElementLevel level = (ElementLevel) openTags.peek(); if ( getTagDescription().hasCData( level.getNamespace(), level.getTagName() ) == false ) { indent( writer ); } } setLineEmpty( false ); writer.write( "<!-- " ); writeTextNormalized( writer, comment, false ); writer.write( " -->" ); doEndOfLine( writer ); }
/** * Conditionally writes an end-of-line character. The End-Of-Line is only written, if the tag description indicates * that the currently open element does not expect any CDATA inside. Writing a newline for CDATA-elements may have * sideeffects. * * @param w the writer. * @throws java.io.IOException if there is an I/O problem. */ private void doEndOfLine( final Writer w ) throws IOException { if ( openTags.isEmpty() ) { if ( isWriteFinalLinebreak() ) { writeNewLine( w ); } } else { final ElementLevel level = (ElementLevel) openTags.peek(); if ( getTagDescription().hasCData( level.getNamespace(), level.getTagName() ) == false ) { writeNewLine( w ); } } }
/** * Writes a closing XML tag. * * @param w the writer. * @throws java.io.IOException if there is an I/O problem. */ public void writeCloseTag( final Writer w ) throws IOException { indentForClose( w ); final ElementLevel level = (ElementLevel) openTags.pop(); setLineEmpty( false ); w.write( "</" ); final String prefix = level.getPrefix(); if ( prefix != null ) { w.write( prefix ); w.write( ":" ); w.write( level.getTagName() ); } else { w.write( level.getTagName() ); } w.write( ">" ); doEndOfLine( w ); }
/** * Indent the line. Called for proper indenting in various places. * * @param writer the writer which should receive the indentention. * @throws java.io.IOException if writing the stream failed. */ public void indent( final Writer writer ) throws IOException { if ( openTags.isEmpty() ) { for ( int i = 0; i < additionalIndent; i++ ) { writer.write( indentString ); } return; } final ElementLevel level = (ElementLevel) openTags.peek(); if ( getTagDescription().hasCData( level.getNamespace(), level.getTagName() ) == false ) { doEndOfLine( writer ); for ( int i = 0; i < openTags.size(); i++ ) { writer.write( indentString ); } for ( int i = 0; i < additionalIndent; i++ ) { writer.write( indentString ); } } }
/** * Writes the XML declaration that usually appears at the top of every XML file. * * @param encoding the encoding that should be declared (this has to match the encoding of the writer, or funny things * may happen when parsing the xml file later). * @throws java.io.IOException if there is a problem writing to the character stream. */ public void writeXmlDeclaration( final Writer writer, final String encoding ) throws IOException { if ( encoding == null ) { writer.write( "<?xml version=\"1.0\"?>" ); writer.write( getLineSeparator() ); return; } writer.write( "<?xml version=\"1.0\" encoding=\"" ); writer.write( encoding ); writer.write( "\"?>" ); writer.write( getLineSeparator() ); setEncoding( encoding ); }
public int getAdditionalIndent() { return support.getAdditionalIndent(); }
public TagDescription getTagDescription() { return support.getTagDescription(); } }
/** * Indent the line. Called for proper indenting in various places. * * @param writer the writer which should receive the indentention. * @throws java.io.IOException if writing the stream failed. */ public void indentForClose( final Writer writer ) throws IOException { if ( openTags.isEmpty() ) { for ( int i = 0; i < additionalIndent; i++ ) { writer.write( indentString ); } return; } final ElementLevel level = (ElementLevel) openTags.peek(); if ( getTagDescription().hasCData( level.getNamespace(), level.getTagName() ) == false ) { doEndOfLine( writer ); for ( int i = 1; i < openTags.size(); i++ ) { writer.write( indentString ); } for ( int i = 0; i < additionalIndent; i++ ) { writer.write( indentString ); } } }
/** * Create a new XmlWriterSupport instance. * * @param safeTags the tags that are safe for line breaks. * @param indentString the indent string. * @param lineseparator the lineseparator that should be used for writing XML files. */ public XmlWriterSupport( final TagDescription safeTags, final String indentString, final String lineseparator ) { if ( indentString == null ) { throw new NullPointerException( "IndentString must not be null" ); } if ( safeTags == null ) { throw new NullPointerException( "SafeTags must not be null" ); } if ( lineseparator == null ) { throw new NullPointerException( "LineSeparator must not be null" ); } this.safeTags = safeTags; openTags = new FastStack(); this.indentString = indentString; lineEmpty = true; writeFinalLinebreak = true; lineSeparator = lineseparator; addImpliedNamespace( "http://www.w3.org/XML/1998/namespace", "xml" ); }
/** * Creates a new XMLWriter instance. * * @param writer the character stream. * @param tagDescription the tags that are safe for line breaks. * @param indentString the indent string. * @param lineSeparator the line separator to be used. */ public XmlWriter( final Writer writer, final TagDescription tagDescription, final String indentString, final String lineSeparator ) { if ( writer == null ) { throw new NullPointerException( "Writer must not be null." ); } this.support = new XmlWriterSupport( tagDescription, indentString, lineSeparator ); this.writer = writer; }