private void onAnnotationStart(final String aType, Attributes attrs) throws SAXParseException { String type = toTypeName(aType); Annotation newAnno = new Annotation(); newAnno.setType(type); newAnno.setBegin(textBuilder.length()); // handle features for (int attrIndex = 0; attrIndex < attrs.getLength(); attrIndex++) { final String attrName = attrs.getLocalName(attrIndex); String featName = attrName; if (featNameAliases.containsKey(attrName)) { featName = featNameAliases.get(attrName); } String attrVal = attrs.getValue(attrIndex); newAnno.setFeatureStringValue(featName, attrVal); } // openAnnotations.addFirst(newAnno); }
@Override public boolean apply(Annotation input) { return Objects.equal(type, input.getType()); } });
cas.setDocumentText(contentHandler.getText()); for (Annotation _anno : contentHandler.getAnnotations()) { String typeName = _anno.getType(); Type type = cas.getTypeSystem().getType(typeName); if (type == null) { throw new IllegalStateException(String.format("Unknown type: %s", typeName)); AnnotationFS anno = cas.createAnnotation(type, _anno.getBegin(), _anno.getEnd()); for (String featName : _anno.getFeatureNames()) { Feature feat = type.getFeatureByBaseName(featName); if (feat == null) throw new IllegalStateException(String.format( "%s does not have feature %s", type.getName(), featName)); String featValStr = _anno.getFeatureStringValue(featName); if (featValStr != null) { anno.setFeatureValueFromString(feat, featValStr);
private void onAnnotationEnd(final String aType) throws SAXParseException { final String type = toTypeName(aType); Optional<Annotation> annoOpt = Iterables.tryFind(openAnnotations, new Predicate<Annotation>() { @Override public boolean apply(Annotation input) { return Objects.equal(type, input.getType()); } }); if (annoOpt.isPresent()) { Annotation anno = annoOpt.get(); openAnnotations.remove(anno); anno.setEnd(textBuilder.length()); annotations.add(anno); } else { throw new SAXParseException("End of an element closes an unknown annotation", locator); } }