Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.apache.catalina.util;
 
 import java.io.Writer;

XMLWriter helper class.

Author(s):
Remy Maucherat
 
 public class XMLWriter {
 
 
     // -------------------------------------------------------------- Constants
 

    
Opening tag.
 
     public static final int OPENING = 0;


    
Closing tag.
 
     public static final int CLOSING = 1;


    
Element with no content.
 
     public static final int NO_CONTENT = 2;
 
 
     // ----------------------------------------------------- Instance Variables
 

    
Buffer.
 
     protected StringBuilder buffer = new StringBuilder();


    
Writer.
 
     protected Writer writer = null;
 
 
     // ----------------------------------------------------------- Constructors
 

    
Constructor.
 
     public XMLWriter() {
     }


    
Constructor.
 
     public XMLWriter(Writer writer) {
         this. = writer;
     }
 
 
     // --------------------------------------------------------- Public Methods
 

    
Retrieve generated XML.

Returns:
String containing the generated XML
 
     public String toString() {
         return .toString();
     }


    
Write property to the XML.

Parameters:
namespace Namespace
namespaceInfo Namespace info
name Property name
value Property value
    public void writeProperty(String namespaceString namespaceInfo,
                              String nameString value) {
        writeElement(namespacenamespaceInfoname);
        .append(value);
        writeElement(namespacenamespaceInfoname);
    }


    
Write property to the XML.

Parameters:
namespace Namespace
name Property name
value Property value
    public void writeProperty(String namespaceString nameString value) {
        writeElement(namespacename);
        .append(value);
        writeElement(namespacename);
    }


    
Write property to the XML.

Parameters:
namespace Namespace
name Property name
    public void writeProperty(String namespaceString name) {
        writeElement(namespacename);
    }


    
Write an element.

Parameters:
name Element name
namespace Namespace abbreviation
type Element type
    public void writeElement(String namespaceString nameint type) {
        writeElement(namespacenullnametype);
    }


    
Write an element.

Parameters:
namespace Namespace abbreviation
namespaceInfo Namespace info
name Element name
type Element type
    public void writeElement(String namespaceString namespaceInfo,
                             String nameint type) {
        if ((namespace != null) && (namespace.length() > 0)) {
            switch (type) {
            case :
                if (namespaceInfo != null) {
                    .append("<" + namespace + ":" + name + " xmlns:"
                                  + namespace + "=\""
                                  + namespaceInfo + "\">");
                } else {
                    .append("<" + namespace + ":" + name + ">");
                }
                break;
            case :
                .append("</" + namespace + ":" + name + ">\n");
                break;
            case :
            default:
                if (namespaceInfo != null) {
                    .append("<" + namespace + ":" + name + " xmlns:"
                                  + namespace + "=\""
                                  + namespaceInfo + "\"/>");
                } else {
                    .append("<" + namespace + ":" + name + "/>");
                }
                break;
            }
        } else {
            switch (type) {
            case :
                .append("<" + name + ">");
                break;
            case :
                .append("</" + name + ">\n");
                break;
            case :
            default:
                .append("<" + name + "/>");
                break;
            }
        }
    }


    
Write text.

Parameters:
text Text to append
    public void writeText(String text) {
        .append(text);
    }


    
Write data.

Parameters:
data Data to append
    public void writeData(String data) {
        .append("<![CDATA[" + data + "]]>");
    }


    
Write XML Header.
    public void writeXMLHeader() {
        .append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n");
    }


    
Send data and reinitializes buffer.
    public void sendData()
        throws IOException {
        if ( != null) {
            .write(.toString());
             = new StringBuilder();
        }
    }
New to GrepCode? Check out our FAQ X