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: /*
043: * PropertyUtil.java
044: *
045: * Created on April 17, 2006, 12:58 PM
046: *
047: * To change this template, choose Tools | Template Manager
048: * and open the template in the editor.
049: */
050:
051: package org.netbeans.modules.xml.wsdl.ui.api.property;
052:
053: import java.util.ArrayList;
054: import java.util.Collection;
055: import java.util.List;
056:
057: import javax.xml.namespace.QName;
058:
059: import org.netbeans.modules.xml.wsdl.model.Binding;
060: import org.netbeans.modules.xml.wsdl.model.Definitions;
061: import org.netbeans.modules.xml.wsdl.model.Message;
062: import org.netbeans.modules.xml.wsdl.model.PortType;
063: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
064: import org.netbeans.modules.xml.wsdl.ui.netbeans.module.Utility;
065:
066: /**
067: *
068: * @author radval
069: */
070: public class PropertyUtil {
071: private static final String COLON = ":";// NOI18N
072:
073: /** Creates a new instance of PropertyUtil */
074: public PropertyUtil() {
075: }
076:
077: public static String[] getAllPortTypes(WSDLModel model,
078: boolean includeBlankItem) {
079: ArrayList<String> portTypesList = new ArrayList<String>();
080: if (includeBlankItem) {
081: portTypesList.add("");
082: }
083:
084: //ifist get all PortTypes in current wsdl document
085: portTypesList.addAll(getAllAvailablePortTypes(model, model));
086:
087: //then get all port type in all explicitly imported documents
088: for (WSDLModel imported : Utility.getImportedDocuments(model)) {
089: portTypesList.addAll(getAllAvailablePortTypes(model,
090: imported));
091: }
092:
093: return portTypesList.toArray(new String[portTypesList.size()]);
094: }
095:
096: private static List<String> getAllAvailablePortTypes(
097: WSDLModel source, WSDLModel document) {
098: ArrayList<String> portTypesList = new ArrayList<String>();
099:
100: Definitions definition = document.getDefinitions();
101:
102: for (PortType portType : definition.getPortTypes()) {
103: String name = portType.getName();
104: String targetNamespace = document.getDefinitions()
105: .getTargetNamespace();
106: String prefix = Utility.getNamespacePrefix(targetNamespace,
107: source);
108: if (name != null) {
109: if (prefix != null) {
110: String portTypeQNameStr = prefix + COLON + name;
111: portTypesList.add(portTypeQNameStr);
112: } else {
113: portTypesList.add(name);
114: }
115: }
116: }
117:
118: return portTypesList;
119: }
120:
121: public static String[] getAllMessages(WSDLModel model) {
122: ArrayList<String> messageList = new ArrayList<String>();
123: //messageList.add("");
124:
125: //first get all messages in current wsdl document
126: messageList.addAll(getAllAvailableMessages(model, model));
127:
128: //then get all messages in all imported wsdl documents
129: Collection<WSDLModel> allWsdls = Utility
130: .getImportedDocuments(model);
131: if (allWsdls != null) {
132: for (WSDLModel doc : allWsdls) {
133: messageList.addAll(getAllAvailableMessages(model, doc));
134:
135: }
136: }
137: return messageList.toArray(new String[messageList.size()]);
138: }
139:
140: private static List<String> getAllAvailableMessages(
141: WSDLModel source, WSDLModel document) {
142: ArrayList<String> messageList = new ArrayList<String>();
143:
144: Definitions definition = document.getDefinitions();
145: for (Message msg : definition.getMessages()) {
146: String name = msg.getName();
147: String targetNamespace = definition.getTargetNamespace();
148: String prefix = Utility.getNamespacePrefix(targetNamespace,
149: source);
150: if (name != null) {
151: if (prefix != null) {
152: String messageQNameStr = prefix + COLON + name;
153: messageList.add(messageQNameStr);
154: } else {
155: messageList.add(name);
156: }
157: }
158:
159: }
160:
161: return messageList;
162: }
163:
164: public static String[] getAllBindings(WSDLModel model) {
165: ArrayList<String> bindingList = new ArrayList<String>();
166: bindingList.add("");
167:
168: //first get all binding in current wsdl document
169: bindingList.addAll(getAllBindings(model, model));
170:
171: //then get all biniding in all explicitly imported documents
172: for (WSDLModel doc : Utility.getImportedDocuments(model)) {
173: bindingList.addAll(getAllBindings(model, doc));
174: }
175:
176: return bindingList.toArray(new String[bindingList.size()]);
177: }
178:
179: private static List<String> getAllBindings(WSDLModel source,
180: WSDLModel document) {
181: ArrayList<String> bindingList = new ArrayList<String>();
182:
183: Definitions definition = document.getDefinitions();
184:
185: for (Binding binding : definition.getBindings()) {
186: String name = binding.getName();
187: String targetNamespace = definition.getTargetNamespace();
188: String prefix = Utility.getNamespacePrefix(targetNamespace,
189: source);
190: if (name != null) {
191: if (prefix != null) {
192: String messageQNameStr = prefix + COLON + name;
193: bindingList.add(messageQNameStr);
194: } else {
195: bindingList.add(name);
196: }
197: }
198:
199: }
200:
201: return bindingList;
202: }
203:
204: public static Message getMessage(MessageProvider prov,
205: WSDLModel model) {
206: Message msg = prov.getWSDLMessage();
207: if (msg != null) {
208: return msg;
209: }
210:
211: String messageName = prov.getMessage();
212: if (messageName == null) {
213: return null;
214: }
215: String[] qnameParts = messageName.split(COLON);
216: String prefix = "";
217: String localPart = messageName;
218: if (qnameParts.length > 1) {
219: prefix = qnameParts[0];
220: localPart = qnameParts[1];
221: }
222: String ns = Utility.getNamespaceURI(prefix, model);
223: if (ns != null) {
224: return model.findComponentByName(new QName(ns, localPart),
225: Message.class);
226: }
227: return model.findComponentByName(localPart, Message.class);
228: }
229: }
|