org.apache.xmlbeans.impl.jam.internal.elements.AnnotationValueImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-xmlbeans Show documentation
Show all versions of commons-xmlbeans Show documentation
The Apache Commons Codec package contains simple encoder and decoders for
various formats such as Base64 and Hexadecimal. In addition to these
widely used encoders and decoders, the codec package also maintains a
collection of phonetic encoding utilities.
The newest version!
/* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.xmlbeans.impl.jam.internal.elements;
import org.apache.xmlbeans.impl.jam.JAnnotation;
import org.apache.xmlbeans.impl.jam.JAnnotationValue;
import org.apache.xmlbeans.impl.jam.JClass;
import org.apache.xmlbeans.impl.jam.internal.classrefs.JClassRef;
import org.apache.xmlbeans.impl.jam.internal.classrefs.QualifiedJClassRef;
/**
* Implementation of JAnnotationValue
*
* @author Patrick Calahan
*/
public class AnnotationValueImpl implements JAnnotationValue {
// ========================================================================
// Variables
private Object mValue = null;
private JClassRef mType = null;
private String mName;
private ElementContext mContext;
// ========================================================================
// Constructors
public AnnotationValueImpl(ElementContext ctx,
String name,
Object value,
JClass type) {
if (ctx == null) throw new IllegalArgumentException("null ctx");
if (name == null) throw new IllegalArgumentException("null name");
if (value == null) throw new IllegalArgumentException("null value");
if (type == null) throw new IllegalArgumentException("null type");
if (value.getClass().isArray()) {
mValue = ensureArrayWrapped(value);
} else {
mValue = value;
}
mContext = ctx;
mName = name;
mType = QualifiedJClassRef.create(type);
}
// ========================================================================
// JAnnotationValue implementation
public boolean isDefaultValueUsed() {
throw new IllegalStateException("NYI");
//return mIsDefaultUsed;
}
public String getName() { return mName; }
public JClass getType() { return mType.getRefClass(); }
public JAnnotation asAnnotation() {
if (mValue instanceof JAnnotation) {
return (JAnnotation)mValue;
} else {
return null; //REVIEW or throw?
}
}
public JClass asClass() {
if (mValue instanceof JClass) {
return (JClass)mValue;
} else {
return null; //REVIEW or throw?
}
}
public String asString() {
if (mValue == null) return null;
return mValue.toString();
}
public int asInt() throws NumberFormatException {
if (mValue == null) return 0;
if (mValue instanceof Number) return ((Number)mValue).intValue();
try {
return Integer.parseInt(mValue.toString().trim());
} catch (NumberFormatException nfe) {
return 0;
}
}
public boolean asBoolean() throws IllegalArgumentException {
if (mValue == null) return false;
return Boolean.valueOf(mValue.toString().trim()).booleanValue();
}
public long asLong() throws NumberFormatException {
if (mValue == null) return 0;
if (mValue instanceof Number) return ((Number)mValue).longValue();
try {
return Long.parseLong(mValue.toString().trim());
} catch (NumberFormatException nfe) {
return 0;
}
}
public short asShort() throws NumberFormatException {
if (mValue == null) return 0;
if (mValue instanceof Number) return ((Number)mValue).shortValue();
try {
return Short.parseShort(mValue.toString().trim());
} catch (NumberFormatException nfe) {
return 0;
}
}
public double asDouble() throws NumberFormatException {
if (mValue == null) return 0;
if (mValue instanceof Number) return ((Number)mValue).doubleValue();
try {
return Double.parseDouble(mValue.toString().trim());
} catch (NumberFormatException nfe) {
return 0;
}
}
public float asFloat() throws NumberFormatException {
if (mValue == null) return 0;
if (mValue instanceof Number) return ((Number)mValue).floatValue();
try {
return Float.parseFloat(mValue.toString().trim());
} catch (NumberFormatException nfe) {
return 0;
}
}
public byte asByte() throws NumberFormatException {
if (mValue == null) return 0;
if (mValue instanceof Number) return ((Number)mValue).byteValue();
try {
return Byte.parseByte(mValue.toString().trim());
} catch (NumberFormatException nfe) {
return 0;
}
}
public char asChar() throws IllegalArgumentException {
//FIXME this is not right
if (mValue == null) return 0;
if (mValue instanceof Character) return ((Character)mValue).charValue();
mValue = mValue.toString();
return (((String)mValue).length() == 0) ? 0 : ((String)mValue).charAt(0);
}
public JClass[] asClassArray() {
if (mValue instanceof JClass[]) {
return (JClass[])mValue;
} else {
return null;
}
}
public JAnnotation[] asAnnotationArray() {
if (mValue instanceof JAnnotation[]) {
return (JAnnotation[])mValue;
} else {
return null;
}
}
public String[] asStringArray() {
if (!mValue.getClass().isArray()) return null;
String[] out = new String[((Object[])mValue).length];
for(int i=0; i