Skip to content

Commit 1e7b568

Browse files
committed
Handle invalid datatypes in literals
1 parent 5e6a22b commit 1e7b568

1 file changed

Lines changed: 23 additions & 15 deletions

File tree

src/ubic/basecode/ontology/jena/OntologyIndexer.java

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package ubic.basecode.ontology.jena;
2020

21+
import com.hp.hpl.jena.datatypes.DatatypeFormatException;
2122
import com.hp.hpl.jena.datatypes.xsd.XSDDateTime;
2223
import com.hp.hpl.jena.ontology.OntModel;
2324
import com.hp.hpl.jena.ontology.OntResource;
@@ -223,28 +224,35 @@ private static Directory index( String name, OntModel model, Analyzer analyzer,
223224
Fieldable f;
224225
if ( s.getObject().isLiteral() ) {
225226
Literal l = s.getObject().asLiteral();
226-
if ( l.getValue() instanceof String ) {
227-
f = new Field( field, l.getString(), Field.Store.NO, indexablePropertiesByField.get( field ).isAnalyzed() ? Field.Index.ANALYZED : Field.Index.NOT_ANALYZED );
228-
} else if ( l.getValue() instanceof Number ) {
227+
Object v;
228+
try {
229+
v = l.getValue();
230+
} catch ( DatatypeFormatException e ) {
231+
log.warn( "Invalid datatype for literal: {}", l, e );
232+
continue;
233+
}
234+
if ( v instanceof String ) {
235+
f = new Field( field, ( String ) v, Field.Store.NO, indexablePropertiesByField.get( field ).isAnalyzed() ? Field.Index.ANALYZED : Field.Index.NOT_ANALYZED );
236+
} else if ( v instanceof Number ) {
229237
NumericField nf = new NumericField( field );
230-
if ( l.getValue() instanceof Integer ) {
231-
nf.setIntValue( s.getInt() );
232-
} else if ( l.getValue() instanceof Long ) {
233-
nf.setLongValue( s.getLong() );
234-
} else if ( l.getValue() instanceof Float ) {
235-
nf.setFloatValue( s.getFloat() );
236-
} else if ( l.getValue() instanceof Double ) {
237-
nf.setDoubleValue( s.getDouble() );
238+
if ( v instanceof Integer ) {
239+
nf.setIntValue( ( Integer ) v );
240+
} else if ( v instanceof Long ) {
241+
nf.setLongValue( ( Long ) v );
242+
} else if ( v instanceof Float ) {
243+
nf.setFloatValue( ( Float ) v );
244+
} else if ( v instanceof Double ) {
245+
nf.setDoubleValue( ( Double ) v );
238246
} else {
239247
log.warn( "Skipping numeric literal of unsupported type: {}", l );
240248
continue;
241249
}
242250
f = nf;
243-
} else if ( l.getValue() instanceof XSDDateTime ) {
251+
} else if ( v instanceof XSDDateTime ) {
244252
f = new NumericField( field )
245-
.setLongValue( ( ( XSDDateTime ) l.getValue() ).asCalendar().getTime().getTime() );
246-
} else if ( l.getValue() instanceof Boolean ) {
247-
f = new NumericField( field ).setIntValue( Boolean.TRUE.equals( l.getValue() ) ? 1 : 0 );
253+
.setLongValue( ( ( XSDDateTime ) v ).asCalendar().getTime().getTime() );
254+
} else if ( v instanceof Boolean ) {
255+
f = new NumericField( field ).setIntValue( Boolean.TRUE.equals( v ) ? 1 : 0 );
248256
} else {
249257
log.warn( "Skipping literal of unsupported type: {}", l );
250258
continue;

0 commit comments

Comments
 (0)