001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package jacareto.struct;
025:
026: import jacareto.parse.RecordTokenizer;
027: import jacareto.system.Environment;
028:
029: import java.lang.reflect.Constructor;
030: import java.lang.reflect.InvocationTargetException;
031:
032: /**
033: * A help structure for importing structures from xml files.
034: *
035: * @author <a href="mailto:amrkus.bois@web.de">Markus Bois</a>
036: * @version 1.0
037: */
038: public class SymbolStructureElement extends StructureElement {
039: /** The name of the class that this represents */
040: private String className;
041:
042: /**
043: * Creates a new help structure element.
044: *
045: * @param env the environment
046: * @param children the child structure elements
047: */
048: public SymbolStructureElement(Environment env,
049: StructureElement[] children) {
050: this (env, children, "");
051: }
052:
053: /**
054: * Creates a new help structure element.
055: *
056: * @param env the environment
057: * @param children the child structure elements
058: * @param className the name of the represented class
059: */
060: public SymbolStructureElement(Environment env,
061: StructureElement[] children, String className) {
062: super (env, children);
063: this .className = className;
064: }
065:
066: /**
067: * Create the correct structure element of the base of itself. It creates the new class for the
068: * stored class name and the added children. The children will replaced if they are a {@link
069: * jacareto.record.PositionRecordable} with the correct recordable;
070: *
071: * @param classLoader the array of the recordables
072: *
073: * @return the corret structure element
074: */
075: public StructureElement createRealElement(ClassLoader classLoader) {
076: StructureElement result = null;
077:
078: try {
079: if (classLoader == null) {
080: classLoader = ClassLoader.getSystemClassLoader();
081: }
082:
083: Class structureClass = Class.forName(className, true,
084: classLoader);
085: Class[] parameterTypes = new Class[2];
086: parameterTypes[0] = Class.forName(
087: "jacareto.system.Environment", true, classLoader);
088: parameterTypes[1] = Class.forName(
089: "[Ljacareto.struct.StructureElement;", true,
090: classLoader);
091:
092: Object[] parameters = new Object[2];
093: parameters[0] = env;
094: parameters[1] = getChildren();
095:
096: try {
097: Class concreteStructureClass = Class.forName(className,
098: true, classLoader);
099: Constructor structureConstructor = concreteStructureClass
100: .getConstructor(parameterTypes);
101:
102: if (structureClass
103: .isAssignableFrom(concreteStructureClass)) {
104: try {
105: logger
106: .debug(language
107: .getString("Structures.SymbolStructureElement.Msg.Construction")
108: + " " + className);
109: result = (StructureElement) structureConstructor
110: .newInstance(parameters);
111: } catch (InstantiationException inst) {
112: logger
113: .error(
114: language
115: .getString("Structures.SymbolStructureElement.Error.Construction"),
116: inst);
117: inst.printStackTrace();
118: } catch (IllegalAccessException ill) {
119: logger
120: .error(
121: language
122: .getString("Structures.SymbolStructureElement.Error.Construction"),
123: ill);
124: ill.printStackTrace();
125: } catch (IllegalArgumentException ila) {
126: logger
127: .error(
128: language
129: .getString("Structures.SymbolStructureElement.Error.Construction"),
130: ila);
131: ila.printStackTrace();
132: } catch (InvocationTargetException inv) {
133: logger
134: .error(
135: language
136: .getString("Structures.SymbolStructureElement.Error.Construction"),
137: inv);
138: inv.printStackTrace();
139: }
140: } else {
141: logger
142: .warn(language
143: .getString("Structures.SymbolStructureElement.Msg.NotSubclass")
144: + ": " + className);
145: }
146: } catch (ClassNotFoundException c) {
147: logger
148: .error(
149: language
150: .getString("Structures.SymbolStructureElement.Error.MissingClass")
151: + ": " + className, c);
152: } catch (NoSuchMethodException n) {
153: logger
154: .error(
155: "Class: "
156: + className
157: + " - Parameters: "
158: + parameterTypes
159: + language
160: .getString("Structures.SymbolStructureElement.Error.MissingConstructor"),
161: n);
162: }
163: } catch (ClassNotFoundException c) {
164: logger
165: .error(
166: language
167: .getString("Structures.SymbolStructureElement.Error.MissingClass"),
168: c);
169: }
170:
171: return result;
172: }
173:
174: /**
175: * Parses a record which is tokenized by the given record tokenizer.
176: *
177: * @param env DOCUMENT ME!
178: * @param recordTokenizer the record tokenizer
179: *
180: * @return a structure element, or <code>null</code> if this class cannot parse the record at
181: * the current position
182: */
183: public static StructureElement parse(Environment env,
184: RecordTokenizer recordTokenizer) {
185: return null;
186: }
187:
188: /**
189: * Returns the name of the help structure
190: *
191: * @return the name
192: */
193: public String getElementName() {
194: return getLanguage().getString(
195: "Structures.SymbolStructureElement.Name");
196: }
197:
198: /**
199: * Returns a description of the help structure.
200: *
201: * @return the description
202: */
203: public String getElementDescription() {
204: return getLanguage().getString(
205: "Structures.SymbolStructureElement.Description");
206: }
207:
208: /**
209: * Returns a String which describes the content of the sturcture shortly.
210: *
211: * @return a string with a short description of the object
212: */
213: public String toShortString() {
214: return getElementName() + " for " + className;
215: }
216:
217: /**
218: * Clones the element.
219: *
220: * @return DOCUMENT ME!
221: */
222: public Object clone() {
223: StructureElement[] clonedChildren = getClonedChildren();
224:
225: return new SymbolStructureElement(env, clonedChildren,
226: className);
227: }
228: }
|