KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > ui > wizard > SchemaImportsGenerator


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.wizard;
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.List JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import javax.xml.XMLConstants JavaDoc;
32
33 import org.netbeans.modules.xml.catalogsupport.DefaultProjectCatalogSupport;
34 import org.netbeans.modules.xml.schema.model.GlobalElement;
35 import org.netbeans.modules.xml.schema.model.GlobalType;
36 import org.netbeans.modules.xml.schema.model.Import;
37 import org.netbeans.modules.xml.schema.model.Schema;
38 import org.netbeans.modules.xml.schema.model.SchemaModel;
39 import org.netbeans.modules.xml.wsdl.model.Definitions;
40 import org.netbeans.modules.xml.wsdl.model.Types;
41 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
42 import org.netbeans.modules.xml.wsdl.model.extensions.xsd.WSDLSchema;
43 import org.netbeans.modules.xml.wsdl.ui.view.ElementOrType;
44 import org.netbeans.modules.xml.wsdl.ui.view.PartAndElementOrTypeTableModel;
45 import org.netbeans.modules.xml.wsdl.ui.view.PartAndElementOrTypeTableModel.PartAndElementOrType;
46 import org.netbeans.modules.xml.wsdl.ui.wsdl.util.RelativePath;
47 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
48 import org.netbeans.modules.xml.xam.locator.CatalogModelException;
49 import org.openide.ErrorManager;
50 import org.openide.filesystems.FileObject;
51 import org.openide.filesystems.FileUtil;
52
53 /**
54  *
55  * @author radval
56  */

