001: package org.jicengine.element.impl;
002:
003: import org.jicengine.element.*;
004: import org.jicengine.expression.FactoryInvocationParser;
005: import org.jicengine.operation.*;
006:
007: /**
008: * <p> </p>
009: *
010: * <p> </p>
011: *
012: * <p> </p>
013: *
014: * <p> </p>
015: *
016: * @author timo laitinen
017: */
018: public class FactoryElementCompiler extends ElementCompiler {
019:
020: public FactoryElementCompiler(String name, Location location)
021: throws ElementException {
022: this (name, location, new String[0]);
023: }
024:
025: public FactoryElementCompiler(String name, Location location,
026: String[] parameters) throws ElementException {
027: super (name, location);
028:
029: String[] parameterNames = new String[parameters.length];
030: Class[] parameterTypes = new Class[parameters.length];
031:
032: for (int i = 0; i < parameters.length; i++) {
033: try {
034: String paramDef = parameters[i].trim();
035: int spaceIndex = paramDef.indexOf(" ");
036: parameterTypes[i] = org.jicengine.expression.ClassParser
037: .toClass(paramDef.substring(0, spaceIndex));
038: parameterNames[i] = paramDef.substring(spaceIndex + 1)
039: .trim();
040: } catch (Exception e) {
041: throw new ElementException(
042: "Problems with parameter definition '"
043: + parameters[i]
044: + "'. Expected format '[class] [name]'",
045: getName(), getLocation());
046: }
047: }
048: getElement().setAction(
049: new RegisterFactoryOperation(name, parameterNames,
050: parameterTypes,
051: FactoryInvocationParser.FACTORY_NAME_PREFIX
052: + name));
053: }
054:
055: /**
056: *
057: * @param child VariableElement
058: * @throws ElementException
059: * @return ActionElement
060: */
061: protected ActionElement handleLooseVariableElement(
062: final VariableElement child) throws ElementException {
063: if (((RegisterFactoryOperation) getElement().getAction())
064: .getFactoryElement() != null) {
065: throw new ElementException(
066: "Illegal child "
067: + child
068: + ". Only single element is allowed inside element of type 'factory'.",
069: getName(), getLocation());
070: }
071:
072: ((RegisterFactoryOperation) getElement().getAction())
073: .setFactoryElement(child);
074:
075: // return a dummy action element in return..
076: return new ActionElement() {
077: public void execute(Context context, Object parentInstance) {
078: }
079:
080: public String getName() {
081: return child.getName();
082: }
083:
084: public Location getLocation() {
085: return child.getLocation();
086: }
087:
088: public boolean isExecuted(Context outerContext,
089: Object parentInstance) {
090: return false;
091: }
092: };
093:
094: }
095:
096: /*
097: private static Context getRootContext(Context context)
098: {
099: if( context instanceof LocalContext){
100: return getRootContext(((LocalContext)context).getParent());
101: }
102: else {
103: return context;
104: }
105: }
106: */
107: }
|