KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > pm > ino > api4j > ElementDefaultHandler


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.pm.ino.api4j;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 import javax.xml.bind.JAXBException;
24
25 import org.apache.ws.jaxme.pm.ino.InoObject;
26 import org.xml.sax.Attributes JavaDoc;
27 import org.xml.sax.Locator JavaDoc;
28 import org.xml.sax.SAXException JavaDoc;
29
30 import com.softwareag.tamino.db.api.objectModel.sax.TSAXElement;
31 import com.softwareag.tamino.db.api.objectModel.sax.TSAXElementDefaultHandler;
32
33
34 /**
35  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
36  */

37 public class ElementDefaultHandler extends TSAXElementDefaultHandler {
38     static class Data {
39         int level = 0;
40         boolean inDocument = false;
41         boolean iHaveCreatedStartDocument = false;
42         final List JavaDoc result = new ArrayList JavaDoc();
43     }
44     static ThreadLocal JavaDoc data = new ThreadLocal JavaDoc();
45     static void initData() {
46         data.set(new Data());
47     }
48     static void resetData() {
49         data.set(null);
50     }
51
52     private Data getData() {
53         return (Data) data.get();
54     }
55
56     public ElementDefaultHandler() {
57     }
58
59     public TSAXElement getFirstElement() {
60         List JavaDoc result = getData().result;
61         if (result.size() > 0) {
62             return new TJMElement((InoObject) result.get(0));
63         } else {
64             return null;
65         }
66     }
67
68     public Iterator JavaDoc getElementIterator() {
69         return new Iterator JavaDoc(){
70             Iterator JavaDoc inner = getData().result.iterator();
71             public void remove() {
72                 inner.remove();
73             }
74             public boolean hasNext() {
75                 return inner.hasNext();
76             }
77             public Object JavaDoc next() {
78                 return new TJMElement((InoObject) inner.next());
79             }
80         };
81     }
82
83     public void startDocument() throws SAXException JavaDoc {
84         Data d = getData();
85         d.iHaveCreatedStartDocument = false;
86         d.inDocument = true;
87         DocumentDefaultHandler.getUnmarshallerHandler().startDocument();
88     }
89
90     public void endDocument() throws SAXException JavaDoc {
91         Data d = getData();
92         if (d.inDocument) {
93             DocumentDefaultHandler.getUnmarshallerHandler().endDocument();
94             try {
95                 d.result.add(DocumentDefaultHandler.getUnmarshallerHandler().getResult());
96             } catch (JAXBException e) {
97                 throw new SAXException JavaDoc(e);
98             }
99             d.inDocument = false;
100             d.iHaveCreatedStartDocument = false;
101         }
102     }
103
104     public void startElement(String JavaDoc pNamespaceURI, String JavaDoc pLocalName, String JavaDoc pQName,
105                              Attributes JavaDoc pAttr) throws SAXException JavaDoc {
106         Data d = getData();
107         if (d.level++ == 0 && !d.inDocument) {
108             startDocument();
109             d.iHaveCreatedStartDocument = true;
110         }
111         DocumentDefaultHandler.getUnmarshallerHandler().startElement(pNamespaceURI, pLocalName, pQName, pAttr);
112     }
113
114     public void endElement(String JavaDoc pNamespaceURI, String JavaDoc pLocalName, String JavaDoc pQName)
115             throws SAXException JavaDoc {
116         Data d = getData();
117         DocumentDefaultHandler.getUnmarshallerHandler().endElement(pNamespaceURI, pLocalName, pQName);
118         if (--d.level == 0 && d.iHaveCreatedStartDocument) {
119             endDocument();
120         }
121     }
122
123     public void characters(char[] pBuffer, int pOffset, int pLen) throws SAXException JavaDoc {
124         Data d = getData();
125         if (d.inDocument) {
126             DocumentDefaultHandler.getUnmarshallerHandler().characters(pBuffer, pOffset, pLen);
127         }
128     }
129
130     public void ignorableWhitespace(char[] pBuffer, int pOffset, int pLen) throws SAXException JavaDoc {
131         Data d = getData();
132         if (d.inDocument) {
133             DocumentDefaultHandler.getUnmarshallerHandler().ignorableWhitespace(pBuffer, pOffset, pLen);
134         }
135     }
136
137     public void processingInstruction(String JavaDoc pTarget, String JavaDoc pData) throws SAXException JavaDoc {
138         Data d = getData();
139         if (d.inDocument) {
140             DocumentDefaultHandler.getUnmarshallerHandler().processingInstruction(pTarget, pData);
141         }
142     }
143
144     public void skippedEntity(String JavaDoc pEntity) throws SAXException JavaDoc {
145         Data d = getData();
146         if (d.inDocument) {
147             DocumentDefaultHandler.getUnmarshallerHandler().skippedEntity(pEntity);
148         }
149     }
150
151     public void endPrefixMapping(String JavaDoc pPrefix) throws SAXException JavaDoc {
152         Data d = getData();
153         if (d.inDocument) {
154             DocumentDefaultHandler.getUnmarshallerHandler().endPrefixMapping(pPrefix);
155         }
156     }
157
158     public void startPrefixMapping(String JavaDoc pPrefix, String JavaDoc pNamespaceURI) throws SAXException JavaDoc {
159         Data d = getData();
160         if (d.inDocument) {
161             DocumentDefaultHandler.getUnmarshallerHandler().startPrefixMapping(pPrefix, pNamespaceURI);
162         }
163     }
164
165     public void setDocumentLocator(Locator JavaDoc pLocator) {
166         Data d = getData();
167         if (d.inDocument) {
168             DocumentDefaultHandler.getUnmarshallerHandler().setDocumentLocator(pLocator);
169         }
170     }
171 }
172
Popular Tags