ad.java
来自「OSGI这是一个中间件,与UPNP齐名,是用于移植到嵌入式平台之上」· Java 代码 · 共 727 行 · 第 1/2 页
JAVA
727 行
/* * Copyright (c) 2003, 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.util.metatype;import org.osgi.framework.*;import org.osgi.service.metatype.*;import org.knopflerfish.util.Text;import java.util.*;import java.lang.reflect.*;import java.io.*;/** * Implementation calss for AttributeDefinition. * * <p> * This class contains get and parse methods for operations * related to constructing AttributeDefinition. * </p> */public class AD implements AttributeDefinition, Comparable { int type; int card; String[] defValue; String desc; String id; String name; String[] optLabels; String[] optValues; boolean bOptional = false; /** * String used for separating array and vector string * representations. */ static final String SEQUENCE_SEP = ","; /** * Create an AttributeDefinition with empty descrition and no option * labels or option values. */ public AD( String id, int type, int card, String name, String[] defValue) { this(id, type, card, name, "", // desc defValue, // defValue null, // optLabels null // optValues ); } /** * Create a new attribute definition. * * @param id Unique id of the definition * @param card cardinality of the definition * @param type One of the type constants <tt>STRING...BOOLEAN</tt> * @throws IllegalArgumentException if <i>type</i> is not supported. * @throws IllegalArgumentException if <i>id</i> is <tt>null</tt> or empty * @throws IllegalArgumentException if <i>desc</i> is <tt>null</tt> */ public AD( String id, int type, int card, String name, String desc, String[] defValue, String[] optLabels, String[] optValues) { if(type < STRING || type > BOOLEAN) { throw new IllegalArgumentException("Unsupported type " + type); } if(id == null || "".equals(id)) { throw new IllegalArgumentException("Bad id '" + id + "'"); } if(desc == null) { throw new IllegalArgumentException("Description cannot be null"); } if(defValue == null) { String s = ""; switch(type) { case STRING: s = ""; break; case INTEGER: case LONG: case SHORT: case BYTE: case BIGINTEGER: s = "0"; break; case BIGDECIMAL: case DOUBLE: case FLOAT: s = "0.0"; break; case CHARACTER: s = "-"; break; case BOOLEAN: s = "false"; break; } defValue = new String[] { s }; } this.type = type; this.card = card; this.desc = desc; this.id = id; this.name = name; this.optLabels = optLabels; this.optValues = optValues; setDefaultValue(defValue); } private AD() { throw new RuntimeException("Not supported"); } public int getCardinality() { return card; } public String[] getDefaultValue() { return defValue; } public void setDescription(String s) { this.desc = s; } /** * Set the default value. * * @throws IllegalArgumentException if any of the values cannot be validated. */ public void setDefaultValue(String[] value) { String s = validate(toString(value)); if(s != null && !"".equals(s)) { throw new IllegalArgumentException("Bad default value '" + toString(value) + "' " + ", id=" + id + ", class=" + getClass(type) + ", err=" + s); } defValue = value; } public String getDescription() { return desc; } public String getID() { return id; } public String getName() { return name; } /** * Set values returned by <tt>getOptionValues</tt> and * <tt>getOptionLabels</tt>. * * @param optValues Values to be return by <tt>getOptionValues</tt>. Can * be <tt>null</tt>. * @param optLabels Values to be return by <tt>getOptionLabels</tt>. Can * be <tt>null</tt> iff <tt>optValues</tt> is <tt>null</tt>. * @throws IllegalArgumentException if optValues and optLabels are not the * same length. */ public void setOptions(String[] optValues, String[] optLabels) { if(optValues != null) { if(optLabels == null || optValues.length != optLabels.length) { throw new IllegalArgumentException("Values must be same length as labels"); } } this.optValues = optValues; this.optLabels = optLabels; } public String[] getOptionLabels() { return optLabels; } public String[] getOptionValues() { return optValues; } public int getType() { return type; } /** * Return true if this attribute is optional. */ public boolean isOptional() { return bOptional; } /** * Get the attribute type given any suported java object. * * @param val Any java object, including arrays of primitive types. * If <i>val</i> is a Vector, it must * contain at least one element. * @return <tt>STRING...BOOLEAN</tt> * @throws IllegalArgumentException if type cannot be derived. */ public static int getType(Object val) { if(val instanceof Vector) { Vector v = (Vector)val; if(v.size() == 0) { throw new IllegalArgumentException("Vector is empty " + "-- no type can be derived"); } else { return getType(v.elementAt(0)); } } else if(val.getClass().isArray()) { return getArrayType(val); } else { return getPrimitiveType(val); } } static Class BIGDECIMAL_PRIMITIVE = Double.TYPE; static Class BIGDECIMAL_OBJECT = Double.class; static Class BIGINTEGER_PRIMITIVE = Integer.TYPE; static Class BIGINTEGER_OBJECT = Integer.class; static final Class[] ARRAY_CLASSES = new Class[BOOLEAN-STRING + 1]; static final Class[] PRIMITIVE_CLASSES = new Class[] { String.class, Long.TYPE, Integer.TYPE, Short.TYPE, Character.TYPE, Byte.TYPE, Double.TYPE, Float.TYPE, BIGINTEGER_PRIMITIVE, BIGDECIMAL_PRIMITIVE, Boolean.TYPE, }; static final Class[] OBJECT_CLASSES = new Class[] { String.class, Long.class, Integer.class, Short.class, Character.class, Byte.class, Double.class, Float.class, BIGINTEGER_OBJECT, BIGDECIMAL_OBJECT, Boolean.class, }; static { try { BIGDECIMAL_PRIMITIVE = Class.forName("java.math.BigDecimal"); BIGDECIMAL_OBJECT = BIGDECIMAL_PRIMITIVE; } catch (Throwable t) { // System.out.println("no BigDecimal"); } try { BIGINTEGER_PRIMITIVE = Class.forName("java.math.BigInteger"); BIGINTEGER_OBJECT = BIGINTEGER_PRIMITIVE; } catch (Throwable t) { // System.out.println("no BigInteger"); } /* System.out.println("BIGINTEGER_PRIMITIVE=" + BIGINTEGER_PRIMITIVE); System.out.println("BIGINTEGER_OBJECT=" + BIGINTEGER_OBJECT); System.out.println("BIGDECIMAL_PRIMITIVE=" + BIGDECIMAL_PRIMITIVE); System.out.println("BIGDECIMAL_OBJECT=" + BIGDECIMAL_OBJECT); */ try { for(int i = STRING; i <= BOOLEAN; i++) { ARRAY_CLASSES[i-STRING] = (Array.newInstance(getPrimitiveClass(i),0)).getClass(); } /* for(int i = STRING; i <= BOOLEAN; i++) { Object array = Array.newInstance(getPrimitiveClass(i), 0); System.out.println(i + ": " + getPrimitiveClass(i).getName() + ", " + getClass(i).getName() + ", " + array.getClass().getName() + ", " + getArrayType(array)); } */ } catch (Exception e) { e.printStackTrace(); System.exit(0); } } /** * Get type from an array object. * * @param val an array object * @return <tt>STRING...BOOLEAN</tt> * @throws IllegalArgumentException if type cannot be derived. */ public static int getArrayType(Object val) { // Isn't there an easier way of doing this? Like // Array.getElementClass() or similar? for(int i = STRING; i <= BOOLEAN; i++) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?