Skip to content
Browse files

Improved type conversion exception handling.

  • Loading branch information...
1 parent fc9d654 commit a7ad5f4469e86aa1e1659c5f36f8ab76d8666cea David A. Solin committed May 18, 2012
View
4 components/sdk/common/rsrc/jovalmsg_en.properties
@@ -262,7 +262,6 @@ ERROR_MOUNT=Error encountered enumerating filesystem mounts
ERROR_PRELOAD=Error encountered during filesystem index preload phase
ERROR_PRELOAD_LINE=Error encountered during preloading: {0}
ERROR_PRELOAD_OVERFLOW=Preload overflowed maximum allowable paths: {0}
-ERROR_RESOLVE_ILLEGAL_ARG=Illegal argument encountered resolving variable {0}: {1}
ERROR_RESOLVE_VAR=Error resolving variable {0}: {1}
ERROR_RESOLVE_ITEM_FIELD=Illegal item field {0} for item type {1}
ERROR_FMRI=Problem reading output from SMF; line={0}
@@ -299,5 +298,6 @@ ERROR_ASCII_CONVERSION=Error converting to ASCII at character {0}: {1}
ERROR_BINARY_LENGTH=Illegal length for binary string {0}: {1}
ERROR_PASSWD_LINE=Bad passwd file line format: {0}
ERROR_TYPED_STATUS=Illegal status {0} for typed data
-ERROR_TYPE_CONVERSION=Cannot cast type from {0} to {1}
+ERROR_TYPE_CONVERSION=Error performing datatype conversion: {0}
+ERROR_TYPE_INCOMPATIBLE=Cannot cast type from {0} to {1}
ERROR_EXCEPTION=Exception follows
View
1 components/sdk/engine/classes.mk
@@ -57,6 +57,7 @@ CLASSES=\
org.joval.oval.types.Ip6AddressType \
org.joval.oval.types.RecordType \
org.joval.oval.types.StringType \
+ org.joval.oval.types.TypeConversionException \
org.joval.oval.types.TypeFactory \
org.joval.oval.types.VersionType \
org.joval.oval.xml.OvalNamespacePrefixMapper \
View
6 src/org/joval/intf/oval/IType.java
@@ -6,6 +6,8 @@
import oval.schemas.common.ComplexDatatypeEnumeration;
import oval.schemas.common.SimpleDatatypeEnumeration;
+import org.joval.oval.types.TypeConversionException;
+
/**
* Generic type interface, for both simple and complex OVAL types.
*
@@ -85,12 +87,12 @@ public ComplexDatatypeEnumeration getComplex() throws UnsupportedOperationExcept
/**
* Cast to another Type.
*/
- IType cast(Type type) throws UnsupportedOperationException;
+ IType cast(Type type) throws TypeConversionException;
/**
* Cast to another Type.
*/
- IType cast(SimpleDatatypeEnumeration type) throws UnsupportedOperationException;
+ IType cast(SimpleDatatypeEnumeration type) throws TypeConversionException;
/**
* @throws UnsupportedOperationException if the Type is RECORD.
View
23 src/org/joval/oval/engine/Engine.java
@@ -141,6 +141,7 @@
import org.joval.oval.types.Ip6AddressType;
import org.joval.oval.types.RecordType;
import org.joval.oval.types.StringType;
+import org.joval.oval.types.TypeConversionException;
import org.joval.oval.types.TypeFactory;
import org.joval.util.JOVALMsg;
import org.joval.util.JOVALSystem;
@@ -778,10 +779,10 @@ private void reset() {
instance.setOperation(simple.getOperation());
result.add(instance);
}
- } catch (UnsupportedOperationException e) {
+ } catch (TypeConversionException e) {
MessageType message = Factories.common.createMessageType();
message.setLevel(MessageLevelEnumeration.ERROR);
- message.setValue(e.getMessage());
+ message.setValue(JOVALMsg.getMessage(JOVALMsg.ERROR_TYPE_CONVERSION, e.getMessage()));
rc.addMessage(message);
}
} else {
@@ -890,10 +891,10 @@ private void reset() {
fieldEntity.setEntityCheck(field.getEntityCheck());
result.add(fieldEntity);
}
- } catch (UnsupportedOperationException e) {
+ } catch (TypeConversionException e) {
MessageType message = Factories.common.createMessageType();
message.setLevel(MessageLevelEnumeration.ERROR);
- message.setValue(e.getMessage());
+ message.setValue(JOVALMsg.getMessage(JOVALMsg.ERROR_TYPE_CONVERSION, e.getMessage()));
rc.addMessage(message);
}
} else {
@@ -1638,10 +1639,9 @@ private ResultEnumeration compare(EntitySimpleBaseType base, EntityItemSimpleBas
cd.addResult(testImpl(varInstance, item));
}
}
- } catch (UnsupportedOperationException e) {
- throw new TestException(JOVALMsg.getMessage(JOVALMsg.ERROR_RESOLVE_VAR, ref, e.getMessage()));
- } catch (IllegalArgumentException e) {
- throw new TestException(JOVALMsg.getMessage(JOVALMsg.ERROR_RESOLVE_ILLEGAL_ARG, ref, e.getMessage()));
+ } catch (TypeConversionException e) {
+ String reason = JOVALMsg.getMessage(JOVALMsg.ERROR_TYPE_CONVERSION, e.getMessage());
+ throw new TestException(JOVALMsg.getMessage(JOVALMsg.ERROR_RESOLVE_VAR, ref, reason));
} catch (NoSuchElementException e) {
String reason = JOVALMsg.getMessage(JOVALMsg.ERROR_VARIABLE_MISSING);
throw new TestException(JOVALMsg.getMessage(JOVALMsg.ERROR_RESOLVE_VAR, ref, reason));
@@ -1679,7 +1679,12 @@ private ResultEnumeration testImpl(EntitySimpleBaseType base, EntityItemSimpleBa
// Let the base dictate the datatype
//
IType baseValue = TypeFactory.createType(base);
- IType itemValue = TypeFactory.createType(item).cast(baseValue.getType());
+ IType itemValue = null;
+ try {
+ itemValue = TypeFactory.createType(item).cast(baseValue.getType());
+ } catch (TypeConversionException e) {
+ throw new TestException(e);
+ }
//
// Validate the operation by datatype, then execute it. See section 5.3.6.3.1 of the specification:
View
2 src/org/joval/oval/engine/FlagData.java
@@ -46,7 +46,7 @@ FlagEnumeration getFlag() throws OvalException {
return FlagEnumeration.ERROR;
} else if (e == 0 && c > 0 && i == 0 && d == 0 && nc == 0 && na == 0) {
return FlagEnumeration.COMPLETE;
- } else if (e == 0 && c >= 0 && i > 0 && d == 0 && nc == 0 && na == 0) {
+ } else if (e == 0 && c >= 0 && i > 0 && d == 0 && nc >= 0 && na == 0) {
return FlagEnumeration.INCOMPLETE;
} else if (e == 0 && c == 0 && i == 0 && d > 0 && nc == 0 && na == 0) {
return FlagEnumeration.DOES_NOT_EXIST;
View
12 src/org/joval/oval/types/AbstractType.java
@@ -17,20 +17,24 @@
abstract class AbstractType implements IType {
AbstractType(){}
- public IType cast(Type type) throws UnsupportedOperationException {
+ public IType cast(Type type) throws TypeConversionException {
if (type == getType()) {
return this;
}
switch(type) {
case RECORD:
- throw new IllegalArgumentException(JOVALMsg.getMessage(JOVALMsg.ERROR_TYPE_CONVERSION, getType(), Type.RECORD));
+ throw new TypeConversionException(JOVALMsg.getMessage(JOVALMsg.ERROR_TYPE_INCOMPATIBLE, getType(), Type.RECORD));
default:
- return TypeFactory.createType(type.getSimple(), getString());
+ try {
+ return TypeFactory.createType(type.getSimple(), getString());
+ } catch (Exception e) {
+ throw new TypeConversionException(e);
+ }
}
}
- public IType cast(SimpleDatatypeEnumeration type) throws UnsupportedOperationException {
+ public IType cast(SimpleDatatypeEnumeration type) throws TypeConversionException {
return cast(TypeFactory.convertType(type));
}
View
2 src/org/joval/oval/types/BinaryType.java
@@ -56,7 +56,7 @@ public int compareTo(IType t) {
BinaryType other = null;
try {
other = (BinaryType)t.cast(getType());
- } catch (UnsupportedOperationException e) {
+ } catch (TypeConversionException e) {
throw new IllegalArgumentException(e);
}
if (data.length > other.data.length) {
View
2 src/org/joval/oval/types/BooleanType.java
@@ -54,7 +54,7 @@ public int compareTo(IType t) {
BooleanType other = null;
try {
other = (BooleanType)t.cast(getType());
- } catch (UnsupportedOperationException e) {
+ } catch (TypeConversionException e) {
throw new IllegalArgumentException(e);
}
if (data == other.data) {
View
2 src/org/joval/oval/types/EvrStringType.java
@@ -46,7 +46,7 @@ public int compareTo(IType t) {
EvrStringType other = null;
try {
other = (EvrStringType)t.cast(getType());
- } catch (UnsupportedOperationException e) {
+ } catch (TypeConversionException e) {
throw new IllegalArgumentException(e);
}
View
2 src/org/joval/oval/types/FilesetRevisionType.java
@@ -38,7 +38,7 @@ public int compareTo(IType t) {
FilesetRevisionType other = null;
try {
other = (FilesetRevisionType)t.cast(getType());
- } catch (UnsupportedOperationException e) {
+ } catch (TypeConversionException e) {
throw new IllegalArgumentException(e);
}
return new EvrStringType(data).compareTo(new EvrStringType(other.data));
View
2 src/org/joval/oval/types/FloatType.java
@@ -59,7 +59,7 @@ public int compareTo(IType t) {
FloatType other = null;
try {
other = (FloatType)t.cast(getType());
- } catch (UnsupportedOperationException e) {
+ } catch (TypeConversionException e) {
throw new IllegalArgumentException(e);
}
return data.compareTo(other.data);
View
2 src/org/joval/oval/types/IntType.java
@@ -44,7 +44,7 @@ public int compareTo(IType t) {
IntType other = null;
try {
other = (IntType)t.cast(getType());
- } catch (UnsupportedOperationException e) {
+ } catch (TypeConversionException e) {
throw new IllegalArgumentException(e);
}
return data.compareTo(other.data);
View
2 src/org/joval/oval/types/IosVersionType.java
@@ -38,7 +38,7 @@ public int compareTo(IType t) {
IosVersionType other = null;
try {
other = (IosVersionType)t.cast(getType());
- } catch (UnsupportedOperationException e) {
+ } catch (TypeConversionException e) {
throw new IllegalArgumentException(e);
}
return new EvrStringType(data).compareTo(new EvrStringType(other.data));
View
2 src/org/joval/oval/types/Ip4AddressType.java
@@ -42,7 +42,7 @@ public int compareTo(IType t) {
Ip4AddressType other = null;
try {
other = (Ip4AddressType)t.cast(getType());
- } catch (UnsupportedOperationException e) {
+ } catch (TypeConversionException e) {
throw new IllegalArgumentException(e);
}
if (getData().equals(other.getData())) {
View
2 src/org/joval/oval/types/Ip6AddressType.java
@@ -42,7 +42,7 @@ public int compareTo(IType t) {
Ip6AddressType other = null;
try {
other = (Ip6AddressType)t.cast(getType());
- } catch (UnsupportedOperationException e) {
+ } catch (TypeConversionException e) {
throw new IllegalArgumentException(e);
}
if (toString().equals(other.toString())) {
View
2 src/org/joval/oval/types/StringType.java
@@ -40,7 +40,7 @@ public int compareTo(IType t) {
StringType other = null;
try {
other = (StringType)t.cast(getType());
- } catch (UnsupportedOperationException e) {
+ } catch (TypeConversionException e) {
throw new IllegalArgumentException(e);
}
return data.compareTo(other.data);
View
20 src/org/joval/oval/types/TypeConversionException.java
@@ -0,0 +1,20 @@
+// Copyright (C) 2012 jOVAL.org. All rights reserved.
+// This software is licensed under the AGPL 3.0 license available at http://www.joval.org/agpl_v3.txt
+
+package org.joval.oval.types;
+
+/**
+ * An exception class for OVAL type conversion errors.
+ *
+ * @author David A. Solin
+ * @version %I% %G%
+ */
+public class TypeConversionException extends Exception {
+ public TypeConversionException(String message) {
+ super(message);
+ }
+
+ public TypeConversionException(Exception e) {
+ super(e);
+ }
+}
View
2 src/org/joval/oval/types/VersionType.java
@@ -43,7 +43,7 @@ public int compareTo(IType t) {
VersionType other = null;
try {
other = (VersionType)t.cast(getType());
- } catch (UnsupportedOperationException e) {
+ } catch (TypeConversionException e) {
throw new IllegalArgumentException(e);
}
return data.compareTo(other.data);
View
2 src/org/joval/oval/xml/OvalNamespacePrefixMapper.java
@@ -108,7 +108,7 @@ private OvalNamespacePrefixMapper(URI uri, Marshaller marshaller) throws Propert
namespaceMap.put("http://oval.mitre.org/XMLSchema/oval-system-characteristics-5#sharepoint", "sharepoint-sc");
namespaceMap.put("http://oval.mitre.org/XMLSchema/oval-system-characteristics-5#solaris", "solaris-sc");
namespaceMap.put("http://oval.mitre.org/XMLSchema/oval-system-characteristics-5#unix", "unix-sc");
- namespaceMap.put("http://oval.mitre.org/XMLSchema/oval-system-characteristics-5#windows", "windows-sc");
+ namespaceMap.put("http://oval.mitre.org/XMLSchema/oval-system-characteristics-5#windows", "win-sc");
switch(uri) {
case SC:
View
2 src/org/joval/util/JOVALMsg.java
@@ -291,7 +291,6 @@
ERROR_PRELOAD_LINE,
ERROR_PRELOAD_OVERFLOW,
ERROR_RESOLVE_VAR,
- ERROR_RESOLVE_ILLEGAL_ARG,
ERROR_RESOLVE_ITEM_FIELD,
ERROR_FMRI,
ERROR_AD_DOMAIN_REQUIRED,
@@ -328,6 +327,7 @@
ERROR_PASSWD_LINE,
ERROR_TYPED_STATUS,
ERROR_TYPE_CONVERSION,
+ ERROR_TYPE_INCOMPATIBLE,
ERROR_EXCEPTION;
private static IMessageConveyor mc;

0 comments on commit a7ad5f4

Please sign in to comment.
Something went wrong with that request. Please try again.