FeatureValueReplacer extends CasAnnotator_ImplBase { @Override public void process(CAS cas) throws AnalysisEngineProcessException { for (AnnotationFS anno : cas.getAnnotationIndex(annoType)) { String featValue = extractValue(anno); if (featValue == null) { return; } Matcher matcher = pattern.matcher(featValue); if (matcher.matches()) { String newFeatValue = matcher.replaceAll(replaceBy); if (!newFeatValue.equals(featValue)) { cas.removeFsFromIndexes(anno); setValue(anno, newFeatValue); cas.addFsToIndexes(anno); getLogger().info(String.format( "Replacement done for feature %s.%s: '%s' => '%s'", annoTypeName, featurePathString, featValue, newFeatValue)); } } } }  @Override void initialize(UimaContext context); @Override void typeSystemInit(TypeSystem ts); @Override void process(CAS cas); static final String PARAM_REPLACE_BY; static final String PARAM_PATTERN; static final String PARAM_FEATURE_PATH; static final String PARAM_ANNO_TYPE; }
@Test public void test() throws UIMAException { AnalysisEngine ae = AnalysisEngineFactory.createEngine(FeatureValueReplacer.class, tsd, FeatureValueReplacer.PARAM_ANNO_TYPE, DocumentMetadata.class.getName(), FeatureValueReplacer.PARAM_FEATURE_PATH, "sourceUri", FeatureValueReplacer.PARAM_PATTERN, "file:.+/([^/]+)$", FeatureValueReplacer.PARAM_REPLACE_BY, "$1"); JCas cas = ae.newCAS().getJCas(); cas.setDocumentText("Bla bla"); DocumentMetadata metaAnno = new DocumentMetadata(cas); metaAnno.setBegin(0); metaAnno.setEnd(0); metaAnno.setSourceUri("file:/d:/somefolder/somemore/foobar.txt"); metaAnno.addToIndexes(); ae.process(cas); metaAnno = (DocumentMetadata) cas.getAnnotationIndex(DocumentMetadata.type).iterator().next(); assertEquals("foobar.txt", metaAnno.getSourceUri()); cas = ae.newCAS().getJCas(); cas.setDocumentText("Bla bla more"); metaAnno = new DocumentMetadata(cas); metaAnno.setBegin(0); metaAnno.setEnd(0); metaAnno.setSourceUri("http: metaAnno.addToIndexes(); ae.process(cas); metaAnno = (DocumentMetadata) cas.getAnnotationIndex(DocumentMetadata.type).iterator().next(); assertEquals("http: }