KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > util > xml > parser > DefaultHandlerContext


1 package org.sapia.util.xml.parser;
2
3
4 // Imports of David Meggison's SAX classes
5
// ---------------------------------------
6
import org.xml.sax.Attributes JavaDoc;
7 import org.xml.sax.SAXException JavaDoc;
8
9 // Import of Sun's JDK classes
10
// ---------------------------
11
import java.util.HashMap JavaDoc;
12 import java.util.LinkedList JavaDoc;
13 import java.util.Map JavaDoc;
14
15
16 // Import of Apache's Log4j classes
17
// --------------------------------
18
//import org.apache.log4j.Logger;
19

20 /**
21  * The <CODE>DefaultHandlerContext</CODE> class is the default implementation of the
22  * <CODE>HandlerContextIF</CODE> interface.
23  *
24  * @see HandlerStateIF
25  * @see StatefullSAXHandler
26  * @author Jean-Cedric Desrochers
27  *
28  * <dl>
29  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
30  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
31  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
32  * </dl>
33  */

34 public class DefaultHandlerContext implements HandlerContextIF {
35   /////////////////////////////////////////////////////////////////////////////////////////
36
////////////////////////////////// CLASS ATTRIBUTES ///////////////////////////////////
37
/////////////////////////////////////////////////////////////////////////////////////////
38

39   /** Defines the logger of this class. */
40
41   // private static final Logger _theLogger = Logger.getLogger(DefaultHandlerContext.class);
42
/////////////////////////////////////////////////////////////////////////////////////////
43
///////////////////////////////// INSTANCE ATTRIBUTES /////////////////////////////////
44
/////////////////////////////////////////////////////////////////////////////////////////
45

46   /** The current handler state. */
47   private HandlerStateIF _theCurrentState;
48
49   /** The list (LIFO) of handler states. */
50   private LinkedList JavaDoc _theHandlerStates;
51
52   /** The map of prefix to namespace. */
53   private Map JavaDoc _theNamespaceMapping;
54
55   /** The map of handler state to their object result. */
56   private Map JavaDoc _theResults;
57
58   /////////////////////////////////////////////////////////////////////////////////////////
59
//////////////////////////////////// CONSTRUCTORS /////////////////////////////////////
60
/////////////////////////////////////////////////////////////////////////////////////////
61

62   /**
63    * Creates a new DefaultHandlerContext instance with the argument passed in.
64    *
65    * @param anInitialState The initial handler state of this context.
66    * @exception IllegalArgumentException If the initial handler state is null.
67    */

68   public DefaultHandlerContext(HandlerStateIF anInitialState) {
69     if (anInitialState == null) {
70       throw new IllegalArgumentException JavaDoc(
71         "The initial handler state passes in is null.");
72     }
73
74     _theCurrentState = anInitialState;
75     _theHandlerStates = new LinkedList JavaDoc();
76     _theNamespaceMapping = new HashMap JavaDoc();
77     _theResults = new HashMap JavaDoc();
78     _theHandlerStates.addFirst(anInitialState);
79   }
80
81   /////////////////////////////////////////////////////////////////////////////////////////
82
/////////////////////////////// INTERACE IMPLEMENTATION ///////////////////////////////
83
/////////////////////////////////////////////////////////////////////////////////////////
84

85   /**
86    * Returns the current handler state of this context.
87    *
88    * @return The current handler state of this context.
89    */

90   public synchronized HandlerStateIF getCurrentState() {
91     return _theCurrentState;
92   }
93
94   /**
95    * Changes the current handler state to the handler state passed in. The collection
96    * of state objects needs to behave like a stack (LIFO - Last in, first out) in
97    * order to go back to the previous state of a handler.<P>
98    *
99    * The handler context will notify the child handler state of the start of the element
100    * by calling it's <CODE>startElement()</CODE> method passing the arguments received.
101    *
102    * @param aHandlerState The new current handler state.
103    * @param anUri The namespace URI of the element that starts.
104    * @param aLocalName The local name of the element that starts.
105    * @param aQualifiedName The qualified name of the element that starts.
106    * @param someAttributes The attributes of the element that starts.
107    * @exception SAXException If an error occurs while setting the new handler state.
108    */

109   public synchronized void setCurrentState(HandlerStateIF aHandlerState,
110     String JavaDoc anUri, String JavaDoc aLocalName, String JavaDoc aQualifiedName,
111     Attributes JavaDoc someAttributes) throws SAXException JavaDoc {
112     _theCurrentState = aHandlerState;
113     _theHandlerStates.addFirst(aHandlerState);
114
115     try {
116       // Notifying the child handler state of the start of the element
117
_theCurrentState.startElement(this, anUri, aLocalName, aQualifiedName,
118         someAttributes);
119     } catch (SAXException JavaDoc se) {
120       String JavaDoc aMessage =
121         "Error when notifying the child handler state of the start of the element " +
122         aLocalName;
123
124       /* _theLogger.error(aMessage, se);
125        if (se.getException() != null) {
126          _theLogger.error("--> SAX NESTED EXCEPTION IS: ", se.getException());
127        }*/

128       throw new SAXException JavaDoc(aMessage, se);
129     }
130   }
131
132   /**
133    * Removes the current handler state from this handler context. The previous handler
134    * state becomes the current state.<P>
135    *
136    * The handler context will notify the parent handler state of the end of the
137    * element by calling it's <CODE>endElement()</CODE> method passing the arguments
138    * received.
139    *
140    * @param anURI The namespace URI of the element that closes.
141    * @param aLocalName The local name of the element that closes.
142    * @param aQualifiedName The qualified name of the element that closes.
143    * @exception SAXException If an error occurs while removing the new handler state.
144    */

145   public synchronized void removeCurrentState(String JavaDoc anUri, String JavaDoc aLocalName,
146     String JavaDoc aQualifiedName) throws SAXException JavaDoc {
147     // Validating the state of this context
148
if (_theHandlerStates.size() == 1) {
149       throw new IllegalStateException JavaDoc(
150         "Could not remove the root handler state.");
151     }
152
153     // Removing the current handler state
154
_theHandlerStates.removeFirst();
155
156     // Setting the previous handler state to be the current
157
_theCurrentState = (HandlerStateIF) _theHandlerStates.getFirst();
158
159     try {
160       // Notifying the parent handler state of the end of the element
161
_theCurrentState.endElement(this, anUri, aLocalName, aQualifiedName);
162     } catch (SAXException JavaDoc se) {
163       String JavaDoc aMessage =
164         "Error when notifying the parent handler state of the end of the element " +
165         aLocalName;
166
167       /*_theLogger.error(aMessage, se);
168       if (se.getException() != null) {
169         _theLogger.error("--> SAX NESTED EXCEPTION IS: ", se.getException());
170       }*/

171       throw new SAXException JavaDoc(aMessage, se);
172     }
173   }
174
175   /**
176    * Sets the namespace URI passed in as the current mapping for the given prefix.
177    *
178    * @param aPrefix The Namespace prefix being declared.
179    * @param anUri The Namespace URI mapped to the prefix.
180    */

181   public void startPrefixMapping(String JavaDoc aPrefix, String JavaDoc anUri) {
182     // Get the list of URI for the prefix
183
LinkedList JavaDoc aNamespaceList = (LinkedList JavaDoc) _theNamespaceMapping.get(aPrefix);
184
185     // If not in the map, create a new list
186
if (aNamespaceList == null) {
187       aNamespaceList = new LinkedList JavaDoc();
188
189       // Adding the list of namespace to the map of prefix
190
_theNamespaceMapping.put(aPrefix, aNamespaceList);
191     }
192
193     // Add the uri in the list of mapping at the top of the list.
194
aNamespaceList.addFirst(anUri);
195   }
196
197   /**
198    * Removes the current mapping of the prefix passed in.
199    *
200    * @param aPrefix The Namespace prefix being declared.
201    */

202   public void endPrefixMapping(String JavaDoc aPrefix) {
203     // Get the list of URI for the prefix
204
LinkedList JavaDoc aNamespaceList = (LinkedList JavaDoc) _theNamespaceMapping.get(aPrefix);
205
206     if (aNamespaceList != null) {
207       aNamespaceList.removeFirst();
208     } else {
209       throw new IllegalStateException JavaDoc(
210         "The prefix mapping is not found in the map.");
211     }
212   }
213
214   /**
215    * Returns the current namespace URI for the passed in prefix.
216    *
217    * @return The current namespace URI or <CODE>null</CODE> if not mapping exists.
218    */

219   public String JavaDoc getNamespaceURIFor(String JavaDoc aPrefix) {
220     String JavaDoc aNamespace = null;
221
222     // Get the namespace list for the prefix
223
LinkedList JavaDoc aNamespaceList = (LinkedList JavaDoc) _theNamespaceMapping.get(aPrefix);
224
225     // Get the current namespace URI, if it exists
226
if ((aNamespaceList != null) && !aNamespaceList.isEmpty()) {
227       aNamespace = (String JavaDoc) aNamespaceList.getFirst();
228     }
229
230     return aNamespace;
231   }
232
233   /**
234    * associates the result object to the handler state passed in.
235    *
236    * @param aHandlerState The handler state for which a result is provided.
237    * @param aResult The result object of the handler state.
238    */

239   public void setResultFor(HandlerStateIF aHandlerState, Object JavaDoc aResult) {
240     _theResults.put(aHandlerState, aResult);
241   }
242
243   /**
244    * Returns the result for the handler state passed in.
245    *
246    * @param aHandlerState The handler state for which to search for a result.
247    * @return The result object found, or <CODE>null</CODE> if no handler state was foud
248    * or if no result was associated for the handler state passed in.
249    */

250   public Object JavaDoc getResultFor(HandlerStateIF aHandlerState) {
251     return _theResults.get(aHandlerState);
252   }
253 }
254
Popular Tags