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-2006 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.model.impl;
043:
044: import java.util.Locale;
045: import org.netbeans.modules.xml.schema.model.SchemaModel;
046: import org.netbeans.modules.xml.schema.model.SchemaModelFactory;
047: import org.netbeans.modules.xml.wsdl.model.Import;
048: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
049: import org.netbeans.modules.xml.wsdl.model.WSDLModelFactory;
050: import org.netbeans.modules.xml.wsdl.model.spi.WSDLComponentBase;
051: import org.netbeans.modules.xml.wsdl.model.visitor.WSDLVisitor;
052: import org.netbeans.modules.xml.xam.dom.DocumentModel;
053: import org.netbeans.modules.xml.xam.locator.CatalogModelException;
054: import org.netbeans.modules.xml.xam.ModelSource;
055: import org.openide.util.NbBundle;
056: import org.w3c.dom.Element;
057:
058: /**
059: *
060: * @author Nam Nguyen
061: */
062: public class ImportImpl extends WSDLComponentBase implements Import {
063:
064: /** Creates a new instance of ImportImpl */
065: public ImportImpl(WSDLModel model, Element e) {
066: super (model, e);
067: }
068:
069: public ImportImpl(WSDLModel model) {
070: this (model, createNewElement(WSDLQNames.IMPORT.getQName(),
071: model));
072: }
073:
074: public void accept(WSDLVisitor visitor) {
075: visitor.visit(this );
076: }
077:
078: public void setNamespace(String namespaceURI) {
079: setAttribute(NAMESPACE_URI_PROPERTY,
080: WSDLAttribute.NAMESPACE_URI, namespaceURI);
081: }
082:
083: public void setLocation(String locationURI) {
084: setAttribute(LOCATION_PROPERTY, WSDLAttribute.LOCATION,
085: locationURI);
086: }
087:
088: public String getNamespace() {
089: return getAttribute(WSDLAttribute.NAMESPACE_URI);
090: }
091:
092: public String getLocation() {
093: return getAttribute(WSDLAttribute.LOCATION);
094: }
095:
096: public WSDLModel getImportedWSDLModel()
097: throws CatalogModelException {
098: DocumentModel m = resolveImportedModel();
099: if (m instanceof WSDLModel) {
100: return (WSDLModel) m;
101: } else {
102: String msg = NbBundle.getMessage(ImportImpl.class,
103: "MSG_CANNOT_LOAD_WSDL", getLocation());
104: throw new CatalogModelException(msg);
105: }
106: }
107:
108: public WSDLModel resolveToWSDLModel() throws CatalogModelException {
109: DocumentModel m = resolveImportedModel();
110: if (m instanceof WSDLModel) {
111: return (WSDLModel) m;
112: } else {
113: return null;
114: }
115: }
116:
117: public SchemaModel resolveToSchemaModel()
118: throws CatalogModelException {
119: DocumentModel m = resolveImportedModel();
120: if (m instanceof SchemaModel) {
121: return (SchemaModel) m;
122: } else {
123: return null;
124: }
125: }
126:
127: public DocumentModel resolveImportedModel()
128: throws CatalogModelException {
129: ModelSource ms = resolveModel(getLocation());
130:
131: String location = getLocation().toLowerCase(Locale.US);
132: if (location.endsWith(".wsdl")) { //NOI18N
133: return loadAsWSDL(ms);
134: } else if (location.endsWith(".xsd")) { //NOI18N
135: return loadAsSchema(ms);
136: } else {
137: DocumentModel m = loadAsWSDL(ms);
138: if (m == null) {
139: m = loadAsSchema(ms);
140: }
141: return m;
142: }
143: }
144:
145: private WSDLModel loadAsWSDL(ModelSource ms) {
146: WSDLModel m = WSDLModelFactory.getDefault().getModel(ms);
147: if (m != null
148: && m.getState() == DocumentModel.State.NOT_WELL_FORMED) {
149: return null;
150: }
151: return m;
152: }
153:
154: private SchemaModel loadAsSchema(ModelSource ms) {
155: SchemaModel m = SchemaModelFactory.getDefault().getModel(ms);
156: if (m != null
157: && m.getState() == DocumentModel.State.NOT_WELL_FORMED) {
158: return null;
159: }
160: return m;
161: }
162: }
|