1 19 20 package org.netbeans.modules.xml.wsdl.ui.view.grapheditor.widget; 21 22 import java.util.Collection ; 23 import java.util.LinkedList ; 24 import javax.xml.namespace.QName ; 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 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())) { 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 createNewMessageName(WSDLModel model) { 69 int i = 0; 70 String 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 <Message> getMessages(WSDLModel model) { 81 Collection <Message> messages = model.getDefinitions().getMessages(); 82 return (messages != null) ? messages : new LinkedList <Message>(); 83 } 84 85 86 public static String createNewPartName(Message message) { 87 int i = 0; 88 String name; 89 90 do { 91 name = "part" + (++i); } while (getPartByName(message, name) != null); 93 94 return name; 95 } 96 97 98 public static Collection <Part> getParts(Message message) { 99 Collection <Part> parts = message.getParts(); 100 return (parts != null) ? parts : new LinkedList <Part>(); 101 } 102 103 104 public static String getPartTypeOrElementString(Part part) { 105 String s = getPartTypeString(part); 106 107 if (s == null) { 108 s = getPartElementString(part); 109 } 110 111 if (s == null) { 112 s = "<Undefined>"; } 114 115 return s; 116 } 117 118 119 public static String getPartTypeString(Part part) { 120 if (part.getType() == null) return null; 121 return convertQNameToString(part.getType().getQName()); 122 } 123 124 125 public static String getPartElementString(Part part) { 126 if (part.getElement() == null) return null; 127 return convertQNameToString(part.getElement().getQName()); 128 } 129 130 131 private static String convertQNameToString(QName qname) { 132 if (qname == null) return null; 133 return qname.getPrefix() + ":" + qname.getLocalPart(); } 135 136 137 138 private static Message getMessageByName(WSDLModel model, String 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 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 |