1 package eu.simuline.util.sgml;
2
3 import eu.simuline.util.ListMap;
4
5 import org.xml.sax.Attributes;
6
7 /**
8 * An **** partial **** implementation
9 * of the SAX-interface <code>Attributes</code>
10 * which allows attributes without values using {@link #NO_VALUE}.
11 *
12 * @author <a href="mailto:ernst.reissner@simuline.eu">Ernst Reissner</a>
13 * @version 1.0
14 */
15 public final class AttributesImpl implements Attributes {
16
17 /* ----------------------------------------------------------------- *
18 * class constants *
19 * ----------------------------------------------------------------- */
20
21 /**
22 * Used as a value in {@link #name2value}
23 * to signify that the corresponding attribute has no value.
24 * This is much better than simply unsing <code>null</code>.
25 * The latter would not allow to rule out a multiple attribute
26 * without a value.
27 */
28 @edu.umd.cs.findbugs.annotations.SuppressWarnings
29 (value = "DM_STRING_VOID_CTOR",
30 justification = "used for equality check")
31 public static final String NO_VALUE = new String(""); // NOPMD
32
33 /* ----------------------------------------------------------------- *
34 * fields *
35 * ----------------------------------------------------------------- */
36
37 /**
38 * Maps the name of an attribute to its value.
39 * The name must be a string.
40 * If there is a value (which is mandatory in xml)
41 * the value is also a string value.
42 * Otherwise it is {@link #NO_VALUE}.
43 */
44 private final ListMap<String, String> name2value;
45
46 /* ----------------------------------------------------------------- *
47 * constructors *
48 * ----------------------------------------------------------------- */
49
50 /**
51 * Creates a new empty <code>AttributesImpl</code>
52 * which represents the given attribute list.
53 *
54 * @param name2value
55 * a <code>ListMap</code> representing an attribute list,
56 * as specified for {@link #name2value}.
57 */
58 AttributesImpl(ListMap<String, String> name2value) {
59 this.name2value = name2value;
60 }
61
62 /* ----------------------------------------------------------------- *
63 * methods *
64 * ----------------------------------------------------------------- */
65 /**
66 * Converts {@link #NO_VALUE} to <code>null</code>
67 * simply casting other arguments to type <code>String</code>.
68 *
69 * @param valueOrNot
70 * a <code>String</code> or the object {@link #NO_VALUE}.
71 * @return
72 * a <code>String</code> which is
73 * <ul>
74 * <li>
75 * <code>null</code> for <code>valueOrNot == NO_VALUE</code>.
76 * <li>
77 * <code>valueOrNot</code> itself casted to a string otherwise.
78 * </ul>
79 */
80 private static String noValueToNull(Object valueOrNot) {
81 return valueOrNot == NO_VALUE ? null : (String) valueOrNot; // NOPMD
82 }
83
84 /* ----------------------------------------------------------------- *
85 * methods implementing Attributes *
86 * ----------------------------------------------------------------- */
87
88 // apidoc provided by javadoc.
89 public int getLength() {
90 return this.name2value.size();
91 }
92 public String getURI(int index) {
93 throw new eu.simuline.util.NotYetImplementedException();
94 }
95 public String getLocalName(int index) {
96 throw new eu.simuline.util.NotYetImplementedException();
97 }
98 public String getQName(int index) {
99 throw new eu.simuline.util.NotYetImplementedException();
100 }
101 public String getType(int index) {
102 throw new eu.simuline.util.NotYetImplementedException();
103 }
104 public String getValue(int index) {
105 throw new eu.simuline.util.NotYetImplementedException();
106 }
107 public int getIndex(String uri,
108 String localPart) {
109 throw new eu.simuline.util.NotYetImplementedException();
110 }
111 public int getIndex(String qName) {
112 throw new eu.simuline.util.NotYetImplementedException();
113 }
114 public String getType(String uri,
115 String localName) {
116 throw new eu.simuline.util.NotYetImplementedException();
117 }
118 public String getType(String qName) {
119 throw new eu.simuline.util.NotYetImplementedException();
120 }
121 public String getValue(String uri,
122 String localName) {
123 throw new eu.simuline.util.NotYetImplementedException();
124 }
125 public String getValue(String qName) {
126 return noValueToNull(this.name2value.get(qName));
127 }
128
129 public AttributesImpl toLowerCase() {
130 throw new eu.simuline.util.NotYetImplementedException();
131 }
132
133 public String toString() {
134 return this.name2value.toString();
135 }
136 } // class AttributesImpl