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.Map;
022:
023: import org.jdom.Element;
024: import org.mandarax.kernel.ComplexTerm;
025: import org.mandarax.kernel.Function;
026: import org.mandarax.kernel.LogicFactory;
027: import org.mandarax.kernel.Term;
028: import org.mandarax.xkb.XKBException;
029:
030: /**
031: * An adapter class for complex terms.
032: * @see org.mandarax.kernel.ComplexTerm
033: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
034: * @version 3.4 <7 March 05>
035: * @since 1.6
036: * @deprecated from v 3.4 - support for new features such as validation will not be added to XKB, please use ZKB instead
037: */
038:
039: public class XMLAdapter4ComplexTerms extends AbstractXMLAdapter {
040: public static final String COMPLEX_TERM = "complex_term";
041: public static final String TERMS = "terms";
042:
043: /**
044: * Export an object, i.e., convert it to an element in the DOM.
045: * @param obj an object
046: * @param driver the generic driver
047: * @param cache a cache used in order to associate the same
048: * id with various occurences of the same object
049: * @exception an XKBException is thrown if export fails
050: */
051: public Element exportObject(Object obj, GenericDriver driver,
052: Map cache) throws XKBException {
053: check(obj, ComplexTerm.class);
054: ComplexTerm term = (ComplexTerm) obj;
055: Element e = new Element(COMPLEX_TERM);
056: // add predicate
057: Function f = term.getFunction();
058: Element ef = exportObject(f, getFunctionType(f), driver, cache);
059: e.addContent(ef);
060: // add terms
061: Term[] terms = term.getTerms();
062: Element eTerms = new Element(TERMS);
063: e.addContent(eTerms);
064: for (int i = 0; i < terms.length; i++) {
065: Element et = exportObject(terms[i], getTermType(terms[i]),
066: driver, cache);
067: eTerms.addContent(et);
068: }
069: return e;
070: }
071:
072: /**
073: * Build an object from an XML element.
074: * @param e an element
075: * @param driver the generic driver
076: * @param cache a cache used to identify objects that have the same id
077: * @param lfactory the logic factory used to create objects
078: * @exception an XKBException is thrown if import fails
079: */
080: public Object importObject(Element e, GenericDriver driver,
081: Map cache, LogicFactory lfactory) throws XKBException {
082: // the first element is functions
083: Element eFunction = (Element) e.getChildren().get(0);
084: XMLAdapter adapter = driver.getAdapterByTagName(eFunction
085: .getName());
086: Function function = (Function) adapter.importObject(eFunction,
087: driver, cache, lfactory);
088: // get the terms
089: Element eTerm = (Element) e.getChild(TERMS);
090: Term[] terms = (Term[]) importChildren(eTerm, null, driver,
091: cache, lfactory, Term.class);
092: return lfactory.createComplexTerm(function, terms);
093:
094: }
095:
096: /**
097: * Get the name of the associated tag (element).
098: * @return a string
099: */
100: public String getTagName() {
101: return COMPLEX_TERM;
102: }
103:
104: /**
105: * Get the kind of object the adapter can export/import.
106: * @return a string
107: */
108: public String getKindOfObject() {
109: return GenericDriver.COMPLEX_TERM;
110: }
111: }
|