KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > xml > XMLAttributeList


1 /*
2  * XML input/output support for FindBugs
3  * Copyright (C) 2004, University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs.xml;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.List JavaDoc;
25
26 import edu.umd.cs.findbugs.annotations.NonNull;
27
28 /**
29  * Helper class to format attributes in an XML tag.
30  *
31  * @author David Hovemeyer
32  */

33 public class XMLAttributeList {
34     public static class NameValuePair {
35         private String JavaDoc name;
36         private String JavaDoc value;
37
38         public NameValuePair(String JavaDoc name, String JavaDoc value) {
39             this.name = name;
40             this.value = value;
41         }
42
43         public String JavaDoc getName() {
44             return name;
45         }
46
47         public String JavaDoc getValue() {
48             return value;
49         }
50     }
51
52     private static class StringBufferQuoteMetaCharacters extends QuoteMetaCharacters {
53         private StringBuffer JavaDoc buf;
54
55         public StringBufferQuoteMetaCharacters(String JavaDoc text, MetaCharacterMap map,
56             StringBuffer JavaDoc buf) {
57             super(text, map);
58             this.buf = buf;
59         }
60
61         @Override JavaDoc
62                  public void process() {
63             try {
64                 super.process();
65             } catch (java.io.IOException JavaDoc e) {
66                 // This can't actually happen - we're writing to a StringBuffer
67
}
68         }
69
70         @Override JavaDoc
71                  public void emitLiteral(String JavaDoc s) {
72             buf.append(s);
73         }
74     }
75
76     // The "<", ">", and "&" must be escaped in
77
// attribute values. I have no idea why this is useful,
78
// but that's standards for you.
79
private static final MetaCharacterMap attrMetaCharacterMap = new MetaCharacterMap();
80     static {
81         attrMetaCharacterMap.addMeta('<', "&lt;");
82         attrMetaCharacterMap.addMeta('>', "&gt;");
83         attrMetaCharacterMap.addMeta('&', "&amp;");
84         attrMetaCharacterMap.addMeta('"', "&quot;");
85     }
86
87     // Fields
88
private List JavaDoc<NameValuePair> nameValuePairList;
89
90     /**
91      * Constructor.
92      * Creates an empty object.
93      */

94     public XMLAttributeList() {
95         this.nameValuePairList = new LinkedList JavaDoc<NameValuePair>();
96     }
97
98     /**
99      * Add a single attribute name and value.
100      *
101      * @param name the attribute name
102      * @param value the attribute value
103      * @return this object (so calls to addAttribute() can be chained)
104      */

105     public XMLAttributeList addAttribute(@NonNull String JavaDoc name, @NonNull String JavaDoc value) {
106         if (name == null) throw new NullPointerException JavaDoc("name must be nonnull");
107         if (value == null) throw new NullPointerException JavaDoc("value must be nonnull");
108         nameValuePairList.add(new NameValuePair(name, value));
109         return this;
110     }
111
112     /**
113      * Return the attribute list as a String which can be
114      * directly output as part of an XML tag.
115      */

116     @Override JavaDoc
117          public String JavaDoc toString() {
118         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
119         for (NameValuePair pair : nameValuePairList) {
120             buf.append(' ');
121             buf.append(pair.getName());
122             buf.append('=');
123             buf.append(getQuotedAttributeValue(pair.getValue()));
124         }
125         return buf.toString();
126     }
127
128     /**
129      * Return an Iterator over NameValuePairs.
130      */

131     public Iterator JavaDoc<NameValuePair> iterator() {
132         return nameValuePairList.iterator();
133     }
134
135     /**
136      * Return a properly quoted form for an attribute value.
137      * @param rawValue the raw value of the attribute
138      * @return a properly quoted representation of the value
139      */

140     public static String JavaDoc getQuotedAttributeValue(@NonNull String JavaDoc rawValue) {
141         if (rawValue == null) throw new NullPointerException JavaDoc("rawValue must be nonnull");
142         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
143         buf.append('"');
144         new StringBufferQuoteMetaCharacters(rawValue, attrMetaCharacterMap, buf).process();
145         buf.append('"');
146         return buf.toString();
147     }
148 }
149
150 // vim:ts=4
151
Popular Tags