KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > ui > view > grapheditor > widget > MessagesUtils


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.wsdl.ui.view.grapheditor.widget;
21
22 import java.util.Collection JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import javax.xml.namespace.QName JavaDoc;
25 import org.netbeans.modules.xml.axi.AXIComponent;
26 import org.netbeans.modules.xml.schema.model.GlobalElement;
27 import org.netbeans.modules.xml.schema.model.GlobalSimpleType;
28 import org.netbeans.modules.xml.schema.model.GlobalType;
29 import org.netbeans.modules.xml.schema.model.Schema;
30 import org.netbeans.modules.xml.schema.model.SchemaComponent;
31 import org.netbeans.modules.xml.schema.model.SchemaComponentReference;
32 import org.netbeans.modules.xml.schema.model.SchemaModelFactory;
33 import org.netbeans.modules.xml.wsdl.model.Message;
34 import org.netbeans.modules.xml.wsdl.model.Part;
35 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
36 import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
37 import org.openide.nodes.Node;
38
39 /**
40  *
41  * @author anjeleevich
42  */

43 public class MessagesUtils {
44     
45
46     public static NamedComponentReference<GlobalType> getDefaultTypeReference(
47             WSDLModel model)
48     {
49         GlobalSimpleType newType = null;
50         
51         Schema schema = SchemaModelFactory.getDefault()
52                 .getPrimitiveTypesModel().getSchema();
53         
54         for (GlobalSimpleType type : schema.getSimpleTypes()) {
55             if ("string".equals(type.getName())) { // NOI18N
56
newType = type;
57                 break;
58             }
59         }
60         
61         if (newType == null) return null;
62         
63         return model.getDefinitions().createSchemaReference(newType,
64                 GlobalType.class);
65     }
66     
67     
68     public static String JavaDoc createNewMessageName(WSDLModel model) {
69         int i = 0;
70         String JavaDoc name;
71         
72         do {
73             name = "message" + (++i);
74         } while (getMessageByName(model, name) != null);
75         
76         return name;
77     }
78     
79     
80     public static Collection JavaDoc<Message> getMessages(WSDLModel model) {
81         Collection JavaDoc<Message> messages = model.getDefinitions().getMessages();
82         return (messages != null) ? messages : new LinkedList JavaDoc<Message>();
83     }
84     
85     
86     public static String JavaDoc createNewPartName(Message message) {
87         int i = 0;
88         String JavaDoc name;
89         
90         do {
91             name = "part" + (++i); // NOI18N
92
} while (getPartByName(message, name) != null);
93         
94         return name;
95     }
96     
97     
98     public static Collection JavaDoc<Part> getParts(Message message) {
99         Collection JavaDoc<Part> parts = message.getParts();
100         return (parts != null) ? parts : new LinkedList JavaDoc<Part>();
101     }
102
103     
104     public static String JavaDoc getPartTypeOrElementString(Part part) {
105         String JavaDoc s = getPartTypeString(part);
106         
107         if (s == null) {
108             s = getPartElementString(part);
109         }
110         
111         if (s == null) {
112             s = "<Undefined>"; // NOI18N
113
}
114         
115         return s;
116     }
117     
118     
119     public static String JavaDoc getPartTypeString(Part part) {
120         if (part.getType() == null) return null;
121         return convertQNameToString(part.getType().getQName());
122     }
123     
124     
125     public static String JavaDoc getPartElementString(Part part) {
126         if (part.getElement() == null) return null;
127         return convertQNameToString(part.getElement().getQName());
128     }
129     
130     
131     private static String JavaDoc convertQNameToString(QName JavaDoc qname) {
132         if (qname == null) return null;
133         return qname.getPrefix() + ":" + qname.getLocalPart(); // NOI18N
134
}
135     
136     
137     
138     private static Message getMessageByName(WSDLModel model, String JavaDoc name) {
139         for (Message message : getMessages(model)) {
140             if (name.equals(message.getName())) return message;
141         }
142         return null;
143     }
144     
145     
146     private static Part getPartByName(Message message, String JavaDoc name) {
147         for (Part part : getParts(message)) {
148             if (name.equals(part.getName())) return part;
149         }
150         return null;
151     }
152     
153     
154     public static SchemaComponent extractSchemaComponent(Node node) {
155         AXIComponent axiComponent = (AXIComponent) node.getLookup()
156                 .lookup(AXIComponent.class);
157
158         SchemaComponent schemaComponent = null;
159
160         if (axiComponent != null) {
161             schemaComponent = axiComponent.getPeer();
162         } else {
163             SchemaComponentReference reference = (SchemaComponentReference)
164                     node.getLookup().lookup(SchemaComponentReference.class);
165
166             if (reference != null) {
167                 schemaComponent = reference.get();
168             }
169
170             if (schemaComponent == null) {
171                 schemaComponent = (SchemaComponent) node.getLookup()
172                         .lookup(SchemaComponent.class);
173             }
174         }
175
176         if (schemaComponent != null
177                 && (schemaComponent instanceof GlobalType
178                 || schemaComponent instanceof GlobalElement))
179         {
180             return schemaComponent;
181         }
182
183         return null;
184     }
185 }
186
Popular Tags