001 /* 002 Copyright (C) 2002 Zachary Medico 003 004 This program is free software; you can redistribute it and/or modify 005 it under the terms of the GNU Lesser General Public License as 006 published by the Free Software Foundation; either version 2 of the 007 License, or (at your option) any later version. 008 009 This program is distributed in the hope that it will be useful, 010 but WITHOUT ANY WARRANTY; without even the implied warranty of 011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 GNU Lesser General Public License for more details. 013 014 You should have received a copy of the GNU Lesser General Public 015 License along with this program; if not, write to the Free Software 016 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 017 USA */ 018 019 package org.objectweb.jac.core.parsers.xml; 020 021 022 import org.apache.log4j.Logger; 023 import org.objectweb.jac.core.ConfigMethod; 024 import org.w3c.dom.*; 025 026 public class MethodElementInterpreter { 027 static Logger logger = Logger.getLogger("xml"); 028 029 public MethodElementInterpreter() { 030 } 031 032 public ConfigMethod interpret(Element methodElement, Class targetClass) 033 throws Exception 034 { 035 logger.debug("interpreting method "+methodElement.getAttribute("name")); 036 NodeList argElements = methodElement.getElementsByTagName("arg"); 037 int argCount = argElements.getLength(); 038 // Class[] types = new Class[argCount]; 039 Object[] values = new Object[argCount]; 040 041 for ( int argIndex=0; argIndex<argCount; argIndex++ ) { 042 Element argElement = (Element)argElements.item( argIndex ); 043 values[argIndex] = interpretArgument(argElement); 044 } 045 046 return new ConfigMethod(methodElement.getAttribute("name"), values); 047 } 048 049 protected Object interpretArgument(Element argElement) { 050 if (argElement.getAttribute("type").equals("java.lang.reflect.Array")) { 051 return interpretArray(argElement); 052 } else { 053 return argElement.getAttribute("value"); 054 } 055 } 056 057 /* handle Array arguments */ 058 protected Object[] interpretArray(Element argElement) 059 { 060 NodeList itemElements = argElement.getElementsByTagName("item"); 061 int itemCount = itemElements.getLength(); 062 Object[] array = new Object[itemCount]; 063 for (int i=0; i<itemCount; i++) { 064 Element itemElement = (Element)itemElements.item(i); 065 array[i] = interpretArgument(itemElement); 066 } 067 return array; 068 } 069 }