001: /*
002: * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.soap.wsdl;
030:
031: import com.caucho.log.Log;
032: import com.caucho.util.IntMap;
033: import com.caucho.util.L10N;
034:
035: import org.xml.sax.Attributes;
036: import org.xml.sax.SAXException;
037: import org.xml.sax.helpers.DefaultHandler;
038:
039: import javax.xml.namespace.QName;
040: import java.util.logging.Logger;
041:
042: /**
043: * WSDL Content handler
044: */
045: public class WSDLContentHandler extends DefaultHandler {
046: private final static Logger log = Log
047: .open(WSDLContentHandler.class);
048: private final static L10N L = new L10N(WSDLContentHandler.class);
049:
050: private final static String WSDL = "http://schemas.xmlsoap.org/wsdl/";
051:
052: private final static int TOP = 0;
053: private final static int WSDL_DEFINITIONS = 1;
054: private final static int WSDL_IMPORT = 2;
055: private final static int WSDL_TYPES = 3;
056: private final static int WSDL_MESSAGE = 4;
057: private final static int WSDL_PORT_TYPE = 5;
058: private final static int WSDL_BINDING = 6;
059: private final static int WSDL_SERVICE = 7;
060: private final static int WSDL_PART = 8;
061: private final static int WSDL_OPERATION = 9;
062: private final static int WSDL_INPUT = 10;
063: private final static int WSDL_OUTPUT = 11;
064: private final static int WSDL_FAULT = 12;
065: private final static int WSDL_PORT = 13;
066: private final static int WSDL_BINDING_OPERATION = 14;
067:
068: private final static IntMap _keywords = new IntMap();
069:
070: private int _state = TOP;
071:
072: /**
073: * Starts a WSDL element.
074: */
075: public void startElement(String uri, String localName,
076: String qName, Attributes attributes) throws SAXException {
077: QName qname = new QName(uri, localName);
078:
079: switch (_state) {
080: case TOP:
081: switch (getKeyword(qname)) {
082: case WSDL_DEFINITIONS:
083: _state = WSDL_DEFINITIONS;
084: break;
085:
086: default:
087: throw error(L
088: .l("Expected <wsdl:descriptions> at <{0}>.",
089: qName));
090: }
091: break;
092:
093: case WSDL_DEFINITIONS:
094: switch (getKeyword(qname)) {
095: case WSDL_IMPORT:
096: _state = WSDL_IMPORT;
097: break;
098:
099: case WSDL_TYPES:
100: _state = WSDL_TYPES;
101: break;
102:
103: case WSDL_MESSAGE:
104: _state = WSDL_MESSAGE;
105: break;
106:
107: case WSDL_PORT_TYPE:
108: _state = WSDL_PORT_TYPE;
109: break;
110:
111: case WSDL_BINDING:
112: _state = WSDL_BINDING;
113: break;
114:
115: case WSDL_SERVICE:
116: _state = WSDL_SERVICE;
117: break;
118:
119: default:
120: throw error(L.l("<{0}> is an unexpected tag.", qName));
121: }
122: break;
123:
124: case WSDL_MESSAGE:
125: switch (getKeyword(qname)) {
126: case WSDL_PART:
127: _state = WSDL_PART;
128: break;
129:
130: default:
131: throw error(L.l("<{0}> is an unexpected tag.", qName));
132: }
133: break;
134:
135: case WSDL_PORT_TYPE:
136: switch (getKeyword(qname)) {
137: case WSDL_OPERATION:
138: _state = WSDL_OPERATION;
139: break;
140:
141: default:
142: throw error(L.l("<{0}> is an unexpected tag.", qName));
143: }
144: break;
145:
146: case WSDL_BINDING:
147: switch (getKeyword(qname)) {
148: case WSDL_OPERATION:
149: _state = WSDL_BINDING_OPERATION;
150: break;
151:
152: default:
153: throw error(L.l("<{0}> is an unexpected tag.", qName));
154: }
155: break;
156:
157: case WSDL_SERVICE:
158: switch (getKeyword(qname)) {
159: case WSDL_PORT:
160: _state = WSDL_PORT;
161: break;
162:
163: default:
164: throw error(L.l("<{0}> is an unexpected tag.", qName));
165: }
166: break;
167:
168: default:
169: throw error(L.l("<{0}> is an unexpected tag.", qName));
170: }
171: }
172:
173: /**
174: * Ends a WSDL element.
175: */
176: public void endElement(String uri, String localName, String qName)
177: throws SAXException {
178: QName qname = new QName(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.",
186: qName));
187: break;
188:
189: case WSDL_IMPORT:
190: if (getKeyword(qname) == WSDL_IMPORT)
191: _state = WSDL_DEFINITIONS;
192: else
193: throw error(L.l("</{0}> is an unexpected end tag.",
194: qName));
195: break;
196:
197: case WSDL_TYPES:
198: if (getKeyword(qname) == WSDL_TYPES)
199: _state = WSDL_DEFINITIONS;
200: else
201: throw error(L.l("</{0}> is an unexpected end tag.",
202: qName));
203: break;
204:
205: case WSDL_MESSAGE:
206: if (getKeyword(qname) == WSDL_MESSAGE)
207: _state = WSDL_DEFINITIONS;
208: else
209: throw error(L.l("</{0}> is an unexpected end tag.",
210: qName));
211: break;
212:
213: case WSDL_PORT_TYPE:
214: if (getKeyword(qname) == WSDL_PORT_TYPE)
215: _state = WSDL_DEFINITIONS;
216: else
217: throw error(L.l("</{0}> is an unexpected end tag.",
218: qName));
219: break;
220:
221: case WSDL_BINDING:
222: if (getKeyword(qname) == WSDL_BINDING)
223: _state = WSDL_DEFINITIONS;
224: else
225: throw error(L.l("</{0}> is an unexpected end tag.",
226: qName));
227: break;
228:
229: case WSDL_SERVICE:
230: if (getKeyword(qname) == WSDL_SERVICE)
231: _state = WSDL_DEFINITIONS;
232: else
233: throw error(L.l("</{0}> is an unexpected end tag.",
234: qName));
235: break;
236:
237: case WSDL_PART:
238: if (getKeyword(qname) == WSDL_PART)
239: _state = WSDL_MESSAGE;
240: else
241: throw error(L.l("</{0}> is an unexpected end tag.",
242: qName));
243: break;
244:
245: case WSDL_OPERATION:
246: if (getKeyword(qname) == WSDL_OPERATION)
247: _state = WSDL_PORT_TYPE;
248: else
249: throw error(L.l("</{0}> is an unexpected end tag.",
250: qName));
251: break;
252:
253: case WSDL_INPUT:
254: if (getKeyword(qname) == WSDL_INPUT)
255: _state = WSDL_OPERATION;
256: else
257: throw error(L.l("</{0}> is an unexpected end tag.",
258: qName));
259: break;
260:
261: case WSDL_OUTPUT:
262: if (getKeyword(qname) == WSDL_OUTPUT)
263: _state = WSDL_OPERATION;
264: else
265: throw error(L.l("</{0}> is an unexpected end tag.",
266: qName));
267: break;
268:
269: case WSDL_FAULT:
270: if (getKeyword(qname) == WSDL_FAULT)
271: _state = WSDL_OPERATION;
272: else
273: throw error(L.l("</{0}> is an unexpected end tag.",
274: qName));
275: break;
276:
277: case WSDL_PORT:
278: if (getKeyword(qname) == WSDL_PORT)
279: _state = WSDL_SERVICE;
280: else
281: throw error(L.l("</{0}> is an unexpected end tag.",
282: qName));
283: break;
284:
285: case WSDL_BINDING_OPERATION:
286: if (getKeyword(qname) == WSDL_OPERATION)
287: _state = WSDL_BINDING;
288: else
289: throw error(L.l("</{0}> is an unexpected end tag.",
290: qName));
291: break;
292:
293: default:
294: throw error(L.l("</{0}> is an unexpected end tag.", qName));
295: }
296: }
297:
298: /**
299: * Returns the keyword for the name.
300: */
301: private int getKeyword(QName qname) {
302: return _keywords.get(qname);
303: }
304:
305: /**
306: * Throws an error.
307: */
308: private SAXException error(String msg) {
309: return new SAXException(msg);
310: }
311:
312: static {
313: _keywords
314: .put(new QName(WSDL, "descriptions"), WSDL_DEFINITIONS);
315: _keywords.put(new QName(WSDL, "import"), WSDL_IMPORT);
316: _keywords.put(new QName(WSDL, "types"), WSDL_TYPES);
317: _keywords.put(new QName(WSDL, "message"), WSDL_MESSAGE);
318: _keywords.put(new QName(WSDL, "portType"), WSDL_PORT_TYPE);
319: _keywords.put(new QName(WSDL, "binding"), WSDL_BINDING);
320: _keywords.put(new QName(WSDL, "service"), WSDL_SERVICE);
321: _keywords.put(new QName(WSDL, "part"), WSDL_PART);
322: _keywords.put(new QName(WSDL, "operation"), WSDL_OPERATION);
323: _keywords.put(new QName(WSDL, "input"), WSDL_INPUT);
324: _keywords.put(new QName(WSDL, "output"), WSDL_OUTPUT);
325: _keywords.put(new QName(WSDL, "fault"), WSDL_FAULT);
326: _keywords.put(new QName(WSDL, "port"), WSDL_PORT);
327: }
328: }
|