KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jicengine.element.impl;
2
3 import org.jicengine.operation.OperationException;
4 import org.jicengine.operation.Context;
5 import org.jicengine.operation.Operation;
6 import org.jicengine.element.*;
7 import java.util.*;
8
9 /**
10  * NOTE: currently, every value-element is handled as an array-element. what if
11  * the element has an action that would need some of the child-value-elements? .
12  *
13  * <p>
14  * Copyright (C) 2004 Timo Laitinen
15  * </p>
16  * @author .timo
17  */

18
19 public class ArrayElementCompiler extends ElementCompiler {
20
21     /**
22      * Description of the Class
23      *
24      * @author timo
25      */

26     public class ArrayConstructor implements Operation {
27         private int size = 0;
28
29         public void increaseSize()
30         {
31             this.size++;
32         }
33
34         public int getSize()
35         {
36             return this.size;
37         }
38
39         public boolean needsParameters()
40         {
41             return false;
42         }
43
44         public boolean needsParameter(String JavaDoc name)
45         {
46             return false;
47         }
48
49         public Object JavaDoc execute(Context context) throws OperationException
50         {
51             Class JavaDoc componentType = getElement().getInstanceClass().getComponentType();
52             Object JavaDoc arrayObject = java.lang.reflect.Array.newInstance(componentType, this.size);
53             return arrayObject;
54         }
55
56         public String JavaDoc toString()
57         {
58             return "{array creator}";
59         }
60     }
61
62
63
64     private int index = 0;
65
66     public ArrayElementCompiler(String JavaDoc name, Location location) throws ElementException
67     {
68         super(name, location);
69
70         getElement().setConstructor(new ArrayConstructor());
71
72     }
73
74     public void elementInitialized() throws ElementException
75     {
76         super.elementInitialized();
77         if( !getElement().getInstanceClass().isArray() ){
78             throw new ElementException("Class '" + getElement().getInstanceClass().getName() + "' is not an array.", getName(), getLocation());
79         }
80     }
81
82     protected ActionElement handleLooseVariableElement(VariableElement child) throws ElementException
83     {
84         // new element to be stored into the array!
85
ArrayConstructor constructor = (ArrayConstructor) getElement().getConstructor();
86
87         constructor.increaseSize();
88         int size = constructor.getSize();
89         int index = size-1;
90         Operation action = new StoreToArrayOperation(index);
91         return new WrapperActionElement(child, child.getLocation(), action);
92     }
93
94     public class StoreToArrayOperation implements Operation {
95         private int index;
96         public StoreToArrayOperation(int index)
97         {
98             this.index = index;
99         }
100         public boolean needsParameter(String JavaDoc name)
101         {
102             return name.equals(Element.VARIABLE_NAME_PARENT_INSTANCE) || name.equals(Element.VARIABLE_NAME_ELEMENT_INSTANCE);
103         }
104         public boolean needsParameters()
105         {
106             return true;
107         }
108
109         public Object JavaDoc execute(Context context) throws OperationException
110         {
111             Object JavaDoc array = context.getObject(Element.VARIABLE_NAME_PARENT_INSTANCE);
112             Object JavaDoc element = context.getObject(Element.VARIABLE_NAME_ELEMENT_INSTANCE);
113
114             try {
115                 java.lang.reflect.Array.set(array, this.index, element);
116             } catch (IllegalArgumentException JavaDoc e){
117                 throw new OperationException("[array-position " + this.index + "]: failed to store value '" + element + " (class '" + element.getClass().getName() + "')", e);
118             }
119             return null;
120         }
121
122         public String JavaDoc toString()
123         {
124             return "java.lang.reflect.Array.set(" + Element.VARIABLE_NAME_PARENT_INSTANCE + ", " + this.index + ", " + Element.VARIABLE_NAME_ELEMENT_INSTANCE + ")";
125         }
126     }
127 }
128
Popular Tags