/* * Copyright (c) 2003-2004, KNOPFLERFISH project * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following * conditions are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials * provided with the distribution. * * - Neither the name of the KNOPFLERFISH project nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. */ package org.knopflerfish.shared.cm; import java.io.PushbackReader; import java.lang.reflect.Array; import java.util.Dictionary; import java.util.Hashtable; import java.util.Stack; import java.util.Vector; import org.osgi.framework.Constants; public class CMDataReader extends XmlReader { public final static String ENCODING = "ISO-8859-1"; public final static String FACTORY_PID = "service.factoryPid"; public final static String SERVICE_PID = "service.pid"; private final static String PID = "pid"; private final static String CONFIGURATION = "configuration"; private final static String FACTORY_CONFIGURATION = "factoryconfiguration"; private final static String PROPERTY = "property"; private final static String VECTOR = "vector"; private final static String ARRAY = "array"; private final static String VALUE = "value"; private final static String PRIMITIVE_VALUE = "primitiveValue"; private final static String NULL = "null"; private final static Object NULL_ELEMENT = new Object(); private final static String NAME = "name"; private final static String TYPE = "type"; private final static String LENGTH = "length"; private final static String ELEMENT_TYPE = "elementType"; private static Class classBigDecimal = null; static { try { classBigDecimal = Class.forName("java.math.BigDecimal"); } catch (Throwable ignore) { classBigDecimal = null; } } private static Class classBigInteger = null; static { try { classBigInteger = Class.forName("java.math.BigInteger"); } catch (Throwable ignore) { classBigInteger = null; } } private final static Hashtable stringToClass = new Hashtable(); static { stringToClass.put("Vector", Vector.class); stringToClass.put("Integer", Integer.class); stringToClass.put("Short", Short.class); stringToClass.put("Long", Long.class); stringToClass.put("String", String.class); stringToClass.put("Float", Float.class); stringToClass.put("Double", Double.class); stringToClass.put("Byte", Byte.class); if (classBigInteger != null) { stringToClass.put("BigInteger", classBigInteger); } stringToClass.put("Character", Character.class); stringToClass.put("Boolean", Boolean.class); if (classBigDecimal != null) { stringToClass.put("BigDecimal", classBigDecimal); } } private final static Hashtable stringToPrimitiveType = new Hashtable(); static { stringToPrimitiveType.put("int", Integer.TYPE); stringToPrimitiveType.put("short", Short.TYPE); stringToPrimitiveType.put("long", Long.TYPE); stringToPrimitiveType.put("float", Float.TYPE); stringToPrimitiveType.put("double", Double.TYPE); stringToPrimitiveType.put("byte", Byte.TYPE); stringToPrimitiveType.put("char", Character.TYPE); stringToPrimitiveType.put("boolean", Boolean.TYPE); } private final static Hashtable primitiveTypeToWrapperType = new Hashtable(); static { primitiveTypeToWrapperType.put("int", Integer.class); primitiveTypeToWrapperType.put("short", Short.class); primitiveTypeToWrapperType.put("long", Long.class); primitiveTypeToWrapperType.put("float", Float.class); primitiveTypeToWrapperType.put("double", Double.class); primitiveTypeToWrapperType.put("byte", Byte.class); primitiveTypeToWrapperType.put("char", Character.class); primitiveTypeToWrapperType.put("boolean", Boolean.class); } private final Stack objects = new Stack(); public CMDataReader() { } public Hashtable readCMData(PushbackReader r) throws Exception { if (objects.size() > 0) { objects.removeAllElements(); } read(r); if (objects.size() != 1) { throwMessage("Failed creating Hashtable from cm_data stream."); } return (Hashtable) objects.peek(); } public Hashtable[] readCMDatas(PushbackReader r) throws Exception { if (objects.size() > 0) { objects.removeAllElements(); } read(r); Hashtable[] configs = new Hashtable[objects.size()]; for (int i=0; i= 0; i--) { ret = Array.newInstance(componentType, dimensions[i]); componentType = ret.getClass(); } return ret; } private static Class toJavaType(String type) throws Exception { Class c = (Class) stringToClass.get(type); if (c == null) { c = (Class) stringToPrimitiveType.get(type); } return c; } private static Class toWrapperType(String type) throws Exception { return (Class) primitiveTypeToWrapperType.get(type); } }