KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > soap > wsdl > WSDLContentHandler


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.soap.wsdl;
30
31 import com.caucho.log.Log;
32 import com.caucho.util.IntMap;
33 import com.caucho.util.L10N;
34
35 import org.xml.sax.Attributes JavaDoc;
36 import org.xml.sax.SAXException JavaDoc;
37 import org.xml.sax.helpers.DefaultHandler JavaDoc;
38
39 import javax.xml.namespace.QName JavaDoc;
40 import java.util.logging.Logger JavaDoc;
41
42 /**
43  * WSDL Content handler
44  */

45 public class WSDLContentHandler extends DefaultHandler JavaDoc {
46   private final static Logger JavaDoc log = Log.open(WSDLContentHandler.class);
47   private final static L10N L = new L10N(WSDLContentHandler.class);
48
49   private final static String JavaDoc WSDL = "http://schemas.xmlsoap.org/wsdl/";
50
51   private final static int TOP = 0;
52   private final static int WSDL_DEFINITIONS = 1;
53   private final static int WSDL_IMPORT = 2;
54   private final static int WSDL_TYPES = 3;
55   private final static int WSDL_MESSAGE = 4;
56   private final static int WSDL_PORT_TYPE = 5;
57   private final static int WSDL_BINDING = 6;
58   private final static int WSDL_SERVICE = 7;
59   private final static int WSDL_PART = 8;
60   private final static int WSDL_OPERATION = 9;
61   private final static int WSDL_INPUT = 10;
62   private final static int WSDL_OUTPUT = 11;
63   private final static int WSDL_FAULT = 12;
64   private final static int WSDL_PORT = 13;
65   private final static int WSDL_BINDING_OPERATION = 14;
66
67   private final static IntMap _keywords = new IntMap();
68
69   private int _state = TOP;
70
71   /**
72    * Starts a WSDL element.
73    */

74   public void startElement (String JavaDoc uri, String JavaDoc localName,
75                             String JavaDoc qName, Attributes JavaDoc attributes)
76     throws SAXException JavaDoc
77   {
78     QName JavaDoc qname = new QName JavaDoc(uri, localName);
79
80     switch (_state) {
81     case TOP:
82       switch (getKeyword(qname)) {
83       case WSDL_DEFINITIONS:
84         _state = WSDL_DEFINITIONS;
85         break;
86
87       default:
88         throw error(L.l("Expected <wsdl:descriptions> at <{0}>.", qName));
89       }
90       break;
91       
92     case WSDL_DEFINITIONS:
93       switch (getKeyword(qname)) {
94       case WSDL_IMPORT:
95         _state = WSDL_IMPORT;
96         break;
97
98       case WSDL_TYPES:
99         _state = WSDL_TYPES;
100         break;
101
102       case WSDL_MESSAGE:
103         _state = WSDL_MESSAGE;
104         break;
105
106       case WSDL_PORT_TYPE:
107         _state = WSDL_PORT_TYPE;
108         break;
109
110       case WSDL_BINDING:
111         _state = WSDL_BINDING;
112         break;
113
114       case WSDL_SERVICE:
115         _state = WSDL_SERVICE;
116         break;
117
118       default:
119         throw error(L.l("<{0}> is an unexpected tag.", qName));
120       }
121       break;
122       
123     case WSDL_MESSAGE:
124       switch (getKeyword(qname)) {
125       case WSDL_PART:
126         _state = WSDL_PART;
127         break;
128         
129       default:
130         throw error(L.l("<{0}> is an unexpected tag.", qName));
131       }
132       break;
133       
134     case WSDL_PORT_TYPE:
135       switch (getKeyword(qname)) {
136       case WSDL_OPERATION:
137         _state = WSDL_OPERATION;
138         break;
139         
140       default:
141         throw error(L.l("<{0}> is an unexpected tag.", qName));
142       }
143       break;
144       
145     case WSDL_BINDING:
146       switch (getKeyword(qname)) {
147       case WSDL_OPERATION:
148         _state = WSDL_BINDING_OPERATION;
149         break;
150         
151       default:
152         throw error(L.l("<{0}> is an unexpected tag.", qName));
153       }
154       break;
155       
156     case WSDL_SERVICE:
157       switch (getKeyword(qname)) {
158       case WSDL_PORT:
159         _state = WSDL_PORT;
160         break;
161         
162       default:
163         throw error(L.l("<{0}> is an unexpected tag.", qName));
164       }
165       break;
166
167     default:
168       throw error(L.l("<{0}> is an unexpected tag.", qName));
169     }
170   }
171   
172   /**
173    * Ends a WSDL element.
174    */

175   public void endElement (String JavaDoc uri, String JavaDoc localName, String JavaDoc qName)
176     throws SAXException JavaDoc
177   {
178     QName JavaDoc qname = new QName JavaDoc(uri, localName);
179
180     switch (_state) {
181     case WSDL_DEFINITIONS:
182       if (getKeyword(qname) == WSDL_DEFINITIONS)
183         _state = TOP;
184       else
185         throw error(L.l("</{0}> is an unexpected end tag.", qName));
186       break;
187       
188     case WSDL_IMPORT:
189       if (getKeyword(qname) == WSDL_IMPORT)
190         _state = WSDL_DEFINITIONS;
191       else
192         throw error(L.l("</{0}> is an unexpected end tag.", qName));
193       break;
194       
195     case WSDL_TYPES:
196       if (getKeyword(qname) == WSDL_TYPES)
197         _state = WSDL_DEFINITIONS;
198       else
199         throw error(L.l("</{0}> is an unexpected end tag.", qName));
200       break;
201       
202     case WSDL_MESSAGE:
203       if (getKeyword(qname) == WSDL_MESSAGE)
204         _state = WSDL_DEFINITIONS;
205       else
206         throw error(L.l("</{0}> is an unexpected end tag.", qName));
207       break;
208       
209     case WSDL_PORT_TYPE:
210       if (getKeyword(qname) == WSDL_PORT_TYPE)
211         _state = WSDL_DEFINITIONS;
212       else
213         throw error(L.l("</{0}> is an unexpected end tag.", qName));
214       break;
215       
216     case WSDL_BINDING:
217       if (getKeyword(qname) == WSDL_BINDING)
218         _state = WSDL_DEFINITIONS;
219       else
220         throw error(L.l("</{0}> is an unexpected end tag.", qName));
221       break;
222       
223     case WSDL_SERVICE:
224       if (getKeyword(qname) == WSDL_SERVICE)
225         _state = WSDL_DEFINITIONS;
226       else
227         throw error(L.l("</{0}> is an unexpected end tag.", qName));
228       break;
229       
230     case WSDL_PART:
231       if (getKeyword(qname) == WSDL_PART)
232         _state = WSDL_MESSAGE;
233       else
234         throw error(L.l("</{0}> is an unexpected end tag.", qName));
235       break;
236       
237     case WSDL_OPERATION:
238       if (getKeyword(qname) == WSDL_OPERATION)
239         _state = WSDL_PORT_TYPE;
240       else
241         throw error(L.l("</{0}> is an unexpected end tag.", qName));
242       break;
243       
244     case WSDL_INPUT:
245       if (getKeyword(qname) == WSDL_INPUT)
246         _state = WSDL_OPERATION;
247       else
248         throw error(L.l("</{0}> is an unexpected end tag.", qName));
249       break;
250       
251     case WSDL_OUTPUT:
252       if (getKeyword(qname) == WSDL_OUTPUT)
253         _state = WSDL_OPERATION;
254       else
255         throw error(L.l("</{0}> is an unexpected end tag.", qName));
256       break;
257       
258     case WSDL_FAULT:
259       if (getKeyword(qname) == WSDL_FAULT)
260         _state = WSDL_OPERATION;
261       else
262         throw error(L.l("</{0}> is an unexpected end tag.", qName));
263       break;
264       
265     case WSDL_PORT:
266       if (getKeyword(qname) == WSDL_PORT)
267         _state = WSDL_SERVICE;
268       else
269         throw error(L.l("</{0}> is an unexpected end tag.", qName));
270       break;
271       
272     case WSDL_BINDING_OPERATION:
273       if (getKeyword(qname) == WSDL_OPERATION)
274         _state = WSDL_BINDING;
275       else
276         throw error(L.l("</{0}> is an unexpected end tag.", qName));
277       break;
278
279     default:
280       throw error(L.l("</{0}> is an unexpected end tag.", qName));
281     }
282   }
283
284   /**
285    * Returns the keyword for the name.
286    */

287   private int getKeyword(QName JavaDoc qname)
288   {
289     return _keywords.get(qname);
290   }
291
292   /**
293    * Throws an error.
294    */

295   private SAXException JavaDoc error(String JavaDoc msg)
296   {
297     return new SAXException JavaDoc(msg);
298   }
299
300   static {
301     _keywords.put(new QName JavaDoc(WSDL, "descriptions"), WSDL_DEFINITIONS);
302     _keywords.put(new QName JavaDoc(WSDL, "import"), WSDL_IMPORT);
303     _keywords.put(new QName JavaDoc(WSDL, "types"), WSDL_TYPES);
304     _keywords.put(new QName JavaDoc(WSDL, "message"), WSDL_MESSAGE);
305     _keywords.put(new QName JavaDoc(WSDL, "portType"), WSDL_PORT_TYPE);
306     _keywords.put(new QName JavaDoc(WSDL, "binding"), WSDL_BINDING);
307     _keywords.put(new QName JavaDoc(WSDL, "service"), WSDL_SERVICE);
308     _keywords.put(new QName JavaDoc(WSDL, "part"), WSDL_PART);
309     _keywords.put(new QName JavaDoc(WSDL, "operation"), WSDL_OPERATION);
310     _keywords.put(new QName JavaDoc(WSDL, "input"), WSDL_INPUT);
311     _keywords.put(new QName JavaDoc(WSDL, "output"), WSDL_OUTPUT);
312     _keywords.put(new QName JavaDoc(WSDL, "fault"), WSDL_FAULT);
313     _keywords.put(new QName JavaDoc(WSDL, "port"), WSDL_PORT);
314   }
315 }
316
Popular Tags