1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package no.feide.moria.servlet;
22
23 import org.xml.sax.Attributes;
24 import org.xml.sax.SAXException;
25 import org.xml.sax.helpers.DefaultHandler;
26 import java.util.HashMap;
27
28 /***
29 *
30 * @author Eva Indal
31 * @version $Revision: 1.11 $
32 *
33 * The AttribsHandler class extends the xml DefaultHandler
34 * to read the simple attribute description file.
35 */
36 public class AttribsHandler extends DefaultHandler {
37
38 /***
39 * Constructor.
40 *
41 */
42 public AttribsHandler() {
43 currentattribute = null;
44 adata = new HashMap();
45 indexcounter = 0;
46 }
47
48 /*** Hash map of parsed attributes. */
49 private HashMap adata;
50
51 /*** The attribute being parsed. */
52 private AttribsData currentattribute;
53
54 /*** String value of current attribute. */
55 private String currentchars;
56
57 /*** Attribute's number. */
58 private int indexcounter;
59
60 /***
61 * Implements callback that is called at start of document. Empty for now.
62 *
63 * @throws SAXException
64 * Required by interface.
65 */
66 public void startDocument() throws SAXException {
67 }
68
69 /***
70 * Implements callback that is called at end of document. Empty for now.
71 *
72 * @throws SAXException
73 * Required by interface.
74 */
75 public void endDocument() throws SAXException {
76 }
77
78 /***
79 * Implements callback that is called at start of an xml element.
80 *
81 * @param namespaceURI Namespace URI.
82 * @param sName The local name (without prefix), or the empty string if Namespace processing is not being performed.
83 * @param qName The qualified name (with prefix), or the empty string if qualified names are not available.
84 * @param attrs The specified or defaulted attributes.
85 * @throws SAXException
86 * Required by interface.
87 *
88 * @see org.xml.sax.helpers.DefaultHandler#startElement
89 * for information about the parameters
90 */
91 public final void startElement(final String namespaceURI, final String sName, final String qName, final Attributes attrs)
92 throws SAXException {
93 String eName = sName;
94 if ("".equals(eName)) eName = qName;
95
96
97 if (eName.equals("attribute")) {
98 currentattribute = new AttribsData(indexcounter);
99 indexcounter++;
100 currentchars = null;
101 } else if (currentattribute != null) {
102 currentchars = "";
103 }
104 }
105
106 /***
107 * Implements callback that is called at end of an xml element.
108 *
109 * @param namespaceURI Namespace URI.
110 * @param sName The local name (without prefix), or the empty string if Namespace processing is not being performed.
111 * @param qName The qualified XML 1.0 name (with prefix), or the empty string if qualified names are not available.
112 * @throws SAXException
113 * Required by interface.
114 *
115 * @see org.xml.sax.helpers.DefaultHandler#endElement
116 * for information about the parameters
117 */
118 public final void endElement(final String namespaceURI, final String sName, final String qName) throws SAXException {
119 String eName = sName;
120 if ("".equals(eName)) eName = qName;
121
122
123 if (eName.equals("attribute")) {
124 adata.put(currentattribute.getData("key"), currentattribute);
125 currentattribute = null;
126 } else if (currentattribute != null) {
127 currentattribute.addData(eName, currentchars);
128 }
129 }
130
131 /***
132 * Implements callback that is called to process data for an element.
133 *
134 * @param buf The characters.
135 * @param offset The start position in the character array.
136 * @param len The number of characters to use from the character array.
137 * @throws SAXException
138 * Required by interface.
139 *
140 * @see org.xml.sax.helpers.DefaultHandler#characters
141 * for information about the parameters
142 */
143 public final void characters(final char[] buf, final int offset, final int len) throws SAXException {
144 String s = new String(buf, offset, len);
145 currentchars += s;
146 }
147
148 /***
149 * Gets parsed attributes.
150 *
151 * Each element in the returned HashMap is an AttribsData instance
152 * @return The parsed attributes.
153 */
154 public final HashMap getAttribs() {
155 return adata;
156 }
157
158 }