package org.xins.common.spec;
import org.xins.common.MandatoryArgumentChecker;
import org.xins.common.Utils;
import org.xins.common.text.TextUtils;
import org.xins.common.types.Type;
public final class ParameterSpec {
private final Class _reference;
private final String _parameterName;
private Type _parameterType;
private final boolean _required;
private final String _description;
private final String _default;
ParameterSpec(Class reference, String name, String type, boolean required, String description, String defaultValue)
throws IllegalArgumentException, InvalidSpecificationException {
MandatoryArgumentChecker.check("reference", reference, "name", name, "description", description);
_reference = reference;
_parameterName = name;
_parameterType = getType(type);
_required = required;
_description = description;
_default = defaultValue;
}
public String getName() {
return _parameterName;
}
public String getDescription() {
return _description;
}
public String getDefault() {
return _default;
}
public boolean isRequired() {
return _required;
}
public Type getType() {
return _parameterType;
}
private Type getType(String typeName) throws InvalidSpecificationException {
if (typeName == null || typeName.equals("") || typeName.equals("_text")) {
return org.xins.common.types.standard.Text.SINGLETON;
} else if (typeName.equals("_int8")) {
return org.xins.common.types.standard.Int8.SINGLETON;
} else if (typeName.equals("_int16")) {
return org.xins.common.types.standard.Int16.SINGLETON;
} else if (typeName.equals("_int32")) {
return org.xins.common.types.standard.Int32.SINGLETON;
} else if (typeName.equals("_int64")) {
return org.xins.common.types.standard.Int64.SINGLETON;
} else if (typeName.equals("_float32")) {
return org.xins.common.types.standard.Float32.SINGLETON;
} else if (typeName.equals("_float64")) {
return org.xins.common.types.standard.Float64.SINGLETON;
} else if (typeName.equals("_boolean")) {
return org.xins.common.types.standard.Boolean.SINGLETON;
} else if (typeName.equals("_date")) {
return org.xins.common.types.standard.Date.SINGLETON;
} else if (typeName.equals("_timestamp")) {
return org.xins.common.types.standard.Timestamp.SINGLETON;
} else if (typeName.equals("_base64")) {
return org.xins.common.types.standard.Base64.SINGLETON;
} else if (typeName.equals("_descriptor")) {
return org.xins.common.types.standard.Descriptor.SINGLETON;
} else if (typeName.equals("_properties")) {
return org.xins.common.types.standard.Properties.SINGLETON;
} else if (typeName.equals("_url")) {
return org.xins.common.types.standard.URL.SINGLETON;
} else if (typeName.equals("_hex")) {
return org.xins.common.types.standard.Hex.SINGLETON;
} else if (typeName.equals("_list")) {
return org.xins.common.types.standard.List.SINGLETON;
} else if (typeName.equals("_set")) {
return org.xins.common.types.standard.Set.SINGLETON;
} else if (typeName.equals("_xml")) {
return org.xins.common.types.standard.XML.SINGLETON;
} else if (typeName.charAt(0) != '_') {
String className = _reference.getName();
int truncatePos = className.lastIndexOf(".capi.CAPI");
if (truncatePos == -1) {
truncatePos = className.lastIndexOf(".api.APIImpl");
}
try {
if (typeName.indexOf('/') != -1) {
typeName = typeName.substring(typeName.indexOf('/') + 1);
}
typeName = TextUtils.firstCharUpper(typeName);
String typeClassName = className.substring(0, truncatePos) + ".types." + typeName;
Class typeClass = Class.forName(typeClassName, true, Utils.getContextClassLoader());
Type type = (Type) typeClass.getField("SINGLETON").get(null);
return type;
} catch (Exception ex) {
throw new InvalidSpecificationException("Invalid type: " + typeName + ".", ex);
}
}
throw new InvalidSpecificationException("Invalid type: " + typeName + ".");
}
}