private void onFeatureAliasStart(Attributes attrs) throws SAXParseException { String key = requiredAttribute(attrs, A_KEY); String featName = requiredAttribute(attrs, A_NAME); featNameAliases.put(key, featName); }
@Override public boolean apply(Annotation input) { return Objects.equal(type, input.getType()); } });
@Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { String elemName = getElementName(uri, localName, qName); if (readingBody) { onAnnotationStart(elemName, attributes); } else if (E_DOC.equals(elemName)) { onDocStart(); } else if (E_META.equals(elemName)) { onMetaStart(attributes); } else if (E_BODY.equals(elemName)) { onBodyStart(); } else if (E_ALIASES.equals(elemName)) { onAliasesStart(); } else if (E_ALIAS.equals(elemName)) { onAliasStart(attributes); } else if (E_FEATURE_ALIAS.equals(elemName)) { onFeatureAliasStart(attributes); } else { throw new SAXParseException(String.format("Unknown element '%s'", elemName), locator); } }
@Override public void endElement(String uri, String localName, String qName) throws SAXException { String elemName = getElementName(uri, localName, qName); if (readingBody) { if (E_BODY.equals(elemName)) { onBodyEnd(); } else { onAnnotationEnd(elemName); } } else if (E_DOC.equals(elemName)) { onDocEnd(); } else if (E_ALIASES.equals(elemName)) { onAliasesEnd(); } else if (E_ALIAS.equals(elemName)) { onAliasEnd(); } else if(E_FEATURE_ALIAS.equals(elemName)) { onFeatureAliasEnd(); } else if (E_META.equals(elemName)) { onMetaEnd(); } else { throw new SAXParseException(String.format("End of unknown element '%s'", elemName), locator); } }
AXMLContentHandler contentHandler = new AXMLContentHandler(cas.getTypeSystem()); xmlReader.setContentHandler(contentHandler); InputSource inputSource = new InputSource(in); xmlReader.parse(inputSource); 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 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); }
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); } }
private void run() throws ResourceInitializationException, IOException, SAXException { Collection<File> inputFiles = FileUtils.listFiles(inputDir, FileFilterUtils.suffixFileFilter(".xml"), null); if (inputFiles.isEmpty()) { return; } TypeSystemDescription tsd = TypeSystemDescriptionFactory .createTypeSystemDescription(typeSystemDescName); CAS cas = CasCreationUtils.createCas(tsd, null, null); for (File inputFile : inputFiles) { AXMLReader.read(inputFile, cas); File outFile = getOutputFile(inputFile); OutputStream out = FileUtils.openOutputStream(outFile); try { XmiCasSerializer.serialize(cas, null, out, true, null); } finally { out.close(); cas.reset(); } } }
@Override public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { if (readingBody && readCharacters) { String newChars = String.valueOf(ch, start, length); textBuilder.append(preprocessChars(newChars)); } }
public static void read(File file, CAS cas) throws IOException, SAXException { BufferedReader in = IoUtils.openReader(file); try { read(in, cas); } finally { IOUtils.closeQuietly(in); } }
private void onAliasStart(Attributes attrs) throws SAXParseException { String key = requiredAttribute(attrs, A_KEY); String type = requiredAttribute(attrs, A_TYPE); if (typeSystem.getType(type) == null) { throw new SAXParseException(String.format("Unknown type: %s", type), locator); } if (typeAliases.put(key, type) != null) { throw new SAXParseException("Duplicate alias declaration", locator); } }
@Override public void characters(char[] ch, int start, int length) throws SAXException { String newChars = String.valueOf(ch, start, length); if (!readCharacters) { if (!StringUtils.isBlank(newChars)) { throw new SAXParseException(String.format( "Unexpected characters '%s'", String.valueOf(ch, start, length)), locator); } } else { textBuilder.append(preprocessChars(newChars)); } }