1 19 package org.netbeans.modules.xslt.model.impl; 20 21 import java.math.BigDecimal ; 22 import java.util.Collection ; 23 import java.util.Collections ; 24 import java.util.LinkedList ; 25 import java.util.List ; 26 import java.util.StringTokenizer ; 27 28 import javax.xml.namespace.QName ; 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 42 class AttributeAccess { 43 44 AttributeAccess( XslComponentImpl component ){ 45 myComponent = component; 46 } 47 48 Object getAttributeValueOf( Attribute attr, String stringValue ) 49 { 50 if (stringValue == null) { 51 return null; 52 } 53 Class clazz = attr.getType(); 54 if (String .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 <T> clazz, String value ) 70 { 71 return value == null ? null : new GlobalReferenceImpl<T>(clazz, 72 getComponent(),value); 73 } 74 75 <T extends QualifiedNameable> List <XslReference<T>> 76 resolveGlobalReferenceList(Class <T> clazz, String 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 StringTokenizer tokenizer = new StringTokenizer (value, " "); 92 List <XslReference<T>> references = new LinkedList <XslReference<T>>(); 93 while (tokenizer.hasMoreTokens()) { 94 String next = tokenizer.nextToken(); 95 XslReference<T> ref = resolveGlobalReference(clazz, next); 96 references.add(ref); 97 } 98 return Collections.unmodifiableList(references); 99 } 100 101 List <QName > getQNameList( String value ){ 102 if (value == null) { 103 return null; 104 } 105 StringTokenizer tokenizer = new StringTokenizer (value, " "); 106 List <QName > result = new LinkedList <QName >(); 107 while (tokenizer.hasMoreTokens()) { 108 String 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 <AttributeValueFactory> FACTORIES = 121 new LinkedList <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 getValue( AttributeAccess access , Attribute attribute, 142 String value ); 143 } 144 145 class EnumValueFactory implements AttributeValueFactory { 146 147 @SuppressWarnings ("unchecked") 148 public Object getValue( AttributeAccess access, Attribute attribute, 149 String value ) 150 { 151 Class clazz = attribute.getType(); 152 Object [] objs = clazz.getEnumConstants(); 153 assert clazz.isAssignableFrom( EnumValue.class ); 154 155 Object invalid = null; 156 for (Object 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 .class.isAssignableFrom( attribute.getType() ); 169 } 170 171 } 172 173 class DoubleValueFactory implements AttributeValueFactory { 174 175 public Object getValue( AttributeAccess access, Attribute attribute, 176 String value ) 177 { 178 return Double.parseDouble( value ); 179 } 180 181 public boolean isApplicable( Attribute attribute ) { 182 return Double .class.isAssignableFrom( attribute.getType() ); 183 } 184 185 } 186 187 class AttributeValueTemplateFactory implements AttributeValueFactory { 188 189 public Object getValue( AttributeAccess access, Attribute attribute, 190 String 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 getValue( AttributeAccess access, Attribute attribute, String value ) { 205 return QNameBuilder.createQName( access.getComponent() , value ); 206 } 207 208 public boolean isApplicable( Attribute attribute ) { 209 return QName .class.isAssignableFrom( attribute.getType() ); 210 } 211 212 } 213 214 class BigDecimalValueFactory implements AttributeValueFactory { 215 216 public Object getValue( AttributeAccess access, Attribute attribute, 217 String value ) 218 { 219 BigDecimal dec = null; 220 try { 221 dec = new BigDecimal ( value ); 222 } 223 catch( NumberFormatException exc ) { 224 } 226 return dec; 227 } 228 229 public boolean isApplicable( Attribute attribute ) { 230 return BigDecimal .class.isAssignableFrom( attribute.getType() ); 231 } 232 233 } 234 235 class GlobalReferenceValueFactory implements AttributeValueFactory { 236 237 @SuppressWarnings ("unchecked") 238 public Object getValue( AttributeAccess access, Attribute attribute, 239 String value ) 240 { 241 return access.resolveGlobalReference( 242 (Class <? 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 ("unchecked") 256 public Object getValue( AttributeAccess access, Attribute attribute, 257 String value ) 258 { 259 return access.resolveGlobalReferenceList( attribute.getMemberType() , 260 value ); 261 } 262 263 @SuppressWarnings ("unchecked") 264 public boolean isApplicable( Attribute attribute ) { 265 return List .class.isAssignableFrom( attribute.getType() ) && 266 attribute.getMemberType().isAssignableFrom( 267 ReferenceableXslComponent.class ); 268 } 269 270 } 271 272 class StringListValueFactory implements AttributeValueFactory { 273 274 public Object getValue( AttributeAccess access, Attribute attribute, String value ) { 275 if (value == null) { 276 return null; 277 } 278 StringTokenizer tokenizer = new StringTokenizer (value, " "); 279 List <String > ret = new LinkedList <String >(); 280 while (tokenizer.hasMoreTokens()) { 281 String next = tokenizer.nextToken(); 282 ret.add(next); 283 } 284 return Collections.unmodifiableList(ret); 285 } 286 287 @SuppressWarnings ("unchecked") 288 public boolean isApplicable( Attribute attribute ) { 289 return List .class.isAssignableFrom( attribute.getType() ) && 290 attribute.getMemberType().isAssignableFrom( String .class ); 291 } 292 293 } | Popular Tags |