KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > pm > ino > InoResponseHandler


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;
18
19 import org.xml.sax.Attributes JavaDoc;
20 import org.xml.sax.ContentHandler JavaDoc;
21 import org.xml.sax.Locator JavaDoc;
22 import org.xml.sax.SAXException JavaDoc;
23
24
25 /** <p>This is a SAX content handler for an ino:response document.</p>
26  *
27  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
28  */

29 public class InoResponseHandler implements ContentHandler JavaDoc {
30   /** <p>The namespace of an INO response document:
31    * <samp>http://namespaces.softwareag.com/tamino/response2</samp>.</p>
32    */

33   public static final String JavaDoc INO_RESPONSE2_URI =
34     "http://namespaces.softwareag.com/tamino/response2";
35   /** <p>The namespace of the XQL section in an INO response
36    * document: <samp>http://metalab.unc.edu/xql/</samp>.</p>
37    */

38   public static final String JavaDoc XQL_URI = "http://metalab.unc.edu/xql/";
39
40
41   private boolean inInoMessage = false;
42   private boolean inInoMessageText;
43   private boolean inXqlResult;
44   private int level;
45   private String JavaDoc inoErrorCode;
46   private StringBuffer JavaDoc inoErrorMessage;
47   private Locator JavaDoc locator;
48   private ContentHandler JavaDoc resultHandler;
49
50   /** Creates a new InoResponseHandler */
51   public InoResponseHandler() {}
52
53   public void setDocumentLocator(Locator JavaDoc l) {
54     locator = l;
55   }
56
57   /** Returns the document {@link Locator}, that was
58    * previously set by the XML parser. May be null, if
59    * the parser didn't supply one.
60    */

61   public Locator JavaDoc getDocumentLocator() {
62     return locator;
63   }
64
65   public void startDocument() throws org.xml.sax.SAXException JavaDoc {
66     inInoMessage = false;
67     inInoMessageText = false;
68     inXqlResult = false;
69     level = 0;
70     if (inoObjectIdList != null) {
71       inoObjectIdList.clear();
72     }
73   }
74
75   public void endDocument() throws org.xml.sax.SAXException JavaDoc {
76   }
77   
78   public void startElement(String JavaDoc namespaceUri, String JavaDoc localName,
79                            String JavaDoc qName, Attributes JavaDoc attr) throws SAXException JavaDoc {
80     if (inXqlResult) {
81       if (resultHandler != null) {
82         if (level == 2) {
83           resultHandler.startDocument();
84         }
85         resultHandler.startElement(namespaceUri, localName, qName, attr);
86       }
87     } else if (inInoMessage) {
88       if (level == 2) {
89         if (INO_RESPONSE2_URI.equals(namespaceUri) &&
90             "messagetext".equals(localName)) {
91           String JavaDoc c = attr.getValue(INO_RESPONSE2_URI, "code");
92           if (c != null) {
93             inoErrorCode = c;
94           }
95           inInoMessageText = true;
96         }
97       }
98     } else if (level == 1) {
99       if (XQL_URI.equals(namespaceUri) && "result".equals(localName)) {
100         inXqlResult = true;
101       } else if (INO_RESPONSE2_URI.equals(namespaceUri)) {
102         if ("message".equals(localName)) {
103           String JavaDoc retval = attr.getValue(INO_RESPONSE2_URI, "returnvalue");
104           if (retval == null || !retval.equals("0")) {
105             inoErrorCode = retval;
106             inoErrorMessage = new StringBuffer JavaDoc();
107             inInoMessage = true;
108           }
109         } else if (inoObjectIdList != null && "object".equals(localName)) {
110           inoObjectIdList.add(attr.getValue(INO_RESPONSE2_URI, "id"));
111         }
112       }
113     }
114     ++level;
115   }
116
117   public void endElement(String JavaDoc namespaceUri, String JavaDoc localName,
118                          String JavaDoc qName) throws SAXException JavaDoc {
119     level--;
120     if (inXqlResult) {
121       if (level == 1) {
122         inXqlResult = false;
123       } else {
124         if (resultHandler != null) {
125           resultHandler.endElement(namespaceUri, localName, qName);
126           if (level == 2) {
127             resultHandler.endDocument();
128           }
129         }
130       }
131     } else if (inInoMessage) {
132       if (level == 1) {
133         if (inoErrorCode == null) {
134           inoErrorCode = "INOUNKNOWN";
135         }
136         throw new InoException(inoErrorCode, inoErrorMessage.toString());
137       } else if (level == 2) {
138         if (inInoMessageText) {
139           inInoMessageText = false;
140         }
141       }
142     }
143   }
144
145   public void startPrefixMapping(String JavaDoc namespaceUri,
146                                  String JavaDoc prefix) throws SAXException JavaDoc {
147     if (inXqlResult) {
148       if (resultHandler != null) {
149         resultHandler.startPrefixMapping(namespaceUri, prefix);
150       }
151     }
152   }
153   
154   public void endPrefixMapping(String JavaDoc prefix) throws SAXException JavaDoc {
155     if (inXqlResult) {
156       if (resultHandler != null) {
157         resultHandler.endPrefixMapping(prefix);
158       }
159     }
160   }
161   
162   public void ignorableWhitespace(char[] ch, int start, int len) throws SAXException JavaDoc {
163     if (inXqlResult) {
164       if (resultHandler != null) {
165         resultHandler.ignorableWhitespace(ch, start, len);
166       }
167     }
168   }
169   
170   public void skippedEntity(String JavaDoc entity) throws SAXException JavaDoc {
171     if (inXqlResult) {
172       if (resultHandler != null) {
173         resultHandler.skippedEntity(entity);
174       }
175     }
176   }
177   
178   public void processingInstruction(String JavaDoc target, String JavaDoc data) throws SAXException JavaDoc {
179     if (inXqlResult) {
180       if (resultHandler != null) {
181         resultHandler.processingInstruction(target, data);
182       }
183     }
184   }
185   
186   public void characters(char[] ch, int start, int len) throws SAXException JavaDoc {
187     if (inXqlResult) {
188       if (resultHandler != null) {
189         resultHandler.characters(ch, start, len);
190       }
191     } else if (inInoMessageText) {
192       inoErrorMessage.append(ch, start, len);
193     }
194   }
195
196   /** <p>Sets the result handler. The result handler is another SAX ContentHandler.
197    * For any result document the InoResponseHandler finds, that is, for any
198    * subelement of xql:result, a stream of SAX events is generated for the
199    * result handler.</p>
200    * <p>If the response document contains more than one result object, then the
201    * result handler must be "restartable". In other words, it must be able to
202    * process multiple startDocument ... endDocument startDocument ...
203    * endDocument sequences.</p>
204    *
205    * @param handler The result handler to use or null to disable SAX events
206    * @see #getResultHandler
207    */

208   public void setResultHandler(ContentHandler JavaDoc handler) {
209     resultHandler = handler;
210   }
211
212   /** <p>Returns a result handler, that was previously set with setResultHandler,
213    * or null.</p>
214    * <p>The result handler is another SAX ContentHandler.
215    * For any result document the InoResponseHandler finds, that is, for any
216    * subelement of xql:result, a stream of SAX events is generated for the
217    * result handler.</p>
218    * <p>If the response document contains more than one result object, then the
219    * result handler must be "restartable". In other words, it must be able to
220    * process multiple startDocument ... endDocument startDocument ...
221    * endDocument sequences.</p>
222    *
223    * @return The result handler or null, if generating SAX events is disabled.
224    */

225   public ContentHandler JavaDoc getResultHandler() {
226     return resultHandler;
227   }
228
229   private java.util.List JavaDoc inoObjectIdList;
230   /** <p>The Tamino response document contains object ID's of inserted
231    * or updated objects. If you use this method, then the ID's are
232    * collected in the given List. A null value disables ID
233    * collection. The list will be cleared within <code>startDocument</code>,
234    * so it's safe to reuse the list over multiple uses of the handler.</p>
235    * <p>More precise, the list will contain all occurences of
236    * ino:response/ino:object/@ino:id.</p>
237    *
238    * @param pList A list where ID's are being collected or null to disable
239    * ID collection.
240    * @see #getInoObjectIdList
241    */

242   public void setInoObjectIdList(java.util.List JavaDoc pList) {
243     inoObjectIdList = pList;
244   }
245   /** <p>Returns the current list for collection of generated ino:id's.</p>
246    *
247    * @see #setInoObjectIdList
248    */

249   public java.util.List JavaDoc getInoObjectIdList() { return inoObjectIdList; }
250 }
251
Popular Tags