DefaultIriExpressionProcessor implements IriExpressionProcessor { @Override public IRI processExpression(String expression, IriExpressionContext context) throws SemanticTranslationException { try { final Expression compiledExpression = PARSER.parseExpression(expression); String result = compiledExpression.getValue(new StandardEvaluationContext(context), String.class); LOG.debug("IRI expression resulted in '{}' with context of type {}", result, context.getClass().getName()); return valueFactory.createIRI(result); } catch (SpelEvaluationException | SpelParseException | IllegalArgumentException e) { throw new SemanticTranslationException("Issue processing IRI expression for expression '" + expression + "' with context of type: " + context.getClass().getName(), e); } }  @Reference void setValueFactory(ValueFactory valueFactory); @Override IRI processExpression(String expression, IriExpressionContext context);  }
@Test public void testClassIriExpression() throws Exception { ExtractedOntology ont = getRequiredOrmFactory(ExtractedOntology.class).createNew(VALUE_FACTORY.createIRI(ONT_URI)); ClassIriExpressionContext context = new DefaultClassIriExpressionContext(ont, "test-ontology", "simple-test-ontology-for-iri-expressions"); String expression = "getOntologyIri().concat('#').concat(getName()).concat('/').concat(getComment())"; IRI result = processor.processExpression(expression, context); Assert.assertEquals(ONT_URI + "#test-ontology/simple-test-ontology-for-iri-expressions", result.stringValue()); }
@Test public void testPropertyIriExpression() throws Exception { ExtractedOntology ont = getRequiredOrmFactory(ExtractedOntology.class).createNew(VALUE_FACTORY.createIRI(ONT_URI)); PropertyIriExpressionContext context = new DefaultPropertyIriExpressionContext(ont, "test-ontology", "simple-test-ontology-for-iri-expressions", VALUE_FACTORY.createIRI("urn: VALUE_FACTORY.createIRI("urn: String expression = "getOntologyIri().concat('#').concat(getName()).concat('/').concat(getComment())"; IRI result = processor.processExpression(expression, context); Assert.assertEquals(ONT_URI + "#test-ontology/simple-test-ontology-for-iri-expressions", result.stringValue()); Assert.assertEquals("urn: context).stringValue()); Assert.assertEquals("urn: context).stringValue()); }
@Test public void testBadExpression() throws Exception { ExtractedOntology ont = getRequiredOrmFactory(ExtractedOntology.class).createNew(VALUE_FACTORY.createIRI(ONT_URI)); PropertyIriExpressionContext context = new DefaultPropertyIriExpressionContext(ont, "test-ontology", "simple-test-ontology-for-iri-expressions", VALUE_FACTORY.createIRI("urn: VALUE_FACTORY.createIRI("urn: try { processor.processExpression("getNoSuchMethod()", context); Assert.fail("Exception should have been thrown when hitting a non-existent method"); } catch (SemanticTranslationException e) { Assert.assertFalse(e.getMessage().isEmpty()); Assert.assertNotNull(e.getCause()); } }