Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2004-2010 Oracle Corporation. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at Contributors: Kohsuke Kawaguchi /
 package org.kohsuke.stapler.export;
Writes XML.

Kohsuke Kawaguchi
 final class XMLDataWriter implements DataWriter {
     private String name;
     private final Stack<StringobjectNames = new Stack<String>();
     private final Stack<BooleanarrayState = new Stack<Boolean>();
     private final Writer out;
     public boolean isArray;
     XMLDataWriter(Object beanWriter outthrows IOException {
         Class c=bean.getClass();
         while (c.isAnonymousClass())
             c = c.getSuperclass();
          = Introspector.decapitalize(c.getSimpleName());
         this. = out;
     XMLDataWriter(Object beanStaplerResponse rspthrows IOException {
     public void name(String name) {
         this. = name;
     public void valuePrimitive(Object vthrows IOException {
     public void value(String vthrows IOException {
         String n = adjustName();
     private String escape(String v) {
         StringBuffer buf = new StringBuffer(v.length()+64);
         forint i=0; i<v.length(); i++ ) {
             char ch = v.charAt(i);
         return buf.toString();
     public void valueNull() {
         // use absence to indicate null.
     public void startArray() {
         // use repeated element to display array
         // this means nested arrays are not supported
          = true;
     public void endArray() {
          = false;
     public void startObject() throws IOException {
         = false;
    public void endObject() throws IOException {
         = .pop();
         = .pop();

Returns the name to be used as an element name by considering isArray
    private String adjustName() {
        String escaped = makeXmlName();
        if(return toSingular(escaped);
        return escaped;
    /*package*/ static String toSingular(String name) {
            return name.substring(0,name.length()-1);
        return name;
    /*package*/ static String makeXmlName(String name) {
        if (name.length()==0)   name="_";
        if (!XmlChars.isNameStart(name.charAt(0))) {
            if (name.length()>1 && XmlChars.isNameStart(name.charAt(1)))
                name = name.substring(1);
                name = '_'+name;
        int i=1;
        while (i<name.length()) {
            if (XmlChars.isNameChar(name.charAt(i)))
                name = name.substring(0,i)+name.substring(i+1);
        return name;
New to GrepCode? Check out our FAQ X