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.Function;
025: import org.mandarax.kernel.LogicFactory;
026: import org.mandarax.xkb.XKBException;
027:
028: /**
029: * An adapter class for functions from the mandarax lib.
030: * @see org.mandarax.lib.AbstractFunction
031: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
032: * @version 3.4 <7 March 05>
033: * @since 1.6
034: * @deprecated from v 3.4 - support for new features such as validation will not be added to XKB, please use ZKB instead
035: */
036: public class XMLAdapter4MandaraxLibFunctions extends AbstractXMLAdapter {
037: public static final String MANDARAX_LIB_FUNCTION = "mandarax_lib_functions";
038: public static final String NAME = "name";
039:
040: /**
041: * Export an object, i.e., convert it to an element in the DOM.
042: * @param obj an object
043: * @param driver the generic driver
044: * @param cache a cache used in order to associate the same
045: * id with various occurences of the same object
046: * @exception an XKBException is thrown if export fails
047: */
048: public Element exportObject(Object obj, GenericDriver driver,
049: Map cache) throws XKBException {
050: check(obj, org.mandarax.lib.AbstractFunction.class);
051: Function f = (Function) obj;
052: Element e = new Element(MANDARAX_LIB_FUNCTION);
053: e.setAttribute(NAME, f.getClass().getName());
054: return e;
055: }
056:
057: /**
058: * Build an object from an XML element.
059: * @param e an element
060: * @param driver the generic driver
061: * @param cache a cache used to identify objects that have the same id
062: * @param lfactory the logic factory used to create objects
063: * @exception an XKBException is thrown if export fails
064: */
065: public Object importObject(Element e, GenericDriver driver,
066: Map cache, LogicFactory lfactory) throws XKBException {
067: String className = e.getAttributeValue(NAME);
068: // test the registries in mandarax lib for an appropriate class
069: Function f = null;
070: f = lookupLib(f, className,
071: org.mandarax.lib.date.DateArithmetic.ALL_FUNCTIONS);
072: f = lookupLib(f, className,
073: org.mandarax.lib.math.IntArithmetic.ALL_FUNCTIONS);
074: f = lookupLib(f, className,
075: org.mandarax.lib.math.DoubleArithmetic.ALL_FUNCTIONS);
076: f = lookupLib(f, className,
077: org.mandarax.lib.text.StringArithmetic.ALL_FUNCTIONS);
078: return f;
079: }
080:
081: /**
082: * Lookup the lib for a function with a certain class name.
083: * Return if the function is not null!
084: * @return a function
085: * @param f a function
086: * @param className the name of a class
087: * @param libFunctions an array of functions
088: */
089: private Function lookupLib(Function f, String className,
090: Function[] libFunctions) {
091: if (f != null)
092: return f;
093: for (int i = 0; i < libFunctions.length; i++) {
094: if (libFunctions[i].getClass().getName().equals(className))
095: return libFunctions[i];
096: }
097: return null;
098: }
099:
100: /**
101: * Get the name of the associated tag (element).
102: * @return a string
103: */
104: public String getTagName() {
105: return MANDARAX_LIB_FUNCTION;
106: }
107:
108: /**
109: * Get the kind of object the adapter can export/import.
110: * @return a string
111: */
112: public String getKindOfObject() {
113: return GenericDriver.MANDARAX_LIB_FUNCTION;
114: }
115: }
|