Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * This file is part of the DiffX library.
   *
   * For licensing information please see the file license.txt included in the release.
   * A copy of this licence can also be found at
   *   http://www.opensource.org/licenses/artistic-license-2.0.php
   */
  package com.topologi.diffx.xml;
  
 import java.io.Writer;
 
A base implementation for XML writers.

Provides methods to generate well-formed XML data easily. wrapping a writer.

This version only supports utf-8 encoding, if writing to a file make sure that the encoding of the file output stream is "utf-8".

The recommended implementation is to use a BufferedWriter to write.

  Writer writer =
     new BufferedWriter(new OutputStreamWriter(new FileOutputStream("foo.out"),"utf-8"));
 

Author(s):
Christophe Lauret
Version:
17 May 2005
 
 abstract class XMLWriterBase implements XMLWriter {

  
Where the XML data goes.
 
   final Writer writer;

  
Encoding of the output xml.
 
   String encoding = "utf-8";

  
Encoding of the output xml.
 
Level of the depth of the xml document currently produced.

This attribute changes depending on the state of the instance.

 
   int depth = 0;

  
Indicates whether the xml should be indented or not.

The default is true (indented).

The indentation is 2 white-spaces.

 
   boolean indent;

  
The default indentation spaces used.
 
   private String indentChars = null;

  
Flag to indicate that the element open tag is not finished yet.
 
   boolean isNude = false;
 
   // constructors -------------------------------------------------------------------------
 
  

Creates a new XML writer.

Parameters:
writer Where this writer should write the XML data.
indent Set the indentation flag.
Throws:
java.lang.NullPointerException If the writer is null.
 
   public XMLWriterBase(Writer writerboolean indentthrows NullPointerException {
     if (writer == null)
       throw new NullPointerException("XMLWriter cannot use a null writer.");
     this. = writer;
     this. = new XMLEscapeWriterUTF8(writer);
     this. = indent;
     if (indent) {
       this. = "  ";
     }
   }
 
   // setup methods ------------------------------------------------------------------------
 
  
  public final void xmlDecl() throws IOException {
    this..write("<?xml version=\"1.0\" encoding=\""+this.+"\"?>");
    if (this.) {
      this..write('\n');
    }
  }

  
  public final void setIndentChars(String spacesthrows IllegalStateExceptionIllegalArgumentException {
    if (this. != 0)
      throw new IllegalStateException("To late to set the indentation characters!");
    // check that this is a valid indentation string
    if (spaces != null) {
      for (int i = 0; i < spaces.length(); i++) {
        if (!Character.isSpaceChar(spaces.charAt(i)))
          throw new IllegalArgumentException("Not a valid indentation string.");
      }
    }
    // update the flags
    this. = spaces;
    this. = spaces != null;
  }

  
Sets the encoding to use.

The encoding must match the encoding used if there is an underlying OutputStreamWriter.

Parameters:
encoding The encoding to use.
Throws:
java.lang.IllegalArgumentException If the encoding is not valid.
java.lang.IllegalStateException If the writer has already been used.
  public final void setEncoding(String encodingthrows IllegalStateExceptionIllegalArgumentException {
    if (this. != 0)
      throw new IllegalStateException("To late to set the encoding!");
    this. = encoding;
  }
  // Write text methods
  // ----------------------------------------------------------------------------------------------

  
  public final void writeText(String textthrows IOException {
    if (text == nullreturn;
    deNude();
    this..writeText(text);
  }

  
  public final void writeText(char[] textint offint lenthrows IOException {
    deNude();
    this..writeText(textofflen);
  }

  
  public final void writeText(char cthrows IOException {
    deNude();
    this..writeText(c);
  }

  
Writes the string value of an object.

Does nothing if the object is null.

Parameters:
o The object that should be written as text.
Throws:
java.io.IOException If thrown by the wrapped writer.
See also:
java.lang.Object.toString()
writeText(java.lang.String)
  public final void writeText(Object othrows IOException {
    // TODO: what about an XML serializable ???
    // TODO: Add to interface ???
    if (o != null) {
      this.writeText(o.toString());
    }
  }
  // Write XML methods
  // ----------------------------------------------------------------------------------------------

  
  public final void writeXML(String textthrows IOException {
    if (text == nullreturn;
    deNude();
    this..write(text);
  }

  
  public final void writeXML(char[] textint offint lenthrows IOException {
    deNude();
    this..write(textofflen);
  }
  // Processing Instructions, CDATA sections and comments
  // ----------------------------------------------------------------------------------------------

  
  public final void writeComment(String commentthrows IOExceptionIllegalArgumentException {
    if (comment == null)
      return;
    if (comment.indexOf("--") >= 0)
      throw new IllegalArgumentException("A comment must not contain '--'.");
    deNude();
    this..write("<!-- ");
    this..write(comment);
    this..write(" -->");
    if (this.) {
      this..write('\n');
    }
  }

  
  public final void writePI(String targetString datathrows IOException {
    deNude();
    this..write("<?");
    this..write(target);
    this..write(' ');
    this..write(data);
    this..write("?>");
    if (this.) {
      this..write('\n');
    }
  }

  
  public final void writeCDATA(String datathrows IOException {
    if (data == nullreturn;
    final String end = "]]>";
    if (data.indexOf(end) >= 0)
      throw new IllegalArgumentException("CDATA sections must not contain \']]>\'");
    deNude();
    this..write("<![CDATA[");
    this..write(data);
    this..write(end);
  }
  // Attribute methods
  // ----------------------------------------------------------------------------------------------

  
  public final void attribute(String nameString value)
      throws IOException {
    if (!this.throw new IllegalStateException("Cannot write attribute: too late!");
    this..write(' ');
    this..write(name);
    this..write('=');
    this..write('"');
    this..writeAttValue(value);
    this..write('"');
  }

  
  public final void attribute(String nameint value)
      throws IOException {
    if (!this.throw new IllegalStateException("Cannot write attribute: too late!");
    this..write(' ');
    this..write(name);
    this..write('=');
    this..write('"');
    this..write(Integer.toString(value));
    this..write('"');
  }
  // Open/close specific elements
  // ----------------------------------------------------------------------------------------------

  
  public void element(String nameString textthrows IOException {
    this.openElement(name);
    this.writeText(text);
    closeElement();
  }
  // Direct access to the writer
  // ----------------------------------------------------------------------------------------------

  
  public final void flush() throws IOException {
    this..flush();
  }
  // Base class and convenience methods
  // ----------------------------------------------------------------------------------------------

  
Writes the end of the open element tag.

After this method is invoked it is not possible to write attributes for an element.

Throws:
java.io.IOException If thrown by the wrapped writer.
  abstract void deNude() throws IOException;

  
Insert the correct amount of space characterss depending on the depth and if the indent flag is set to true.

Throws:
java.io.IOException If thrown by the wrapped writer.
  void indent() throws IOException {
    if (this.) {
      for (int i = 0; i < this.i++) {
        this..write(this.);
      }
    }
  }

  
Does nothing.

This method exists so that we can explicitly say that we should do nothing in certain conditions.

  static final void doNothing() {
    return;
  }
New to GrepCode? Check out our FAQ X