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.conceptualmodel;
025:
026: import jacareto.conceptualmodel.ConceptAttributeElement;
027: import jacareto.conceptualmodel.ConceptualModelElement;
028: import jacareto.convert.RecursiveConceptualModelConverter;
029: import jacareto.convert.SelectionConceptualModelConverter;
030: import jacareto.system.Environment;
031:
032: import org.jdom.Element;
033:
034: import java.util.Iterator;
035:
036: /**
037: * A converter which is able to convert task to xml representations, and vice versa.
038: *
039: * @author <a href="mailto:markus.bois@web.de">Markus Bois</a>
040: * @version 1.02
041: */
042: public class XMLConceptAttributeConverter extends
043: RecursiveConceptualModelConverter {
044: /**
045: * Creates a new converter.
046: *
047: * @param env DOCUMENT ME!
048: */
049: public XMLConceptAttributeConverter(Environment env) {
050: super (env);
051: setSelectionConceptualModelConverter(new SelectionConceptualModelConverter(
052: env, "ConceptualModelElements.Converters",
053: SelectionConceptualModelConverter.INIT_CUSTOM));
054: }
055:
056: /**
057: * Creates a new recursive converter
058: *
059: * @param env the enviroment
060: * @param selectionConceptualModelConverter the available converters
061: */
062: public XMLConceptAttributeConverter(
063: Environment env,
064: SelectionConceptualModelConverter selectionConceptualModelConverter) {
065: super (env);
066: setSelectionConceptualModelConverter(selectionConceptualModelConverter);
067: }
068:
069: /**
070: * Returns whether this converter is able to transform the specified conceptual-model element
071: * to an internal structure representation. This converter is responsible for the given
072: * conceptual-model element if it is of type {@link
073: * jacareto.conceptualmodel.ConceptAttributeElement}.
074: *
075: * @param element the conceptual-model element
076: *
077: * @return <code>true</code> if this converter is responsible for the given conceptual-model
078: * element; otherwise <code>false</code>.
079: */
080: public boolean handlesElement(ConceptualModelElement element) {
081: return (element != null)
082: && element instanceof ConceptAttributeElement;
083: }
084:
085: /**
086: * Converts the specified object to an object, if this converter is responsible for it. For
087: * responsibility see {@link #handlesElement(ConceptualModelElement)}.
088: *
089: * @param element the conceptual-model element to convert
090: *
091: * @return the structure object (which is a xml element)
092: */
093: public Object convertElement(ConceptualModelElement element) {
094: ConceptAttributeElement conceptAttributeElement = (ConceptAttributeElement) element;
095: Iterator it = conceptAttributeElement.children();
096:
097: Element result = new Element("concept-attribute");
098: Element name = new Element("name");
099: Element type = new Element("type");
100:
101: name.setText(conceptAttributeElement.getName());
102: type.setText(conceptAttributeElement.getType());
103: result.addContent(name);
104:
105: while (it.hasNext()) {
106: ConceptualModelElement next = (ConceptualModelElement) it
107: .next();
108:
109: if ((next != null)
110: && !(next instanceof ConceptAttributeElement)) {
111: if (selectionConceptualModelConverter
112: .handlesElement(next)) {
113: Object toAdd = selectionConceptualModelConverter
114: .convertElement(next);
115:
116: if ((toAdd != null)
117: && !(next instanceof ConceptAttributeElement)) {
118: result.addContent((Element) toAdd);
119: }
120: }
121: }
122: }
123:
124: return result;
125: }
126:
127: /**
128: * Returns whether this converter is able to transform the specified other representation (here
129: * a xml element) to a conceptual-model element. This converter is responsible if the other
130: * representation is of type <code>jdom.org.Element</code> and the name of the element is
131: * "concept"
132: *
133: * @param other the other representation
134: *
135: * @return <code>true</code> if this converter is responsible for the specified other
136: * representation; otherwise <code>false</code>.
137: */
138: public boolean handlesOther(Object other) {
139: try {
140: Element e = (Element) other;
141:
142: return e.getName().equals("concept-attribute");
143: } catch (Throwable t) {
144: return false;
145: }
146: }
147:
148: /**
149: * Converts the specified other representation to a conceptual-model element, if this converter
150: * is responsible for it (reverses {@link #convertElement(ConceptualModelElement)}). For
151: * responsibility see {@link #handlesOther(Object)}.
152: *
153: * @param other the other representation to convert
154: *
155: * @return the conceptual-model element (of type {@link
156: * jacareto.conceptualmodel.ConceptAttributeElement})
157: */
158: public ConceptualModelElement convertOther(Object other) {
159: Element element = (Element) other;
160: String name = element.getAttributeValue("name");
161: String type = element.getAttributeValue("type", "");
162:
163: return new ConceptAttributeElement(env, name, type);
164: }
165: }
|