KickJava   Java API By Example, From Geeks To Geeks.

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


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
20 package org.netbeans.modules.xslt.model.impl;
21
22
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.List JavaDoc;
27
28 import javax.xml.namespace.QName JavaDoc;
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 JavaDoc;
43 import org.w3c.dom.Element JavaDoc;
44 import org.w3c.dom.Node JavaDoc;
45 import org.w3c.dom.NodeList JavaDoc;
46 import org.w3c.dom.Text JavaDoc;
47
48
49 /**
50  * @author ads
51  *
52  */

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 JavaDoc<? extends XslComponent> getComponentType();
67
68
69     public abstract void accept( XslVisitor visitor );
70
71     
72     /***************************************************************************
73      *
74      * The methods below are frequently used in specific impls. So I place them here.
75      *
76      ***************************************************************************
77      */

78     
79     public AttributeValueTemplate createTemplate( QName JavaDoc qName ) {
80         return AttributeValueTemplateImpl.creatAttributeValueTemplate( qName );
81     }
82     
83     public AttributeValueTemplate createTemplate( String JavaDoc value ) {
84         return AttributeValueTemplateImpl.creatAttributeValueTemplate( this ,
85                 value );
86     }
87     
88     
89     public String JavaDoc getSelect() {
90         return getAttribute( XslAttributes.SELECT );
91     }
92
93     public void setSelect( String JavaDoc select ) {
94         setAttribute( XslAttributes.SELECT, select );
95     }
96     
97     /* (non-Javadoc)
98      * @see org.netbeans.modules.xslt.model.ContentElement#getContent()
99      */

100     public String JavaDoc getContent() {
101         StringBuilder JavaDoc text = new StringBuilder JavaDoc();
102         NodeList JavaDoc nodeList = getPeer().getChildNodes();
103         for (int i = 0; i < nodeList.getLength(); i++) {
104             Node JavaDoc node = nodeList.item(i);
105             if ( node instanceof Element ) {
106                 break;
107             }
108             if (node instanceof Text JavaDoc && ! ( node instanceof Comment JavaDoc ) ) {
109                 text.append(node.getNodeValue());
110             }
111         }
112         return text.toString();
113     }
114
115     /* (non-Javadoc)
116      * @see org.netbeans.modules.xslt.model.ContentElement#setContent(java.lang.String)
117      */

