View Javadoc

1   /*
2    * Copyright (c) 2004 UNINETT FAS
3    *
4    * This program is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU General Public License as published by the Free
6    * Software Foundation; either version 2 of the License, or (at your option)
7    * any later version.
8    *
9    * This program is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11   * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12   * more details.
13   *
14   * You should have received a copy of the GNU General Public License along with
15   * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16   * Place - Suite 330, Boston, MA 02111-1307, USA.
17   *
18   * $Id: AttribsHandler.java,v 1.11 2006/01/12 11:53:12 catoolsen Exp $
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          /* look for <attribute> and allocate a new AttribsData if found */
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         /* wait for </attribute> */
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 }