1 package org.jicengine.element.impl; 2 3 import org.jicengine.element.ActionElement; 4 import org.jicengine.element.CdataHandler; 5 import org.jicengine.element.Element; 6 import org.jicengine.element.ElementCompiler; 7 import org.jicengine.element.ElementException; 8 import org.jicengine.element.Location; 9 import org.jicengine.element.VariableElement; 10 import org.jicengine.expression.ClassParser; 11 import org.jicengine.expression.FactoryInvocationParser; 12 import org.jicengine.operation.Context; 13 14 public class CdataConverterElementCompiler extends ElementCompiler { 15 16 private Class targetClass; 17 public CdataConverterElementCompiler(String name, Location location, String [] parameters) throws ElementException 18 { 19 super(name, location); 20 21 try { 22 Class targetClass = ClassParser.toClass((String )parameters[0]); 23 this.targetClass = targetClass; 24 25 27 if( CdataHandler.isDefaultCdataConversionType(targetClass)){ 28 throw new ElementException("User-defined CDATA conversion can not override default conversion for '" + parameters[0] + "'", getName(), getLocation()); 29 } 30 31 getElement().setAction(new RegisterFactoryOperation(name,new String []{Element.VARIABLE_NAME_CDATA},new Class []{String .class}, toContextVariableName(targetClass))); 32 33 } catch (ClassNotFoundException e){ 34 throw new ElementException("Target class '" + parameters[0] + "' not found.", getName(), getLocation()); 35 } 36 } 37 38 39 45 protected ActionElement handleLooseVariableElement(final VariableElement child) throws ElementException 46 { 47 if( ((RegisterFactoryOperation)getElement().getAction()).getFactoryElement() != null ){ 48 throw new ElementException("Illegal child " + child + ". Only single element is allowed inside element of type 'factory'.", getName(), getLocation()); 49 } 50 51 if( !org.jicengine.operation.ReflectionUtils.isAssignableFrom(this.targetClass, child.getInstanceClass()) ){ 52 throw new ElementException("Expected '" + child.getName() + "' to be '" + this.targetClass.getName() + "', was '" + child.getInstanceClass() + "'", getName(), getLocation()); 53 } 54 55 ((RegisterFactoryOperation)getElement().getAction()).setFactoryElement(child); 56 57 return new ActionElement(){ 59 public void execute(Context context, Object parentInstance) 60 { 61 } 62 public String getName() 63 { 64 return child.getName(); 65 } 66 public Location getLocation() 67 { 68 return child.getLocation(); 69 } 70 public boolean isExecuted(Context outerContext, Object parentInstance) 71 { 72 return false; 73 } 74 }; 75 76 } 77 78 public static String toContextVariableName(Class targetClass) 79 { 80 return "cdata-converter::" + targetClass.getName(); 81 } 82 83 } 84 | Popular Tags |