1 19 package org.netbeans.modules.xslt.model.impl; 20 21 22 import java.util.LinkedHashSet ; 23 import java.util.List ; 24 25 import javax.xml.namespace.QName ; 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 41 class GlobalReferenceImpl<T extends QualifiedNameable> extends 42 AbstractReference<T> implements 43 XslReference<T> 44 { 45 46 GlobalReferenceImpl( T referenced, Class <T> referencedType, 48 XslComponentImpl parent ) 49 { 50 super(referenced, referencedType, parent); 51 } 52 53 GlobalReferenceImpl( Class <T> referencedType, 55 XslComponentImpl parent, String ref ) 56 { 57 super(referencedType, parent, ref); 58 initReferenceString( ref ); 59 } 60 61 64 public T get() { 65 if (getReferenced() == null) { 66 setReferenced( find() ); 67 } 68 return super.getReferenced(); 69 } 70 71 @Override 72 public String getRefString() { 73 if (refString == null) { 74 T referenced = super.getReferenced(); 75 assert referenced != null; 76 myQname = referenced.getName(); 77 if ( myQname != null ) { 78 String 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 102 public QName getQName() { 103 checkParentNotRemovedFromModel(); 104 if ( myQname == null) { 105 T referenced = super.getReferenced(); 106 myQname = referenced.getName(); 107 } 108 return myQname; 109 } 110 111 @Override 112 public boolean equals( Object 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 129 public int hashCode() 130 { 131 return getParent().hashCode(); 132 } 133 134 @Override 135 public XslComponentImpl getParent() { 136 return (XslComponentImpl) super.getParent(); 137 } 138 139 @Override 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 ( 160 "Referencing component has been removed from model."); } 162 if (super.getReferenced().getModel() == null) { 163 throw new IllegalStateException ( 164 "Referenced component has been removed from model."); } 166 } 167 return super.getReferenced(); 168 } 169 170 173 protected void checkParentNotRemovedFromModel() { 174 if (getParent().getModel() == null) { 175 throw new IllegalStateException ( 176 "Referencing component has been removed from model."); } 178 } 179 180 183 protected void checkParentPartOfModel() { 184 if (! getParent().isInDocumentModel()) { 185 throw new IllegalStateException ( 186 "Referencing component is not part of model."); } 188 } 189 190 194 protected QName calculateQNameLocally() { 195 String prefix = getPrefix(); 196 String localPart = getLocalPart(); 197 String namespace = null; 198 namespace = getParent().lookupNamespaceURI(prefix); 199 if (namespace == null) { 200 String 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 (namespace, localPart); 211 } 212 else { 213 return new QName (namespace, localPart, prefix); 214 } 215 } 216 217 private String getLocalPart() { 218 if ( myLocalPart == null ) { 219 String ref = getRefString(); 220 if ( ref == null ) { 221 return null; 222 } 223 String [] parts = ref.split(":"); 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 getPrefix() { 237 if ( myPrefix == null ) { 238 String ref = getRefString(); 239 if ( ref == null ) { 240 return null; 241 } 242 String [] parts = ref.split(":"); 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 ref ) { 256 assert ref!=null; 257 refString = ref; 258 String [] parts = refString.split(":"); 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 <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 <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 <T> children ) { 288 for (T t : children) { 289 QName name = t.getName(); 290 if ( name == null ) { 291 continue; 292 } 293 String localPart = name.getLocalPart(); 294 String 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 myQname; 305 306 private String myPrefix; 307 308 private String myLocalPart; 309 310 } 311 | Popular Tags |