Source code: com/telefonicasoluciones/search/server/parser/xml/AttributeList.java
1 package com.telefonicasoluciones.search.server.parser.xml;
2
3 import java.util.Hashtable;
4 import java.util.Vector;
5 import java.util.Enumeration;
6
7 public class AttributeList {
8 private Hashtable list;
9 public AttributeList () {
10 list = new Hashtable();
11 }
12 public boolean exists (String name) {
13 if (name==null) return false;
14 if (list==null) return false;
15 return list.containsKey(name.toLowerCase());
16 }
17 public String get (String name) {
18 if (name==null) return null;
19 if (list == null) return null;
20 return (String) list.get(name.toLowerCase());
21 }
22 /**
23 * Returns an attribute with all double quote characters
24 * escaped with a backslash.
25 * @param name the name of the attribute.
26 */
27 public String getQuoted (String name) {
28
29 String value; // Stores the value of the attribute.
30 char[] array; // Character array from 'value'.
31 StringBuffer quoted; // Stores the quoted version of the value.
32 int i; // Loop variable.
33
34 // Check the name of the attribute is not null.
35 if (name == null) return null;
36
37 // Check that the attribute list is there.
38 if (list == null) return null;
39
40 // Get the value of the attribute.
41 value = (String) list.get(name.toLowerCase());
42
43 // Return nothing if there is no such attribute.
44 if (value == null) return null;
45
46 // Return an empty string if that is what is stored.
47 if (value.length() == 0) return "";
48
49 // Convert the value into a character array.
50 array = value.toCharArray();
51
52 // Create a new StringBuffer to store the quoted value.
53 quoted = new StringBuffer(array.length);
54
55 // Loop round the characters in the array.
56 for (i = 0; i < array.length; i++) {
57
58 // Escape any quotation marks.
59 if (array[i] == '"') {
60 quoted.append("\\\"");
61 continue;
62 }
63
64 // Escape any additional backslash characters.
65 if (array[i] == '\\') {
66 quoted.append("\\\\");
67 continue;
68 }
69
70 // Otherwise append the character without an escape.
71 quoted.append(array[i]);
72 }
73
74 // Return a string version of the buffer.
75 return quoted.toString();
76 }
77 public Enumeration names () {
78 if (list == null) return null;
79 return list.keys();
80 }
81 public void set (String name, String value) {
82 if (name==null) return;
83 if (value==null) value = "";
84 if (list==null) return;
85 list.put(name.toLowerCase(), value);
86 }
87 public int size () {
88 return list.size();
89 }
90 public String toString () {
91 StringBuffer buffer = new StringBuffer();
92 Enumeration nameList = names();
93 String name;
94 String attr;
95
96 while (nameList.hasMoreElements()) {
97 name = (String) nameList.nextElement();
98 attr = toString(name);
99 buffer.append(attr);
100 if (nameList.hasMoreElements())
101 buffer.append(' ');
102 }
103 return buffer.toString();
104 }
105 public String toString (String name) {
106 String value;
107 if (name == null) return "";
108 if (! exists(name)) return "";
109 value = getQuoted(name);
110 if (value == null) return name;
111 if (value.length()>0)
112 return name+"=\""+value+'"';
113 else
114 return name;
115 }
116 public void unset(String name) {
117 if (name==null) return;
118 if (list==null) return;
119 list.remove(name.toLowerCase());
120 }
121 }