Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
   *
   * Licensed under the Apache License, Version 2.0 (the "License").
   * You may not use this file except in compliance with the License.
   * A copy of the License is located at
   *
   *  http://aws.amazon.com/apache2.0
   *
  * or in the "license" file accompanying this file. This file 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 com.amazonaws.services.s3.internal;
 import static com.amazonaws.util.StringUtils.UTF8;
 
 import java.util.List;


Basic XML Writer helper library. It does not do full XML Well-Formedness checks, but provides basic element and attribute value escaping, and tracks open tags to simplify use.

Sampe usage:

 
 XmlWriter w = new XmlWriter();
 w.start(DOC_TAG, "xmlns", DOC_NAMESPACE);
 if (foo != null)
   w.start(FOO_TAG).value(foo).end();
 w.end();
 
 
 
 public class XmlWriter {
     List<Stringtags = new ArrayList<String>();
     StringBuilder sb = new StringBuilder();
 
     public XmlWriter start(String name) {
         .append("<").append(name).append(">");
         .add(name);
         return this;
     }
 
     public XmlWriter start(String nameString attrString value) {
         .append("<").append(name);
         writeAttr(attrvalue);
         .append(">");
         .add(name);
         return this;
     }
 
     public XmlWriter start(String nameString[] attrsString[] values) {
         .append("<").append(name);
         for (int i = 0; i < Math.min(attrs.lengthvalues.length); i++) {
             writeAttr(attrs[i], values[i]);
         }
         .append(">");
         .add(name);
         return this;
     }
 
     public XmlWriter end() {
         assert(.size() > 0);
         String name = .remove(.size() - 1);
         .append("</").append(name).append(">");
         return this;
     }
 
     public byte[] getBytes() {
         assert(.size() == 0);
         return this.toString().getBytes();
     }
 
     public String toString() {
         return .toString();
     }
 
     public XmlWriter value(String value) {
         appendEscapedString(value);
         return this;
     }
 
     private void writeAttr(String nameString value) {
         .append(' ').append(name).append("=\"");
         appendEscapedString(value);
         .append("\"");
     }

    
Appends the specified string (with any non-XML-compatible characters replaced with the corresponding escape code) to the specified StringBuilder.

Parameters:
s The string to escape and append to the specified StringBuilder.
builder The StringBuilder to which the escaped string should be appened.
    private void appendEscapedString(String sStringBuilder builder) {
        if (s == null)
            s = "";
        int pos;
        int start = 0;
        int len = s.length();
        for (pos = 0; pos < lenpos++) {
            char ch = s.charAt(pos);
            String escape;
            switch (ch) {
            case '\t':
                escape = "&#9;";
                break;
            case '\n':
                escape = "&#10;";
                break;
            case '\r':
                escape = "&#13;";
                break;
            case '&':
                escape = "&amp;";
                break;
            case '"':
                escape = "&quot;";
                break;
            case '<':
                escape = "&lt;";
                break;
            case '>':
                escape = "&gt;";
                break;
            default:
                escape = null;
                break;
            }
            // If we found an escape character, write all the characters up to that
            // character, then write the escaped char and get back to scanning
            if (escape != null) {
                if (start < pos)
                    builder.append(sstartpos);
                .append(escape);
                start = pos + 1;
            }
        }
        // Write anything that's left
        if (start < pos.append(sstartpos);
    }
New to GrepCode? Check out our FAQ X