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.convert.xml;
025:
026: import jacareto.record.ActionEventRecordable;
027: import jacareto.struct.StructureElement;
028: import jacareto.system.Environment;
029: import jacareto.toolkit.UUIDGen;
030:
031: import org.jdom.Element;
032:
033: /**
034: * A converter which is able to convert a {@link jacareto.record.ActionEventRecordable} to a xml
035: * element, and vice versa.
036: *
037: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
038: * @version 1.03
039: */
040: public class XMLActionEventConverter extends XMLAWTEventConverter {
041: /**
042: * Creates a new converter.
043: *
044: * @param env the environment
045: */
046: public XMLActionEventConverter(Environment env) {
047: super (env);
048: }
049:
050: /**
051: * Returns whether this converter is able to transform the specified structure element to an
052: * other representation. This converter is responsible for the given structure element if it
053: * is of type {@link jacareto.record.ActionEventRecordable}.
054: *
055: * @param element the structure element
056: *
057: * @return <code>true</code> if this converter is responsible for this structure element;
058: * otherwise <code>false</code>.
059: */
060: public boolean handlesElement(StructureElement element) {
061: return (element != null)
062: && (element instanceof ActionEventRecordable);
063: }
064:
065: /**
066: * Converts the specified element to an other representation, if this converter is responsible
067: * for it. For responsibility see {@link #handlesElement(StructureElement)}.
068: *
069: * @param element the structure element to convert
070: *
071: * @return the other representation
072: */
073: public Object convertElement(StructureElement element) {
074: ActionEventRecordable actionEventRecordable = (ActionEventRecordable) element;
075:
076: Element result = (Element) super .convertElement(element);
077:
078: result.setName("ActionEvent");
079:
080: String actionCommand = actionEventRecordable.getActionCommand();
081:
082: if (actionCommand == null) {
083: actionCommand = "";
084: }
085:
086: result.setAttribute("command", actionCommand);
087: result.setAttribute("modifiers", ""
088: + actionEventRecordable.getModifiers());
089:
090: return result;
091: }
092:
093: /**
094: * Returns whether this converter is able to transform the specified other representation to a
095: * structure element. This converter is responsible if the other representation is of type
096: * <code>jdom.org.Element</code> and the name of the xml element is "ActionEvent"
097: *
098: * @param other the other representation
099: *
100: * @return <code>true</code> if this converter is responsible for this other representation;
101: * otherwise <code>false</code>.
102: */
103: public boolean handlesOther(Object other) {
104: try {
105: Element e = (Element) other;
106:
107: return e.getName().equals("ActionEvent");
108: } catch (Throwable t) {
109: return false;
110: }
111: }
112:
113: /**
114: * Converts the specified other representation to a structure element, if this converter is
115: * responsible for it (reverses {@link #convertElement(StructureElement)}). For responsibility
116: * see {@link #handlesOther(Object)}.
117: *
118: * @param other the other representation to convert
119: *
120: * @return the key event element
121: */
122: public StructureElement convertOther(Object other) {
123: Element element = (Element) other;
124:
125: String sourceName = element.getAttributeValue("source");
126: String sourceClass = element.getAttributeValue("class");
127:
128: if (sourceClass == null) {
129: sourceClass = "";
130: }
131:
132: int ID = Integer.parseInt(element.getAttributeValue("ID")
133: .toString());
134:
135: // workaround because of the changes relativeTime -> duration
136: String duration;
137:
138: if ((duration = element.getAttributeValue("duration")) == null) {
139: duration = element.getAttributeValue("relativeTime");
140: }
141:
142: String procTime = element.getAttributeValue("procTime");
143:
144: long durationTime = Long.parseLong(duration);
145: long procTimeTime = 0;
146:
147: if (procTime != null) {
148: procTimeTime = Long.parseLong(procTime);
149: }
150:
151: String command = element.getAttributeValue("command");
152: int modifiers = Integer.parseInt(element
153: .getAttributeValue("modifiers"));
154:
155: ActionEventRecordable returnValue = new ActionEventRecordable(
156: env, ID, sourceName, sourceClass, durationTime,
157: procTimeTime, modifiers, command);
158:
159: String uuidString = element.getAttributeValue("uuid");
160:
161: if (uuidString == null) {
162: returnValue.setUUIDString(UUIDGen.getInstance()
163: .generateUUID());
164: } else {
165: returnValue.setUUIDString(uuidString);
166: }
167:
168: return returnValue;
169: }
170: }
|