118     public void setContent( String JavaDoc text ) {
119         verifyWrite();
120         StringBuilder JavaDoc oldValue = new StringBuilder JavaDoc();
121         ArrayList JavaDoc<Node JavaDoc> toRemove = new ArrayList JavaDoc<Node JavaDoc>();
122         NodeList JavaDoc nodeList = getPeer().getChildNodes();
123
124         Element ref = null;
125         for (int i = 0; i < nodeList.getLength(); i++) {
126             Node JavaDoc 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 JavaDoc && 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 JavaDoc 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     /* (non-Javadoc)
153      * @see org.netbeans.modules.xslt.model.SequenceElement#getTrailingText()
154      */

155     public String JavaDoc getTrailingText() {
156         XslComponentImpl parent = getParent();
157         if( parent == null ) {
158             return null;
159         }
160         return parent.getTrailingText( this );
161     }
162
163     /* (non-Javadoc)
164      * @see org.netbeans.modules.xslt.model.SequenceElement#setTrailingText(java.lang.String)
165      */

166     public void setTrailingText( String JavaDoc text ) {
167         XslComponentImpl parent = getParent();
168         if( parent == null ) {
169             throw new IllegalStateException JavaDoc("Trailing text cannot be set for " + // NOI18N
170
"component that doesn't have parent element"); // NOI18N
171
}
172         parent.setTrailingText( SequenceElement.TEXT_CONTENT_PROPERTY, text,
173                 this );
174     }
175
176     /*
177      ***************************************************************************
178      */

179     
180     /*
181      * (non-Javadoc)
182      * @see org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent#getModel()
183      */

184     @Override JavaDoc
185     public XslModelImpl getModel() {
186         return (XslModelImpl)super.getModel();
187     }
188     
189     /* (non-Javadoc)
190      * @see org.netbeans.modules.xml.xam.AbstractComponent#getParent()
191      */

192     @Override JavaDoc
193     public XslComponentImpl getParent() {
194         return (XslComponentImpl)super.getParent();
195     }
196     
197     /* (non-Javadoc)
198      * @see org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent#lookupNamespaceURI(java.lang.String)
199      */

200     @Override JavaDoc
201     public String JavaDoc lookupNamespaceURI(String JavaDoc prefix) {
202         return lookupNamespaceURI(prefix, true);
203     }
204     
205     /* (non-Javadoc)
206      * @see org.netbeans.modules.xslt.model.XslComponent#createReferenceTo(org.netbeans.modules.xslt.model.ReferenceableXslComponent, java.lang.Class)
207      */

208     @SuppressWarnings JavaDoc("unchecked")
209     public <T extends ReferenceableXslComponent> XslReference<T> createReferenceTo(
210             T referenced, Class JavaDoc<T> type )
211     {
212         // currently we only know how to resolve QualifiedNameable elements.
213
// later this impl could be changed respectively.
214
assert type.isAssignableFrom( QualifiedNameable.class );
215         return new GlobalReferenceImpl( (QualifiedNameable) referenced , type ,
216                 this );
217     }
218
219     /* (non-Javadoc)
220      * @see org.netbeans.modules.xslt.model.XslComponent#fromSameModel(org.netbeans.modules.xslt.model.XslComponent)
221      */

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"; // NOI18N
229
setAttribute( attribute, (Object JavaDoc)value);
230     }
231     
232     protected void setAttribute( XslAttributes attribute,
233             AttributeValueTemplate avt)
234     {
235         verifyWrite();
236         if( avt == null ) {
237             setAttribute( attribute, (Object JavaDoc)null);
238         }
239         Object JavaDoc resultValue = avt;
240         if ( !avt.isTemplate() ) {
241             QName JavaDoc 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 JavaDoc ) {
249             Object JavaDoc old = null;
250             String JavaDoc s = getAttribute(attribute);
251             if (s != null) {
252                 old = getAttributeValueOf(attribute, s);
253             }
254             setAttributeAndFireChange(attribute, (String JavaDoc)resultValue, old, avt );
255         }
256         else {
257             setAttribute(attribute, (Object JavaDoc)avt);
258         }
259     }
260     
261     protected void setAttribute( XslAttributes attribute, Object JavaDoc value ) {
262         setAttribute( attribute.getName() , attribute, value);
263     }
264     
265     protected void setAttributeTokenList( XslAttributes attribute,
266             List JavaDoc<String JavaDoc> 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 JavaDoc)null);
277         }
278         QName JavaDoc qName = value.getQName();
279         assert qName!= null;
280         String JavaDoc resultValue = getPrefixedName( qName.getNamespaceURI(),
281                             qName.getLocalPart(), null, true);
282         Object JavaDoc old = null;
283         String JavaDoc 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 JavaDoc<T> clazz, XslAttributes attrName )
292     {
293         String JavaDoc value = getAttribute(attrName);
294         return getAtttributeAccess().resolveGlobalReference(clazz, value);
295     }
296     
297     protected <T extends QualifiedNameable> List JavaDoc<XslReference<T>>
298         resolveGlobalReferenceList( Class JavaDoc<T> clazz, XslAttributes attrName )
299     {
300         String JavaDoc value = getAttribute(attrName);
301         return getAtttributeAccess().resolveGlobalReferenceList(clazz, value);
302     }
303     
304     @SuppressWarnings JavaDoc("unchecked")
305     protected <T extends ReferenceableXslComponent> void setAttributeList(
306             XslAttributes attr, List JavaDoc<XslReference<T>> collection )
307     {
308         setAttribute( attr, collection, Lazy.REFERENCE_STRATEGY );
309     }
310     
311     protected void setAttribute( XslAttributes attr , List JavaDoc<QName JavaDoc> list ) {
312         setAttribute(attr, list, Lazy.QNAME_STRATEGY );
313     }
314     
315     protected List JavaDoc<QName JavaDoc> getQNameList( String JavaDoc value ){
316         return getAtttributeAccess().getQNameList(value);
317     }
318
319     /* (non-Javadoc)
320      * @see org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent#getAttributeValueOf(org.netbeans.modules.xml.xam.dom.Attribute, java.lang.String)
321      */

