KickJava   Java API By Example, From Geeks To Geeks.

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


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.math.BigDecimal JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.LinkedList JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.StringTokenizer JavaDoc;
27
28 import javax.xml.namespace.QName JavaDoc;
29
30 import org.netbeans.modules.xml.xam.dom.Attribute;
31 import org.netbeans.modules.xslt.model.AttributeValueTemplate;
32 import org.netbeans.modules.xslt.model.QualifiedNameable;
33 import org.netbeans.modules.xslt.model.ReferenceableXslComponent;
34 import org.netbeans.modules.xslt.model.XslReference;
35 import org.netbeans.modules.xslt.model.enums.EnumValue;
36
37
38 /**
39  * @author ads
40  *
41  */

42 class AttributeAccess {
43
44     AttributeAccess( XslComponentImpl component ){
45         myComponent = component;
46     }
47     
48     Object JavaDoc getAttributeValueOf( Attribute attr, String JavaDoc stringValue )
49     {
50         if (stringValue == null) {
51             return null;
52         }
53         Class JavaDoc clazz = attr.getType();
54         if (String JavaDoc.class.isAssignableFrom(clazz)) {
55             return stringValue;
56         }
57         else {
58             for( AttributeValueFactory factory : Lazy.FACTORIES ) {
59                 if ( factory.isApplicable(attr)) {
60                     return factory.getValue(this, attr, stringValue);
61                 }
62             }
63         }
64         assert false;
65         return null;
66     }
67     
68     <T extends QualifiedNameable> GlobalReferenceImpl<T> resolveGlobalReference(
69             Class JavaDoc<T> clazz, String JavaDoc value )
70     {
71         return value == null ? null : new GlobalReferenceImpl<T>(clazz,
72                 getComponent(),value);
73     }
74     
75     <T extends QualifiedNameable> List JavaDoc<XslReference<T>>
76         resolveGlobalReferenceList(Class JavaDoc<T> clazz, String JavaDoc value )
77     {
78         if (value == null) {
79             return null;
80         }
81         for( ReferenceListResolveFactory factory :
82             ReferenceListResolveFactory.Factories.FACTORIES)
83         {
84             if ( factory.isApplicable( clazz )) {
85                 return factory.resolve( this , clazz, value);
86             }
87         }
88         
89         // in the case when we didn't find appropriate custom resolver
90
// we use standard resolving
91
StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(value, " ");
92         List JavaDoc<XslReference<T>> references = new LinkedList JavaDoc<XslReference<T>>();
93         while (tokenizer.hasMoreTokens()) {
94             String JavaDoc next = tokenizer.nextToken();
95             XslReference<T> ref = resolveGlobalReference(clazz, next);
96             references.add(ref);
97         }
98         return Collections.unmodifiableList(references);
99     }
100     
101     List JavaDoc<QName JavaDoc> getQNameList( String JavaDoc value ){
102         if (value == null) {
103             return null;
104         }
105         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(value, " ");
106         List JavaDoc<QName JavaDoc> result = new LinkedList JavaDoc<QName JavaDoc>();
107         while (tokenizer.hasMoreTokens()) {
108             String JavaDoc next = tokenizer.nextToken();
109             result.add( QNameBuilder.createQName( getComponent(), next) );
110         }
111         return Collections.unmodifiableList(result);
112     }
113     
114     XslComponentImpl getComponent() {
115         return myComponent;
116     }
117     
118     static class Lazy {
119         
120         static final Collection JavaDoc<AttributeValueFactory> FACTORIES =
121             new LinkedList JavaDoc<AttributeValueFactory>();
122         
123         static {
124             FACTORIES.add( new EnumValueFactory() );
125             FACTORIES.add( new DoubleValueFactory() );
126             FACTORIES.add( new AttributeValueTemplateFactory() );
127             FACTORIES.add( new QNameValueFactory() );
128             FACTORIES.add( new BigDecimalValueFactory() );
129             FACTORIES.add( new GlobalReferenceValueFactory() );
130             FACTORIES.add( new ReferncesListValueFactory() );
131             FACTORIES.add( new StringListValueFactory() );
132         }
133     }
134     
135     private XslComponentImpl myComponent;
136 }
137
138 interface AttributeValueFactory {
139     boolean isApplicable( Attribute attribute );
140     
141     Object JavaDoc getValue( AttributeAccess access , Attribute attribute,
142             String JavaDoc value );
143 }
144
145 class EnumValueFactory implements AttributeValueFactory {
146
147     @SuppressWarnings JavaDoc("unchecked")
148     public Object JavaDoc getValue( AttributeAccess access, Attribute attribute,
149             String JavaDoc value )
150     {
151         Class JavaDoc clazz = attribute.getType();
152         Object JavaDoc[] objs = clazz.getEnumConstants();
153         assert clazz.isAssignableFrom( EnumValue.class );
154         
155         Object JavaDoc invalid = null;
156         for (Object JavaDoc object : objs) {
157             if ( ((EnumValue)object).isInvalid() ) {
158                 invalid = object;
159             }
160             if ( value.equals( object.toString()) ) {
161                 return object;
162             }
163         }
164         return invalid;
165     }
166
167     public boolean isApplicable( Attribute attribute ) {
168         return Enum JavaDoc.class.isAssignableFrom( attribute.getType() );
169     }
170     
171 }
172
173 class DoubleValueFactory implements AttributeValueFactory {
174
175     public Object JavaDoc getValue( AttributeAccess access, Attribute attribute,
176             String JavaDoc value )
177     {
178         return Double.parseDouble( value );
179     }
180
181     public boolean isApplicable( Attribute attribute ) {
182         return Double JavaDoc.class.isAssignableFrom( attribute.getType() );
183     }
184     
185 }
186
187 class AttributeValueTemplateFactory implements AttributeValueFactory {
188
189     public Object JavaDoc getValue( AttributeAccess access, Attribute attribute,
190             String JavaDoc value )
191     {
192         return AttributeValueTemplateImpl.creatAttributeValueTemplate(
193                 access.getComponent() , value );
194     }
195
196     public boolean isApplicable( Attribute attribute ) {
197         return AttributeValueTemplate.class.isAssignableFrom( attribute.getType() );
198     }
199     
200 }
201
202 class QNameValueFactory implements AttributeValueFactory {
203
204     public Object JavaDoc getValue( AttributeAccess access, Attribute attribute, String JavaDoc value ) {
205         return QNameBuilder.createQName( access.getComponent() , value );
206     }
207
208     public boolean isApplicable( Attribute attribute ) {
209         return QName JavaDoc.class.isAssignableFrom( attribute.getType() );
210     }
211     
212 }
213
214 class BigDecimalValueFactory implements AttributeValueFactory {
215
216     public Object JavaDoc getValue( AttributeAccess access, Attribute attribute,
217             String JavaDoc value )
218     {
219         BigDecimal JavaDoc dec = null;
220         try {
221             dec = new BigDecimal JavaDoc( value );
222         }
223         catch( NumberFormatException JavaDoc exc ) {
224             // ignore this exception
225
}
226         return dec;
227     }
228
229     public boolean isApplicable( Attribute attribute ) {
230         return BigDecimal JavaDoc.class.isAssignableFrom( attribute.getType() );
231     }
232     
233 }
234
235 class GlobalReferenceValueFactory implements AttributeValueFactory {
236
237     @SuppressWarnings JavaDoc("unchecked")
238     public Object JavaDoc getValue( AttributeAccess access, Attribute attribute,
239             String JavaDoc value )
240     {
241         return access.resolveGlobalReference(
242                 (Class JavaDoc<? extends QualifiedNameable>)attribute.getClass(),
243                 value );
244     }
245
246     public boolean isApplicable( Attribute attribute ) {
247         return ReferenceableXslComponent.class.isAssignableFrom(
248                 attribute.getType() );
249     }
250     
251 }
252
253 class ReferncesListValueFactory implements AttributeValueFactory {
254
255     @SuppressWarnings JavaDoc("unchecked")
256     public Object JavaDoc getValue( AttributeAccess access, Attribute attribute,
257             String JavaDoc value )
258     {
259         return access.resolveGlobalReferenceList( attribute.getMemberType() ,
260                 value );
261     }
262
263     @SuppressWarnings JavaDoc("unchecked")
264     public boolean isApplicable( Attribute attribute ) {
265         return List JavaDoc.class.isAssignableFrom( attribute.getType() ) &&
266             attribute.getMemberType().isAssignableFrom(
267                     ReferenceableXslComponent.class );
268     }
269     
270 }
271
272 class StringListValueFactory implements AttributeValueFactory {
273
274     public Object JavaDoc getValue( AttributeAccess access, Attribute attribute, String JavaDoc value ) {
275         if (value == null) {
276             return null;
277         }
278         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(value, " ");
279         List JavaDoc<String JavaDoc> ret = new LinkedList JavaDoc<String JavaDoc>();
280         while (tokenizer.hasMoreTokens()) {
281             String JavaDoc next = tokenizer.nextToken();
282             ret.add(next);
283         }
284         return Collections.unmodifiableList(ret);
285     }
286
287     @SuppressWarnings JavaDoc("unchecked")
288     public boolean isApplicable( Attribute attribute ) {
289         return List JavaDoc.class.isAssignableFrom( attribute.getType() ) &&
290             attribute.getMemberType().isAssignableFrom( String JavaDoc.class );
291     }
292     
293 }
Popular Tags