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.Method;
022: import java.util.List;
023: import java.util.Map;
024:
025: import org.jdom.Element;
026: import org.mandarax.kernel.LogicFactory;
027: import org.mandarax.xkb.XKBException;
028:
029: /**
030: * An adapter class for methods.
031: * @see java.lang.reflect.Method
032: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
033: * @version 3.4 <7 March 05>
034: * @since 1.6
035: * @deprecated from v 3.4 - support for new features such as validation will not be added to XKB, please use ZKB instead
036: */
037: public class XMLAdapter4Methods extends AbstractXMLAdapter {
038: public static final String METHOD = "method";
039: public static final String NAME = "name";
040: public static final String DECLARING_TYPE = "declaring_type";
041: public static final String PARAMETER_TYPES = "parameter_types";
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 cache a cache used in order to associate the same
048: * id with various occurences of the same object
049: * @exception an XKBException is thrown if export fails
050: */
051: public Element exportObject(Object obj, GenericDriver driver,
052: Map cache) throws XKBException {
053: check(obj, Method.class);
054: Method m = (Method) obj;
055: Element e = new Element(METHOD);
056:
057: // add name
058: e.setAttribute(NAME, m.getName());
059:
060: // add declaring type
061: Element edt = new Element(DECLARING_TYPE);
062: e.addContent(edt);
063: Element e2 = exportObject(m.getDeclaringClass(),
064: GenericDriver.TYPE, driver, cache);
065: edt.addContent(e2);
066:
067: // add parameter types
068: Element ets = new Element(PARAMETER_TYPES);
069: e.addContent(ets);
070: exportChildren(m.getParameterTypes(), ets, GenericDriver.TYPE,
071: driver, cache);
072:
073: return e;
074: }
075:
076: /**
077: * Build an object from an XML element.
078: * @param e an element
079: * @param driver the generic driver
080: * @param cache a cache used to identify objects that have the same id
081: * @param lfactory the logic factory used to create objects
082: * @exception an XKBException is thrown if import fails
083: */
084: public Object importObject(Element e, GenericDriver driver,
085: Map cache, LogicFactory lfactory) throws XKBException {
086: // get name
087: String methodName = e.getAttributeValue(NAME);
088: // get declaring type
089: Element eDecl = e.getChild(DECLARING_TYPE);
090: Element eType = (Element) eDecl.getChildren().get(0);
091: XMLAdapter adapter = driver
092: .getAdapterByTagName(eType.getName());
093: Class declType = (Class) adapter.importObject(eType, driver,
094: cache, lfactory);
095: // get parameter types
096: Element eParam = e.getChild(PARAMETER_TYPES);
097: List eTypes = eParam.getChildren();
098: Class[] parTypes = new Class[eTypes.size()];
099: for (int i = 0; i < eTypes.size(); i++) {
100: eType = (Element) eTypes.get(i);
101: parTypes[i] = (Class) adapter.importObject(eType, driver,
102: cache, lfactory);
103: }
104: // build method
105: try {
106: return declType.getMethod(methodName, parTypes);
107: } catch (Exception x) {
108: StringBuffer msg = new StringBuffer();
109: msg.append("Cannot find method ");
110: msg.append(methodName);
111: msg.append('(');
112: for (int i = 0; i < parTypes.length; i++) {
113: if (i > 0)
114: msg.append(',');
115: msg.append(parTypes[i].toString());
116: }
117: msg.append(") in class ");
118: msg.append(declType);
119:
120: throw new XKBException(msg.toString());
121: }
122: }
123:
124: /**
125: * Get the name of the associated tag (element).
126: * @return a string
127: */
128: public String getTagName() {
129: return METHOD;
130: }
131:
132: /**
133: * Get the kind of object the adapter can export/import.
134: * @return a string
135: */
136: public String getKindOfObject() {
137: return GenericDriver.METHOD;
138: }
139: }
|