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.ChangeEventRecordable;
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.ChangeEventRecordable} to a xml
035: * element, and vice versa.
036: *
037: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
038: * @version 1.00
039: */
040: public class XMLChangeEventConverter extends XMLEventObjectConverter {
041: /**
042: * Creates a new converter.
043: *
044: * @param env the environment
045: */
046: public XMLChangeEventConverter(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.ChangeEventRecordable}.
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 ChangeEventRecordable);
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: ChangeEventRecordable ceRecordable = (ChangeEventRecordable) element;
075: Element result = (Element) super .convertElement(element);
076:
077: result.setName("ChangeEvent");
078: result.setAttribute("additionalInformation", ceRecordable
079: .getAdditionalInformation());
080:
081: return result;
082: }
083:
084: /**
085: * Returns whether this converter is able to transform the specified other representation to a
086: * structure element. This converter is responsible if the other representation is of type
087: * <code>jdom.org.Element</code> and the name of the xml element is "ChangeEvent"
088: *
089: * @param other the other representation
090: *
091: * @return <code>true</code> if this converter is responsible for this other representation;
092: * otherwise <code>false</code>.
093: */
094: public boolean handlesOther(Object other) {
095: try {
096: Element e = (Element) other;
097:
098: return e.getName().equals("ChangeEvent");
099: } catch (Throwable t) {
100: return false;
101: }
102: }
103:
104: /**
105: * Converts the specified other representation to a structure element, if this converter is
106: * responsible for it (reverses {@link #convertElement(StructureElement)}). For responsibility
107: * see {@link #handlesOther(Object)}.
108: *
109: * @param other the other representation to convert
110: *
111: * @return the key event element
112: */
113: public StructureElement convertOther(Object other) {
114: Element element = (Element) other;
115:
116: String sourceName = element.getAttributeValue("source");
117: String sourceClass = element.getAttributeValue("class");
118:
119: if (sourceClass == null) {
120: sourceClass = "";
121: }
122:
123: // workaround because of the changes relativeTime -> duration
124: String duration;
125:
126: if ((duration = element.getAttributeValue("duration")) == null) {
127: duration = element.getAttributeValue("relativeTime");
128: }
129:
130: String procTime = element.getAttributeValue("procTime");
131:
132: long durationTime = Long.parseLong(duration);
133: long procTimeTime = 0;
134:
135: if (procTime != null) {
136: procTimeTime = Long.parseLong(procTime);
137: }
138:
139: String additionalInformation = element
140: .getAttributeValue("additionalInformation");
141:
142: if (additionalInformation == null) {
143: additionalInformation = "";
144: }
145:
146: ChangeEventRecordable returnValue = new ChangeEventRecordable(
147: env, sourceName, sourceClass, durationTime,
148: procTimeTime, additionalInformation);
149:
150: String uuidString = element.getAttributeValue("uuid");
151:
152: if (uuidString == null) {
153: returnValue.setUUIDString(UUIDGen.getInstance()
154: .generateUUID());
155: } else {
156: returnValue.setUUIDString(uuidString);
157: }
158:
159: return returnValue;
160: }
161: }
|