57 public class SchemaImportsGenerator implements Command {
58     
59     private WSDLModel mModel;
60     
61     private Map JavaDoc mConfigurationMap;
62     
63     private Collection JavaDoc<Import> mImports = new ArrayList JavaDoc<Import>();
64     
65     /** Creates a new instance of OperationGenerator */
66     public SchemaImportsGenerator(WSDLModel model, Map JavaDoc configurationMap) {
67         this.mModel = model;
68         this.mConfigurationMap = configurationMap;
69     }
70     
71     public Collection JavaDoc<Import> getImports() {
72         return this.mImports;
73     }
74     
75     public void execute() {
76         if(mModel != null) {
77             List JavaDoc<PartAndElementOrTypeTableModel.PartAndElementOrType> inputMessageParts =
78                 (List JavaDoc<PartAndElementOrTypeTableModel.PartAndElementOrType>) this.mConfigurationMap.get(WizardPortTypeConfigurationStep.OPERATION_INPUT);
79             
80             List JavaDoc<PartAndElementOrTypeTableModel.PartAndElementOrType> outputMessageParts =
81                 (List JavaDoc<PartAndElementOrTypeTableModel.PartAndElementOrType>) this.mConfigurationMap.get(WizardPortTypeConfigurationStep.OPERATION_OUTPUT);
82             
83             List JavaDoc<PartAndElementOrTypeTableModel.PartAndElementOrType> faultMessageParts =
84                 (List JavaDoc<PartAndElementOrTypeTableModel.PartAndElementOrType>) this.mConfigurationMap.get(WizardPortTypeConfigurationStep.OPERATION_FAULT);
85             
86             List JavaDoc<PartAndElementOrTypeTableModel.PartAndElementOrType> allParts = new ArrayList JavaDoc<PartAndElementOrTypeTableModel.PartAndElementOrType>();
87             if (inputMessageParts != null) {
88                 allParts.addAll(inputMessageParts);
89             }
90             if (outputMessageParts != null) {
91                 allParts.addAll(outputMessageParts);
92             }
93             if (faultMessageParts != null) {
94                 allParts.addAll(faultMessageParts);
95             }
96             
97             Map JavaDoc<String JavaDoc, String JavaDoc> namespaceToPrefixMap = (Map JavaDoc) this.mConfigurationMap.get(WizardPortTypeConfigurationStep.NAMESPACE_TO_PREFIX_MAP);
98             if (namespaceToPrefixMap != null) {
99                 for (String JavaDoc namespace : namespaceToPrefixMap.keySet()) {
100                     ((AbstractDocumentComponent) mModel.getDefinitions()).addPrefix(namespaceToPrefixMap.get(namespace), namespace);
101                 }
102             }
103             boolean fromWizard = false;
104             if (mConfigurationMap.containsKey(WizardPortTypeConfigurationStep.IS_FROM_WIZARD)) {
105                 fromWizard = true;
106             }
107             
108             processImports(allParts, fromWizard);
109         }
110         
111     }
112     
113     /* Similiar logic can be found in Utility.addSchemaImport(). So if there are changes here, also change in Utility*/
114      private void processImports(List JavaDoc<PartAndElementOrType> allParts, boolean fromWizard) {
115          Map JavaDoc<String JavaDoc, String JavaDoc> locationToNamespaceMap = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
116          Map JavaDoc<String JavaDoc, String JavaDoc> existingLocationToNamespaceMap = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
117          
118          FileObject wsdlFileObj = (FileObject) mModel.getModelSource().getLookup().lookup(FileObject.class);
119          URI JavaDoc wsdlFileURI = FileUtil.toFile(wsdlFileObj).toURI();
120          
121          Definitions def = mModel.getDefinitions();
122          Types types = def.getTypes();
123          if (types == null) {
124              types = mModel.getFactory().createTypes();
125              def.setTypes(types);
126          }
127          Schema defaultInlineSchema = null;
128          String JavaDoc wsdlTNS = def.getTargetNamespace();
129          if (wsdlTNS != null) {
130              Collection JavaDoc<Schema> schmas = types.getSchemas();
131              if (schmas != null) {
132                  for (Schema s : schmas) {
133                      if (s.getTargetNamespace() != null && s.getTargetNamespace().equals(wsdlTNS)) {
134                          defaultInlineSchema = s;
135                          break;
136                      }
137                  }
138              }
139          }
140          
141          WSDLSchema wsdlSchema = null;
142          if (defaultInlineSchema == null) {
143              wsdlSchema = mModel.getFactory().createWSDLSchema();
144              SchemaModel schemaModel = wsdlSchema.getSchemaModel();
145              defaultInlineSchema = schemaModel.getSchema();
146              defaultInlineSchema.setTargetNamespace(mModel.getDefinitions().getTargetNamespace());
147          }
148
149          //if any import with same namespace is present, dont import it.
150
Collection JavaDoc<Import> imports = defaultInlineSchema.getImports();
151          for (Import imp : imports) {
152              existingLocationToNamespaceMap.put(imp.getSchemaLocation(), imp.getNamespace());
153          }
154          
155          if (!fromWizard) {
156              Collection JavaDoc<Schema> schemas = types.getSchemas();
157              if (schemas != null) {
158                   for (Schema schema : schemas) {
159                       Collection JavaDoc<Import> schemaImports = schema.getImports();
160                       for (Import imp : schemaImports) {
161                           existingLocationToNamespaceMap.put(imp.getSchemaLocation(), imp.getNamespace());
162                       }
163                   }
164              }
165          }
166          
167          for (PartAndElementOrType part : allParts) {
168              ElementOrType eot = part.getElementOrType();
169              GlobalElement element = eot.getElement();
170              GlobalType type = eot.getType();
171              SchemaModel model = null;
172              if (element != null) {
173                  model = element.getModel();
174              } else if (type != null) {
175                  model = type.getModel();
176              }
177              
178              if (model != null) {
179                  String JavaDoc schemaTNS = model.getSchema().getTargetNamespace();
180                  if (schemaTNS != null &&
181                          !schemaTNS.equals(XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
182                      
183                      FileObject fo = (FileObject) model.getModelSource().getLookup().lookup(FileObject.class);
184                      
185                      if (fo != null) {
186                          String JavaDoc path = null;
187
188                          if (fromWizard) {
189                              // generate absolute URI, this will get changed later in wizard post process import
190
path = FileUtil.toFile(fo).toURI().toString();
191                          } else if (!FileUtil.toFile(fo).toURI().equals(wsdlFileURI)) {
192                              //should be different files. in case of inline schemas.
193
DefaultProjectCatalogSupport catalogSupport = DefaultProjectCatalogSupport.getInstance(wsdlFileObj);
194                              if (catalogSupport.needsCatalogEntry(wsdlFileObj, fo)) {
195                                  // Remove the previous catalog entry, then create new one.
196
URI JavaDoc uri;
197                                  try {
198                                      uri = catalogSupport.getReferenceURI(wsdlFileObj, fo);
199                                      catalogSupport.removeCatalogEntry(uri);
200                                      catalogSupport.createCatalogEntry(wsdlFileObj, fo);
201                                      path = catalogSupport.getReferenceURI(wsdlFileObj, fo).toString();
202                                  } catch (URISyntaxException JavaDoc use) {
203                                      ErrorManager.getDefault().notify(use);
204                                  } catch (IOException JavaDoc ioe) {
205                                      ErrorManager.getDefault().notify(ioe);
206                                  } catch (CatalogModelException cme) {
207                                      ErrorManager.getDefault().notify(cme);
208                                  }
209                              } else {
210                                  path = RelativePath.getRelativePath(FileUtil.toFile(wsdlFileObj).getParentFile(), FileUtil.toFile(fo));
211                              }
212                          }
213                          if (path != null && (!existingLocationToNamespaceMap.containsKey(path) ||
214                                  existingLocationToNamespaceMap.get(path) == null ||
215                                  !existingLocationToNamespaceMap.get(path).equals(schemaTNS)))
216                          {
217                              locationToNamespaceMap.put(path, schemaTNS);
218                          }
219                      }
220                  }
221              }
222          }
223          
224          
225          for (String JavaDoc location : locationToNamespaceMap.keySet()) {
226              Import schemaImport =
227                  defaultInlineSchema.getModel().getFactory().createImport();
228              String JavaDoc namespace = locationToNamespaceMap.get(location);
229              schemaImport.setNamespace(namespace);
230              schemaImport.setSchemaLocation(location);
231              defaultInlineSchema.addExternalReference(schemaImport);
232              mImports.add(schemaImport);
233              
234          }
235          
236          if (wsdlSchema != null && !locationToNamespaceMap.isEmpty()) {
237              types.addExtensibilityElement(wsdlSchema);
238          }
239          
240         
241     }
242
243 }
244
Popular Tags