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.Iterator;
022: import java.util.List;
023: import java.util.Map;
024: import java.util.Vector;
025:
026: import org.jdom.Element;
027: import org.mandarax.kernel.Fact;
028: import org.mandarax.kernel.LogicFactory;
029: import org.mandarax.kernel.Query;
030: import org.mandarax.xkb.XKBException;
031:
032: /**
033: * An adapter class for queries.
034: * @see org.mandarax.kernel.Rule
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 1.9
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:
041: public class XMLAdapter4Queries extends AbstractXMLAdapter {
042: public static final String QUERY = "query";
043: public static final String BODY = "_body";
044: public static final String NAME = "name";
045:
046: /**
047: * Export an object, i.e., convert it to an element in the DOM.
048: * @param obj an object
049: * @param driver the generic driver
050: * @param cache a cache used in order to associate the same
051: * id with various occurences of the same object
052: * @exception an XKBException is thrown if export fails
053: */
054: public Element exportObject(Object obj, GenericDriver driver,
055: Map cache) throws XKBException {
056: check(obj, Query.class);
057: Query query = (Query) obj;
058: Element e = new Element(QUERY);
059: String queryName = query.getName();
060: if (queryName == null)
061: queryName = "";
062: e.setAttribute(NAME, queryName);
063: Element eBody = new Element(BODY);
064: e.addContent(eBody);
065: Fact[] queryFacts = query.getFacts();
066: for (int i = 0; i < queryFacts.length; i++) {
067: Element el = exportObject(queryFacts[i],
068: GenericDriver.FACT, driver, cache);
069: eBody.addContent(el);
070: }
071: return e;
072: }
073:
074: /**
075: * Build an object from an XML element.
076: * @param e an element
077: * @param driver the generic driver
078: * @param cache a cache used to identify objects that have the same id
079: * @param lfactory the logic factory used to create objects
080: * @exception an XKBException is thrown if import fails
081: */
082: public Object importObject(Element e, GenericDriver driver,
083: Map cache, LogicFactory lfactory) throws XKBException {
084: // get body
085: Element eBody = e.getChild(BODY);
086: List body = new Vector();
087: XMLAdapter adapter4Facts = null;
088: List children = eBody.getChildren();
089: for (Iterator it = children.iterator(); it.hasNext();) {
090: Element eFact = (Element) it.next();
091: if (adapter4Facts == null)
092: adapter4Facts = driver.getAdapterByTagName(eFact
093: .getName());
094: Fact queryFact = (Fact) adapter4Facts.importObject(eFact,
095: driver, cache, lfactory);
096: body.add(queryFact);
097:
098: }
099: Fact[] facts = new Fact[body.size()];
100: for (int i = 0; i < body.size(); i++)
101: facts[i] = (Fact) body.get(i);
102: String queryName = e.getAttributeValue(NAME);
103:
104: // build query
105: Query q = lfactory.createQuery(facts, queryName == null ? ""
106: : queryName);
107: if (queryName == null)
108: q.setName(q.toString());
109:
110: return q;
111: }
112:
113: /**
114: * Get the name of the associated tag (element).
115: * @return a string
116: */
117: public String getTagName() {
118: return QUERY;
119: }
120:
121: /**
122: * Get the kind of object the adapter can export/import.
123: * @return a string
124: */
125: public String getKindOfObject() {
126: return GenericDriver.QUERY;
127: }
128: }
|