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: package org.netbeans.modules.compapp.test.wsdl;
042:
043: import java.io.StringWriter;
044: import java.util.ArrayList;
045: import java.util.Collection;
046: import java.util.Collections;
047: import java.util.Comparator;
048: import java.util.List;
049: import org.apache.xmlbeans.XmlObject;
050: import org.apache.xmlbeans.XmlOptions;
051: import java.util.logging.Logger;
052: import org.netbeans.modules.xml.wsdl.model.Binding;
053: import org.netbeans.modules.xml.wsdl.model.BindingOperation;
054: import org.netbeans.modules.xml.wsdl.model.Definitions;
055: import org.netbeans.modules.xml.wsdl.model.ExtensibilityElement;
056: import org.netbeans.modules.xml.wsdl.model.Import;
057: import org.netbeans.modules.xml.wsdl.model.Port;
058: import org.netbeans.modules.xml.wsdl.model.Service;
059: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
060: import org.netbeans.modules.xml.xam.locator.CatalogModelException;
061: import org.openide.util.Exceptions;
062:
063: /**
064: * Util.java
065: *
066: * Created on February 2, 2006, 3:27 PM
067: *
068: * @author Bing Lu
069: * @author Jun Qian
070: */
071: public class Util {
072: private static final Logger mLog = Logger
073: .getLogger("org.netbeans.modules.compapp.test.wsdl.Util"); // NOI18N
074:
075: public static ExtensibilityElement getAssignableExtensiblityElement(
076: List<ExtensibilityElement> list, Class type) {
077: List<ExtensibilityElement> eelist = getAssignableExtensiblityElementList(
078: list, type);
079: return eelist.size() > 0 ? eelist.get(0) : null;
080: }
081:
082: public static List<ExtensibilityElement> getAssignableExtensiblityElementList(
083: List<ExtensibilityElement> list, Class type) {
084: List<ExtensibilityElement> result = new ArrayList<ExtensibilityElement>();
085:
086: for (ExtensibilityElement ee : list) {
087: if (type.isAssignableFrom(ee.getClass())) {
088: result.add(ee);
089: }
090: }
091:
092: return result;
093: }
094:
095: public static String getPrettyText(XmlObject xmlObject)
096: throws Exception {
097: StringWriter writer = new StringWriter();
098: XmlOptions options = new XmlOptions();
099: options.setSavePrettyPrint();
100: options.setSavePrettyPrintIndent(2);
101: options.setSaveNoXmlDecl();
102: options.setSaveAggressiveNamespaces();
103: xmlObject.save(writer, options);
104: return writer.toString();
105: }
106:
107: private static void getBindings(WSDLModel wsdlModel,
108: boolean recursive, Collection<Binding> bindings) {
109:
110: Definitions definitions = wsdlModel.getDefinitions();
111: bindings.addAll(definitions.getBindings());
112:
113: if (recursive) {
114: for (Import imp : definitions.getImports()) {
115: try {
116: WSDLModel importedWsdlModel = imp
117: .getImportedWSDLModel();
118: getBindings(importedWsdlModel, recursive, bindings);
119: } catch (CatalogModelException ex) {
120: Exceptions.printStackTrace(ex);
121: }
122: }
123: }
124: }
125:
126: public static List<Binding> getSortedBindings(WSDLModel wsdlModel) {
127: List<Binding> bindings = new ArrayList<Binding>();
128:
129: getBindings(wsdlModel, true, bindings);
130:
131: Collections.sort(bindings, new Comparator() {
132: public int compare(Object o1, Object o2) {
133: return ((Binding) o1).getName().compareTo(
134: ((Binding) o2).getName());
135: }
136:
137: public boolean equals(Object obj) {
138: return this == obj;
139: }
140: });
141:
142: return bindings;
143: }
144:
145: private static void getPorts(WSDLModel wsdlModel,
146: boolean recursive, Collection<Port> ports) {
147:
148: Definitions definitions = wsdlModel.getDefinitions();
149: for (Service service : definitions.getServices()) {
150: ports.addAll(service.getPorts());
151: }
152:
153: if (recursive) {
154: for (Import imp : definitions.getImports()) {
155: try {
156: WSDLModel importedWsdlModel = imp
157: .getImportedWSDLModel();
158: getPorts(importedWsdlModel, recursive, ports);
159: } catch (CatalogModelException ex) {
160: Exceptions.printStackTrace(ex);
161: }
162: }
163: }
164: }
165:
166: public static List<Port> getSortedPorts(WSDLModel wsdlModel) {
167: List<Port> ports = new ArrayList<Port>();
168:
169: getPorts(wsdlModel, true, ports);
170:
171: Collections.sort(ports, new Comparator() {
172: public int compare(Object o1, Object o2) {
173: return ((Port) o1).getName().compareTo(
174: ((Port) o2).getName());
175: }
176:
177: public boolean equals(Object obj) {
178: return this == obj;
179: }
180: });
181:
182: return ports;
183: }
184:
185: public static List<BindingOperation> getSortedBindingOperations(
186: Binding binding) {
187: List<BindingOperation> bindingOps = new ArrayList<BindingOperation>(
188: binding.getBindingOperations());
189:
190: Collections.sort(bindingOps, new Comparator() {
191: public int compare(Object o1, Object o2) {
192: return ((BindingOperation) o1).getName().compareTo(
193: ((BindingOperation) o2).getName());
194: }
195:
196: public boolean equals(Object obj) {
197: return this == obj;
198: }
199: });
200:
201: return bindingOps;
202: }
203: }
|