KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > xs > parser > impl > AbstractXsSAXParser


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15
16  */

17 package org.apache.ws.jaxme.xs.parser.impl;
18
19 import java.lang.reflect.InvocationTargetException JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21 import java.lang.reflect.UndeclaredThrowableException JavaDoc;
22
23 import org.apache.ws.jaxme.xs.parser.AttributeSetter;
24 import org.apache.ws.jaxme.xs.parser.ChildSetter;
25 import org.apache.ws.jaxme.xs.parser.XSContext;
26 import org.apache.ws.jaxme.xs.parser.XsSAXParser;
27 import org.xml.sax.Attributes JavaDoc;
28 import org.xml.sax.ContentHandler JavaDoc;
29 import org.xml.sax.Locator JavaDoc;
30 import org.xml.sax.SAXException JavaDoc;
31
32
33 /**
34  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
35  */

36 public abstract class AbstractXsSAXParser implements ContentHandler JavaDoc, XsSAXParser {
37   private final static Class JavaDoc[] ZERO_CLASSES = new Class JavaDoc[0];
38   private final static Object JavaDoc[] ZERO_OBJECTS = new Object JavaDoc[0];
39   
40   private Object JavaDoc bean;
41   private int level;
42   private String JavaDoc qName, namespaceURI, localName;
43   private ContentHandler JavaDoc childHandler;
44
45   protected abstract XSContext getData();
46
47   public AbstractXsSAXParser(Object JavaDoc pBean) {
48     bean = pBean;
49   }
50
51   public Object JavaDoc getBean() {
52     return bean;
53   }
54
55   public void setQName(String JavaDoc pQName) {
56     qName = pQName;
57   }
58
59   public void setNamespaceURI(String JavaDoc pNamespaceURI) {
60     namespaceURI = pNamespaceURI;
61   }
62
63   public String JavaDoc getNamespaceURI() {
64     return namespaceURI;
65   }
66
67   public void setLocalName(String JavaDoc pLocalName) {
68     localName = pLocalName;
69   }
70
71   public String JavaDoc getLocalName() {
72     return localName;
73   }
74
75   public String JavaDoc getQName() {
76     return qName;
77   }
78
79   public ContentHandler JavaDoc getChildHandler() {
80     return childHandler;
81   }
82
83   public void startPrefixMapping(String JavaDoc pPrefix, String JavaDoc pURI)
84       throws SAXException JavaDoc {
85     if (childHandler == null) {
86       getData().getNamespaceSupport().declarePrefix(pPrefix, pURI);
87     } else {
88       childHandler.startPrefixMapping(pPrefix, pURI);
89     }
90   }
91
92   public void endPrefixMapping(String JavaDoc pPrefix) throws SAXException JavaDoc {
93     if (childHandler != null) {
94       childHandler.endPrefixMapping(pPrefix);
95     }
96   }
97
98   public void startDocument() throws SAXException JavaDoc {
99     getData().getNamespaceSupport().pushContext();
100   }
101
102   public void endDocument() throws SAXException JavaDoc {
103     getData().getNamespaceSupport().popContext();
104   }
105
106   public void characters(char[] pBuffer, int pOffset, int pLen) throws SAXException JavaDoc {
107     if (childHandler == null) {
108       try {
109         getData().getTextSetter().addText(new String JavaDoc(pBuffer, pOffset, pLen));
110       } catch (SAXException JavaDoc e) {
111         throw e;
112       } catch (RuntimeException JavaDoc e) {
113         Exception JavaDoc ex = e;
114         for (;;) {
115           UndeclaredThrowableException JavaDoc te = null;
116           Throwable JavaDoc t;
117           if (ex instanceof UndeclaredThrowableException JavaDoc) {
118             te = ((UndeclaredThrowableException JavaDoc) ex);
119             t = te.getUndeclaredThrowable();
120           } else if (ex instanceof InvocationTargetException JavaDoc) {
121             t = ((InvocationTargetException JavaDoc) ex).getTargetException();
122           } else {
123             break;
124           }
125           if (t instanceof Exception JavaDoc) {
126             ex = (Exception JavaDoc) t;
127           } else {
128             if (te == null) {
129               te = new UndeclaredThrowableException JavaDoc(t);
130             }
131             t.printStackTrace();
132             throw te;
133           }
134         }
135         throw new LocSAXException(ex.getClass().getName() + ": " + ex.getMessage(),
136                                    getData().getLocator(), ex);
137       }
138     } else {
139       childHandler.characters(pBuffer, pOffset, pLen);
140     }
141   }
142
143   public void ignorableWhitespace(char[] pBuffer, int pOffset, int pLen)
144       throws SAXException JavaDoc {
145     if (childHandler == null) {
146       characters(pBuffer, pOffset, pLen);
147     } else {
148       childHandler.ignorableWhitespace(pBuffer, pOffset, pLen);
149     }
150   }
151
152   public void skippedEntity(String JavaDoc pEntity) throws SAXException JavaDoc {
153     if (childHandler == null) {
154       throw new LocSAXException("Unable to skip entities: " + pEntity,
155                                  getData().getLocator());
156     } else {
157       skippedEntity(pEntity);
158     }
159   }
160
161   public void processingInstruction(String JavaDoc pTarget, String JavaDoc pData)
162       throws SAXException JavaDoc {
163     if (childHandler != null) {
164       childHandler.processingInstruction(pTarget, pData);
165     }
166   }
167
168   public void startElement(String JavaDoc pNamespaceURI, String JavaDoc pLocalName, String JavaDoc pQName, Attributes JavaDoc pAttr)
169       throws SAXException JavaDoc {
170     switch (++level) {
171       case 1:
172         setQName(pQName);
173         setNamespaceURI(pNamespaceURI);
174         setLocalName(pLocalName);
175         if (pAttr != null) {
176           for (int i = 0; i < pAttr.getLength(); i++) {
177             try {
178               AttributeSetter attrSetter = getData().getAttributeSetter();
179               attrSetter.setAttribute(pAttr.getQName(i), pAttr.getURI(i),
180                                       pAttr.getLocalName(i), pAttr.getValue(i));
181             } catch (SAXException JavaDoc e) {
182               throw e;
183             } catch (RuntimeException JavaDoc e) {
184               Exception JavaDoc ex = e;
185               for (;;) {
186                 UndeclaredThrowableException JavaDoc te = null;
187                 Throwable JavaDoc t;
188                 if (ex instanceof UndeclaredThrowableException JavaDoc) {
189                   te = ((UndeclaredThrowableException JavaDoc) ex);
190                   t = te.getUndeclaredThrowable();
191                 } else if (ex instanceof InvocationTargetException JavaDoc) {
192                   t = ((InvocationTargetException JavaDoc) ex).getTargetException();
193                 } else {
194                   break;
195                 }
196                 if (t instanceof Exception JavaDoc) {
197                   ex = (Exception JavaDoc) t;
198                 } else {
199                   if (te == null) {
200                     te = new UndeclaredThrowableException JavaDoc(t);
201                   }
202                   t.printStackTrace();
203                   throw te;
204                 }
205               }
206               throw new LocSAXException(ex.getClass().getName() + ": " + ex.getMessage(),
207                                          getData().getLocator(), ex);
208             }
209           }
210         }
211         break;
212       case 2:
213         try {
214           ChildSetter childSetter = getData().getChildSetter();
215           childHandler = childSetter.getChildHandler(pQName, pNamespaceURI, pLocalName);
216         } catch (SAXException JavaDoc e) {
217           throw e;
218         } catch (RuntimeException JavaDoc e) {
219           Exception JavaDoc ex = e;
220           for (;;) {
221             UndeclaredThrowableException JavaDoc te = null;
222             Throwable JavaDoc t;
223             if (ex instanceof UndeclaredThrowableException JavaDoc) {
224               te = ((UndeclaredThrowableException JavaDoc) ex);
225               t = te.getUndeclaredThrowable();
226             } else if (ex instanceof InvocationTargetException JavaDoc) {
227               t = ((InvocationTargetException JavaDoc) ex).getTargetException();
228             } else {
229               break;
230             }
231             if (t instanceof Exception JavaDoc) {
232               ex = (Exception JavaDoc) t;
233             } else {
234               if (te == null) {
235                 te = new UndeclaredThrowableException JavaDoc(t);
236               }
237               t.printStackTrace();
238               throw te;
239             }
240           }
241           throw new LocSAXException(ex.getClass().getName() + ": " + ex.getMessage(),
242                                      getData().getLocator(), ex);
243         }
244         getData().setCurrentContentHandler(childHandler);
245         childHandler.startDocument();
246         childHandler.startElement(pNamespaceURI, pLocalName, pQName, pAttr);
247         break;
248       default:
249         childHandler.startElement(pNamespaceURI, pLocalName, pQName, pAttr);
250         break;
251     }
252   }
253
254   public void endElement(String JavaDoc pNamespaceURI, String JavaDoc pLocalName, String JavaDoc pQName)
255       throws SAXException JavaDoc {
256     switch (level--) {
257       case 1:
258         Object JavaDoc o = getBean();
259         if (o != null) {
260           Method JavaDoc m = null;
261           try {
262             m = o.getClass().getMethod("validate", ZERO_CLASSES);
263           } catch (NoSuchMethodException JavaDoc e) {
264           }
265           if (m != null) {
266             try {
267               m.invoke(o, ZERO_OBJECTS);
268             } catch (RuntimeException JavaDoc e) {
269               throw new LocSAXException(e.getClass().getName() + ": " + e.getMessage(),
270                                          getData().getLocator(), e);
271             } catch (InvocationTargetException JavaDoc e) {
272               Throwable JavaDoc t = e.getTargetException();
273               if (t instanceof SAXException JavaDoc) {
274                 throw (SAXException JavaDoc) t;
275               } else if (t instanceof RuntimeException JavaDoc) {
276                 throw new LocSAXException(t.getClass().getName() + ": " + t.getMessage(),
277                                            getData().getLocator(),
278                                            (RuntimeException JavaDoc) t);
279               } else if (t instanceof Exception JavaDoc) {
280                 throw new LocSAXException("Failed to invoke method validate() " +
281                                            " of class " + m.getDeclaringClass() +
282                                            " with argument " + o.getClass().getName() + ": " +
283                                            t.getClass().getName() + ", " + t.getMessage(),
284                                            getData().getLocator(),
285                                            (Exception JavaDoc) t);
286               } else {
287                 throw new LocSAXException("Failed to invoke method validate() " +
288                                            " of class " + m.getDeclaringClass() +
289                                            " with argument " + o.getClass().getName() + ": " +
290                                            t.getClass().getName() + ", " + t.getMessage(),
291                                            getData().getLocator(), e);
292               }
293             } catch (IllegalAccessException JavaDoc e) {
294               throw new LocSAXException("Failed to invoke method validate() " +
295                                            " of class " + m.getDeclaringClass() +
296                                            " with argument " + o.getClass().getName() + ": IllegalAccessException, " +
297                                            e.getMessage(),
298                                            getData().getLocator(), e);
299
300             }
301           }
302         }
303         break;
304       case 2:
305         childHandler.endElement(pNamespaceURI, pLocalName, pQName);
306         childHandler.endDocument();
307         getData().setCurrentContentHandler(this);
308         childHandler = null;
309         break;
310       default:
311         childHandler.endElement(pNamespaceURI, pLocalName, pQName);
312         break;
313     }
314   }
315
316   public void setDocumentLocator(Locator JavaDoc pLocator) {
317     getData().setLocator(pLocator);
318     if (childHandler != null) {
319       childHandler.setDocumentLocator(pLocator);
320     }
321   }
322 }
323
Popular Tags