001: package org.mandarax.xkb.framework;
002:
003: /**
004: * Copyright (C) 1999-2004 Jens Dietrich (mailto:mandarax@jbdietrich.com)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: import java.util.Comparator;
022: import java.util.List;
023: import java.util.Map;
024: import java.util.StringTokenizer;
025:
026: import org.jdom.Element;
027: import org.mandarax.kernel.LogicFactory;
028: import org.mandarax.util.comparators.DefaultClauseSetComparator;
029: import org.mandarax.xkb.XKBException;
030:
031: /**
032: * An adapter class for comparators.
033: * @see java.util.Comparator
034: * @see org.mandarax.util.comparators.DefaultClauseSetComparator
035: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
036: * @version 3.4 <7 March 05>
037: * @since 2.2
038: * @deprecated from v 3.4 - support for new features such as validation will not be added to XKB, please use ZKB instead
039: */
040: public class XMLAdapter4Comparators extends AbstractXMLAdapter {
041: public static final String COMPARATOR = "comparator";
042: public static final String TYPE = "type";
043: public static final String TOP_LEVEL_ORDER = "top_level_order";
044: public static final String COMPARATOR4RULES = "comparator_for_rules";
045: public static final String COMPARATOR4FACTS = "comparator_for_facts";
046: public static final String COMPARATOR4SQL_CLAUSE_SETS = "comparator_for_sql_clausesets";
047: public static final String COMPARATOR4OTHERS = "comparator_for_others";
048: public static final String SEPARATOR = ",";
049:
050: /**
051: * Export an object, i.e., convert it to an element in the DOM.
052: * @param obj an object
053: * @param driver the generic driver
054: * @param cache a cache used in order to associate the same
055: * id with various occurences of the same object
056: * @exception an XKBException is thrown if export fails
057: */
058: public Element exportObject(Object obj, GenericDriver driver,
059: Map cache) throws XKBException {
060: check(obj, Comparator.class);
061: Element e = new Element(COMPARATOR);
062: // add class name
063: e.setAttribute(TYPE, obj.getClass().getName());
064:
065: if (obj instanceof DefaultClauseSetComparator) {
066: DefaultClauseSetComparator comp = (DefaultClauseSetComparator) obj;
067: String[] tlo = comp.getTopLevelOrder();
068: StringBuffer buf = new StringBuffer();
069: for (int i = 0; i < tlo.length; i++) {
070: if (i > 0)
071: buf.append(SEPARATOR);
072: buf.append(tlo[i]);
073: }
074: e.setAttribute(TOP_LEVEL_ORDER, buf.toString());
075:
076: Element child = new Element(COMPARATOR4RULES);
077: e.addContent(child);
078: Comparator[] comparators = comp.getComparators4Rules();
079: for (int i = 0; i < comparators.length; i++)
080: child.addContent(exportObject(comparators[i], driver,
081: cache));
082:
083: child = new Element(COMPARATOR4FACTS);
084: e.addContent(child);
085: comparators = comp.getComparators4Facts();
086: for (int i = 0; i < comparators.length; i++)
087: child.addContent(exportObject(comparators[i], driver,
088: cache));
089:
090: child = new Element(COMPARATOR4SQL_CLAUSE_SETS);
091: e.addContent(child);
092: comparators = comp.getComparators4SQLClauseSets();
093: for (int i = 0; i < comparators.length; i++)
094: child.addContent(exportObject(comparators[i], driver,
095: cache));
096:
097: child = new Element(COMPARATOR4OTHERS);
098: e.addContent(child);
099: comparators = comp.getComparators4Others();
100: for (int i = 0; i < comparators.length; i++)
101: child.addContent(exportObject(comparators[i], driver,
102: cache));
103: }
104: return e;
105: }
106:
107: /**
108: * Build an object from an XML element.
109: * @param e an element
110: * @param driver the generic driver
111: * @param cache a cache used to identify objects that have the same id
112: * @param lfactory the logic factory used to create objects
113: * @exception an XKBException is thrown if import fails
114: */
115: public Object importObject(Element e, GenericDriver driver,
116: Map cache, LogicFactory lfactory) throws XKBException {
117: String className = e.getAttributeValue(TYPE);
118: try {
119: Comparator comp = (Comparator) Class.forName(className)
120: .newInstance();
121:
122: if (comp instanceof DefaultClauseSetComparator) {
123: DefaultClauseSetComparator defComp = (DefaultClauseSetComparator) comp;
124:
125: // top level order
126: String tloAsString = e
127: .getAttributeValue(TOP_LEVEL_ORDER);
128: if (tloAsString != null) {
129: StringTokenizer tokenizer = new StringTokenizer(
130: tloAsString, SEPARATOR);
131: int s = tokenizer.countTokens();
132: String[] tokens = new String[s];
133: for (int i = 0; i < s; i++)
134: tokens[i] = tokenizer.nextToken().trim();
135: defComp.setTopLevelOrder(tokens);
136: }
137:
138: // comparators for categories
139: Comparator[] comparators = null;
140: List children = null;
141: Element child = null;
142:
143: child = e.getChild(COMPARATOR4FACTS);
144: if (child != null) {
145: children = child.getChildren(COMPARATOR);
146: comparators = new Comparator[children.size()];
147: for (int i = 0; i < comparators.length; i++)
148: comparators[i] = (Comparator) importObject(
149: (Element) children.get(i), driver,
150: cache, lfactory);
151: } else
152: comparators = new Comparator[0];
153: defComp.setComparators4Facts(comparators);
154:
155: child = e.getChild(COMPARATOR4RULES);
156: if (child != null) {
157: children = child.getChildren(COMPARATOR);
158: comparators = new Comparator[children.size()];
159: for (int i = 0; i < comparators.length; i++)
160: comparators[i] = (Comparator) importObject(
161: (Element) children.get(i), driver,
162: cache, lfactory);
163: } else
164: comparators = new Comparator[0];
165: defComp.setComparators4Rules(comparators);
166:
167: child = e.getChild(COMPARATOR4SQL_CLAUSE_SETS);
168: if (child != null) {
169: children = child.getChildren(COMPARATOR);
170: comparators = new Comparator[children.size()];
171: for (int i = 0; i < comparators.length; i++)
172: comparators[i] = (Comparator) importObject(
173: (Element) children.get(i), driver,
174: cache, lfactory);
175: } else
176: comparators = new Comparator[0];
177: defComp.setComparators4SQLClauseSets(comparators);
178:
179: child = e.getChild(COMPARATOR4OTHERS);
180: if (child != null) {
181: children = child.getChildren(COMPARATOR);
182: comparators = new Comparator[children.size()];
183: for (int i = 0; i < comparators.length; i++)
184: comparators[i] = (Comparator) importObject(
185: (Element) children.get(i), driver,
186: cache, lfactory);
187: } else
188: comparators = new Comparator[0];
189: defComp.setComparators4Others(comparators);
190:
191: }
192: return comp;
193: } catch (ClassNotFoundException x) {
194: throw new XKBException("Cannot import class " + className
195: + " - check classpath");
196: } catch (InstantiationException x) {
197: throw new XKBException("Cannot instanciate class "
198: + className);
199: } catch (IllegalAccessException x) {
200: throw new XKBException("Cannot instanciate class "
201: + className);
202: } catch (ClassCastException x) {
203: throw new XKBException("Class " + className
204: + " is not a comparator");
205: }
206: }
207:
208: /**
209: * Get the name of the associated tag (element).
210: * @return a string
211: */
212: public String getTagName() {
213: return COMPARATOR;
214: }
215:
216: /**
217: * Get the kind of object the adapter can export/import.
218: * @return a string
219: */
220: public String getKindOfObject() {
221: return GenericDriver.COMPARATOR;
222: }
223: }
|