001: package org.jicengine.element.impl;
002:
003: import org.jicengine.operation.OperationException;
004: import org.jicengine.operation.Context;
005: import org.jicengine.operation.Operation;
006: import org.jicengine.element.*;
007: import java.util.*;
008:
009: /**
010: * NOTE: currently, every value-element is handled as an array-element. what if
011: * the element has an action that would need some of the child-value-elements? .
012: *
013: * <p>
014: * Copyright (C) 2004 Timo Laitinen
015: * </p>
016: * @author .timo
017: */
018:
019: public class ArrayElementCompiler extends ElementCompiler {
020:
021: /**
022: * Description of the Class
023: *
024: * @author timo
025: */
026: public class ArrayConstructor implements Operation {
027: private int size = 0;
028:
029: public void increaseSize() {
030: this .size++;
031: }
032:
033: public int getSize() {
034: return this .size;
035: }
036:
037: public boolean needsParameters() {
038: return false;
039: }
040:
041: public boolean needsParameter(String name) {
042: return false;
043: }
044:
045: public Object execute(Context context)
046: throws OperationException {
047: Class componentType = getElement().getInstanceClass()
048: .getComponentType();
049: Object arrayObject = java.lang.reflect.Array.newInstance(
050: componentType, this .size);
051: return arrayObject;
052: }
053:
054: public String toString() {
055: return "{array creator}";
056: }
057: }
058:
059: private int index = 0;
060:
061: public ArrayElementCompiler(String name, Location location)
062: throws ElementException {
063: super (name, location);
064:
065: getElement().setConstructor(new ArrayConstructor());
066:
067: }
068:
069: public void elementInitialized() throws ElementException {
070: super .elementInitialized();
071: if (!getElement().getInstanceClass().isArray()) {
072: throw new ElementException("Class '"
073: + getElement().getInstanceClass().getName()
074: + "' is not an array.", getName(), getLocation());
075: }
076: }
077:
078: protected ActionElement handleLooseVariableElement(
079: VariableElement child) throws ElementException {
080: // new element to be stored into the array!
081: ArrayConstructor constructor = (ArrayConstructor) getElement()
082: .getConstructor();
083:
084: constructor.increaseSize();
085: int size = constructor.getSize();
086: int index = size - 1;
087: Operation action = new StoreToArrayOperation(index);
088: return new WrapperActionElement(child, child.getLocation(),
089: action);
090: }
091:
092: public class StoreToArrayOperation implements Operation {
093: private int index;
094:
095: public StoreToArrayOperation(int index) {
096: this .index = index;
097: }
098:
099: public boolean needsParameter(String name) {
100: return name.equals(Element.VARIABLE_NAME_PARENT_INSTANCE)
101: || name
102: .equals(Element.VARIABLE_NAME_ELEMENT_INSTANCE);
103: }
104:
105: public boolean needsParameters() {
106: return true;
107: }
108:
109: public Object execute(Context context)
110: throws OperationException {
111: Object array = context
112: .getObject(Element.VARIABLE_NAME_PARENT_INSTANCE);
113: Object element = context
114: .getObject(Element.VARIABLE_NAME_ELEMENT_INSTANCE);
115:
116: try {
117: java.lang.reflect.Array.set(array, this .index, element);
118: } catch (IllegalArgumentException e) {
119: throw new OperationException("[array-position "
120: + this .index + "]: failed to store value '"
121: + element + " (class '"
122: + element.getClass().getName() + "')", e);
123: }
124: return null;
125: }
126:
127: public String toString() {
128: return "java.lang.reflect.Array.set("
129: + Element.VARIABLE_NAME_PARENT_INSTANCE + ", "
130: + this .index + ", "
131: + Element.VARIABLE_NAME_ELEMENT_INSTANCE + ")";
132: }
133: }
134: }
|