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;
 import java.util.List;

A simple writer for XML data that does not support namespaces.

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"));
 

This class is not synchronised and does not support namespaces, and will therefore throw an unsupported operation exception for each call to a method that uses namespaces.

Author(s):
Christophe Lauret
Version:
6 December 2008
 
 public final class XMLWriterImpl extends XMLWriterBase implements XMLWriter {

  
The root node.
 
   private static final Element ROOT;
   static {
      = new Element(""true);
   }

  
A stack of elements to close the elements automatically.
 
   private final List<Elementelements = new ArrayList<Element>();
 
   // Constructors
   // ----------------------------------------------------------------------------------------------
 
  

Creates a new XML writer.

Sets the depth attribute to 0 and the indentation to true.

Parameters:
writer Where this writer should write the XML data.
Throws:
java.lang.NullPointerException If the writer is null.
 
   public XMLWriterImpl(Writer writerthrows NullPointerException {
     super(writerfalse);
     this..add();
   }

  

Create 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 XMLWriterImpl(Writer writerboolean indentthrows NullPointerException {
     super(writerindent);
     this..add();
   }
 
   // Writing text
   // ----------------------------------------------------------------------------------------------
 
  
Writes the angle bracket if the element open tag is not finished.

Throws:
java.io.IOException If thrown by the wrapped writer.
 
   @Override
   void deNude() throws IOException {
     if (this.) {
       this..write('>');
       if (peekElement(). && this.) {
         this..write('\n');
       }
       this. = false;
     }
   }
 
  // Open/close specific elements
  // ----------------------------------------------------------------------------------------------

  
Writes a start element tag correctly indented.

It is the same as openElement(null, name, false)

Parameters:
name The name of the element
Throws:
java.io.IOException If thrown by the wrapped writer.
See also:
openElement(java.lang.String,java.lang.String,boolean)
  public void openElement(String namethrows IOException {
    openElement(namefalse);
  }

  
Writes a start element tag correctly indented.

Use the hasChildren parameter to specify whether this element is terminal node or not, which affects the indenting.

The name can contain attributes and should be a valid xml name.

Parameters:
name The name of the element.
hasChildren true if this element has children.
Throws:
java.io.IOException If thrown by the wrapped writer.
  public void openElement(String nameboolean hasChildrenthrows IOException {
    deNude();
    indent();
    this..add(new Element(namehasChildren));
    this..write('<');
    this..write(name);
    this. = true;
    this.++;
  }

  
Write the end element tag.

Throws:
java.io.IOException If thrown by the wrapped writer.
IllegalCloseElementException If there is no element to close
  public void closeElement() throws IOExceptionIllegalCloseElementException {
    Element elt = popElement();
    // reaching the end of the document
    if (elt == )
      throw new IllegalCloseElementException();
    this.--;
    // this is an empty element
    if (this.) {
      this..write('/');
      this. = false;
      // the element contains text
    } else {
      if (elt.hasChildren) {
        indent();
      }
      this..write('<');
      this..write('/');
      int x = elt.name.indexOf(' ');
      if (x < 0) {
        this..write(elt.name);
      } else {
        this..write(elt.name.substring(0, x));
      }
    }
    this..write('>');
    // take care of the new line if the indentation is on
    if (super.) {
      Element parent = peekElement();
      if (parent.hasChildren && parent != ) {
        this..write('\n');
      }
    }
  }

  
Same as emptyElement(null, element);.

It is possible for the element to contain attributes, however, since there is no character escaping, great care must be taken not to introduce invalid characters. For example:

    <example test="yes"/>
 

Parameters:
element the name of the element
Throws:
java.io.IOException If thrown by the wrapped writer.
  public void emptyElement(String elementthrows IOException {
    deNude();
    indent();
    this..write('<');
    this..write(element);
    this..write('/');
    this..write('>');
    if (this.) {
      Element parent = peekElement();
      if (parent.hasChildren && parent != ) {
        this..write('\n');
      }
    }
  }

  
Returns the last element in the list.

Returns:
The current element.
  private Element peekElement() {
    return this..get(this..size() - 1);
  }

  
Removes the last element in the list.

Returns:
The current element.
  private Element popElement() {
    return this..remove(this..size() - 1);
  }
  // Unsupported operations
  // ----------------------------------------------------------------------------------------------

  
Not supported.

Parameters:
uri This parameter is ignored.
name This parameter is ignored.
Throws:
java.lang.UnsupportedOperationException This class does not handle namespaces.
  public void openElement(String uriString namethrows UnsupportedOperationException {
    throw new UnsupportedOperationException("This class does not handle namespaces.");
  }

  
Not supported.

Parameters:
uri This parameter is ignored.
name This parameter is ignored.
hasChildren This parameter is ignored.
Throws:
java.lang.UnsupportedOperationException This class does not handle namespaces.
  public void openElement(String uriString nameboolean hasChildren)
      throws UnsupportedOperationException {
    throw new UnsupportedOperationException("This class does not handle namespaces.");
  }

  
Not supported.

Parameters:
uri This parameter is ignored.
element This parameter is ignored.
Throws:
java.lang.UnsupportedOperationException This class does not handle namespaces.
  public void emptyElement(String uriString element)
      throws UnsupportedOperationException {
    throw new UnsupportedOperationException("This class does not handle namespaces");
  }

  
Not supported.

Parameters:
uri This parameter is ignored.
prefix This parameter is ignored.
Throws:
java.lang.UnsupportedOperationException This class does not handle namespaces.
  public void setPrefixMapping(String uriString prefix)
      throws UnsupportedOperationException {
    throw new UnsupportedOperationException("This class does not handle namespaces");
  }

  
Not supported.

Parameters:
uri This parameter is ignored.
name The name of the attribute.
value The value of the attribute.
Throws:
java.lang.UnsupportedOperationException This class does not handle namespaces.
  public void attribute(String uriString nameString value)
      throws UnsupportedOperationException {
    throw new UnsupportedOperationException("This class does not handle namespaces");
  }

  
Not supported.

Parameters:
uri This parameter is ignored.
name The name of the attribute.
value The value of the attribute.
Throws:
java.lang.UnsupportedOperationException This class does not handle namespaces.
  public void attribute(String uriString nameint value)
      throws UnsupportedOperationException {
    throw new UnsupportedOperationException("This class does not handle namespaces");
  }

  
Close the writer.

Throws:
java.io.IOException If thrown by the wrapped writer.
UnclosedElementException If an element has been left open.
  public void close() throws IOExceptionUnclosedElementException {
    Element open = peekElement();
    if (open != )
      throw new UnclosedElementException(open.name);
    this..close();
  }
  // Inner class: Element
  // ----------------------------------------------------------------------------------------------

  
A light object to keep track of the element.

This object does not support namespaces.

Author(s):
Christophe Lauret
Version:
7 March 2005
  private static final class Element {

    
The fully qualified name of the element.
    private final String name;

    
Indicates whether the element has children.
    private final boolean hasChildren;

    
Creates a new Element.

Parameters:
name The qualified name of the element.
hasChildren Whether the element has children.
    public Element(String nameboolean hasChildren) {
      this. = name;
      this. = hasChildren;
    }
  }
New to GrepCode? Check out our FAQ X