KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xam > dom > AbstractNamedComponentReference


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.xam.dom;
21
22 import org.netbeans.modules.xml.xam.NamedReferenceable;
23 import javax.xml.namespace.QName JavaDoc;
24 import org.netbeans.modules.xml.xam.AbstractReference;
25
26 /**
27  * Abstract implementation of reference by name to a component.
28  * On writing, this indirection help serialize the referenced component as an
29  * attribute string value. On reading, the referenced can be resolved on demand.
30  * <p>
31  * Note: Client code should always check for brokeness before access the referenced.
32  *
33  * @author rico
34  * @author Nam Nguyen
35  * @author Chris Webster
36  */

37 public abstract class AbstractNamedComponentReference<T extends NamedReferenceable>
38         extends AbstractReference<T> implements NamedComponentReference<T> {
39
40     protected String JavaDoc prefix;
41     protected String JavaDoc localName;
42     protected QName JavaDoc qname;
43     
44     /**
45      * Constructor for writing.
46      * @param referenced the component being referenced
47      * @param referencedType type of the referenced component
48      * @param parent referencing component on which the referenced is serialized
49      * as an attribute string value.
50      */

51     public AbstractNamedComponentReference(T referenced, Class JavaDoc<T> referencedType, AbstractDocumentComponent parent) {
52         super(referenced, referencedType, parent);
53     }
54     
55     /**
56      * Constructor for reading.
57      * @param referencedType type of the referenced component
58      * @param parent referencing component on which the referenced is serialized
59      * as an attribute string value.
60      * @param ref the string value used in resolving.
61      */

62     public AbstractNamedComponentReference(Class JavaDoc<T> referencedType, AbstractDocumentComponent parent, String JavaDoc ref){
63         super(referencedType, parent, ref);
64         initReferenceString(ref);
65     }
66     
67     /**
68      * Return true if this reference refers to target. This method is more
69      * efficient than invoking get
70      */

71     public boolean references(T target) {
72         return target.getName() != null &&
73                 target.getName().equals(getLocalName()) &&
74                 ! isBroken() &&
75                 get() == target;
76     }
77     
78     /**
79      * @return string to use in persiting the reference as attribute value of
80      * the containing component
81      */

82     public synchronized String JavaDoc getRefString() {
83         if (refString == null) {
84             assert super.getReferenced() != null;
85             prefix = getParent().lookupPrefix(getEffectiveNamespace());
86             localName = super.getReferenced().getName();
87             if (prefix == null || prefix.length() == 0) {
88                 refString = localName;
89             } else {
90                 refString = prefix + ":" + localName; //NOI18N
91
}
92         }
93         return refString;
94     }
95     
96     private void initReferenceString(String JavaDoc ref) {
97         if (ref == null) {
98             throw new IllegalArgumentException JavaDoc("Reference string null"); //NOI18N
99
}
100         refString = ref;
101         String JavaDoc[] parts = refString.split(":"); //NOI18N
102
if (parts.length == 2) {
103             prefix = parts[0];
104             localName = parts[1];
105         } else {
106             prefix = null;
107             localName = parts[0];
108         }
109     }
110     
111     protected String JavaDoc getPrefix() {
112         getRefString();
113         return prefix;
114     }
115     
116     protected String JavaDoc getLocalName() {
117         getRefString();
118         return localName;
119     }
120     
121     protected T getReferenced() {
122         if (super.getReferenced() == null) {
123             checkParentPartOfModel();
124         } else {
125             if (super.getParent().getModel() == null) {
126                 throw new IllegalStateException JavaDoc("Referencing component has been removed from model."); //NOI18N
127
}
128             if (super.getReferenced().getModel() == null) {
129                 throw new IllegalStateException JavaDoc("Referenced component has been removed from model."); //NOI18N
130
}
131         }
132         return super.getReferenced();
133     }
134     
135     /**
136      * Note this method will first attempt to build the QName base on local lookup
137      * before trying to resolve the referenced. Subclasses need to override
138      * if local calculation is not desirable.
139      * @exception IllegalStateException if referencing component is discarded from
140      * the model.
141      */

142     public synchronized QName JavaDoc getQName() {
143         checkParentNotRemovedFromModel();
144         if (qname == null) {
145            if (super.getReferenced() == null) {
146                qname = calculateQNameLocally();
147            }
148            
149            if (qname == null && ! isBroken()) {
150                qname = new QName JavaDoc(getEffectiveNamespace(), get().getName());
151            }
152            
153            if (qname == null) {
154                return new QName JavaDoc("");
155            }
156         }
157         return qname;
158     }
159     
160     /**
161      * Returns parent referencing component.
162      */

163     protected AbstractDocumentComponent getParent() {
164         return (AbstractDocumentComponent) super.getParent();
165     }
166     
167     /**
168      * @exception IllegalStateException if parent is not part of a model.
169      */

170     protected void checkParentPartOfModel() {
171         if (! getParent().isInDocumentModel()) {
172             throw new IllegalStateException JavaDoc("Referencing component is not part of model."); //NOI18N
173
}
174     }
175     
176     /**
177      * @exception IllegalStateException if parent is already removed from a model.
178      */

179     protected void checkParentNotRemovedFromModel() {
180         if (getParent().getModel() == null) {
181             throw new IllegalStateException JavaDoc("Referencing component has been removed from model."); //NOI18N
182
}
183     }
184     
185    /**
186     * Calculate the QName based on the local information
187     * without loading the referenced object.
188     */

189     protected QName JavaDoc calculateQNameLocally() {
190         assert localName != null;
191         String JavaDoc namespace = null;
192             namespace = getParent().lookupNamespaceURI(prefix);
193             if (namespace == null) {
194                //prefix part is namespace name, which could be the namespace uri itself
195
String JavaDoc temp = getParent().lookupPrefix(prefix);
196                if (temp != null) {
197                    prefix = temp;
198                    namespace = prefix;
199                }
200            }
201
202        if (prefix == null) {
203            return new QName JavaDoc(namespace, localName);
204        } else {
205            return new QName JavaDoc(namespace, localName, prefix);
206        }
207     }
208     
209     @Override JavaDoc
210             public boolean equals(Object JavaDoc obj) {
211         return (this == obj) || (obj != null &&
212                 getClass().equals(obj.getClass()) &&
213                 getParent().equals(((AbstractNamedComponentReference) obj).getParent()) &&
214                 getQName().equals(((AbstractNamedComponentReference)obj).getQName()));
215     }
216     
217     @Override JavaDoc
218             public int hashCode() {
219         return getParent().hashCode();
220     }
221     
222     public synchronized void refresh() {
223         getRefString();
224         setReferenced(null);
225     }
226 }
227
Popular Tags