KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > ui > netbeans > module > Utility


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.netbeans.module;
21
22 import java.io.IOException JavaDoc;
23 import java.net.URI JavaDoc;
24 import java.net.URISyntaxException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Set JavaDoc;
33
34 import javax.xml.XMLConstants JavaDoc;
35 import javax.xml.namespace.QName JavaDoc;
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 JavaDoc getNamespacePrefix(String JavaDoc 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 JavaDoc getNamespacePrefix(String JavaDoc 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 JavaDoc getNamespaceURI(String JavaDoc 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 JavaDoc getNamespaceURI(String JavaDoc 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 JavaDoc namespace, WSDLModel model) {
99         Collection JavaDoc imports = model.getDefinitions().getImports();
100         if (imports != null) {
101             Iterator JavaDoc 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 JavaDoc<WSDLModel> getImportedDocuments(WSDLModel model) {
113         Collection JavaDoc<Import> imports = model.getDefinitions().getImports();
114         Collection JavaDoc<WSDLModel> returnImports = new ArrayList JavaDoc<WSDLModel>();
115         if (imports != null) {
116             Iterator JavaDoc iter = imports.iterator();
117             for (;iter.hasNext();) {
118                 Import existingImport = (Import) iter.next();
119                 List JavaDoc<WSDLModel> impModels = model.findWSDLModel(existingImport.getNamespace());
120                 returnImports.addAll(impModels);
121             }
122         }
123         return returnImports;
124     }
125     
126     public static Map JavaDoc getNamespaces(Definitions def) {
127         return ((AbstractDocumentComponent) def).getPrefixes();
128     }
129     
130     public static GlobalElement findGlobalElement(Schema schema, String JavaDoc elementName) {
131         Collection JavaDoc gElem = schema.findAllGlobalElements();
132         if (gElem !=null){
133             Iterator JavaDoc 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 JavaDoc typeName) {
145         Collection JavaDoc gTypes = schema.findAllGlobalTypes();
146         if (gTypes !=null){
147             Iterator JavaDoc 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 JavaDoc fromQNameToString(QName JavaDoc 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 JavaDoc getNameAndDropPrefixIfInCurrentModel(String JavaDoc ns, String JavaDoc localPart, WSDLModel model) {
166         if (ns == null || model == null)
167             return localPart;
168         
169         String JavaDoc tns = model.getDefinitions().getTargetNamespace();
170         if (tns != null && !tns.equals(ns)) {
171             String JavaDoc prefix = getNamespacePrefix(ns, model);
172             if (prefix != null)
173                 return prefix + ":" + localPart;
174         }
175
176         return localPart;
177     }
178     
179     
180     public static List JavaDoc<QName JavaDoc> getExtensionAttributes(WSDLComponent comp) {
181         ArrayList JavaDoc<QName JavaDoc> result = new ArrayList JavaDoc<QName JavaDoc>();
182         Map JavaDoc<QName JavaDoc, String JavaDoc> attrMap = comp.getAttributeMap();
183         Set JavaDoc<QName JavaDoc> set = attrMap.keySet();
184         if (set != null) {
185             Iterator JavaDoc<QName JavaDoc> iter = set.iterator();
186             while (iter.hasNext()) {
187                 QName JavaDoc name = iter.next();
188                 String JavaDoc 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 JavaDoc<QName JavaDoc> getOptionalAttributes(WSDLComponent comp, Element elem) {
197         ArrayList JavaDoc<QName JavaDoc> result = new ArrayList JavaDoc<QName JavaDoc>();
198         Map JavaDoc<QName JavaDoc, String JavaDoc> attrMap = comp.getAttributeMap();
199         Set JavaDoc<QName JavaDoc> set = attrMap.keySet();
200         if (set != null) {
201             Iterator JavaDoc<QName JavaDoc> iter = set.iterator();
202             while (iter.hasNext()) {
203                 QName JavaDoc name = iter.next();
204                 String JavaDoc ns = name.getNamespaceURI();
205                 if (ns != null && ns.trim().length() > 0 && !ns.equals(comp.getModel().getDefinitions().getTargetNamespace())) {
206                     //extension attibute
207
//do nothing
208
} else {
209                     //not a extension attribute
210
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     /**
222      * Expands nodes on the treeview till given levels
223      * @param tv the treeview object
224      * @param level the level till which the nodes should be expanded. 0 means none.
225      * @param rootNode the rootNode
226      */

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]); //Expand node
236
expandNodes(tv, level - 1, nodes[i]); //expand children
237
}
238             }
239         }
240     }
241     
242     public static void addNamespacePrefix(Element schemaElement,
243             WSDLComponent element,
244             String JavaDoc 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 JavaDoc prefix) {
254         assert model != null;
255         if(schema != null) {
256             Definitions definitions = model.getDefinitions();
257             String JavaDoc targetNamespace = schema.getTargetNamespace();
258             String JavaDoc computedPrefix = null;
259
260             if(targetNamespace != null) {
261                 if (Utility.getNamespacePrefix(targetNamespace, model) != null) {
262                     //already exists, doesnt need to be added
263
return;
264                 }
265                 //Use the prefix (in parameter) or generate new one.
266
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 JavaDoc prefix) {
282         // Issue 93424, create a single transaction to encapsulate all changes.
283
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 JavaDoc<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 JavaDoc<Operation> listData = new ArrayList JavaDoc<Operation>(portType.getOperations().size());
309         Set JavaDoc<String JavaDoc> bindingOperationsSet = new HashSet JavaDoc<String JavaDoc>();
310         Collection JavaDoc<BindingOperation> bindingOperations = binding.getBindingOperations();
311         if (bindingOperations != null) {
312             Iterator JavaDoc<BindingOperation> iter = bindingOperations.iterator();
313             while (iter.hasNext()) {
314                 bindingOperationsSet.add(iter.next().getOperation().get().getName());
315             }
316             
317         }
318         Iterator JavaDoc 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 JavaDoc("unchecked")
333     public static Map JavaDoc<String JavaDoc,String JavaDoc> getPrefixes(WSDLComponent wsdlComponent) {
334         AbstractDocumentComponent comp = ((AbstractDocumentComponent) wsdlComponent);
335         Map JavaDoc<String JavaDoc,String JavaDoc> 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 JavaDoc<ExtensibilityElement> list,
345             Set JavaDoc<String JavaDoc> specialTargetNamespaces,
346             List JavaDoc<ExtensibilityElement> specialExtensibilityElements,
347             List JavaDoc<ExtensibilityElement> nonSpecialExtensibilityElements) {
348         if (specialExtensibilityElements == null) {
349             specialExtensibilityElements = new ArrayList JavaDoc<ExtensibilityElement>();
350         }
351         if (nonSpecialExtensibilityElements == null) {
352             nonSpecialExtensibilityElements = new ArrayList JavaDoc<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 JavaDoc<ExtensibilityElement> getSpecialExtensibilityElements(List JavaDoc<ExtensibilityElement> list,
367             String JavaDoc specialNamespace) {
368         List JavaDoc<ExtensibilityElement> specialList = new ArrayList JavaDoc<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     /**
380      * This method finds the absolute index in the definitions where the component needs to be inserted,
381      * such that the component is at given index with respect to its kind.
382      *
383      * For example, There are 5 messages, and one needs to insert another at index 4. Then this method will insert
384      * it at some index on Definitions, which will make it look like the 4th Message.
385      *
386      * it doesnt call startTransaction or endTransaction, so its the responsibility of the caller to do it.
387      *
388      * @param index
389      * @param model
390      * @param compToInsert
391      * @param propertyName
392      */

393     public static void insertIntoDefinitionsAtIndex(int index, WSDLModel model, WSDLComponent compToInsert, String JavaDoc propertyName) {
394         assert model.isIntransaction() : "Need to call startTransaction on this model, before calling this method";
395         //find index among all definitions elements.
396
//for inserting at index = 5, find index of the 4th PLT and insert after this index
397
int defIndex = -1;
398         int indexOfPreviousPLT = index - 1;
399         List JavaDoc<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     /* Similiar logic can be found in SchemaImportsGenerator.processImports(). So if there are changes here, also change in SchemaImportsGenerator*/
415     public static void addSchemaImport(SchemaComponent comp1, WSDLModel wsdlModel) {
416         Map JavaDoc<String JavaDoc, String JavaDoc> existingLocationToNamespaceMap = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
417         
418         FileObject wsdlFileObj = (FileObject) wsdlModel.getModelSource().getLookup().lookup(FileObject.class);
419         URI JavaDoc 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 JavaDoc wsdlTNS = def.getTargetNamespace();
430         if (wsdlTNS != null) {
431             Collection JavaDoc<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         //if any import with same namespace is present, dont import it.
451
Collection JavaDoc<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 JavaDoc<Schema> schemas = types.getSchemas();
457         if (schemas != null) {
458              for (Schema schema : schemas) {
459                  Collection JavaDoc<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 JavaDoc 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 JavaDoc path = null;
479                     //should be different files. in case of inline schemas.
480
if (!FileUtil.toFile(fo).toURI().equals(wsdlFileURI)) {
481                         DefaultProjectCatalogSupport catalogSupport = DefaultProjectCatalogSupport.getInstance(wsdlFileObj);
482                         if (catalogSupport.needsCatalogEntry(wsdlFileObj, fo)) {
483                             // Remove the previous catalog entry, then create new one.
484
URI JavaDoc 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 JavaDoc use) {
491                                 ErrorManager.getDefault().notify(use);
492                             } catch (IOException JavaDoc 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