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.model.api.support;
020:
021: import java.net.URI;
022: import java.net.URISyntaxException;
023: import java.util.Collection;
024: import java.util.LinkedList;
025:
026: import org.netbeans.modules.bpel.model.api.BpelModel;
027: import org.netbeans.modules.bpel.model.api.Import;
028: import org.netbeans.modules.xml.schema.model.Schema;
029: import org.netbeans.modules.xml.schema.model.SchemaModel;
030: import org.netbeans.modules.xml.schema.model.SchemaModelFactory;
031: import org.netbeans.modules.xml.wsdl.model.Types;
032: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
033: import org.netbeans.modules.xml.wsdl.model.WSDLModelFactory;
034: import org.netbeans.modules.xml.xam.Model;
035: import org.netbeans.modules.xml.xam.ModelSource;
036: import org.netbeans.modules.xml.xam.locator.CatalogModelException;
037: import org.netbeans.modules.xml.xam.locator.CatalogModelFactory;
038:
039: /**
040: * Helper class for accessing to WSDL or XSD models by import statement.
041: * @author ads
042: */
043: public final class ImportHelper {
044:
045: /**
046: * This is helper class, we don't want its instance.
047: */
048: private ImportHelper() {
049: }
050:
051: /**
052: * Returns wsdl model respectively given import <code>imp</code>.
053: * @param model BPEL OM
054: * @param location location uri
055: * @param importType type of import
056: *
057: */
058: public static WSDLModel getWsdlModel(BpelModel model,
059: String location, String importType) {
060: return getWsdlModel(model, location, importType, true);
061: }
062:
063: /**
064: * Returns wsdl model respectively given import <code>imp</code>.
065: * @param model BPEL OM
066: * @param location location uri
067: * @param importType type of import
068: * @param checkWellFormed if true method will return null if model is not valid
069: */
070: public static WSDLModel getWsdlModel(BpelModel model,
071: String location, String importType, boolean checkWellFormed)
072:
073: {
074: if (!Import.WSDL_IMPORT_TYPE.equals(importType)) {
075: return null;
076: }
077: WSDLModel wsdlModel;
078: if (location == null) {
079: return null;
080: }
081: try {
082: URI uri = new URI(location);
083: ModelSource source = CatalogModelFactory.getDefault()
084: .getCatalogModel(model.getModelSource())
085: .getModelSource(uri, model.getModelSource());
086: wsdlModel = WSDLModelFactory.getDefault().getModel(source);
087: } catch (URISyntaxException e) {
088: wsdlModel = null;
089: } catch (CatalogModelException e) {
090: wsdlModel = null;
091: }
092: if (wsdlModel != null
093: && wsdlModel.getState() == Model.State.NOT_WELL_FORMED
094: && checkWellFormed) {
095: return null;
096: }
097: return wsdlModel;
098: }
099:
100: /**
101: * Returns wsdl model respectively given import <code>imp</code>.
102: * @param imp import statement in BPEL OM
103: */
104: public static WSDLModel getWsdlModel(Import imp) {
105: return getWsdlModel(imp, true);
106: }
107:
108: /**
109: * Returns wsdl model respectively given import <code>imp</code>.
110: * @param imp import statement in BPEL OM
111: * @param checkWellFormed if true method will return null if model is not valid
112: */
113: public static WSDLModel getWsdlModel(Import imp,
114: boolean checkWellFormed) {
115: return getWsdlModel(imp.getBpelModel(), imp.getLocation(), imp
116: .getImportType(), checkWellFormed);
117: }
118:
119: /**
120: * Returns schema model respectively <code>imp</code> import
121: * statement in BPEL OM and <code>namespace</code>.
122: * @param bpelModel BPEL OM
123: * @param location location uri
124: * @param importType type of import
125: * @param namespace schema namespace
126: */
127: public static Collection<SchemaModel> getInlineSchema(
128: BpelModel bpelModel, String namespace, String location,
129: String importType) {
130: WSDLModel model = getWsdlModel(bpelModel, location, importType);
131: if (model == null) {
132: return null;
133: }
134: Types types = model.getDefinitions().getTypes();
135: if (types == null) {
136: return null;
137: }
138: Collection<Schema> collection = types.getSchemas();
139: Collection<SchemaModel> models = null;
140: for (Schema schema : collection) {
141: if (namespace.equals(schema.getTargetNamespace())) {
142: if (models == null) {
143: models = new LinkedList<SchemaModel>();
144: }
145: models.add(schema.getModel());
146: }
147: }
148: return models;
149: }
150:
151: /**
152: * Returns schema model respectively <code>imp</code> import
153: * statement in BPEL OM and <code>namespace</code>.
154: * @param imp import statement in BPEL OM
155: * @param namespace Namespace for desired inline schema model
156: */
157: public static Collection<SchemaModel> getInlineSchema(Import imp,
158: String namespace) {
159: return getInlineSchema(imp.getBpelModel(), namespace, imp
160: .getLocation(), imp.getImportType());
161: }
162:
163: /**
164: * Returns schema model respectively given import <code>imp</code>.
165: * @param model BPEL OM
166: * @param location location uri
167: * @param importType type of import
168: */
169: public static SchemaModel getSchemaModel(BpelModel model,
170: String location, String importType) {
171: return getSchemaModel(model, location, importType, true);
172: }
173:
174: /**
175: * Returns schema model respectively given import <code>imp</code>.
176: * @param model BPEL OM
177: * @param location location uri
178: * @param importType type of import
179: */
180: public static SchemaModel getSchemaModel(BpelModel model,
181: String location, String importType, boolean checkWellFormed) {
182: if (!Import.SCHEMA_IMPORT_TYPE.equals(importType)) {
183: return null;
184: }
185: SchemaModel schemaModel;
186: if (location == null) {
187: return null;
188: }
189: try {
190: URI uri = new URI(location);
191: ModelSource modelSource = CatalogModelFactory.getDefault()
192: .getCatalogModel(model.getModelSource())
193: .getModelSource(uri, model.getModelSource());
194:
195: schemaModel = SchemaModelFactory.getDefault().getModel(
196: modelSource);
197: } catch (URISyntaxException e) {
198: schemaModel = null;
199: } catch (CatalogModelException e) {
200: schemaModel = null;
201: }
202: if (schemaModel != null
203: && schemaModel.getState() == Model.State.NOT_WELL_FORMED
204: && checkWellFormed) {
205: schemaModel = null;
206: }
207: return schemaModel;
208: }
209:
210: /**
211: * Returns schema model respectively given import <code>imp</code>.
212: * @param imp import statement in BPEL OM
213: */
214: public static SchemaModel getSchemaModel(Import imp,
215: boolean checkWellFormed) {
216: return getSchemaModel(imp.getBpelModel(), imp.getLocation(),
217: imp.getImportType(), checkWellFormed);
218: }
219:
220: /**
221: * Returns schema model respectively given import <code>imp</code>.
222: * @param imp import statement in BPEL OM
223: */
224: public static SchemaModel getSchemaModel(Import imp) {
225: return getSchemaModel(imp.getBpelModel(), imp.getLocation(),
226: imp.getImportType());
227: }
228: }
|