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.lang.reflect.Array;
022: import java.util.Collection;
023: import java.util.Enumeration;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Map;
027:
028: import org.jdom.Element;
029: import org.mandarax.kernel.ClauseSet;
030: import org.mandarax.kernel.Function;
031: import org.mandarax.kernel.LogicFactory;
032: import org.mandarax.kernel.Predicate;
033: import org.mandarax.kernel.Query;
034: import org.mandarax.kernel.Term;
035: import org.mandarax.util.logging.LogCategories;
036: import org.mandarax.xkb.XKBException;
037:
038: /**
039: * An abstract class implementing XMLAdapter. Some utility methods are implemented here.
040: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
041: * @version 3.4 <7 March 05>
042: * @since 1.6
043: * @deprecated from v 3.4 - support for new features such as validation will not be added to XKB, please use ZKB instead
044: */
045: public abstract class AbstractXMLAdapter implements XMLAdapter,
046: LogCategories {
047: protected LogicFactory lfactory = LogicFactory.getDefaultFactory();
048:
049: /**
050: * Utility method that locates the appropriate driver and exports an object using this driver.
051: * @param obj an object
052: * @param kindOfObject the kind of object, usually a constant defined in GenericDriver
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: * @return an element
057: * @exception an XKBException is thrown if export fails
058: */
059: protected Element exportObject(Object obj, String kindOfObject,
060: GenericDriver driver, Map cache) throws XKBException {
061: XMLAdapter adapter = driver
062: .getAdapterByKindOfObject(kindOfObject);
063: return adapter.exportObject(obj, driver, cache);
064: }
065:
066: /**
067: * Utility method to export an array of objects and add the elements to the parent.
068: * @param objs the objects
069: * @param parent the parent element
070: * @param kindOfObject the kind of object, usually a constant defined in GenericDriver
071: * @param driver the generic driver
072: * @param cache a cache used in order to associate the same
073: * id with various occurences of the same object
074: * @exception an XKBException is thrown if export fails
075: */
076: protected void exportChildren(Object[] objs, Element parent,
077: String kindOfObject, GenericDriver driver, Map cache)
078: throws XKBException {
079: XMLAdapter adapter = driver
080: .getAdapterByKindOfObject(kindOfObject);
081: for (int i = 0; i < objs.length; i++) {
082: Element e = adapter.exportObject(objs[i], driver, cache);
083: parent.addContent(e);
084: }
085: }
086:
087: /**
088: * Utility method to export a collection of objects and add the elements to the parent.
089: * @param objs the objects
090: * @param parent the parent element
091: * @param kindOfObject the kind of object, usually a constant defined in GenericDriver
092: * @param driver the generic driver
093: * @param cache a cache used in order to associate the same
094: * id with various occurences of the same object
095: * @exception an XKBException is thrown if export fails
096: */
097: protected void exportChildren(Collection objs, Element parent,
098: String kindOfObject, GenericDriver driver, Map cache)
099: throws XKBException {
100: XMLAdapter adapter = driver
101: .getAdapterByKindOfObject(kindOfObject);
102: for (Iterator it = objs.iterator(); it.hasNext();) {
103: Element e = adapter.exportObject(it.next(), driver, cache);
104: parent.addContent(e);
105: }
106: }
107:
108: /**
109: * Utility method to import a child (the first child) with a certain name (not the tag name but the symbolic name
110: * as defined in GenericDriver) of the given parent element.
111: * @param e the parent element
112: * @param name the name of the child tag
113: * @param driver the generic driver
114: * @param cache a cache used to identify objects that have an id
115: * @param lfactory the logic factory used to create objects
116: * @return an object
117: * @exception an XKBException is thrown if import fails
118: */
119: protected Object importChild(Element parent, String name,
120: GenericDriver driver, Map cache, LogicFactory lfactory)
121: throws XKBException {
122: XMLAdapter adapter = driver.getAdapterByKindOfObject(name);
123: String tagName = adapter.getTagName();
124: Element child = parent.getChild(tagName);
125: return adapter.importObject(child, driver, cache, lfactory);
126: }
127:
128: /**
129: * Utility method to import the children with a certain name (not the tag name but the symbolic name
130: * as defined in GenericDriver) of the given parent element. The name can be null, in this case all
131: * child elements will be imported. The target type argument specifies the type of the objects
132: * in the array. Note that we return an object , not an array. But this object can be casted to
133: * an array of the target type, e.g. like in<br>
134: * <code>Term[] terms = importChildren(e,null,driver,cache,lfactory,Term.class)</code>
135: * @param e the parent element
136: * @param name the name of the child tag
137: * @param driver the generic driver
138: * @param cache a cache used to identify objects that have an id
139: * @param lfactory the logic factory used to create objects
140: * @param targetType the expected type of elements in the array
141: * @return an object that can be casted to an array of elements of the target type
142: * @exception an XKBException is thrown if import fails
143: */
144: protected Object importChildren(Element parent, String name,
145: GenericDriver driver, Map cache, LogicFactory lfactory,
146: Class targetType) throws XKBException {
147: XMLAdapter adapter = name == null ? null : driver
148: .getAdapterByKindOfObject(name);
149: String tagName = name == null ? null : adapter.getTagName();
150: List children = name == null ? parent.getChildren() : parent
151: .getChildren(tagName);
152: Object array = java.lang.reflect.Array.newInstance(targetType,
153: children.size());
154: for (int i = 0; i < children.size(); i++) {
155: if (name == null)
156: adapter = driver
157: .getAdapterByTagName(((Element) children.get(i))
158: .getName());
159: Array.set(array, i, adapter.importObject((Element) children
160: .get(i), driver, cache, lfactory));
161: }
162: return array;
163: }
164:
165: /**
166: * Utility method that checks the type of an object and throws an exception
167: * if the object is not an instance of the respective class.
168: * @param obj an object
169: * @param clazz a class
170: */
171: protected void check(Object obj, Class clazz) throws XKBException {
172: if (obj == null)
173: throw new XKBException("Adapter " + getClass()
174: + " cannot export/import null");
175: if (!(clazz.isAssignableFrom(obj.getClass())))
176: throw new XKBException("Adapter " + getClass()
177: + " cannot export/import " + obj
178: + " since it does not have the following type: "
179: + clazz);
180: }
181:
182: /**
183: * Get the type of the term, e.g. GenericDriver.COMPLEX_TERM or GenericDriver.VARIABLE_TERM.
184: * @param t a term
185: * @return a string
186: */
187: protected String getTermType(Term t) {
188: if (t instanceof org.mandarax.kernel.VariableTerm)
189: return GenericDriver.VARIABLE_TERM;
190: if (t instanceof org.mandarax.kernel.ConstantTerm)
191: return GenericDriver.CONSTANT_TERM;
192: if (t instanceof org.mandarax.kernel.ComplexTerm)
193: return GenericDriver.COMPLEX_TERM;
194: return null;
195: }
196:
197: /**
198: * Get the type of the function, e.g. GenericDriver.JFunction or GenericDriver.SQL_FUNCTION.
199: * @param f a function
200: * @return a string
201: */
202: protected String getFunctionType(Function f) {
203: if (f instanceof org.mandarax.kernel.meta.JFunction)
204: return GenericDriver.JFUNCTION;
205: if (f instanceof org.mandarax.kernel.meta.DynaBeanFunction)
206: return GenericDriver.DYNA_BEAN_FUNCTION;
207: if (f instanceof org.mandarax.sql.SQLFunction)
208: return GenericDriver.SQL_FUNCTION;
209: if (f instanceof org.mandarax.lib.AbstractFunction)
210: return GenericDriver.MANDARAX_LIB_FUNCTION;
211: return null;
212: }
213:
214: /**
215: * Get the type of the predicate, e.g. GenericDriver.JPREDICATE or GenericDriver.SQL_PREDICATE.
216: * @param p a predicate
217: * @return a string
218: */
219: protected String getPredicateType(Predicate p) {
220: if (p instanceof org.mandarax.kernel.meta.JPredicate)
221: return GenericDriver.JPREDICATE;
222: if (p instanceof org.mandarax.kernel.SimplePredicate)
223: return GenericDriver.SIMPLE_PREDICATE;
224: if (p instanceof org.mandarax.sql.SQLPredicate)
225: return GenericDriver.SQL_PREDICATE;
226: if (p instanceof org.mandarax.lib.AbstractPredicate)
227: return GenericDriver.MANDARAX_LIB_PREDICATE;
228: return null;
229: }
230:
231: /**
232: * Add additional properties to an element representing a clause set.
233: * @param e an element
234: * @param cs a clause set
235: */
236: protected void addProperties(Element e, ClauseSet cs) {
237: Element eProperties = null;
238: for (Enumeration en = cs.propertyNames(); en.hasMoreElements();) {
239: String nextName = en.nextElement().toString();
240: if (eProperties == null)
241: eProperties = new Element(GenericDriver.PROPERTIES);
242: Element eProperty = new Element(GenericDriver.PROPERTY);
243: eProperty.setAttribute(GenericDriver.PROPERTY_NAME,
244: nextName);
245: eProperty.setAttribute(GenericDriver.PROPERTY_VALUE, cs
246: .getProperty(nextName));
247: eProperties.addContent(eProperty);
248: }
249: if (eProperties != null)
250: e.addContent(eProperties);
251: }
252:
253: /**
254: * Add additional properties to an element representing a query.
255: * @param e an element
256: * @param q a query
257: */
258: protected void addProperties(Element e, Query q) {
259: Element eProperties = null;
260: for (Enumeration en = q.propertyNames(); en.hasMoreElements();) {
261: String nextName = en.nextElement().toString();
262: if (eProperties == null)
263: eProperties = new Element(GenericDriver.PROPERTIES);
264: Element eProperty = new Element(GenericDriver.PROPERTY);
265: eProperty.setAttribute(GenericDriver.PROPERTY_NAME,
266: nextName);
267: eProperty.setAttribute(GenericDriver.PROPERTY_VALUE, q
268: .getProperty(nextName));
269: eProperties.addContent(eProperty);
270: }
271: if (eProperties != null)
272: e.addContent(eProperties);
273: }
274:
275: /**
276: * Load the properties into the clause set.
277: * @param e an element (the super element of the properties element)
278: * @param cs a clause set
279: */
280: protected void loadProperties(Element e, ClauseSet cs) {
281: Element eProperties = e.getChild(GenericDriver.PROPERTIES);
282: if (eProperties != null) {
283: List children = eProperties
284: .getChildren(GenericDriver.PROPERTY);
285: for (Iterator iter = children.iterator(); iter.hasNext();) {
286: Element eProperty = (Element) iter.next();
287: cs
288: .setProperty(
289: eProperty
290: .getAttributeValue(GenericDriver.PROPERTY_NAME),
291: eProperty
292: .getAttributeValue(GenericDriver.PROPERTY_VALUE));
293: }
294: }
295: }
296:
297: /**
298: * Load the properties into the query.
299: * @param e an element (the super element of the properties element)
300: * @param q a query
301: */
302: protected void loadProperties(Element e, Query q) {
303: Element eProperties = e.getChild(GenericDriver.PROPERTIES);
304: if (eProperties != null) {
305: List children = eProperties
306: .getChildren(GenericDriver.PROPERTY);
307: for (Iterator iter = children.iterator(); iter.hasNext();) {
308: Element eProperty = (Element) iter.next();
309: q
310: .setProperty(
311: eProperty
312: .getAttributeValue(GenericDriver.PROPERTY_NAME),
313: eProperty
314: .getAttributeValue(GenericDriver.PROPERTY_VALUE));
315: }
316: }
317: }
318: }
|