KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > model > extensions > xsd > impl > SchemaReferenceImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.wsdl.model.extensions.xsd.impl;
21
22 import javax.xml.XMLConstants JavaDoc;
23 import org.netbeans.modules.xml.schema.model.ReferenceableSchemaComponent;
24 import org.netbeans.modules.xml.schema.model.Schema;
25 import org.netbeans.modules.xml.schema.model.SchemaModel;
26 import org.netbeans.modules.xml.schema.model.SchemaModelFactory;
27 import org.netbeans.modules.xml.wsdl.model.Import;
28 import org.netbeans.modules.xml.wsdl.model.Types;
29 import org.netbeans.modules.xml.wsdl.model.impl.ImportImpl;
30 import org.netbeans.modules.xml.wsdl.model.spi.WSDLComponentBase;
31 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
32 import org.netbeans.modules.xml.xam.dom.AbstractNamedComponentReference;
33 import org.netbeans.modules.xml.xam.dom.DocumentModel;
34 import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
35 import org.netbeans.modules.xml.xam.locator.CatalogModelException;
36
37 /**
38  *
39  * @author Nam Nguyen
40  */

41 public class SchemaReferenceImpl<T extends ReferenceableSchemaComponent>
42         extends AbstractNamedComponentReference<T> implements NamedComponentReference<T> {
43     
44     /** Creates a new instance of SchemaReferenceImpl */
45     public SchemaReferenceImpl(
46             T referenced,
47             Class JavaDoc<T> type,
48             AbstractDocumentComponent parent) {
49         super(referenced, type, parent);
50     }
51     
52     //for use by resolve methods
53
public SchemaReferenceImpl(Class JavaDoc<T> type, AbstractDocumentComponent parent, String JavaDoc refString){
54         super(type, parent, refString);
55     }
56
57     public T get() {
58         if (getReferenced() == null) {
59             String JavaDoc localName = getLocalName();
60             String JavaDoc namespace = getEffectiveNamespace();
61             T target = null;
62
63             if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(namespace)) {
64                 SchemaModel primitiveModel = SchemaModelFactory.getDefault().getPrimitiveTypesModel();
65                 target = primitiveModel.resolve(namespace, localName, getType());
66             } else {
67                 Types types = getParent().getModel().getDefinitions().getTypes();
68                 if (types != null) {
69                     for (Schema s : types.getSchemas()) {
70                         target = s.getModel().resolve(namespace, localName, getType());
71                         if (target != null) {
72                             break;
73                         }
74                     }
75                 }
76                 if (target == null) {
77                     for (Import i : getParent().getModel().getDefinitions().getImports()) {
78                         DocumentModel m = null;
79                         try {
80                             m = ((ImportImpl)i).resolveImportedModel();
81                         } catch(CatalogModelException ex) {
82                             // checked for null so ignore
83
}
84                         if (m instanceof SchemaModel) {
85                             target = ((SchemaModel)m).resolve(namespace, localName, getType());
86                         }
87                         if (target != null) {
88                             break;
89                         }
90                     }
91                 }
92             }
93             
94             if (target != null) {
95                 setReferenced(target);
96             }
97         }
98         return getReferenced();
99     }
100
101     public WSDLComponentBase getParent() {
102         return (WSDLComponentBase) super.getParent();
103     }
104     
105     public String JavaDoc getEffectiveNamespace() {
106         if (refString == null) {
107             assert getReferenced() != null;
108             return getReferenced().getModel().getSchema().getTargetNamespace();
109         } else {
110             if (getPrefix() == null) {
111                 return null;
112             } else {
113                 return getParent().lookupNamespaceURI(getPrefix());
114             }
115         }
116     }
117 }
118
Popular Tags