1 19 20 package org.netbeans.modules.xml.xam.dom; 21 22 import org.netbeans.modules.xml.xam.NamedReferenceable; 23 import javax.xml.namespace.QName ; 24 import org.netbeans.modules.xml.xam.AbstractReference; 25 26 37 public abstract class AbstractNamedComponentReference<T extends NamedReferenceable> 38 extends AbstractReference<T> implements NamedComponentReference<T> { 39 40 protected String prefix; 41 protected String localName; 42 protected QName qname; 43 44 51 public AbstractNamedComponentReference(T referenced, Class <T> referencedType, AbstractDocumentComponent parent) { 52 super(referenced, referencedType, parent); 53 } 54 55 62 public AbstractNamedComponentReference(Class <T> referencedType, AbstractDocumentComponent parent, String ref){ 63 super(referencedType, parent, ref); 64 initReferenceString(ref); 65 } 66 67 71 public boolean references(T target) { 72 return target.getName() != null && 73 target.getName().equals(getLocalName()) && 74 ! isBroken() && 75 get() == target; 76 } 77 78 82 public synchronized String 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; } 92 } 93 return refString; 94 } 95 96 private void initReferenceString(String ref) { 97 if (ref == null) { 98 throw new IllegalArgumentException ("Reference string null"); } 100 refString = ref; 101 String [] parts = refString.split(":"); 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 getPrefix() { 112 getRefString(); 113 return prefix; 114 } 115 116 protected String 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 ("Referencing component has been removed from model."); } 128 if (super.getReferenced().getModel() == null) { 129 throw new IllegalStateException ("Referenced component has been removed from model."); } 131 } 132 return super.getReferenced(); 133 } 134 135 142 public synchronized QName 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 (getEffectiveNamespace(), get().getName()); 151 } 152 153 if (qname == null) { 154 return new QName (""); 155 } 156 } 157 return qname; 158 } 159 160 163 protected AbstractDocumentComponent getParent() { 164 return (AbstractDocumentComponent) super.getParent(); 165 } 166 167 170 protected void checkParentPartOfModel() { 171 if (! getParent().isInDocumentModel()) { 172 throw new IllegalStateException ("Referencing component is not part of model."); } 174 } 175 176 179 protected void checkParentNotRemovedFromModel() { 180 if (getParent().getModel() == null) { 181 throw new IllegalStateException ("Referencing component has been removed from model."); } 183 } 184 185 189 protected QName calculateQNameLocally() { 190 assert localName != null; 191 String namespace = null; 192 namespace = getParent().lookupNamespaceURI(prefix); 193 if (namespace == null) { 194 String 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 (namespace, localName); 204 } else { 205 return new QName (namespace, localName, prefix); 206 } 207 } 208 209 @Override 210 public boolean equals(Object 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 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 |