KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > xml > handler > AbstractUnmarshallerDecorator


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31
32 package org.objectweb.proactive.core.xml.handler;
33
34 import org.objectweb.proactive.core.xml.io.Attributes;
35
36 /**
37  *
38  * Receives SAX event and pass them on
39  *
40  * @author Lionel Mestre
41  * @version 0.91
42  *
43  */

44 public abstract class AbstractUnmarshallerDecorator implements UnmarshallerHandler {
45     
46     
47   private java.util.HashMap JavaDoc handlersMap;
48   private int elementCounter = 0;
49   private UnmarshallerHandler currentActiveHandler;
50   private boolean lenient;
51
52   
53   //
54
// -- CONSTRUCTORS -----------------------------------------------
55
//
56

57   public AbstractUnmarshallerDecorator(boolean lenient) {
58     handlersMap = new java.util.HashMap JavaDoc();
59     this.lenient = lenient;
60   }
61   
62   public AbstractUnmarshallerDecorator() {
63     this(true);
64   }
65   
66
67   //
68
// -- PUBLIC METHODS -----------------------------------------------
69
//
70

71   public void addHandler(String JavaDoc elementName, UnmarshallerHandler handler) {
72     handlersMap.put(elementName, handler);
73   }
74
75
76   //
77
// -- implements UnmarshallerHandler ------------------------------------------------------
78
//
79

80   // left abstract
81

82   
83   //
84
// -- implements XMLHandler ------------------------------------------------------
85
//
86

87
88   public void startElement(String JavaDoc name, Attributes attributes) throws org.xml.sax.SAXException JavaDoc {
89     //System.out.println("AbstractCompositeUnmarshaller "+this.getClass().getName()+" startElement="+name);
90
elementCounter++;
91     if (currentActiveHandler == null) {
92       // we look for an handler able to handle the tag
93
currentActiveHandler = getHandler(name);
94       if (currentActiveHandler == null) {
95         if (lenient) {
96           currentActiveHandler = new NullUnmarshallerHandler();
97         } else {
98           throw new org.xml.sax.SAXException JavaDoc("Cannot find an handler registered for element "+name);
99         }
100       }
101       currentActiveHandler.startContextElement(name, attributes);
102     } else {
103       currentActiveHandler.startElement(name, attributes);
104     }
105   }
106
107  
108   public void endElement(String JavaDoc name) throws org.xml.sax.SAXException JavaDoc {
109     //System.out.println("AbstractCompositeUnmarshaller "+this.getClass().getName()+" endElement="+name+" elementCounter="+elementCounter);
110
checkActiveHandler();
111     elementCounter--;
112     if (elementCounter == 0) {
113       // the element that triggered the currentActiveHandler is closed, we set
114
// the handler to null
115
notifyEndActiveHandler(name, currentActiveHandler);
116       currentActiveHandler = null;
117     } else {
118       currentActiveHandler.endElement(name);
119     }
120   }
121
122   
123   public void readValue(String JavaDoc value) throws org.xml.sax.SAXException JavaDoc {
124     //System.out.println("AbstractCompositeUnmarshaller "+this.getClass().getName()+" readValue="+value);
125
if (currentActiveHandler != null) currentActiveHandler.readValue(value);
126   }
127
128   
129   public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) throws org.xml.sax.SAXException JavaDoc {
130 // System.out.println("prefix "+prefix+" uri "+uri);
131
checkActiveHandler();
132     currentActiveHandler.startPrefixMapping(prefix, uri);
133   }
134
135
136   public void endPrefixMapping(String JavaDoc prefix) throws org.xml.sax.SAXException JavaDoc {
137     checkActiveHandler();
138     currentActiveHandler.endPrefixMapping(prefix);
139   }
140
141
142
143   //
144
// -- PROTECTED METHODS ------------------------------------------------------
145
//
146

147   protected void checkActiveHandler() throws org.xml.sax.SAXException JavaDoc {
148     if (currentActiveHandler == null) throw new org.xml.sax.SAXException JavaDoc("No handler is currently defined");
149   }
150   
151   
152   protected abstract void notifyEndActiveHandler(String JavaDoc name, UnmarshallerHandler activeHandler) throws org.xml.sax.SAXException JavaDoc;
153   
154   
155   protected boolean checkNonEmpty(String JavaDoc s) {
156     return (s != null) && (s.length() > 0);
157   }
158
159   
160   protected UnmarshallerHandler getHandler(String JavaDoc elementName) {
161     Object JavaDoc o = handlersMap.get(elementName);
162     if (o == null) return null;
163     return (UnmarshallerHandler) o;
164   }
165
166
167   //
168
// -- PRIVATE METHODS ------------------------------------------------------
169
//
170

171
172   //
173
// -- INNER CLASSES ------------------------------------------------------
174
//
175

176   private class NullUnmarshallerHandler extends BasicUnmarshaller {
177     public void startElement(String JavaDoc name, Attributes attributes) throws org.xml.sax.SAXException JavaDoc {
178       //System.out.println(name+" ignored");
179
}
180     public void startContextElement(String JavaDoc name, Attributes attributes) throws org.xml.sax.SAXException JavaDoc {
181       //System.out.println(name+" ignored");
182
}
183     public Object JavaDoc getResultObject() throws org.xml.sax.SAXException JavaDoc {
184       return null;
185     }
186   }
187 }
Popular Tags