1 19 20 package org.netbeans.modules.xml.wsdl.ui.netbeans.module; 21 22 import java.io.IOException ; 23 import java.net.URI ; 24 import java.net.URISyntaxException ; 25 import java.util.ArrayList ; 26 import java.util.Collection ; 27 import java.util.HashMap ; 28 import java.util.HashSet ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Map ; 32 import java.util.Set ; 33 34 import javax.xml.XMLConstants ; 35 import javax.xml.namespace.QName ; 36 37 import org.netbeans.modules.xml.catalogsupport.DefaultProjectCatalogSupport; 38 import org.netbeans.modules.xml.schema.model.Element; 39 import org.netbeans.modules.xml.schema.model.GlobalElement; 40 import org.netbeans.modules.xml.schema.model.GlobalType; 41 import org.netbeans.modules.xml.schema.model.Schema; 42 import org.netbeans.modules.xml.schema.model.SchemaComponent; 43 import org.netbeans.modules.xml.schema.model.SchemaModel; 44 import org.netbeans.modules.xml.wsdl.model.Binding; 45 import org.netbeans.modules.xml.wsdl.model.BindingOperation; 46 import org.netbeans.modules.xml.wsdl.model.Definitions; 47 import org.netbeans.modules.xml.wsdl.model.ExtensibilityElement; 48 import org.netbeans.modules.xml.wsdl.model.Import; 49 import org.netbeans.modules.xml.wsdl.model.Operation; 50 import org.netbeans.modules.xml.wsdl.model.PortType; 51 import org.netbeans.modules.xml.wsdl.model.Types; 52 import org.netbeans.modules.xml.wsdl.model.WSDLComponent; 53 import org.netbeans.modules.xml.wsdl.model.WSDLModel; 54 import org.netbeans.modules.xml.wsdl.model.extensions.xsd.WSDLSchema; 55 import org.netbeans.modules.xml.wsdl.ui.actions.NameGenerator; 56 import org.netbeans.modules.xml.wsdl.ui.actions.schema.ExtensibilityElementCreatorVisitor; 57 import org.netbeans.modules.xml.wsdl.ui.schema.visitor.OptionalAttributeFinderVisitor; 58 import org.netbeans.modules.xml.wsdl.ui.wsdl.util.RelativePath; 59 import org.netbeans.modules.xml.xam.AbstractComponent; 60 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent; 61 import org.netbeans.modules.xml.xam.locator.CatalogModelException; 62 import org.openide.ErrorManager; 63 import org.openide.explorer.view.TreeView; 64 import org.openide.filesystems.FileObject; 65 import org.openide.filesystems.FileUtil; 66 import org.openide.nodes.Children; 67 import org.openide.nodes.Node; 68 69 public class Utility { 70 71 public static String getNamespacePrefix(String namespace, WSDLModel model) { 72 if (model != null && namespace != null) { 73 return ((AbstractDocumentComponent) model.getDefinitions()).lookupPrefix(namespace); 74 } 75 return null; 76 } 77 78 public static String getNamespacePrefix(String namespace, WSDLComponent element) { 79 if (element != null && namespace != null) { 80 return ((AbstractDocumentComponent) element).lookupPrefix(namespace); 81 } 82 return null; 83 } 84 85 public static String getNamespaceURI(String prefix, WSDLComponent element) { 86 if (element != null && prefix != null) { 87 return ((AbstractDocumentComponent) element).lookupNamespaceURI(prefix, true); 88 } 89 return null; 90 } 91 public static String getNamespaceURI(String prefix, WSDLModel model) { 92 if (model != null && prefix != null) { 93 return ((AbstractDocumentComponent) model.getDefinitions()).lookupNamespaceURI(prefix, true); 94 } 95 return null; 96 } 97 98 public static Import getImport(String namespace, WSDLModel model) { 99 Collection imports = model.getDefinitions().getImports(); 100 if (imports != null) { 101 Iterator iter = imports.iterator(); 102 for (;iter.hasNext();) { 103 Import existingImport = (Import) iter.next(); 104 if (existingImport.getNamespace().equals(namespace)) { 105 return existingImport; 106 } 107 } 108 } 109 return null; 110 } 111 112 public static Collection <WSDLModel> getImportedDocuments(WSDLModel model) { 113 Collection <Import> imports = model.getDefinitions().getImports(); 114 Collection <WSDLModel> returnImports = new ArrayList <WSDLModel>(); 115 if (imports != null) { 116 Iterator iter = imports.iterator(); 117 for (;iter.hasNext();) { 118 Import existingImport = (Import) iter.next(); 119 List <WSDLModel> impModels = model.findWSDLModel(existingImport.getNamespace()); 120 returnImports.addAll(impModels); 121 } 122 } 123 return returnImports; 124 } 125 126 public static Map getNamespaces(Definitions def) { 127 return ((AbstractDocumentComponent) def).getPrefixes(); 128 } 129 130 public static GlobalElement findGlobalElement(Schema schema, String elementName) { 131 Collection gElem = schema.findAllGlobalElements(); 132 if (gElem !=null){ 133 Iterator iter = gElem.iterator(); 134 while (iter.hasNext()) { 135 GlobalElement elem = (GlobalElement) iter.next(); 136 if (elem.getName().equals(elementName)) { 137 return elem; 138 } 139 } 140 } 141 return null; 142 } 143 144 public static GlobalType findGlobalType(Schema schema, String typeName) { 145 Collection gTypes = schema.findAllGlobalTypes(); 146 if (gTypes !=null){ 147 Iterator iter = gTypes.iterator(); 148 while (iter.hasNext()) { 149 GlobalType type = (GlobalType) iter.next(); 150 if (type.getName().equals(typeName)) { 151 return type; 152 } 153 } 154 } 155 return null; 156 } 157 158 public static String fromQNameToString(QName qname) { 159 if (qname.getPrefix() != null && qname.getPrefix().trim().length() > 0) { 160 return qname.getPrefix() + ":" + qname.getLocalPart(); 161 } 162 return qname.getLocalPart(); 163 } 164 165 public static String getNameAndDropPrefixIfInCurrentModel(String ns, String localPart, WSDLModel model) { 166 if (ns == null || model == null) 167 return localPart; 168 169 String tns = model.getDefinitions().getTargetNamespace(); 170 if (tns != null && !tns.equals(ns)) { 171 String prefix = getNamespacePrefix(ns, model); 172 if (prefix != null) 173 return prefix + ":" + localPart; 174 } 175 176 return localPart; 177 } 178 179 180 public static List <QName > getExtensionAttributes(WSDLComponent comp) { 181 ArrayList <QName > result = new ArrayList <QName >(); 182 Map <QName , String > attrMap = comp.getAttributeMap(); 183 Set <QName > set = attrMap.keySet(); 184 if (set != null) { 185 Iterator <QName > iter = set.iterator(); 186 while (iter.hasNext()) { 187 QName name = iter.next(); 188 String ns = name.getNamespaceURI(); 189 if (ns != null && ns.trim().length() > 0 && !ns.equals(((AbstractDocumentComponent) comp).getQName().getNamespaceURI())) { 190 result.add(name); 191 } 192 } 193 } 194 return result; 195 } 196 public static List <QName > getOptionalAttributes(WSDLComponent comp, Element elem) { 197 ArrayList <QName > result = new ArrayList <QName >(); 198 Map <QName , String > attrMap = comp.getAttributeMap(); 199 Set <QName > set = attrMap.keySet(); 200 if (set != null) { 201 Iterator <QName > iter = set.iterator(); 202 while (iter.hasNext()) { 203 QName name = iter.next(); 204 String ns = name.getNamespaceURI(); 205 if (ns != null && ns.trim().length() > 0 && !ns.equals(comp.getModel().getDefinitions().getTargetNamespace())) { 206 } else { 209 OptionalAttributeFinderVisitor visitor = new OptionalAttributeFinderVisitor(name.getLocalPart()); 211 elem.accept(visitor); 212 if (visitor.isOptional()) { 213 result.add(name); 214 } 215 } 216 } 217 } 218 return result; 219 } 220 221 227 public static void expandNodes(TreeView tv, int level, Node rootNode) { 228 if (level == 0) return; 229 230 Children children = rootNode.getChildren(); 231 if (children != null) { 232 Node[] nodes = children.getNodes(); 233 if (nodes != null) { 234 for (int i= 0; i < nodes.length; i++) { 235 tv.expandNode(nodes[i]); expandNodes(tv, level - 1, nodes[i]); } 238 } 239 } 240 } 241 242 public static void addNamespacePrefix(Element schemaElement, 243 WSDLComponent element, 244 String prefix) { 245 if(schemaElement != null && element.getModel() != null) { 246 addNamespacePrefix(schemaElement.getModel().getSchema(), element.getModel(), prefix); 247 } 248 249 } 250 251 public static void addNamespacePrefix(Schema schema, 252 WSDLModel model, 253 String prefix) { 254 assert model != null; 255 if(schema != null) { 256 Definitions definitions = model.getDefinitions(); 257 String targetNamespace = schema.getTargetNamespace(); 258 String computedPrefix = null; 259 260 if(targetNamespace != null) { 261 if (Utility.getNamespacePrefix(targetNamespace, model) != null) { 262 return; 264 } 265 if(prefix != null) { 267 computedPrefix = prefix; 268 } else { 269 computedPrefix = NameGenerator.getInstance().generateNamespacePrefix(null, model.getDefinitions()); 270 } 271 boolean isAlreadyInTransaction = Utility.startTransaction(model); 272 ((AbstractDocumentComponent)definitions).addPrefix(computedPrefix, schema.getTargetNamespace()); 273 274 Utility.endTransaction(model, isAlreadyInTransaction); 275 276 } 277 278 } 279 } 280 281 public static void addExtensibilityElement(WSDLComponent element, Element schemaElement, String prefix) { 282 WSDLModel model = element.getModel(); 284 boolean in = startTransaction(model); 285 Utility.addNamespacePrefix(schemaElement, element, prefix); 286 ExtensibilityElementCreatorVisitor eeCreator = new ExtensibilityElementCreatorVisitor(element); 287 schemaElement.accept(eeCreator); 288 endTransaction(model, in); 289 } 290 291 public static boolean startTransaction(WSDLModel model) { 292 boolean isInTransaction = model.isIntransaction(); 293 if (isInTransaction) return true; 294 model.startTransaction(); 295 return false; 296 } 297 298 public static void endTransaction(WSDLModel model, boolean isInTransaction) { 299 if (isInTransaction) return; 300 model.endTransaction(); 301 } 302 303 public static Collection <Operation> getImplementableOperations(PortType portType, Binding binding) { 304 if (portType == null || portType.getOperations() == null || portType.getOperations().size() == 0 305 || binding == null) { 306 return null; 307 } 308 List <Operation> listData = new ArrayList <Operation>(portType.getOperations().size()); 309 Set <String > bindingOperationsSet = new HashSet <String >(); 310 Collection <BindingOperation> bindingOperations = binding.getBindingOperations(); 311 if (bindingOperations != null) { 312 Iterator <BindingOperation> iter = bindingOperations.iterator(); 313 while (iter.hasNext()) { 314 bindingOperationsSet.add(iter.next().getOperation().get().getName()); 315 } 316 317 } 318 Iterator it = portType.getOperations().iterator(); 319 320 while(it.hasNext()) { 321 Operation operation = (Operation) it.next(); 322 if(operation.getName() != null) { 323 if (!bindingOperationsSet.contains(operation.getName())) { 324 listData.add(operation); 325 } 326 } 327 } 328 329 return listData; 330 } 331 332 @SuppressWarnings ("unchecked") 333 public static Map <String ,String > getPrefixes(WSDLComponent wsdlComponent) { 334 AbstractDocumentComponent comp = ((AbstractDocumentComponent) wsdlComponent); 335 Map <String ,String > prefixes = comp.getPrefixes(); 336 while(comp.getParent() != null) { 337 comp = (AbstractDocumentComponent) comp.getParent(); 338 prefixes.putAll(comp.getPrefixes()); 339 } 340 341 return prefixes; 342 } 343 344 public static void splitExtensibilityElements(List <ExtensibilityElement> list, 345 Set <String > specialTargetNamespaces, 346 List <ExtensibilityElement> specialExtensibilityElements, 347 List <ExtensibilityElement> nonSpecialExtensibilityElements) { 348 if (specialExtensibilityElements == null) { 349 specialExtensibilityElements = new ArrayList <ExtensibilityElement>(); 350 } 351 if (nonSpecialExtensibilityElements == null) { 352 nonSpecialExtensibilityElements = new ArrayList <ExtensibilityElement>(); 353 } 354 355 if (list != null) { 356 for (ExtensibilityElement element : list) { 357 if (specialTargetNamespaces.contains(element.getQName().getNamespaceURI())) { 358 specialExtensibilityElements.add(element); 359 } else { 360 nonSpecialExtensibilityElements.add(element); 361 } 362 } 363 } 364 } 365 366 public static List <ExtensibilityElement> getSpecialExtensibilityElements(List <ExtensibilityElement> list, 367 String specialNamespace) { 368 List <ExtensibilityElement> specialList = new ArrayList <ExtensibilityElement>(); 369 if (list != null) { 370 for (ExtensibilityElement element : list) { 371 if (specialNamespace.equals(element.getQName().getNamespaceURI())) { 372 specialList.add(element); 373 } 374 } 375 } 376 return specialList; 377 } 378 379 393 public static void insertIntoDefinitionsAtIndex(int index, WSDLModel model, WSDLComponent compToInsert, String propertyName) { 394 assert model.isIntransaction() : "Need to call startTransaction on this model, before calling this method"; 395 int defIndex = -1; 398 int indexOfPreviousPLT = index - 1; 399 List <WSDLComponent> comps = model.getDefinitions().getChildren(); 400 for (int i = 0; i < comps.size(); i++) { 401 WSDLComponent comp = comps.get(i); 402 if (compToInsert.getClass().isAssignableFrom(comp.getClass())) { 403 if (indexOfPreviousPLT > defIndex) { 404 defIndex ++; 405 } else { 406 ((AbstractComponent<WSDLComponent>) model.getDefinitions()).insertAtIndex(propertyName, compToInsert, i); 407 break; 408 } 409 } 410 } 411 } 412 413 414 415 public static void addSchemaImport(SchemaComponent comp1, WSDLModel wsdlModel) { 416 Map <String , String > existingLocationToNamespaceMap = new HashMap <String , String >(); 417 418 FileObject wsdlFileObj = (FileObject) wsdlModel.getModelSource().getLookup().lookup(FileObject.class); 419 URI wsdlFileURI = FileUtil.toFile(wsdlFileObj).toURI(); 420 421 Definitions def = wsdlModel.getDefinitions(); 422 Types types = def.getTypes(); 423 if (types == null) { 424 types = wsdlModel.getFactory().createTypes(); 425 def.setTypes(types); 426 } 427 428 Schema defaultInlineSchema = null; 429 String wsdlTNS = def.getTargetNamespace(); 430 if (wsdlTNS != null) { 431 Collection <Schema> schmas = types.getSchemas(); 432 if (schmas != null) { 433 for (Schema s : schmas) { 434 if (s.getTargetNamespace() != null && s.getTargetNamespace().equals(wsdlTNS)) { 435 defaultInlineSchema = s; 436 break; 437 } 438 } 439 } 440 } 441 442 WSDLSchema wsdlSchema = null; 443 if (defaultInlineSchema == null) { 444 wsdlSchema = wsdlModel.getFactory().createWSDLSchema(); 445 SchemaModel schemaModel = wsdlSchema.getSchemaModel(); 446 defaultInlineSchema = schemaModel.getSchema(); 447 defaultInlineSchema.setTargetNamespace(wsdlTNS); 448 } 449 450 Collection <org.netbeans.modules.xml.schema.model.Import> imports = defaultInlineSchema.getImports(); 452 for (org.netbeans.modules.xml.schema.model.Import imp : imports) { 453 existingLocationToNamespaceMap.put(imp.getSchemaLocation(), imp.getNamespace()); 454 } 455 456 Collection <Schema> schemas = types.getSchemas(); 457 if (schemas != null) { 458 for (Schema schema : schemas) { 459 Collection <org.netbeans.modules.xml.schema.model.Import> schemaImports = schema.getImports(); 460 for (org.netbeans.modules.xml.schema.model.Import imp : schemaImports) { 461 existingLocationToNamespaceMap.put(imp.getSchemaLocation(), imp.getNamespace()); 462 } 463 } 464 } 465 466 SchemaModel model = comp1.getModel(); 467 468 if (model != null) { 469 470 String schemaTNS = model.getSchema().getTargetNamespace(); 471 if (schemaTNS != null && 472 !schemaTNS.equals(XMLConstants.W3C_XML_SCHEMA_NS_URI)) { 473 474 FileObject fo = (FileObject) model.getModelSource().getLookup().lookup(FileObject.class); 475 476 477 if (fo != null) { 478 String path = null; 479 if (!FileUtil.toFile(fo).toURI().equals(wsdlFileURI)) { 481 DefaultProjectCatalogSupport catalogSupport = DefaultProjectCatalogSupport.getInstance(wsdlFileObj); 482 if (catalogSupport.needsCatalogEntry(wsdlFileObj, fo)) { 483 URI uri; 485 try { 486 uri = catalogSupport.getReferenceURI(wsdlFileObj, fo); 487 catalogSupport.removeCatalogEntry(uri); 488 catalogSupport.createCatalogEntry(wsdlFileObj, fo); 489 path = catalogSupport.getReferenceURI(wsdlFileObj, fo).toString(); 490 } catch (URISyntaxException use) { 491 ErrorManager.getDefault().notify(use); 492 } catch (IOException ioe) { 493 ErrorManager.getDefault().notify(ioe); 494 } catch (CatalogModelException cme) { 495 ErrorManager.getDefault().notify(cme); 496 } 497 } else { 498 path = RelativePath.getRelativePath(FileUtil.toFile(wsdlFileObj).getParentFile(), FileUtil.toFile(fo)); 499 } 500 } 501 if (path != null && (!existingLocationToNamespaceMap.containsKey(path) || 502 existingLocationToNamespaceMap.get(path) == null || 503 !existingLocationToNamespaceMap.get(path).equals(schemaTNS))) 504 { 505 org.netbeans.modules.xml.schema.model.Import schemaImport = 506 defaultInlineSchema.getModel().getFactory().createImport(); 507 schemaImport.setNamespace(schemaTNS); 508 schemaImport.setSchemaLocation(path); 509 defaultInlineSchema.addExternalReference(schemaImport); 510 if (wsdlSchema != null) { 511 types.addExtensibilityElement(wsdlSchema); 512 } 513 } 514 } 515 } 516 } 517 518 } 519 520 } 521 | Popular Tags |