KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jicengine > element > impl > CdataConverterElementCompiler


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 JavaDoc targetClass;
17   public CdataConverterElementCompiler(String JavaDoc name, Location location, String JavaDoc[] parameters) throws ElementException
18   {
19     super(name, location);
20     
21     try {
22       Class JavaDoc targetClass = ClassParser.toClass((String JavaDoc)parameters[0]);
23       this.targetClass = targetClass;
24       
25       //System.out.println(this.targetClass);
26

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 JavaDoc[]{Element.VARIABLE_NAME_CDATA},new Class JavaDoc[]{String JavaDoc.class}, toContextVariableName(targetClass)));
32       
33     } catch (ClassNotFoundException JavaDoc e){
34       throw new ElementException("Target class '" + parameters[0] + "' not found.", getName(), getLocation());
35     }
36   }
37   
38   
39   /**
40    *
41    * @param child VariableElement
42    * @throws ElementException
43    * @return ActionElement
44    */

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 a dummy action element in return..
58
return new ActionElement(){
59       public void execute(Context context, Object JavaDoc parentInstance)
60       {
61       }
62       public String JavaDoc getName()
63       {
64         return child.getName();
65       }
66       public Location getLocation()
67       {
68         return child.getLocation();
69       }
70       public boolean isExecuted(Context outerContext, Object JavaDoc parentInstance)
71       {
72         return false;
73       }
74     };
75
76   }
77
78   public static String JavaDoc toContextVariableName(Class JavaDoc targetClass)
79   {
80     return "cdata-converter::" + targetClass.getName();
81   }
82
83 }
84
Popular Tags