322     @Override JavaDoc
323     protected Object JavaDoc getAttributeValueOf( Attribute attr, String JavaDoc stringValue )
324     {
325         return getAtttributeAccess().getAttributeValueOf(attr, stringValue);
326     }
327     
328     /* (non-Javadoc)
329      * @see org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent#populateChildren(java.util.List)
330      */

331     @Override JavaDoc
332     protected void populateChildren( List JavaDoc<XslComponent> children )
333     {
334         NodeList JavaDoc nl = getPeer().getChildNodes();
335         if (nl != null) {
336             for (int i = 0; i < nl.getLength(); i++) {
337                 Node JavaDoc 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 JavaDoc
350     protected int findDomainIndex(Element e) {
351         int result = super.findDomainIndex( e );
352         if ( result != -1 ) {
353             return result;
354         }
355         
356         // only sequence constructor could have non-xsl components.
357
if ( !( this instanceof SequenceConstructor )) {
358             return -1;
359         }
360         
361         int domainInsertIndex = 0;
362         NodeList JavaDoc list = getPeer().getChildNodes();
363         for (int i=0; i<list.getLength(); i++) {
364             Node JavaDoc 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 JavaDoc getTrailingText( XslComponent child) {
376         return getText(child, false, false);
377     }
378     
379     protected void setTrailingText(String JavaDoc propName, String JavaDoc 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 JavaDoc<T> list,
394             AttributeListValueStartegy<T> strategy )
395     {
396         if ( list == null ) {
397             setAttribute( attribute, list );
398         }
399         verifyWrite();
400         StringBuilder JavaDoc builder = new StringBuilder JavaDoc();
401         for ( T t: list ) {
402             assert t!=null;
403             String JavaDoc resultValue = strategy.toString( t , this );
404             builder.append( resultValue );
405             builder.append( " " );
406         }
407         String JavaDoc 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 JavaDoc old = null;
415         String JavaDoc 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 JavaDoc
423             newStringValue, Object JavaDoc oldValue , Object JavaDoc newValue )
424     {
425         setAttributeQuietly(attr, newStringValue );
426         firePropertyChange( attr.getName(), oldValue, newValue );
427         fireValueChanged();
428     }
429
430     @SuppressWarnings JavaDoc("unchecked")
431     protected static final Collection JavaDoc<Class JavaDoc<? extends XslComponent>> EMPTY =
432         Collections.EMPTY_LIST;
433     
434     protected static final Collection JavaDoc<Class JavaDoc<? extends XslComponent>>
435         SEQUENCE_ELEMENTS = new ArrayList JavaDoc<Class JavaDoc<? 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 JavaDoc toString( T token , XslComponentImpl comp );
446     }
447     
448     static class SimpleStrategy implements AttributeListValueStartegy<String JavaDoc> {
449
450         public String JavaDoc toString( String JavaDoc token , XslComponentImpl comp ) {
451             return token;
452         }
453     }
454     
455     static class ReferenceStrategy<T> implements
456         AttributeListValueStartegy<T>
457     {
458
459         public String JavaDoc toString( T token , XslComponentImpl comp ) {
460             assert token instanceof XslReference;
461             QName JavaDoc 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 JavaDoc> {
468
469         public String JavaDoc toString( QName JavaDoc 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