You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stanbol.apache.org by rw...@apache.org on 2012/07/23 08:51:06 UTC

svn commit: r1364515 - /incubator/stanbol/trunk/entityhub/model/clerezza/src/main/java/org/apache/stanbol/entityhub/model/clerezza/impl/Resource2ValueAdapter.java

Author: rwesten
Date: Mon Jul 23 06:51:06 2012
New Revision: 1364515

URL: http://svn.apache.org/viewvc?rev=1364515&view=rev
Log:
fixes STANBOL-698 by catching RuntimeExceptions while converting TypedLiterals to Java Objects and using the lexical form (String value) in case of an Exception.

Modified:
    incubator/stanbol/trunk/entityhub/model/clerezza/src/main/java/org/apache/stanbol/entityhub/model/clerezza/impl/Resource2ValueAdapter.java

Modified: incubator/stanbol/trunk/entityhub/model/clerezza/src/main/java/org/apache/stanbol/entityhub/model/clerezza/impl/Resource2ValueAdapter.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/entityhub/model/clerezza/src/main/java/org/apache/stanbol/entityhub/model/clerezza/impl/Resource2ValueAdapter.java?rev=1364515&r1=1364514&r2=1364515&view=diff
==============================================================================
--- incubator/stanbol/trunk/entityhub/model/clerezza/src/main/java/org/apache/stanbol/entityhub/model/clerezza/impl/Resource2ValueAdapter.java (original)
+++ incubator/stanbol/trunk/entityhub/model/clerezza/src/main/java/org/apache/stanbol/entityhub/model/clerezza/impl/Resource2ValueAdapter.java Mon Jul 23 06:51:06 2012
@@ -31,59 +31,73 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * Converts the Resources (used to store field values in the Clerezza triple
- * store) back to values as defined by the {@link Representation} interface
+ * Converts the Resources (used to store field values in the Clerezza triple store) back to values as defined
+ * by the {@link Representation} interface
+ * 
  * @author Rupert Westenthaler
- *
- * @param <T> the type of the Resource that can be converted to values
+ * 
+ * @param <T>
+ *            the type of the Resource that can be converted to values
  */
-public class Resource2ValueAdapter<T extends Resource> implements Adapter<T, Object> {
+public class Resource2ValueAdapter<T extends Resource> implements Adapter<T,Object> {
 
     private static Logger log = LoggerFactory.getLogger(Resource2ValueAdapter.class);
 
-
     private final LiteralFactory literalFactory = LiteralFactory.getInstance();
 
     private RdfValueFactory valueFactory = RdfValueFactory.getInstance();
 
     @Override
     public final Object adapt(T value, Class<Object> type) {
-        if(value instanceof UriRef){
-            return valueFactory.createReference((UriRef)value);
-        } else if(value instanceof PlainLiteral){
-            return valueFactory.createText((Literal)value);
-        } else if(value instanceof TypedLiteral){
+        if (value instanceof UriRef) {
+            return valueFactory.createReference((UriRef) value);
+        } else if (value instanceof PlainLiteral) {
+            return valueFactory.createText((Literal) value);
+        } else if (value instanceof TypedLiteral) {
             TypedLiteral literal = (TypedLiteral) value;
-            if(literal.getDataType() == null){ //if no dataType is defined
-                //return a Text without a language
+            if (literal.getDataType() == null) { // if no dataType is defined
+                // return a Text without a language
                 return valueFactory.createText(literal);
             } else {
                 XsdDataTypeEnum mapping = RdfResourceUtils.XSD_DATATYPE_VALUE_MAPPING.get(literal.getDataType());
-                if(mapping != null){
-                    if(mapping.getMappedClass() != null){
-                        return literalFactory.createObject(mapping.getMappedClass(), literal);
-                    } else { //if no mapped class
-                        //bypass the LiteralFactory and return the string representation
+                if (mapping != null) {
+                    if (mapping.getMappedClass() != null) {
+                        try {
+                            return literalFactory.createObject(mapping.getMappedClass(), literal);
+                        } catch (RuntimeException e){
+                            log.info("Unable to convert Literal value {} to Java Class {} because of {} with message {}",
+                                new Object[]{literal,mapping.getMappedClass().getSimpleName(),
+                                             e.getClass().getSimpleName(),e.getMessage()});
+                            log.trace("Exception:",e);
+                            //STANBOL-698: Decide what to do in such cases
+                            //(a) throw an exception
+                            // throw e;
+                            //(b) ignore illegal values
+                            //return null;
+                            //(c) use the lexical form
+                            return literal.getLexicalForm();
+                        }
+                    } else { // if no mapped class
+                        // bypass the LiteralFactory and return the string
+                        // representation
                         return literal.getLexicalForm();
                     }
-                } else { //if dataType is not supported
+                } else { // if dataType is not supported
                     /*
-                     * this could indicate two things:
-                     * 1) the SimpleLiteralFactory supports a new DataType and
-                     *    because of that it creates Literals with this Type
-                     * 2) Literals with this data type where created by other
-                     *    applications.
-                     * In the first case one need to update the enumeration. In
-                     * the second case using the LexicalForm should be OK
-                     * Rupert Westenthaler 2010.10.28
+                     * this could indicate two things: 1) the SimpleLiteralFactory supports a new DataType and
+                     * because of that it creates Literals with this Type 2) Literals with this data type
+                     * where created by other applications. In the first case one need to update the
+                     * enumeration. In the second case using the LexicalForm should be OK Rupert Westenthaler
+                     * 2010.10.28
                      */
-                    log.warn("Missing Mapping for DataType "+literal.getDataType().getUnicodeString()
-                            +" -> return String representation");
+                    log.warn("Missing Mapping for DataType {} -> return String representation",
+                        literal.getDataType().getUnicodeString());
                     return literal.getLexicalForm();
                 }
             }
         } else {
-            log.warn("Unsupported Resource Type "+value.getClass()+" -> return String by using the toString method");
+            log.warn("Unsupported Resource Type {} -> return String by using the toString method",
+                value.getClass());
             return value.toString();
         }
     }