1 19 20 package org.netbeans.modules.xslt.model.impl; 21 22 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.Collections ; 26 import java.util.List ; 27 28 import javax.xml.namespace.QName ; 29 30 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent; 31 import org.netbeans.modules.xml.xam.dom.Attribute; 32 import org.netbeans.modules.xslt.model.AttributeValueTemplate; 33 import org.netbeans.modules.xslt.model.ContentElement; 34 import org.netbeans.modules.xslt.model.QualifiedNameable; 35 import org.netbeans.modules.xslt.model.ReferenceableXslComponent; 36 import org.netbeans.modules.xslt.model.SequenceConstructor; 37 import org.netbeans.modules.xslt.model.SequenceElement; 38 import org.netbeans.modules.xslt.model.XslComponent; 39 import org.netbeans.modules.xslt.model.XslReference; 40 import org.netbeans.modules.xslt.model.XslVisitor; 41 import org.netbeans.modules.xslt.model.enums.EnumValue; 42 import org.w3c.dom.Comment ; 43 import org.w3c.dom.Element ; 44 import org.w3c.dom.Node ; 45 import org.w3c.dom.NodeList ; 46 import org.w3c.dom.Text ; 47 48 49 53 abstract class XslComponentImpl extends AbstractDocumentComponent<XslComponent> 54 implements XslComponent 55 { 56 57 XslComponentImpl( XslModelImpl model, Element e ) { 58 super(model, e); 59 myAttributeAccess = new AttributeAccess( this ); 60 } 61 62 XslComponentImpl( XslModelImpl model , XslElements type ) { 63 this( model , createNewElement( type, model) ); 64 } 65 66 public abstract Class <? extends XslComponent> getComponentType(); 67 68 69 public abstract void accept( XslVisitor visitor ); 70 71 72 78 79 public AttributeValueTemplate createTemplate( QName qName ) { 80 return AttributeValueTemplateImpl.creatAttributeValueTemplate( qName ); 81 } 82 83 public AttributeValueTemplate createTemplate( String value ) { 84 return AttributeValueTemplateImpl.creatAttributeValueTemplate( this , 85 value ); 86 } 87 88 89 public String getSelect() { 90 return getAttribute( XslAttributes.SELECT ); 91 } 92 93 public void setSelect( String select ) { 94 setAttribute( XslAttributes.SELECT, select ); 95 } 96 97 100 public String getContent() { 101 StringBuilder text = new StringBuilder (); 102 NodeList nodeList = getPeer().getChildNodes(); 103 for (int i = 0; i < nodeList.getLength(); i++) { 104 Node node = nodeList.item(i); 105 if ( node instanceof Element ) { 106 break; 107 } 108 if (node instanceof Text && ! ( node instanceof Comment ) ) { 109 text.append(node.getNodeValue()); 110 } 111 } 112 return text.toString(); 113 } 114 115 118 public void setContent( String text ) { 119 verifyWrite(); 120 StringBuilder oldValue = new StringBuilder (); 121 ArrayList <Node > toRemove = new ArrayList <Node >(); 122 NodeList nodeList = getPeer().getChildNodes(); 123 124 Element ref = null; 125 for (int i = 0; i < nodeList.getLength(); i++) { 126 Node node = nodeList.item(i); 127 if (node != null && node.getNodeType() == Node.ELEMENT_NODE) { 128 ref = (Element) node; 129 break; 130 } 131 if (node instanceof Text && node.getNodeType() != Node.COMMENT_NODE) { 132 toRemove.add(node); 133 oldValue.append(node.getNodeValue()); 134 } 135 } 136 137 getModel().getAccess().removeChildren(getPeer(), toRemove, this); 138 if ( text != null) { 139 Text newNode = getModel().getDocument().createTextNode(text); 140 if (ref != null) { 141 getModel().getAccess().insertBefore(getPeer(), newNode, ref, this); 142 } else { 143 getModel().getAccess().appendChild(getPeer(), newNode, this); 144 } 145 } 146 147 firePropertyChange(ContentElement.TEXT_CONTENT_PROPERTY, 148 oldValue == null ? null : oldValue.toString(), text ); 149 fireValueChanged(); 150 } 151 152 155 public String getTrailingText() { 156 XslComponentImpl parent = getParent(); 157 if( parent == null ) { 158 return null; 159 } 160 return parent.getTrailingText( this ); 161 } 162 163 166 public void setTrailingText( String text ) { 167 XslComponentImpl parent = getParent(); 168 if( parent == null ) { 169 throw new IllegalStateException ("Trailing text cannot be set for " + "component that doesn't have parent element"); } 172 parent.setTrailingText( SequenceElement.TEXT_CONTENT_PROPERTY, text, 173 this ); 174 } 175 176 179 180 184 @Override 185 public XslModelImpl getModel() { 186 return (XslModelImpl)super.getModel(); 187 } 188 189 192 @Override 193 public XslComponentImpl getParent() { 194 return (XslComponentImpl)super.getParent(); 195 } 196 197 200 @Override 201 public String lookupNamespaceURI(String prefix) { 202 return lookupNamespaceURI(prefix, true); 203 } 204 205 208 @SuppressWarnings ("unchecked") 209 public <T extends ReferenceableXslComponent> XslReference<T> createReferenceTo( 210 T referenced, Class <T> type ) 211 { 212 assert type.isAssignableFrom( QualifiedNameable.class ); 215 return new GlobalReferenceImpl( (QualifiedNameable) referenced , type , 216 this ); 217 } 218 219 222 public boolean fromSameModel( XslComponent other ) { 223 return getModel().equals(other.getModel()); 224 } 225 226 protected void setAttribute( XslAttributes attribute, EnumValue value ) { 227 assert value==null || !value.isInvalid() : 228 "Attempt to set up invalid enumeration value"; setAttribute( attribute, (Object )value); 230 } 231 232 protected void setAttribute( XslAttributes attribute, 233 AttributeValueTemplate avt) 234 { 235 verifyWrite(); 236 if( avt == null ) { 237 setAttribute( attribute, (Object )null); 238 } 239 Object resultValue = avt; 240 if ( !avt.isTemplate() ) { 241 QName qName = avt.getQName(); 242 if ( qName!= null ) { 243 resultValue = 244 getPrefixedName( qName.getNamespaceURI(), 245 qName.getLocalPart(), null, true); 246 } 247 } 248 if ( resultValue instanceof String ) { 249 Object old = null; 250 String s = getAttribute(attribute); 251 if (s != null) { 252 old = getAttributeValueOf(attribute, s); 253 } 254 setAttributeAndFireChange(attribute, (String )resultValue, old, avt ); 255 } 256 else { 257 setAttribute(attribute, (Object )avt); 258 } 259 } 260 261 protected void setAttribute( XslAttributes attribute, Object value ) { 262 setAttribute( attribute.getName() , attribute, value); 263 } 264 265 protected void setAttributeTokenList( XslAttributes attribute, 266 List <String > value ) 267 { 268 setAttribute(attribute, value, Lazy.SIMPLE_STRATEGY ); 269 } 270 271 protected void setAttribute( XslAttributes attribute, 272 XslReference<? extends ReferenceableXslComponent> value ) 273 { 274 verifyWrite(); 275 if( value == null ) { 276 setAttribute( attribute, (Object )null); 277 } 278 QName qName = value.getQName(); 279 assert qName!= null; 280 String resultValue = getPrefixedName( qName.getNamespaceURI(), 281 qName.getLocalPart(), null, true); 282 Object old = null; 283 String s = getAttribute(attribute); 284 if (s != null) { 285 old = getAttributeValueOf(attribute, s); 286 } 287 setAttributeAndFireChange(attribute, resultValue, old, value); 288 } 289 290 protected <T extends QualifiedNameable> GlobalReferenceImpl<T> 291 resolveGlobalReference( Class <T> clazz, XslAttributes attrName ) 292 { 293 String value = getAttribute(attrName); 294 return getAtttributeAccess().resolveGlobalReference(clazz, value); 295 } 296 297 protected <T extends QualifiedNameable> List <XslReference<T>> 298 resolveGlobalReferenceList( Class <T> clazz, XslAttributes attrName ) 299 { 300 String value = getAttribute(attrName); 301 return getAtttributeAccess().resolveGlobalReferenceList(clazz, value); 302 } 303 304 @SuppressWarnings ("unchecked") 305 protected <T extends ReferenceableXslComponent> void setAttributeList( 306 XslAttributes attr, List <XslReference<T>> collection ) 307 { 308 setAttribute( attr, collection, Lazy.REFERENCE_STRATEGY ); 309 } 310 311 protected void setAttribute( XslAttributes attr , List <QName > list ) { 312 setAttribute(attr, list, Lazy.QNAME_STRATEGY ); 313 } 314 315 protected List <QName > getQNameList( String value ){ 316 return getAtttributeAccess().getQNameList(value); 317 } 318 319 322 @Override 323 protected Object getAttributeValueOf( Attribute attr, String stringValue ) 324 { 325 return getAtttributeAccess().getAttributeValueOf(attr, stringValue); 326 } 327 328 331 @Override 332 protected void populateChildren( List <XslComponent> children ) 333 { 334 NodeList nl = getPeer().getChildNodes(); 335 if (nl != null) { 336 for (int i = 0; i < nl.getLength(); i++) { 337 Node n = nl.item(i); 338 if (n instanceof Element) { 339 XslComponent comp = (XslComponent) getModel().getFactory() 340 .create((Element) n, this); 341 if (comp != null) { 342 children.add(comp); 343 } 344 } 345 } 346 } 347 } 348 349 @Override 350 protected int findDomainIndex(Element e) { 351 int result = super.findDomainIndex( e ); 352 if ( result != -1 ) { 353 return result; 354 } 355 356 if ( !( this instanceof SequenceConstructor )) { 358 return -1; 359 } 360 361 int domainInsertIndex = 0; 362 NodeList list = getPeer().getChildNodes(); 363 for (int i=0; i<list.getLength(); i++) { 364 Node node = list.item( i ); 365 if (list.item(i) == e) { 366 return domainInsertIndex; 367 } 368 if ( node instanceof Element ) { 369 domainInsertIndex++; 370 } 371 } 372 return -1; 373 } 374 375 protected String getTrailingText( XslComponent child) { 376 return getText(child, false, false); 377 } 378 379 protected void setTrailingText(String propName, String text, 380 XslComponent child ) 381 { 382 setText(propName, text, child, false, false ); 383 } 384 385 protected static Element createNewElement(XslElements type, XslModelImpl model){ 386 return model.getDocument().createElementNS( XSL_NAMESPACE, type.getName()); 387 } 388 389 private AttributeAccess getAtttributeAccess() { 390 return myAttributeAccess; 391 } 392 393 private <T> void setAttribute( XslAttributes attribute, List <T> list, 394 AttributeListValueStartegy<T> strategy ) 395 { 396 if ( list == null ) { 397 setAttribute( attribute, list ); 398 } 399 verifyWrite(); 400 StringBuilder builder = new StringBuilder (); 401 for ( T t: list ) { 402 assert t!=null; 403 String resultValue = strategy.toString( t , this ); 404 builder.append( resultValue ); 405 builder.append( " " ); 406 } 407 String result = null; 408 if ( builder.length() > 0 ) { 409 result = builder.substring( 0, builder.length() -1 ); 410 } 411 else { 412 result = builder.toString(); 413 } 414 Object old = null; 415 String s = getAttribute(attribute); 416 if (s != null) { 417 old = getAttributeValueOf(attribute, s); 418 } 419 setAttributeAndFireChange(attribute, result, old, list ); 420 } 421 422 private void setAttributeAndFireChange( XslAttributes attr , String 423 newStringValue, Object oldValue , Object newValue ) 424 { 425 setAttributeQuietly(attr, newStringValue ); 426 firePropertyChange( attr.getName(), oldValue, newValue ); 427 fireValueChanged(); 428 } 429 430 @SuppressWarnings ("unchecked") 431 protected static final Collection <Class <? extends XslComponent>> EMPTY = 432 Collections.EMPTY_LIST; 433 434 protected static final Collection <Class <? extends XslComponent>> 435 SEQUENCE_ELEMENTS = new ArrayList <Class <? extends XslComponent>>(1); 436 437 private AttributeAccess myAttributeAccess; 438 439 static { 440 SEQUENCE_ELEMENTS.add( SequenceElement.class ); 441 } 442 443 interface AttributeListValueStartegy<T> { 444 445 String toString( T token , XslComponentImpl comp ); 446 } 447 448 static class SimpleStrategy implements AttributeListValueStartegy<String > { 449 450 public String toString( String token , XslComponentImpl comp ) { 451 return token; 452 } 453 } 454 455 static class ReferenceStrategy<T> implements 456 AttributeListValueStartegy<T> 457 { 458 459 public String toString( T token , XslComponentImpl comp ) { 460 assert token instanceof XslReference; 461 QName qName = ((XslReference)token).getQName(); 462 return comp.getPrefixedName( qName.getNamespaceURI(), 463 qName.getLocalPart(), null, true); 464 } 465 } 466 467 static class QNameStrategy implements AttributeListValueStartegy<QName > { 468 469 public String toString( QName token, XslComponentImpl comp ) { 470 return comp.getPrefixedName( token.getNamespaceURI(), 471 token.getLocalPart(), null, true); 472 } 473 474 } 475 476 static class Lazy { 477 static final SimpleStrategy SIMPLE_STRATEGY = new SimpleStrategy(); 478 479 static final ReferenceStrategy REFERENCE_STRATEGY = 480 new ReferenceStrategy(); 481 482 static final QNameStrategy QNAME_STRATEGY = new QNameStrategy(); 483 } 484 } 485 | Popular Tags |