KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xslt > model > impl > GlobalReferenceImpl


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xslt.model.impl;
20
21
22 import java.util.LinkedHashSet JavaDoc;
23 import java.util.List JavaDoc;
24
25 import javax.xml.namespace.QName JavaDoc;
26
27 import org.netbeans.modules.xml.xam.AbstractReference;
28 import org.netbeans.modules.xml.xam.Model;
29 import org.netbeans.modules.xslt.model.QualifiedNameable;
30 import org.netbeans.modules.xslt.model.ReferenceableXslComponent;
31 import org.netbeans.modules.xslt.model.Stylesheet;
32 import org.netbeans.modules.xslt.model.XslModel;
33 import org.netbeans.modules.xslt.model.XslReference;
34
35
36 /**
37  *
38  * @author ads
39  *
40  */

41 class GlobalReferenceImpl<T extends QualifiedNameable> extends
42     AbstractReference<T> implements
43         XslReference<T>
44 {
45
46     // used by XslComponentImpl#createReferenceTo method
47
GlobalReferenceImpl( T referenced, Class JavaDoc<T> referencedType,
48             XslComponentImpl parent )
49     {
50         super(referenced, referencedType, parent);
51     }
52
53     // used by resolve method
54
GlobalReferenceImpl( Class JavaDoc<T> referencedType,
55             XslComponentImpl parent, String JavaDoc ref )
56     {
57         super(referencedType, parent, ref);
58         initReferenceString( ref );
59     }
60
61     /* (non-Javadoc)
62      * @see org.netbeans.modules.xml.xam.Reference#get()
63      */

64     public T get() {
65         if (getReferenced() == null) {
66             setReferenced( find() );
67         }
68         return super.getReferenced();
69     }
70     
71     @Override JavaDoc
72     public String JavaDoc getRefString() {
73         if (refString == null) {
74             T referenced = super.getReferenced();
75             assert referenced != null;
76             myQname = referenced.getName();
77             if ( myQname != null ) {
78                 String JavaDoc result;
79                 myPrefix = myQname.getPrefix();
80                 myLocalPart = myQname.getLocalPart();
81                 if ( myPrefix == null || myPrefix.length() == 0 ) {
82                     result = myLocalPart;
83                 }
84                 else {
85                     result = myPrefix + ":" +myLocalPart;
86                 }
87                 
88                 if ( getParent().isInDocumentModel() ) {
89                     refString = result;
90                 }
91                 else {
92                     return result;
93                 }
94             }
95         }
96         return super.getRefString();
97     }
98
99     /* (non-Javadoc)
100      * @see org.netbeans.modules.xslt.model.XslReference#getQName()
101      */

102     public QName JavaDoc getQName() {
103         checkParentNotRemovedFromModel();
104         if ( myQname == null) {
105             T referenced = super.getReferenced();
106             myQname = referenced.getName();
107          }
108          return myQname;
109     }
110     
111     @Override JavaDoc
112     public boolean equals( Object JavaDoc obj )
113     {
114         if (this == obj) {
115             return true;
116         }
117         else if ( !( obj instanceof GlobalReferenceImpl )) {
118             return false;
119         }
120         else {
121             GlobalReferenceImpl<? extends ReferenceableXslComponent> ref =
122                 (GlobalReferenceImpl<? extends ReferenceableXslComponent>) obj;
123             return getParent().equals( ref.getParent() )&&
124                 getQName().equals( ref.getQName() );
125         }
126     }
127
128     @Override JavaDoc
129     public int hashCode()
130     {
131         return getParent().hashCode();
132     }
133     
134     @Override JavaDoc
135     public XslComponentImpl getParent() {
136         return (XslComponentImpl) super.getParent();
137     }
138     
139     @Override JavaDoc
140     public boolean references(T target) {
141         if ( target instanceof QualifiedNameable ) {
142             QualifiedNameable nameble = (QualifiedNameable) target;
143             return nameble.getName()!= null && nameble.getName().equals(
144                     getQName() ) && !isBroken() && get() == target;
145         }
146         return super.references(target);
147     }
148     
149     public void refresh() {
150         getRefString();
151         setReferenced(null);
152     }
153     
154     protected T getReferenced() {
155         if (super.getReferenced() == null) {
156             checkParentPartOfModel();
157         } else {
158             if (super.getParent().getModel() == null) {
159                 throw new IllegalStateException JavaDoc(
160                         "Referencing component has been removed from model."); //NOI18N
161
}
162             if (super.getReferenced().getModel() == null) {
163                 throw new IllegalStateException JavaDoc(
164                         "Referenced component has been removed from model."); //NOI18N
165
}
166         }
167         return super.getReferenced();
168     }
169     
170     /**
171      * @exception IllegalStateException if parent is already removed from a model.
172      */

173     protected void checkParentNotRemovedFromModel() {
174         if (getParent().getModel() == null) {
175             throw new IllegalStateException JavaDoc(
176                     "Referencing component has been removed from model."); // NOI18N
177
}
178     }
179     
180     /**
181      * @exception IllegalStateException if parent is not part of a model.
182      */

183     protected void checkParentPartOfModel() {
184         if (! getParent().isInDocumentModel()) {
185             throw new IllegalStateException JavaDoc(
186                     "Referencing component is not part of model."); //NOI18N
187
}
188     }
189     
190     /**
191      * Calculate the QName based on the local information
192      * without loading the referenced object.
193      */

194      protected QName JavaDoc calculateQNameLocally() {
195         String JavaDoc prefix = getPrefix();
196         String JavaDoc localPart = getLocalPart();
197         String JavaDoc namespace = null;
198         namespace = getParent().lookupNamespaceURI(prefix);
199         if (namespace == null) {
200             // prefix part is namespace name, which could be the namespace uri
201
// itself
202
String JavaDoc temp = getParent().lookupPrefix(prefix);
203             if (temp != null) {
204                 prefix = temp;
205                 namespace = prefix;
206             }
207         }
208
209         if (prefix == null) {
210             return new QName JavaDoc(namespace, localPart);
211         }
212         else {
213             return new QName JavaDoc(namespace, localPart, prefix);
214         }
215      }
216      
217      private String JavaDoc getLocalPart() {
218          if ( myLocalPart == null ) {
219              String JavaDoc ref = getRefString();
220              if ( ref == null ) {
221                  return null;
222              }
223              String JavaDoc[] parts = ref.split(":"); //NOI18N
224
if (parts.length == 2) {
225                  return parts[1];
226              }
227              else {
228                  return parts[0];
229              }
230          }
231          else {
232              return myLocalPart;
233          }
234      }
235      
236      private String JavaDoc getPrefix() {
237          if ( myPrefix == null ) {
238              String JavaDoc ref = getRefString();
239              if ( ref == null ) {
240                  return null;
241              }
242              String JavaDoc[] parts = ref.split(":"); //NOI18N
243
if (parts.length == 2) {
244                  return parts[0];
245              }
246              else {
247                  return null;
248              }
249          }
250          else {
251              return myPrefix;
252          }
253      }
254      
255      private void initReferenceString( String JavaDoc ref ) {
256          assert ref!=null;
257          refString = ref;
258          String JavaDoc[] parts = refString.split(":"); //NOI18N
259
if (parts.length == 2) {
260              myPrefix = parts[0];
261              myLocalPart = parts[1];
262          } else {
263              myPrefix = null;
264              myLocalPart = parts[0];
265          }
266      }
267      
268      private T find() {
269          LinkedHashSet JavaDoc<XslModel> list = Utilities.getAvailibleModels(
270                  getParent().getModel());
271          for (XslModel model : list) {
272             if ( Model.State.VALID.equals( model.getState() )) {
273                 Stylesheet stylesheet = model.getStylesheet();
274                 if ( stylesheet == null ) {
275                     continue;
276                 }
277                 List JavaDoc<T> children = stylesheet.getChildren( getType() );
278                 T result = find( children );
279                 if ( result != null ) {
280                     return result;
281                 }
282             }
283          }
284          return null;
285      }
286      
287      private T find( List JavaDoc<T> children ) {
288          for (T t : children) {
289                 QName JavaDoc name = t.getName();
290                 if ( name == null ) {
291                     continue;
292                 }
293                 String JavaDoc localPart = name.getLocalPart();
294                 String JavaDoc ns = name.getNamespaceURI();
295                 if ( getLocalPart().equals( localPart)
296                     && Utilities.equals( getQName().getNamespaceURI() , ns ) )
297                 {
298                     return t;
299                 }
300         }
301         return null;
302      }
303
304     private QName JavaDoc myQname;
305      
306     private String JavaDoc myPrefix;
307      
308     private String JavaDoc myLocalPart;
309
310 }
311
Popular Tags