KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.netbeans.modules.xslt.model.impl;
20
21 import java.util.Collection JavaDoc;
22 import java.util.LinkedHashSet JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
26
27 import javax.xml.namespace.QName JavaDoc;
28
29 import org.netbeans.modules.xml.xam.Model;
30 import org.netbeans.modules.xml.xam.Reference;
31 import org.netbeans.modules.xslt.model.AttributeSet;
32 import org.netbeans.modules.xslt.model.ReferenceableXslComponent;
33 import org.netbeans.modules.xslt.model.Stylesheet;
34 import org.netbeans.modules.xslt.model.XslModel;
35 import org.netbeans.modules.xslt.model.XslReference;
36
37
38 /**
39  * This is custom resolver for AttributeSet reference list.
40  *
41  * @author ads
42  *
43  */

44 class AttributeSetReferenceListResolver implements
45         ReferenceListResolveFactory
46 {
47
48     /* (non-Javadoc)
49      * @see org.netbeans.modules.xslt.model.impl.ReferenceListResolveFactory#isApplicable(java.lang.Class)
50      */

51     public boolean isApplicable( Class JavaDoc referenceType ) {
52         return AttributeSet.class.isAssignableFrom( referenceType );
53     }
54
55     /* (non-Javadoc)
56      * @see org.netbeans.modules.xslt.model.impl.ReferenceListResolveFactory#resolve(org.netbeans.modules.xslt.model.impl.XslComponentImpl, java.lang.Class, java.lang.String)
57      */

58     public <T extends ReferenceableXslComponent> List JavaDoc<XslReference<T>> resolve(
59             AttributeAccess access, Class JavaDoc<T> clazz, String JavaDoc value )
60     {
61         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(value, " ");
62         List JavaDoc<Reference<T>> references = new LinkedList JavaDoc<Reference<T>>();
63         while (tokenizer.hasMoreTokens()) {
64             String JavaDoc next = tokenizer.nextToken();
65             Collection JavaDoc<Reference<T>> collection = find( clazz , next , access );
66             references.addAll( collection );
67         }
68         return null;
69     }
70
71     @SuppressWarnings JavaDoc("unchecked")
72     private <T extends ReferenceableXslComponent> Collection JavaDoc<Reference<T>>
73         find( Class JavaDoc<T> clazz, String JavaDoc next , AttributeAccess access)
74     {
75         assert AttributeSet.class.isAssignableFrom( clazz );
76         LinkedHashSet JavaDoc<XslModel> list = Utilities.getAvailibleModels(
77                 access.getComponent().getModel());
78         Collection JavaDoc<Reference<T>> collection = new LinkedList JavaDoc<Reference<T>>();
79         QName JavaDoc qName = getQName( next , access );
80         for (XslModel model : list) {
81            if ( Model.State.VALID.equals( model.getState() )) {
82                Stylesheet stylesheet = model.getStylesheet();
83                if ( stylesheet == null ) {
84                    continue;
85                }
86                List JavaDoc<AttributeSet> children =
87                    stylesheet.getChildren( AttributeSet.class );
88                Collection JavaDoc<AttributeSet> result = find( children , qName );
89                for (AttributeSet set : result) {
90                    XslReference<AttributeSet> ref =
91                        new GlobalReferenceImpl<AttributeSet>( set ,
92                            AttributeSet.class , access.getComponent() );
93                    if ( result != null ) {
94                        collection.add( (Reference<T>) ref );
95                    }
96                }
97            }
98         }
99         return null;
100     }
101     
102     private QName JavaDoc getQName( String JavaDoc value, AttributeAccess access ) {
103         assert value!=null;
104         String JavaDoc[] parts = value.split(":"); //NOI18N
105
String JavaDoc prefix = null;
106         String JavaDoc localPart;
107         if (parts.length == 2) {
108             prefix = parts[0];
109             localPart = parts[1];
110         } else {
111             localPart = parts[0];
112         }
113         String JavaDoc ns = access.getComponent().lookupNamespaceURI(prefix);
114         return new QName JavaDoc( ns , localPart , prefix );
115     }
116
117     private Collection JavaDoc<AttributeSet> find(
118             List JavaDoc<AttributeSet> children , QName JavaDoc qName)
119     {
120         Collection JavaDoc<AttributeSet> collection = new LinkedList JavaDoc<AttributeSet>();
121         assert qName != null;
122         for (AttributeSet set : children) {
123                QName JavaDoc name = set.getName();
124                if ( name == null ) {
125                    continue;
126                }
127                String JavaDoc localPart = name.getLocalPart();
128                String JavaDoc ns = name.getNamespaceURI();
129                if ( qName.getLocalPart().equals( localPart)
130                    && Utilities.equals( qName.getNamespaceURI() , ns ) )
131                {
132                    collection.add( set );
133                }
134        }
135        return collection;
136     }
137
138 }
139
Popular Tags