001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019: package org.netbeans.modules.bpel.properties;
020:
021: import java.util.Collection;
022: import org.netbeans.modules.bpel.core.BPELCatalog;
023: import org.netbeans.modules.bpel.model.api.BpelModel;
024: import org.netbeans.modules.bpel.model.api.Import;
025: import org.netbeans.modules.bpel.model.api.Process;
026: import org.netbeans.modules.bpel.model.api.events.VetoException;
027: import org.netbeans.modules.bpel.properties.Constants.StandardImportType;
028: import org.netbeans.modules.xml.schema.model.Schema;
029: import org.netbeans.modules.xml.schema.model.SchemaModel;
030: import org.netbeans.modules.xml.wsdl.model.Definitions;
031: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
032:
033: import org.netbeans.modules.xml.xam.Model;
034: import org.netbeans.modules.xml.xam.locator.CatalogModelException;
035: import org.openide.ErrorManager;
036:
037: import org.openide.filesystems.FileObject;
038:
039: /**
040: *
041: * @author Alexey
042: */
043: public class ImportRegistrationHelper {
044: public ImportRegistrationHelper(BpelModel model) {
045: this .model = model;
046: }
047:
048: public void addImport(Model imp_model) {
049: //System.out.println();
050: //System.out.println("ADD INPORT");
051: addImport(createImport(imp_model));
052:
053: if (!(imp_model instanceof WSDLModel)) {
054: return;
055: }
056: Definitions defs = ((WSDLModel) imp_model).getDefinitions();
057:
058: if (defs == null) {
059: return;
060: }
061: Collection<org.netbeans.modules.xml.wsdl.model.Import> imps = defs
062: .getImports();
063:
064: if (imps == null) {
065: return;
066: }
067: for (org.netbeans.modules.xml.wsdl.model.Import i : imps) {
068: try {
069: // check if the imported model is itself #87107
070: Model tmpImpModel = i.getImportedWSDLModel();
071:
072: if (tmpImpModel == null
073: || tmpImpModel.equals(i.getModel())) {
074: continue;
075: }
076: if (!ResolverUtility
077: .isModelImported(tmpImpModel, model)) {
078: addImport(tmpImpModel);
079: }
080: } catch (CatalogModelException e) {
081: //Just ignore this type of exception
082: }
083: }
084: }
085:
086: public void addImport(Import new_imp) {
087: //System.out.println();
088: //System.out.println("add import: " + new_imp);
089: if (new_imp == null) {
090: return;
091: }
092: Process process = model.getProcess();
093: //System.out.println("process: " + process);
094:
095: if (process == null) {
096: return;
097: }
098: if (!isImported(new_imp)) {
099: //System.out.println("ADD !!!");
100: process.addImport(new_imp);
101: }
102: }
103:
104: public Import createImport(Model model) {
105: FileObject modelFo = Util.getFileObjectByModel(model);
106: if (modelFo != null) {
107: return createImport(modelFo);
108: }
109: // may be this model is known by global catalog
110: BPELCatalog bpelCatalog = BPELCatalog.getDefault();
111: String namespace = null;
112: StandardImportType importType = null;
113: if (model instanceof SchemaModel) {
114: Schema schema = ((SchemaModel) model).getSchema();
115: namespace = schema == null ? null : schema
116: .getTargetNamespace();
117: importType = StandardImportType.IMPORT_SCHEMA;
118: } else if (model instanceof WSDLModel) {
119: Definitions defs = ((WSDLModel) model).getDefinitions();
120: namespace = defs == null ? null : defs.getTargetNamespace();
121: importType = StandardImportType.IMPORT_WSDL;
122: }
123:
124: String modelUri = null;
125: if (namespace != null) {
126: modelUri = bpelCatalog.resolveURI(namespace);
127: }
128:
129: if (modelUri != null && importType != null) {
130: return createImport(namespace, modelUri, importType
131: .getImportType());
132: }
133:
134: return null;
135: }
136:
137: public Import createImport(FileObject fo) {
138: if (fo != null) {
139: StandardImportType importType = StandardImportType
140: .forExt(fo.getExt());
141:
142: return createImport(Util.getNewModelNamespace(fo,
143: importType), Util.getNewModelLocation(model, fo),
144: importType.getImportType());
145: }
146: return null;
147: }
148:
149: public Import createImport(String namespace, String location,
150: String type) {
151: Import imp = model.getBuilder().createImport();
152: if (namespace != null) {
153: try {
154: imp.setNamespace(namespace);
155: } catch (VetoException ex) {
156: ErrorManager.getDefault().notify(
157: ErrorManager.INFORMATIONAL, ex);
158: }
159: }
160:
161: if (location != null) {
162: try {
163: //Fix for IZ84824
164: location = ResolverUtility.encodeLocation(location);
165:
166: imp.setLocation(location);
167: } catch (VetoException ex) {
168: ErrorManager.getDefault().notify(
169: ErrorManager.INFORMATIONAL, ex);
170: }
171: }
172:
173: if (type != null) {
174: try {
175: imp.setImportType(type);
176: } catch (VetoException ex) {
177: ErrorManager.getDefault().notify(
178: ErrorManager.INFORMATIONAL, ex);
179: }
180: }
181: return imp;
182: }
183:
184: private boolean isImported(Import new_imp) {
185: Process process = model.getProcess();
186:
187: if (process == null) {
188: return false;
189: }
190: Import[] imports = process.getImports();
191:
192: if (imports == null) {
193: return false;
194: }
195: String namespace = new_imp.getNamespace();
196: String location = ResolverUtility.decodeLocation(new_imp
197: .getLocation());
198: String type = new_imp.getImportType();
199:
200: for (Import imp : imports) {
201: if (namespace != null
202: && !namespace.equals(imp.getNamespace())) {
203: continue;
204: }
205: if (location != null
206: && !location.equals(ResolverUtility
207: .decodeLocation(imp.getLocation()))) {
208: continue;
209: }
210: if (type != null && !type.equals(imp.getImportType())) {
211: continue;
212: }
213: return true;
214: }
215: return false;
216: }
217:
218: private BpelModel model;
219: }
|