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.ArrayList;
045: import java.util.Collection;
046: import java.util.Collections;
047: import java.util.HashMap;
048: import java.util.List;
049: import java.util.Map;
050: import java.util.Set;
051: import java.util.logging.Level;
052: import java.util.logging.Logger;
053: import javax.xml.namespace.QName;
054: import org.netbeans.modules.xml.schema.model.Schema;
055: import org.netbeans.modules.xml.schema.model.SchemaModel;
056: import org.netbeans.modules.xml.wsdl.model.spi.GenericExtensibilityElement;
057: import org.netbeans.modules.xml.xam.ModelSource;
058: import org.netbeans.modules.xml.wsdl.model.Definitions;
059: import org.netbeans.modules.xml.wsdl.model.Documentation;
060: import org.netbeans.modules.xml.wsdl.model.ExtensibilityElement;
061: import org.netbeans.modules.xml.wsdl.model.Import;
062: import org.netbeans.modules.xml.wsdl.model.ReferenceableWSDLComponent;
063: import org.netbeans.modules.xml.wsdl.model.Types;
064: import org.netbeans.modules.xml.wsdl.model.WSDLComponent;
065: import org.netbeans.modules.xml.wsdl.model.WSDLComponentFactory;
066: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
067: import org.netbeans.modules.xml.wsdl.model.extensions.xsd.WSDLSchema;
068: import org.netbeans.modules.xml.wsdl.model.visitor.FindReferencedVisitor;
069: import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
070: import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel;
071: import org.netbeans.modules.xml.xam.dom.DocumentComponent;
072: import org.netbeans.modules.xml.xam.ComponentUpdater;
073: import org.netbeans.modules.xml.xam.dom.ChangeInfo;
074: import org.netbeans.modules.xml.xam.dom.DocumentModelAccess;
075: import org.netbeans.modules.xml.xam.dom.SyncUnit;
076: import org.w3c.dom.Element;
077: import org.w3c.dom.Node;
078:
079: /**
080: *
081: * @author rico
082: * @author Nam Nguyen
083: */
084: public class WSDLModelImpl extends WSDLModel {
085: private Definitions definitions;
086: private WSDLComponentFactory wcf;
087:
088: public WSDLModelImpl(ModelSource source) {
089: super (source);
090: wcf = new WSDLComponentFactoryImpl(this );
091: }
092:
093: public WSDLComponent createRootComponent(Element root) {
094: DefinitionsImpl newDefinitions = null;
095: QName q = root == null ? null : AbstractDocumentComponent
096: .getQName(root);
097: if (root != null && WSDLQNames.DEFINITIONS.getQName().equals(q)) {
098: newDefinitions = new DefinitionsImpl(this , root);
099: setDefinitions(newDefinitions);
100: } else {
101: return null;
102: }
103:
104: return getDefinitions();
105: }
106:
107: public WSDLComponent getRootComponent() {
108: return definitions;
109: }
110:
111: public WSDLComponent createComponent(WSDLComponent parent,
112: Element element) {
113: return getFactory().create(element, parent);
114: }
115:
116: protected ComponentUpdater<WSDLComponent> getComponentUpdater() {
117: return new ChildComponentUpdateVisitor<WSDLComponent>();
118: }
119:
120: public WSDLComponentFactory getFactory() {
121: return wcf;
122: }
123:
124: public void setDefinitions(Definitions def) {
125: assert (def instanceof DefinitionsImpl);
126: definitions = DefinitionsImpl.class.cast(def);
127: }
128:
129: public Definitions getDefinitions() {
130: return definitions;
131: }
132:
133: ElementFactoryRegistry getElementRegistry() {
134: return ElementFactoryRegistry.getDefault();
135: }
136:
137: public List<WSDLModel> getImportedWSDLModels() {
138: List<WSDLModel> ret = new ArrayList<WSDLModel>();
139: Collection<Import> imports = getDefinitions().getImports();
140: for (Import i : imports) {
141: try {
142: WSDLModel m = i.getImportedWSDLModel();
143: if (m != null) {
144: ret.add(m);
145: }
146: } catch (Exception e) {
147: Logger.getLogger(this .getClass().getName()).log(
148: Level.FINE, "getImportedWSDLModels", e);
149: }
150: }
151: return ret;
152: }
153:
154: public List<SchemaModel> getImportedSchemaModels() {
155: List<SchemaModel> ret = new ArrayList<SchemaModel>();
156: Collection<Import> imports = getDefinitions().getImports();
157: for (Import i : imports) {
158: try {
159: SchemaModel m = ((ImportImpl) i).resolveToSchemaModel();
160: if (m != null) {
161: ret.add(m);
162: }
163: } catch (Exception e) {
164: Logger.getLogger(this .getClass().getName()).log(
165: Level.FINE, "getImportedSchemaModels", e); //NOI18N
166: }
167: }
168: return ret;
169: }
170:
171: public List<SchemaModel> getEmbeddedSchemaModels() {
172: List<SchemaModel> ret = new ArrayList<SchemaModel>();
173: Types types = getDefinitions().getTypes();
174: List<WSDLSchema> embeddedSchemas = Collections.emptyList();
175: if (types != null) {
176: embeddedSchemas = types
177: .getExtensibilityElements(WSDLSchema.class);
178: }
179: for (WSDLSchema wschema : embeddedSchemas) {
180: ret.add(wschema.getSchemaModel());
181: }
182: return ret;
183: }
184:
185: public List<WSDLModel> findWSDLModel(String namespace) {
186: if (namespace == null) {
187: return Collections.emptyList();
188: }
189:
190: List<WSDLModel> models = getImportedWSDLModels();
191: models.add(0, this );
192:
193: List<WSDLModel> ret = new ArrayList<WSDLModel>();
194: for (WSDLModel m : models) {
195: String targetNamespace = m.getDefinitions()
196: .getTargetNamespace();
197: if (namespace.equals(targetNamespace)) {
198: ret.add(m);
199: }
200: }
201: return ret;
202: }
203:
204: public List<Schema> findSchemas(String namespace) {
205: List<Schema> ret = new ArrayList<Schema>();
206: for (SchemaModel sm : getEmbeddedSchemaModels()) {
207: try {
208: ret.addAll(sm.findSchemas(namespace));
209: } catch (Exception ex) {
210: Logger.getLogger(this .getClass().getName()).log(
211: Level.FINE, "findSchemas", ex);
212: }
213: }
214: SchemaModel sm = findSchemaModelFromImports(namespace);
215: if (sm != null) {
216: ret.add(sm.getSchema());
217: }
218: return ret;
219: }
220:
221: private SchemaModel findSchemaModelFromImports(String namespace) {
222: if (namespace == null) {
223: return null;
224: }
225:
226: List<SchemaModel> models = getImportedSchemaModels();
227: for (SchemaModel m : models) {
228: String targetNamespace = m.getSchema().getTargetNamespace();
229: if (namespace.equals(targetNamespace)) {
230: return m;
231: }
232: }
233: return null;
234: }
235:
236: public <T extends ReferenceableWSDLComponent> T findComponentByName(
237: String name, Class<T> type) {
238: return type.cast(new FindReferencedVisitor(getDefinitions())
239: .find(name, type));
240: }
241:
242: public <T extends ReferenceableWSDLComponent> T findComponentByName(
243: QName name, Class<T> type) {
244: String namespace = name.getNamespaceURI();
245: if (namespace == null) {
246: return findComponentByName(name.getLocalPart(), type);
247: } else {
248: for (WSDLModel targetModel : findWSDLModel(namespace)) {
249: T found = targetModel.findComponentByName(name
250: .getLocalPart(), type);
251: if (found != null) {
252: return found;
253: }
254: }
255: }
256: return null;
257: }
258:
259: public Set<QName> getQNames() {
260: return getElementRegistry().getKnownQNames();
261: }
262:
263: public Set<String> getElementNames() {
264: return getElementRegistry().getKnownElementNames();
265: }
266:
267: public ChangeInfo prepareChangeInfo(List<Node> pathToRoot) {
268: ChangeInfo change = super .prepareChangeInfo(pathToRoot);
269: DocumentComponent parentComponent = findComponent(change
270: .getRootToParentPath());
271: if (parentComponent == null) {
272: return change;
273: }
274: if (!(parentComponent.getModel() instanceof WSDLModel)) {
275: getElementRegistry().addEmbeddedModelQNames(
276: (AbstractDocumentModel) parentComponent.getModel());
277: change = super .prepareChangeInfo(pathToRoot);
278: } else if (isDomainElement(parentComponent.getPeer())
279: && !change.isDomainElement()
280: && change.getChangedElement() != null) {
281: if (change.getOtherNonDomainElementNodes() == null
282: || change.getOtherNonDomainElementNodes().isEmpty()) {
283: // case add or remove generic extensibility element
284: change.setDomainElement(true);
285: change.setParentComponent(null);
286: } else if (!(parentComponent instanceof Documentation)) {
287: List<Element> rootToChanged = new ArrayList<Element>(
288: change.getRootToParentPath());
289: rootToChanged.add(change.getChangedElement());
290: DocumentComponent changedComponent = findComponent(rootToChanged);
291: if (changedComponent != null
292: && changedComponent
293: .getClass()
294: .isAssignableFrom(
295: GenericExtensibilityElement.class)) {
296: // case generic extensibility element changed
297: change.markNonDomainChildAsChanged();
298: change.setParentComponent(null);
299: }
300: }
301: } else {
302: change.setParentComponent(parentComponent);
303: }
304: return change;
305: }
306:
307: public SyncUnit prepareSyncUnit(ChangeInfo changes, SyncUnit unit) {
308: unit = super .prepareSyncUnit(changes, unit);
309: if (unit != null) {
310: return new SyncReviewVisitor().review(unit);
311: }
312: return null;
313: }
314:
315: public AbstractDocumentComponent findComponent(
316: AbstractDocumentComponent current,
317: List<org.w3c.dom.Element> pathFromRoot, int iCurrent) {
318:
319: if (current instanceof ExtensibilityElement.EmbeddedModel) {
320: ExtensibilityElement.EmbeddedModel emb = (ExtensibilityElement.EmbeddedModel) current;
321: AbstractDocumentModel axm = (AbstractDocumentModel) emb
322: .getEmbeddedModel();
323: AbstractDocumentComponent embedded = (AbstractDocumentComponent) axm
324: .getRootComponent();
325: return axm.findComponent(embedded, pathFromRoot, iCurrent);
326: } else {
327: return super .findComponent(current, pathFromRoot, iCurrent);
328: }
329: }
330:
331: @Override
332: public Map<QName, List<QName>> getQNameValuedAttributes() {
333: return WSDLAttribute.getQNameValuedAttributes();
334: }
335:
336: }
|