📄 abstractadapter.java
字号:
package org.mandarax.zkb.framework;
/**
* Copyright (C) 1999-2004 Jens Dietrich (mailto:mandarax@jbdietrich.com)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import org.jdom.Element;
import org.mandarax.kernel.ClauseSet;
import org.mandarax.kernel.Fact;
import org.mandarax.kernel.LogicFactory;
import org.mandarax.kernel.PropertiesSupport;
import org.mandarax.kernel.Rule;
import org.mandarax.kernel.Term;
import org.mandarax.sql.SQLClauseSet;
import org.mandarax.util.logging.LogCategories;
import org.mandarax.zkb.ObjectPersistencyService;
import org.mandarax.zkb.ZKBException;
/**
* An abstract class implementing XMLAdapter. Some utility methods are implemented here.
* @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
* @version 3.4 <7 March 05>
* @since 2.2
*/
public abstract class AbstractAdapter
implements Adapter, LogCategories, TagAndAttributeNames {
protected LogicFactory lfactory = LogicFactory.getDefaultFactory();
// constants for dtd generation
public static final String ZERO_OR_ONE = "?";
public static final String ONE_OR_MANY = "+";
public static final String ZERO_OR_MANY = "*";
public static final String ZONE = "*";
/**
* Utility method that locates the appropriate driver and exports an object using this driver.
* @param obj an object
* @param kindOfObject the kind of object, usually a constant defined in GenericDriver
* @param driver the generic driver
* @param ops the object persistency service
* @return an element
* @exception a ZKBException is thrown if export fails
*/
protected Element exportObject(
Object obj,
String kindOfObject,
GenericDriver driver,
ObjectPersistencyService ops)
throws ZKBException {
Adapter adapter = driver.getAdapter(kindOfObject);
return adapter.exportObject(obj, driver, ops);
}
/**
* Utility method to export an array of objects and add the elements to the parent.
* @param objs the objects
* @param parent the parent element
* @param kindOfObject the kind of object, usually a constant defined in GenericDriver
* @param driver the generic driver
* @param ops the object persistency service
* @exception a ZKBException is thrown if export fails
*/
protected void exportChildren(
Object[] objs,
Element parent,
String kindOfObject,
GenericDriver driver,
ObjectPersistencyService ops)
throws ZKBException {
Adapter adapter = driver.getAdapter(kindOfObject);
for (int i = 0; i < objs.length; i++) {
Element e = adapter.exportObject(objs[i], driver, ops);
parent.addContent(e);
}
}
/**
* Utility method to export a collection of objects and add the elements to the parent.
* @param objs the objects
* @param parent the parent element
* @param kindOfObject the kind of object, usually a constant defined in GenericDriver
* @param driver the generic driver
* @param ops the object persistency service
* @exception an ZKBException is thrown if export fails
*/
protected void exportChildren(
Collection objs,
Element parent,
String kindOfObject,
GenericDriver driver,
ObjectPersistencyService ops)
throws ZKBException {
Adapter adapter = driver.getAdapter(kindOfObject);
for (Iterator it = objs.iterator(); it.hasNext();) {
Element e = adapter.exportObject(it.next(), driver, ops);
parent.addContent(e);
}
}
/**
* Utility method to import a child (the first child) with a certain name (not the tag name but the symbolic name
* as defined in GenericDriver) of the given parent element.
* @param e the parent element
* @param name the name of the child tag
* @param driver the generic driver
* @param ops the object persistency service
* @param lfactory the logic factory used to create objects
* @return an object
* @exception an ZKBException is thrown if import fails
*/
protected Object importChild(
Element parent,
String name,
GenericDriver driver,
ObjectPersistencyService ops,
LogicFactory lfactory)
throws ZKBException {
Adapter adapter = driver.getAdapter(name);
String tagName = adapter.getTagName();
Element child = parent.getChild(tagName);
return adapter.importObject(child, driver, ops, lfactory);
}
/**
* Utility method to import the children with a certain name (not the tag name but the symbolic name
* as defined in GenericDriver) of the given parent element. The name can be null, in this case all
* child elements will be imported. The target type argument specifies the type of the objects
* in the array. Note that we return an object , not an array. But this object can be casted to
* an array of the target type, e.g. like in<br>
* <code>Term[] terms = importChildren(e,null,driver,cache,lfactory,Term.class)</code>
* @param e the parent element
* @param name the name of the child tag
* @param driver the generic driver
* @param ops the object persistency service
* @param lfactory the logic factory used to create objects
* @param targetType the expected type of elements in the array
* @return an object that can be casted to an array of elements of the target type
* @exception an ZKBException is thrown if import fails
*/
protected Object importChildren(
Element parent,
String name,
GenericDriver driver,
ObjectPersistencyService ops,
LogicFactory lfactory,
Class targetType)
throws ZKBException {
Adapter adapter = name == null ? null : driver.getAdapter(name);
String tagName = name == null ? null : adapter.getTagName();
List children =
name == null ? parent.getChildren() : parent.getChildren(tagName);
Object array =
java.lang.reflect.Array.newInstance(targetType, children.size());
for (int i = 0; i < children.size(); i++) {
if (name == null)
adapter =
driver.getAdapter(((Element) children.get(i)).getName());
Array.set(
array,
i,
adapter.importObject(
(Element) children.get(i),
driver,
ops,
lfactory));
}
return array;
}
/**
* Utility method that checks the type of an object and throws an exception
* if the object is not an instance of the respective class.
* @param obj an object
* @param clazz a class
*/
protected void check(Object obj, Class clazz) throws ZKBException {
if (obj == null)
throw new ZKBException(
"Adapter " + getClass() + " cannot export/import null");
if (!(clazz.isAssignableFrom(obj.getClass())))
throw new ZKBException(
"Adapter "
+ getClass()
+ " cannot export/import "
+ obj
+ " since it does not have the following type: "
+ clazz);
}
/**
* Assign an object id to an object and add the respective attribute to the element.
* Use an existing id if possible.
* @param e an element
* @param ops an object persistency service
* @param obj the object
*/
protected void assignURI(
Element e,
ObjectPersistencyService ops,
Object obj) {
String uri = ops.findOrBind(obj);
e.addContent(uri);
}
/**
* Get the object with the uri found in the tag.
* @param e an element
* @param ops the object persistency service
* @return an object
*/
protected Object getObjectByURI(Element e, ObjectPersistencyService ops) {
String uri = e.getTextTrim();
return ops.lookup(uri);
}
/**
* Set the type attribute.
* @param e an element
* @param term a term
*/
protected void setTypeAttribute(Element e, Term t) {
e.setAttribute(TYPE, t.getType().getName());
}
/**
* Get the class for a name, throw an ZKB exception if class cannot be loaded.
* @param name the name of the class
* @return a class
*/
protected Class class4Name(String name) throws ZKBException {
try {
return Class.forName(name);
} catch (ClassNotFoundException x) {
throw new ZKBException(x.getMessage());
}
}
/**
* Print the DTD associated with this adapter on a string buffer.
* @param out the buffer to print on.
*/
public abstract void printDTD(StringBuffer out) ;
/**
* Export the properties of an object implementing PropertiesSupport.
* @param parent the parent element
* @param ps the object implementing PropertiesSupport
*/
protected void exportProperties(Element parent,PropertiesSupport ps) {
Element e = new Element(PROPERTIES);
boolean hasProperties = false;
for (Enumeration en = ps.propertyNames();en.hasMoreElements();) {
String key = en.nextElement().toString();
String value = ps.getProperty(key);
if (value!=null) {
Element eP = new Element(PROPERTY);
eP.setAttribute(KEY,key);
eP.setAttribute(VALUE,value);
e.addContent(eP);
hasProperties = true;
}
else LOG_ZKB.warn("Cannot export property value null for key " + key);
}
parent.addContent(e);
}
/**
* Import the properties of an object implementing PropertiesSupport.
* @param parent the parent element
* @param ps the object implementing PropertiesSupport
*/
protected void importProperties(Element parent,PropertiesSupport ps) {
Element e = parent.getChild(PROPERTIES);
if (e==null) {
if (LOG_ZKB.isDebugEnabled()) LOG_ZKB.debug("No properties tag found, cannot import properties for object " + ps);
}
else {
List ePs = e.getChildren(PROPERTY);
for (int i=0;i<ePs.size();i++) {
Element eP = (Element)ePs.get(i);
String key = eP.getAttributeValue(KEY);
String value = eP.getAttributeValue(VALUE);
if (key!=null && value!=null) ps.setProperty(key,value);
}
}
}
/**
* Get the type of clause set, e.g. GenericDriver.FACT or GenericDriver.RULE.
* @param cs a clause set
* @return a string
*/
protected String getClauseSetType(ClauseSet cs) {
if (cs instanceof Rule) return RULE;
else if (cs instanceof Fact) return FACT;
else if (cs instanceof SQLClauseSet) return SQL_CLAUSE_SET;
else return CUSTOM_CLAUSE_SET;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -