001: package org.mandarax.zkb.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.Vector;
024:
025: import org.jdom.Element;
026: import org.mandarax.kernel.Fact;
027: import org.mandarax.kernel.LogicFactory;
028: import org.mandarax.kernel.Query;
029: import org.mandarax.zkb.ObjectPersistencyService;
030: import org.mandarax.zkb.ZKBException;
031:
032: /**
033: * An adapter class for queries.
034: * @see org.mandarax.kernel.Query
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: */
039:
040: public class Adapter4Queries extends AbstractAdapter {
041: public static final String BODY = "_body";
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 ops the object persistency service
048: * @exception a XKBException is thrown if export fails
049: */
050: public Element exportObject(Object obj, GenericDriver driver,
051: ObjectPersistencyService ops) throws ZKBException {
052: check(obj, Query.class);
053: Query query = (Query) obj;
054: Element e = new Element(QUERY);
055: String queryName = query.getName();
056: if (queryName == null)
057: queryName = "";
058: e.setAttribute(NAME, queryName);
059: Element eBody = new Element(BODY);
060: e.addContent(eBody);
061: Fact[] queryFacts = query.getFacts();
062: for (int i = 0; i < queryFacts.length; i++) {
063: Element el = exportObject(queryFacts[i],
064: TagAndAttributeNames.FACT, driver, ops);
065: eBody.addContent(el);
066: }
067: exportProperties(e, query);
068: return e;
069: }
070:
071: /**
072: * Build an object from an XML element.
073: * @param e an element
074: * @param driver the generic driver
075: * @param ops the object persistency service
076: * @param lfactory the logic factory used to create objects
077: * @exception a XKBException is thrown if import fails
078: */
079: public Object importObject(Element e, GenericDriver driver,
080: ObjectPersistencyService ops, LogicFactory lfactory)
081: throws ZKBException {
082: // get body
083: Element eBody = e.getChild(BODY);
084: List body = new Vector();
085: Adapter adapter4Facts = null;
086: List children = eBody.getChildren(FACT);
087: for (Iterator it = children.iterator(); it.hasNext();) {
088: Element eFact = (Element) it.next();
089: if (adapter4Facts == null)
090: adapter4Facts = driver.getAdapter(eFact.getName());
091: Fact queryFact = (Fact) adapter4Facts.importObject(eFact,
092: driver, ops, lfactory);
093: body.add(queryFact);
094: }
095: Fact[] facts = new Fact[body.size()];
096: for (int i = 0; i < body.size(); i++)
097: facts[i] = (Fact) body.get(i);
098: String queryName = e.getAttributeValue(NAME);
099:
100: // build query
101: Query q = lfactory.createQuery(facts, queryName == null ? ""
102: : queryName);
103: if (queryName == null)
104: q.setName(q.toString());
105: importProperties(e, q);
106: return q;
107: }
108:
109: /**
110: * Get the name of the associated tag (element).
111: * @return a string
112: */
113: public String getTagName() {
114: return QUERY;
115: }
116:
117: /**
118: * Print the DTD associated with this adapter on a string buffer.
119: * @param out the buffer to print on.
120: */
121: public void printDTD(StringBuffer out) {
122: out.append("<!ELEMENT query (_body,properties?)>\n");
123: out.append("<!ATTLIST query name CDATA #REQUIRED>\n");
124: }
125:
126: }
|