001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.websvc.rest.component.palette;
043:
044: import java.io.IOException;
045: import java.util.ArrayList;
046: import java.util.Collections;
047: import java.util.List;
048: import javax.xml.parsers.*;
049: import javax.xml.xpath.XPathExpressionException;
050: import org.netbeans.modules.websvc.rest.RestUtils;
051: import org.openide.filesystems.*;
052: import org.openide.util.Exceptions;
053: import org.w3c.dom.Attr;
054: import org.w3c.dom.Document;
055: import org.w3c.dom.NamedNodeMap;
056: import org.w3c.dom.Node;
057: import org.w3c.dom.NodeList;
058:
059: /**
060: *
061: * @author Owner
062: */
063: public class RestComponentData {
064:
065: public static final String WSDL = "WSDL";
066: public static final String WSDL_NAMESPACE = "http://schemas.xmlsoap.org/wsdl/";
067: public static final String WADL = "WADL";
068: public static final String WADL_NAMESPACE = "http://research.sun.com/wadl/2006/10";
069: public static final String CUSTOM = "custom";
070: public static final String CLASS_NAME = "org.netbeans.modules.websvc.rest.component.palette.RestComponentHandler";
071: public static final String ICON16 = "org/netbeans/modules/websvc/rest/resources/RESTServiceIcon.png";
072: public static final String ICON32 = "org/netbeans/modules/websvc/rest/resources/RESTServiceIcon.png";
073: public static final String BUNDLE_NAME = "org.netbeans.modules.websvc.rest.component.palette.Bundle";
074: private Document doc;
075: private String name;
076: private String displayName;
077: private String categoryName;
078: private String categoryPath;
079: private String description;
080: private String className = CLASS_NAME;
081: private List<Icon> iconList;
082: private Service service;
083:
084: public RestComponentData(Document doc) {
085: this .doc = doc;
086: this .iconList = new ArrayList<Icon>();
087: try {
088: init(doc);
089: } catch (XPathExpressionException ex) {
090: Exceptions.printStackTrace(ex);
091: } catch (IOException ex) {
092: Exceptions.printStackTrace(ex);
093: }
094: }
095:
096: /*
097: *
098: * return component name
099: */
100: public String getName() {
101: return name;
102: }
103:
104: /*
105: *
106: * return localized name from bundle, else name if not found in bundle
107: */
108: public String getDisplayName() {
109: return displayName;
110: }
111:
112: /*
113: *
114: * return component path (path of components parent)
115: */
116: public String getCategoryPath() {
117: return categoryPath;
118: }
119:
120: /*
121: *
122: * return localized name from bundle, null if not found in bundle
123: */
124: public String getCategoryName() {
125: return categoryName;
126: }
127:
128: /*
129: *
130: * return description from bundle when <description bundle="" name=""/> is used, else
131: return text node value of description. If text node not found, then
132: * return displayname of component
133: */
134: public String getDescription() {
135: return description;
136: }
137:
138: /*
139: *
140: * return category name from bundle, null if not found in bundle
141: */
142: public String getClassName() {
143: return className;
144: }
145:
146: /*
147: *
148: * return size 16 icon default to ICON16
149: */
150: public String getIcon16() {
151: for (Icon icon : iconList) {
152: if (icon.getSize().equals("16")) {
153: return icon.getUrl();
154: }
155: }
156: return ICON16;
157: }
158:
159: /*
160: *
161: * return size 32 icon default to ICON32
162: */
163: public String getIcon32() {
164: for (Icon icon : iconList) {
165: if (icon.getSize().equals("32")) {
166: return icon.getUrl();
167: }
168: }
169: return ICON32;
170: }
171:
172: /*
173: *
174: * return all available icons for use by client code.
175: */
176: public List<Icon> getIcons() {
177: return iconList;
178: }
179:
180: private void addIcon(Icon ic) {
181: iconList.add(ic);
182: }
183:
184: /*
185: *
186: * return Service data for the component
187: */
188: public Service getService() {
189: return service;
190: }
191:
192: public static boolean isWSDL(String type) {
193: return type.equalsIgnoreCase(WSDL)
194: || type.contains(WSDL_NAMESPACE);
195: }
196:
197: public static boolean isWADL(String type) {
198: return type.equalsIgnoreCase(WADL)
199: || type.contains(WADL_NAMESPACE);
200: }
201:
202: public static boolean isCustom(String type) {
203: return type.equalsIgnoreCase(CUSTOM);
204: }
205:
206: public void init(Document doc) throws XPathExpressionException,
207: IOException {
208:
209: name = RestUtils.getAttributeValue(doc, "//component", "name");
210: categoryPath = RestUtils.getAttributeValue(doc, "//component",
211: "category");
212: if (name == null || categoryPath == null) {
213: return;
214: }
215: String nameKey = RestUtils.getAttributeValue(doc,
216: "//component", "nameKey");
217: String categoryKey = RestUtils.getAttributeValue(doc,
218: "//component", "categoryKey");
219: String bundle = RestUtils.getAttributeValue(doc, "//component",
220: "bundle");
221: displayName = RestPaletteUtils.getLocalizedString(bundle,
222: nameKey, name);
223: categoryName = RestPaletteUtils.getLocalizedString(bundle,
224: categoryKey,
225: categoryPath.startsWith("/") ? categoryPath
226: .substring(1) : categoryPath);
227:
228: NodeList descNodes = RestUtils.getNodeList(doc,
229: "//component/description");
230: if (descNodes != null && descNodes.getLength() > 0) {
231: Node descNode = descNodes.item(0);
232: if (descNode.getAttributes() != null
233: && descNode.getAttributes().getNamedItem("key") != null
234: && descNode.getAttributes().getNamedItem("bundle") != null) {
235: description = RestPaletteUtils.getLocalizedString(
236: descNode.getAttributes().getNamedItem("bundle")
237: .getNodeValue(), descNode
238: .getAttributes().getNamedItem("key")
239: .getNodeValue(), displayName);
240: }
241: }
242:
243: //Icons
244: NodeList icons = RestUtils.getNodeList(doc,
245: "//component/icons/icon");
246: if (icons != null && icons.getLength() > 0) {
247: for (int i = 0; i < icons.getLength(); i++) {
248: Node icon = icons.item(i);
249: String iconUrl = null;
250: String iconSize = "16";
251: Attr urlAttr = (Attr) icon.getAttributes()
252: .getNamedItem("url");
253: if (urlAttr != null) {
254: iconUrl = urlAttr.getNodeValue();
255: }
256: Attr sizeAttr = (Attr) icon.getAttributes()
257: .getNamedItem("size");
258: if (sizeAttr != null) {
259: iconSize = sizeAttr.getNodeValue();
260: }
261: if (iconUrl != null) {
262: addIcon(new Icon(iconUrl, iconSize));
263: }
264: }
265: }
266:
267: //Service
268: NodeList serviceNodes = RestUtils.getNodeList(doc,
269: "//component/service");
270: if (serviceNodes != null && serviceNodes.getLength() > 0) {
271: service = new Service(RestUtils.getAttributeValue(doc,
272: "//component/service", "name"));
273:
274: //Methods
275: List<Method> methodList = new ArrayList<Method>();
276: NodeList methods = RestUtils.getNodeList(doc,
277: "//component/service/method");
278: if (methods != null && methods.getLength() > 0) {
279: for (int i = 0; i < methods.getLength(); i++) {
280: Node method = methods.item(i);
281: NamedNodeMap attrList = method.getAttributes();
282: String methodName = null;
283: String serviceName = null;
284: String portName = null;
285: String type = null;
286: String typeUrl = null;
287: String url = null;
288: Attr nameAttr = (Attr) attrList
289: .getNamedItem("name");
290: if (nameAttr != null) {
291: methodName = nameAttr.getNodeValue();
292: }
293: Attr serviceNameAttr = (Attr) attrList
294: .getNamedItem("serviceName");
295: if (serviceNameAttr != null) {
296: serviceName = serviceNameAttr.getNodeValue();
297: }
298: Attr portNameAttr = (Attr) attrList
299: .getNamedItem("portName");
300: if (portNameAttr != null) {
301: portName = portNameAttr.getNodeValue();
302: }
303: Attr urlAttr = (Attr) attrList.getNamedItem("url");
304: if (urlAttr != null) {
305: url = urlAttr.getNodeValue();
306: }
307: Attr typeAttr = (Attr) attrList
308: .getNamedItem("type");
309: if (typeAttr != null) {
310: type = typeAttr.getNodeValue();
311: }
312: List<Parameter> inputParams = getInputParams(method);
313: String mediaType = getMediaType(method);
314:
315: if (methodName != null) {
316: service.addMethod(new Method(methodName,
317: serviceName, portName, type, url,
318: inputParams, mediaType));
319: }
320: }
321: }
322: } else {
323: NodeList classNodes = RestUtils.getNodeList(doc,
324: "//component/classes/class");
325: if (classNodes != null && classNodes.getLength() > 0) {
326: className = RestUtils.getAttributeValue(doc,
327: "//component/classes/class", "name");
328: }
329: }
330: }
331:
332: private List<Parameter> getInputParams(Node methodNode) {
333: List<Parameter> inputParams = new ArrayList<Parameter>();
334: Node inputNode = getChildNode(methodNode, "input"); //NOI18N
335: if (inputNode != null) {
336: Node paramsNode = getChildNode(inputNode, "params");
337:
338: if (paramsNode != null) {
339: NodeList children = paramsNode.getChildNodes();
340: int size = children.getLength();
341:
342: for (int i = 0; i < size; i++) {
343: Node paramNode = children.item(i);
344:
345: if (paramNode.getNodeName().equals("param")) {
346: NamedNodeMap attributes = paramNode
347: .getAttributes();
348: String name = attributes.getNamedItem("name")
349: .getNodeValue();
350: String type = attributes.getNamedItem("type")
351: .getNodeValue();
352: String defaultValue = null;
353:
354: Attr defaultAttr = (Attr) attributes
355: .getNamedItem("default");
356:
357: if (defaultAttr != null) {
358: defaultValue = defaultAttr.getNodeValue();
359: }
360:
361: inputParams.add(new Parameter(name, type,
362: defaultValue));
363: }
364: }
365: }
366: }
367:
368: return inputParams;
369: }
370:
371: private String getMediaType(Node methodNode) {
372: String mediaType = "application/xml";
373: Node outputNode = getChildNode(methodNode, "output"); //NOI18N
374: if (outputNode != null) {
375: Node mediaNode = getChildNode(outputNode, "media"); //NOI18N
376: if (mediaNode != null) {
377: NamedNodeMap attributes = mediaNode.getAttributes();
378: mediaType = attributes.getNamedItem("type")
379: .getNodeValue();
380: }
381: }
382:
383: return mediaType;
384: }
385:
386: private Node getChildNode(Node node, String name) {
387: NodeList children = node.getChildNodes();
388: int size = children.getLength();
389:
390: for (int i = 0; i < size; i++) {
391: Node child = children.item(i);
392:
393: if (child.getNodeName().equals(name)) {
394: return child;
395: }
396: }
397:
398: return null;
399: }
400:
401: public class Icon {
402:
403: String url;
404: String size;
405:
406: public Icon(String url, String size) {
407: this .url = url;
408: this .size = size;
409: }
410:
411: public String getUrl() {
412: return url;
413: }
414:
415: public String getSize() {
416: return size;
417: }
418: }
419:
420: public class Service {
421:
422: private String name;
423: private List<Method> methodList = Collections.emptyList();
424:
425: public Service(String name) {
426: this .name = name;
427: this .methodList = new ArrayList<Method>();
428: }
429:
430: public String getName() {
431: return name;
432: }
433:
434: public List<Method> getMethods() {
435: return methodList;
436: }
437:
438: private void addMethod(Method m) {
439: methodList.add(m);
440: }
441: }
442:
443: public class Method {
444:
445: private String name;
446: private String serviceName;
447: private String portName;
448: /*'type' defines the type of service WSDL, WADL etc.,
449: For WSDL it is WSDL or 'http://schemas.xmlsoap.org/wsdl/' or
450: WSDL:[http://schemas.xmlsoap.org/wsdl/],
451: for WADL it is WADL or 'http://research.sun.com/wadl/2006/10' or
452: WADL:[http://research.sun.com/wadl/2006/10]
453: */
454: private String type;
455: //URL of document (In case of WSDL it is the url of WSDL document)
456: private String url;
457: private List<Parameter> inputParams;
458: private String mediaType;
459:
460: public Method(String name, String serviceName, String portName,
461: String type, String url, List<Parameter> inputParams,
462: String mediaType) {
463: this .name = name;
464: this .serviceName = serviceName;
465: this .portName = portName;
466: this .type = type;
467: this .url = url;
468: this .inputParams = inputParams;
469: this .mediaType = mediaType;
470: }
471:
472: public String getName() {
473: return name;
474: }
475:
476: public String getServiceName() {
477: return serviceName;
478: }
479:
480: public String getPortName() {
481: return portName;
482: }
483:
484: public String getType() {
485: return type;
486: }
487:
488: public String getUrl() {
489: return url;
490: }
491:
492: public List<Parameter> getInputParams() {
493: return inputParams;
494: }
495:
496: public String getMediaType() {
497: return mediaType;
498: }
499: }
500:
501: public class Parameter {
502:
503: private String name;
504: private String type;
505: private String defaultValue;
506:
507: public Parameter(String name, String type, String defaultValue) {
508: this .name = name;
509: this .type = type;
510: this .defaultValue = defaultValue;
511: }
512:
513: public String getName() {
514: return name;
515: }
516:
517: public String getType() {
518: return type;
519: }
520:
521: public String getDefaultValue() {
522: return defaultValue;
523: }
524: }
525: }
|