001: package org.jicengine.element.impl;
002:
003: import org.jicengine.element.ActionElement;
004: import org.jicengine.element.CdataHandler;
005: import org.jicengine.element.Element;
006: import org.jicengine.element.ElementCompiler;
007: import org.jicengine.element.ElementException;
008: import org.jicengine.element.Location;
009: import org.jicengine.element.VariableElement;
010: import org.jicengine.expression.ClassParser;
011: import org.jicengine.expression.FactoryInvocationParser;
012: import org.jicengine.operation.Context;
013:
014: public class CdataConverterElementCompiler extends ElementCompiler {
015:
016: private Class targetClass;
017:
018: public CdataConverterElementCompiler(String name,
019: Location location, String[] parameters)
020: throws ElementException {
021: super (name, location);
022:
023: try {
024: Class targetClass = ClassParser
025: .toClass((String) parameters[0]);
026: this .targetClass = targetClass;
027:
028: //System.out.println(this.targetClass);
029:
030: if (CdataHandler.isDefaultCdataConversionType(targetClass)) {
031: throw new ElementException(
032: "User-defined CDATA conversion can not override default conversion for '"
033: + parameters[0] + "'", getName(),
034: getLocation());
035: }
036:
037: getElement()
038: .setAction(
039: new RegisterFactoryOperation(
040: name,
041: new String[] { Element.VARIABLE_NAME_CDATA },
042: new Class[] { String.class },
043: toContextVariableName(targetClass)));
044:
045: } catch (ClassNotFoundException e) {
046: throw new ElementException("Target class '" + parameters[0]
047: + "' not found.", getName(), getLocation());
048: }
049: }
050:
051: /**
052: *
053: * @param child VariableElement
054: * @throws ElementException
055: * @return ActionElement
056: */
057: protected ActionElement handleLooseVariableElement(
058: final VariableElement child) throws ElementException {
059: if (((RegisterFactoryOperation) getElement().getAction())
060: .getFactoryElement() != null) {
061: throw new ElementException(
062: "Illegal child "
063: + child
064: + ". Only single element is allowed inside element of type 'factory'.",
065: getName(), getLocation());
066: }
067:
068: if (!org.jicengine.operation.ReflectionUtils.isAssignableFrom(
069: this .targetClass, child.getInstanceClass())) {
070: throw new ElementException("Expected '" + child.getName()
071: + "' to be '" + this .targetClass.getName()
072: + "', was '" + child.getInstanceClass() + "'",
073: getName(), getLocation());
074: }
075:
076: ((RegisterFactoryOperation) getElement().getAction())
077: .setFactoryElement(child);
078:
079: // return a dummy action element in return..
080: return new ActionElement() {
081: public void execute(Context context, Object parentInstance) {
082: }
083:
084: public String getName() {
085: return child.getName();
086: }
087:
088: public Location getLocation() {
089: return child.getLocation();
090: }
091:
092: public boolean isExecuted(Context outerContext,
093: Object parentInstance) {
094: return false;
095: }
096: };
097:
098: }
099:
100: public static String toContextVariableName(Class targetClass) {
101: return "cdata-converter::" + targetClass.getName();
102: }
103:
104: }
|