FileDocCategorySizeDatePackage
XMLWriter.javaAPI DocApache Tomcat 6.0.146269Fri Jul 20 04:20:32 BST 2007org.apache.catalina.util

XMLWriter

public class XMLWriter extends Object
XMLWriter helper class.
author
Remy Maucherat

Fields Summary
public static final int
OPENING
Opening tag.
public static final int
CLOSING
Closing tag.
public static final int
NO_CONTENT
Element with no content.
protected StringBuffer
buffer
Buffer.
protected Writer
writer
Writer.
Constructors Summary
public XMLWriter()
Constructor.



    // ----------------------------------------------------------- Constructors


          
      
    
public XMLWriter(Writer writer)
Constructor.

        this.writer = writer;
    
Methods Summary
public voidsendData()
Send data and reinitializes buffer.

        if (writer != null) {
            writer.write(buffer.toString());
            buffer = new StringBuffer();
        }
    
public java.lang.StringtoString()
Retrieve generated XML.

return
String containing the generated XML

        return buffer.toString();
    
public voidwriteData(java.lang.String data)
Write data.

param
data Data to append

        buffer.append("<![CDATA[" + data + "]]>");
    
public voidwriteElement(java.lang.String namespace, java.lang.String name, int type)
Write an element.

param
name Element name
param
namespace Namespace abbreviation
param
type Element type

        writeElement(namespace, null, name, type);
    
public voidwriteElement(java.lang.String namespace, java.lang.String namespaceInfo, java.lang.String name, int type)
Write an element.

param
namespace Namespace abbreviation
param
namespaceInfo Namespace info
param
name Element name
param
type Element type

        if ((namespace != null) && (namespace.length() > 0)) {
            switch (type) {
            case OPENING:
                if (namespaceInfo != null) {
                    buffer.append("<" + namespace + ":" + name + " xmlns:"
                                  + namespace + "=\""
                                  + namespaceInfo + "\">");
                } else {
                    buffer.append("<" + namespace + ":" + name + ">");
                }
                break;
            case CLOSING:
                buffer.append("</" + namespace + ":" + name + ">\n");
                break;
            case NO_CONTENT:
            default:
                if (namespaceInfo != null) {
                    buffer.append("<" + namespace + ":" + name + " xmlns:"
                                  + namespace + "=\""
                                  + namespaceInfo + "\"/>");
                } else {
                    buffer.append("<" + namespace + ":" + name + "/>");
                }
                break;
            }
        } else {
            switch (type) {
            case OPENING:
                buffer.append("<" + name + ">");
                break;
            case CLOSING:
                buffer.append("</" + name + ">\n");
                break;
            case NO_CONTENT:
            default:
                buffer.append("<" + name + "/>");
                break;
            }
        }
    
public voidwriteProperty(java.lang.String namespace, java.lang.String namespaceInfo, java.lang.String name, java.lang.String value)
Write property to the XML.

param
namespace Namespace
param
namespaceInfo Namespace info
param
name Property name
param
value Property value

        writeElement(namespace, namespaceInfo, name, OPENING);
        buffer.append(value);
        writeElement(namespace, namespaceInfo, name, CLOSING);

    
public voidwriteProperty(java.lang.String namespace, java.lang.String name, java.lang.String value)
Write property to the XML.

param
namespace Namespace
param
name Property name
param
value Property value

        writeElement(namespace, name, OPENING);
        buffer.append(value);
        writeElement(namespace, name, CLOSING);
    
public voidwriteProperty(java.lang.String namespace, java.lang.String name)
Write property to the XML.

param
namespace Namespace
param
name Property name

        writeElement(namespace, name, NO_CONTENT);
    
public voidwriteText(java.lang.String text)
Write text.

param
text Text to append

        buffer.append(text);
    
public voidwriteXMLHeader()
Write XML Header.

        buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n");