001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.xml.wsdl.ui.wizard;
043:
044: import java.io.IOException;
045: import java.net.URI;
046: import java.net.URISyntaxException;
047: import java.util.ArrayList;
048: import java.util.Collection;
049: import java.util.HashMap;
050: import java.util.List;
051: import java.util.Map;
052:
053: import javax.xml.XMLConstants;
054:
055: import org.netbeans.modules.xml.catalogsupport.DefaultProjectCatalogSupport;
056: import org.netbeans.modules.xml.schema.model.GlobalElement;
057: import org.netbeans.modules.xml.schema.model.GlobalType;
058: import org.netbeans.modules.xml.schema.model.Import;
059: import org.netbeans.modules.xml.schema.model.Schema;
060: import org.netbeans.modules.xml.schema.model.SchemaModel;
061: import org.netbeans.modules.xml.wsdl.model.Definitions;
062: import org.netbeans.modules.xml.wsdl.model.Types;
063: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
064: import org.netbeans.modules.xml.wsdl.model.extensions.xsd.WSDLSchema;
065: import org.netbeans.modules.xml.wsdl.ui.view.ElementOrType;
066: import org.netbeans.modules.xml.wsdl.ui.view.PartAndElementOrTypeTableModel;
067: import org.netbeans.modules.xml.wsdl.ui.view.PartAndElementOrTypeTableModel.PartAndElementOrType;
068: import org.netbeans.modules.xml.wsdl.ui.wsdl.util.RelativePath;
069: import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
070: import org.netbeans.modules.xml.xam.locator.CatalogModelException;
071: import org.openide.ErrorManager;
072: import org.openide.filesystems.FileObject;
073: import org.openide.filesystems.FileUtil;
074:
075: /**
076: *
077: * @author radval
078: */
079: public class SchemaImportsGenerator implements Command {
080:
081: private WSDLModel mModel;
082:
083: private Map mConfigurationMap;
084:
085: private Collection<Import> mImports = new ArrayList<Import>();
086:
087: /** Creates a new instance of OperationGenerator */
088: public SchemaImportsGenerator(WSDLModel model, Map configurationMap) {
089: this .mModel = model;
090: this .mConfigurationMap = configurationMap;
091: }
092:
093: public Collection<Import> getImports() {
094: return this .mImports;
095: }
096:
097: public void execute() {
098: if (mModel != null) {
099: List<PartAndElementOrTypeTableModel.PartAndElementOrType> inputMessageParts = (List<PartAndElementOrTypeTableModel.PartAndElementOrType>) this .mConfigurationMap
100: .get(WizardPortTypeConfigurationStep.OPERATION_INPUT);
101:
102: List<PartAndElementOrTypeTableModel.PartAndElementOrType> outputMessageParts = (List<PartAndElementOrTypeTableModel.PartAndElementOrType>) this .mConfigurationMap
103: .get(WizardPortTypeConfigurationStep.OPERATION_OUTPUT);
104:
105: List<PartAndElementOrTypeTableModel.PartAndElementOrType> faultMessageParts = (List<PartAndElementOrTypeTableModel.PartAndElementOrType>) this .mConfigurationMap
106: .get(WizardPortTypeConfigurationStep.OPERATION_FAULT);
107:
108: List<PartAndElementOrTypeTableModel.PartAndElementOrType> allParts = new ArrayList<PartAndElementOrTypeTableModel.PartAndElementOrType>();
109: if (inputMessageParts != null) {
110: allParts.addAll(inputMessageParts);
111: }
112: if (outputMessageParts != null) {
113: allParts.addAll(outputMessageParts);
114: }
115: if (faultMessageParts != null) {
116: allParts.addAll(faultMessageParts);
117: }
118:
119: Map<String, String> namespaceToPrefixMap = (Map) this .mConfigurationMap
120: .get(WizardPortTypeConfigurationStep.NAMESPACE_TO_PREFIX_MAP);
121: if (namespaceToPrefixMap != null) {
122: for (String namespace : namespaceToPrefixMap.keySet()) {
123: ((AbstractDocumentComponent) mModel
124: .getDefinitions()).addPrefix(
125: namespaceToPrefixMap.get(namespace),
126: namespace);
127: }
128: }
129: boolean fromWizard = false;
130: if (mConfigurationMap
131: .containsKey(WizardPortTypeConfigurationStep.IS_FROM_WIZARD)) {
132: fromWizard = true;
133: }
134:
135: processImports(allParts, fromWizard);
136: }
137:
138: }
139:
140: /* Similiar logic can be found in Utility.addSchemaImport(). So if there are changes here, also change in Utility*/
141: private void processImports(List<PartAndElementOrType> allParts,
142: boolean fromWizard) {
143: Map<String, String> locationToNamespaceMap = new HashMap<String, String>();
144: Map<String, String> existingLocationToNamespaceMap = new HashMap<String, String>();
145:
146: FileObject wsdlFileObj = mModel.getModelSource().getLookup()
147: .lookup(FileObject.class);
148: URI wsdlFileURI = FileUtil.toFile(wsdlFileObj).toURI();
149:
150: Definitions def = mModel.getDefinitions();
151: Types types = def.getTypes();
152: if (types == null) {
153: types = mModel.getFactory().createTypes();
154: def.setTypes(types);
155: }
156: Schema defaultInlineSchema = null;
157: String wsdlTNS = def.getTargetNamespace();
158: if (wsdlTNS != null) {
159: Collection<Schema> schmas = types.getSchemas();
160: if (schmas != null) {
161: for (Schema s : schmas) {
162: if (s.getTargetNamespace() != null
163: && s.getTargetNamespace().equals(wsdlTNS)) {
164: defaultInlineSchema = s;
165: break;
166: }
167: }
168: }
169: }
170:
171: WSDLSchema wsdlSchema = null;
172: if (defaultInlineSchema == null) {
173: wsdlSchema = mModel.getFactory().createWSDLSchema();
174: SchemaModel schemaModel = wsdlSchema.getSchemaModel();
175: defaultInlineSchema = schemaModel.getSchema();
176: defaultInlineSchema.setTargetNamespace(mModel
177: .getDefinitions().getTargetNamespace());
178: }
179:
180: //if any import with same namespace is present, dont import it.
181: Collection<Import> imports = defaultInlineSchema.getImports();
182: for (Import imp : imports) {
183: existingLocationToNamespaceMap.put(imp.getSchemaLocation(),
184: imp.getNamespace());
185: }
186:
187: if (!fromWizard) {
188: Collection<Schema> schemas = types.getSchemas();
189: if (schemas != null) {
190: for (Schema schema : schemas) {
191: Collection<Import> schemaImports = schema
192: .getImports();
193: for (Import imp : schemaImports) {
194: existingLocationToNamespaceMap.put(imp
195: .getSchemaLocation(), imp
196: .getNamespace());
197: }
198: }
199: }
200: }
201:
202: for (PartAndElementOrType part : allParts) {
203: ElementOrType eot = part.getElementOrType();
204: GlobalElement element = eot.getElement();
205: GlobalType type = eot.getType();
206: SchemaModel model = null;
207: if (element != null) {
208: model = element.getModel();
209: } else if (type != null) {
210: model = type.getModel();
211: }
212:
213: if (model != null) {
214: String schemaTNS = model.getSchema()
215: .getTargetNamespace();
216: if (schemaTNS != null
217: && !schemaTNS
218: .equals(XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
219:
220: FileObject fo = model.getModelSource().getLookup()
221: .lookup(FileObject.class);
222:
223: if (fo != null) {
224: String path = null;
225:
226: if (fromWizard) {
227: // generate absolute URI, this will get changed later in wizard post process import
228: path = FileUtil.toFile(fo).toURI()
229: .toString();
230: } else if (!FileUtil.toFile(fo).toURI().equals(
231: wsdlFileURI)) {
232: //should be different files. in case of inline schemas.
233: DefaultProjectCatalogSupport catalogSupport = DefaultProjectCatalogSupport
234: .getInstance(wsdlFileObj);
235: if (catalogSupport.needsCatalogEntry(
236: wsdlFileObj, fo)) {
237: // Remove the previous catalog entry, then create new one.
238: URI uri;
239: try {
240: uri = catalogSupport
241: .getReferenceURI(
242: wsdlFileObj, fo);
243: catalogSupport
244: .removeCatalogEntry(uri);
245: catalogSupport.createCatalogEntry(
246: wsdlFileObj, fo);
247: path = catalogSupport
248: .getReferenceURI(
249: wsdlFileObj, fo)
250: .toString();
251: } catch (URISyntaxException use) {
252: ErrorManager.getDefault().notify(
253: use);
254: } catch (IOException ioe) {
255: ErrorManager.getDefault().notify(
256: ioe);
257: } catch (CatalogModelException cme) {
258: ErrorManager.getDefault().notify(
259: cme);
260: }
261: } else {
262: path = RelativePath.getRelativePath(
263: FileUtil.toFile(wsdlFileObj)
264: .getParentFile(),
265: FileUtil.toFile(fo));
266: }
267: }
268: if (path != null
269: && (!existingLocationToNamespaceMap
270: .containsKey(path)
271: || existingLocationToNamespaceMap
272: .get(path) == null || !existingLocationToNamespaceMap
273: .get(path).equals(schemaTNS))) {
274: locationToNamespaceMap.put(path, schemaTNS);
275: }
276: }
277: }
278: }
279: }
280:
281: for (String location : locationToNamespaceMap.keySet()) {
282: Import schemaImport = defaultInlineSchema.getModel()
283: .getFactory().createImport();
284: String namespace = locationToNamespaceMap.get(location);
285: schemaImport.setNamespace(namespace);
286: schemaImport.setSchemaLocation(location);
287: defaultInlineSchema.addExternalReference(schemaImport);
288: mImports.add(schemaImport);
289:
290: }
291:
292: if (wsdlSchema != null && !locationToNamespaceMap.isEmpty()) {
293: types.addExtensibilityElement(wsdlSchema);
294: }
295:
296: }
297:
298: }
|