1 19 20 package org.netbeans.modules.xml.wsdl.ui.wizard; 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.List ; 29 import java.util.Map ; 30 31 import javax.xml.XMLConstants ; 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 57 public class SchemaImportsGenerator implements Command { 58 59 private WSDLModel mModel; 60 61 private Map mConfigurationMap; 62 63 private Collection <Import> mImports = new ArrayList <Import>(); 64 65 66 public SchemaImportsGenerator(WSDLModel model, Map configurationMap) { 67 this.mModel = model; 68 this.mConfigurationMap = configurationMap; 69 } 70 71 public Collection <Import> getImports() { 72 return this.mImports; 73 } 74 75 public void execute() { 76 if(mModel != null) { 77 List <PartAndElementOrTypeTableModel.PartAndElementOrType> inputMessageParts = 78 (List <PartAndElementOrTypeTableModel.PartAndElementOrType>) this.mConfigurationMap.get(WizardPortTypeConfigurationStep.OPERATION_INPUT); 79 80 List <PartAndElementOrTypeTableModel.PartAndElementOrType> outputMessageParts = 81 (List <PartAndElementOrTypeTableModel.PartAndElementOrType>) this.mConfigurationMap.get(WizardPortTypeConfigurationStep.OPERATION_OUTPUT); 82 83 List <PartAndElementOrTypeTableModel.PartAndElementOrType> faultMessageParts = 84 (List <PartAndElementOrTypeTableModel.PartAndElementOrType>) this.mConfigurationMap.get(WizardPortTypeConfigurationStep.OPERATION_FAULT); 85 86 List <PartAndElementOrTypeTableModel.PartAndElementOrType> allParts = new ArrayList <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 <String , String > namespaceToPrefixMap = (Map ) this.mConfigurationMap.get(WizardPortTypeConfigurationStep.NAMESPACE_TO_PREFIX_MAP); 98 if (namespaceToPrefixMap != null) { 99 for (String 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 114 private void processImports(List <PartAndElementOrType> allParts, boolean fromWizard) { 115 Map <String , String > locationToNamespaceMap = new HashMap <String , String >(); 116 Map <String , String > existingLocationToNamespaceMap = new HashMap <String , String >(); 117 118 FileObject wsdlFileObj = (FileObject) mModel.getModelSource().getLookup().lookup(FileObject.class); 119 URI 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 wsdlTNS = def.getTargetNamespace(); 129 if (wsdlTNS != null) { 130 Collection <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 Collection <Import> imports = defaultInlineSchema.getImports(); 151 for (Import imp : imports) { 152 existingLocationToNamespaceMap.put(imp.getSchemaLocation(), imp.getNamespace()); 153 } 154 155 if (!fromWizard) { 156 Collection <Schema> schemas = types.getSchemas(); 157 if (schemas != null) { 158 for (Schema schema : schemas) { 159 Collection <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 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 path = null; 187 188 if (fromWizard) { 189 path = FileUtil.toFile(fo).toURI().toString(); 191 } else if (!FileUtil.toFile(fo).toURI().equals(wsdlFileURI)) { 192 DefaultProjectCatalogSupport catalogSupport = DefaultProjectCatalogSupport.getInstance(wsdlFileObj); 194 if (catalogSupport.needsCatalogEntry(wsdlFileObj, fo)) { 195 URI 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 use) { 203 ErrorManager.getDefault().notify(use); 204 } catch (IOException 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 location : locationToNamespaceMap.keySet()) { 226 Import schemaImport = 227 defaultInlineSchema.getModel().getFactory().createImport(); 228 String 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 |