📄 eaf24parser.java
字号:
// is thrown in such case File f = new File(fileName); if (f.exists()) { try { FileInputStream fis = new FileInputStream(f); InputSource source = new InputSource(fis); reader.parse(source); // just catch any exception } catch (Exception ee) { System.out.println("Parsing retry error: " + ee.getMessage()); } } } catch (IOException e) { System.out.println("IO error: " + e.getMessage()); // the SAX parser can have difficulties with certain characters in // the filepath: try to create an InputSource for the parser // HS Mar 2007: depending on Xerces version a SAXException or an IOException // is thrown in such case File f = new File(fileName); if (f.exists()) { try { FileInputStream fis = new FileInputStream(f); InputSource source = new InputSource(fis); reader.parse(source); // just catch any exception } catch (Exception ee) { System.out.println("Parsing retry error: " + ee.getMessage()); } } } catch (Exception e) { printErrorLocationInfo("Fatal(?) Error! " + e.getMessage()); } //long duration = System.currentTimeMillis() - start; // System.out.println("Parsing took " + duration + " milli seconds"); } private void println(String s) { if (verbose) { System.out.println(s); } } private void printErrorLocationInfo(String message) { System.out.println(message); System.out.println("Exception for " + currentFileName); System.out.println("Tier id " + currentTierId); System.out.println("Annotation id " + currentAnnotationId); } /** * An error handler for the eaf parser.<br> * The exception thrown (by Xerces 2.6.2) contains apart from file name, * line and column number, only a description of the problem in it's message. * To really deal with a problem a handler would need to parse the message * for certain strings (defined in a Xerces resource .properties file) and/or * read the file to the specified problem line. * Problematic... * * @author Han Sloetjes, MPI */ class EAFErrorHandler implements ErrorHandler { /** * DOCUMENT ME! * * @param exception DOCUMENT ME! * * @throws SAXException DOCUMENT ME! */ public void error(SAXParseException exception) throws SAXException { System.out.println("Error: " + exception.getMessage()); // system id is the file path System.out.println("System id" + exception.getSystemId()); System.out.println("Public id" + exception.getPublicId()); System.out.println("Line: " + exception.getLineNumber()); System.out.println("Column: " + exception.getColumnNumber()); } /** * DOCUMENT ME! * * @param exception DOCUMENT ME! * * @throws SAXException DOCUMENT ME! */ public void fatalError(SAXParseException exception) throws SAXException { System.out.println("FatalError: " + exception.getMessage()); } /** * DOCUMENT ME! * * @param exception DOCUMENT ME! * * @throws SAXException DOCUMENT ME! */ public void warning(SAXParseException exception) throws SAXException { System.out.println("Warning: " + exception.getMessage()); } } /** * EAF 2.4 content handler. */ class EAFContentHandler implements ContentHandler { private Locator locator; /** * DOCUMENT ME! * * @param locator DOCUMENT ME! */ public void setDocumentLocator(Locator locator) { this.locator = locator; } /** * DOCUMENT ME! * * @param prefix DOCUMENT ME! * @param uri DOCUMENT ME! */ public void startPrefixMapping(String prefix, String uri) { } /** * DOCUMENT ME! * * @param prefix DOCUMENT ME! */ public void endPrefixMapping(String prefix) { } /** * DOCUMENT ME! * * @param ch DOCUMENT ME! * @param start DOCUMENT ME! * @param end DOCUMENT ME! * * @throws SAXException DOCUMENT ME! */ public void ignorableWhitespace(char[] ch, int start, int end) throws SAXException { } /** * DOCUMENT ME! * * @param name DOCUMENT ME! * * @throws SAXException DOCUMENT ME! */ public void skippedEntity(String name) throws SAXException { } /** * DOCUMENT ME! * * @param target DOCUMENT ME! * @param data DOCUMENT ME! * * @throws SAXException DOCUMENT ME! */ public void processingInstruction(String target, String data) throws SAXException { } /** * ContentHandler method * * @throws SAXException DOCUMENT ME! */ public void startDocument() throws SAXException { parseError = false; } /** * ContentHandler method * * @throws SAXException DOCUMENT ME! */ public void endDocument() throws SAXException { } /** * ContentHandler method * * @param nameSpaceURI DOCUMENT ME! * @param name DOCUMENT ME! * @param rawName DOCUMENT ME! * @param attributes DOCUMENT ME! * * @throws SAXException DOCUMENT ME! */ public void startElement(String nameSpaceURI, String name, String rawName, Attributes attributes) throws SAXException { // System.out.println("startElement called for name:" + name); content = ""; if (name.equals("ANNOTATION_DOCUMENT")) { author = attributes.getValue("AUTHOR"); } else if (name.equals("HEADER")) { // implement when dealing with MediaObject mediaFile = attributes.getValue("MEDIA_FILE"); // remove, there is not such attribute in the eaf schema svgFile = attributes.getValue("SVG_FILE"); } else if (name.equals("MEDIA_DESCRIPTOR")) { String mediaURL = attributes.getValue("MEDIA_URL"); String mimeType = attributes.getValue("MIME_TYPE"); MediaDescriptor md = new MediaDescriptor(mediaURL, mimeType); long timeOrigin = 0; if (attributes.getValue("TIME_ORIGIN") != null) { timeOrigin = Long.parseLong(attributes.getValue( "TIME_ORIGIN")); md.timeOrigin = timeOrigin; } String extractedFrom = ""; if (attributes.getValue("EXTRACTED_FROM") != null) { extractedFrom = attributes.getValue("EXTRACTED_FROM"); md.extractedFrom = extractedFrom; } mediaDescriptors.add(md); } else if (name.equals("LINKED_FILE_DESCRIPTOR")) { String linkURL = attributes.getValue("LINK_URL"); String mime = attributes.getValue("MIME_TYPE"); LinkedFileDescriptor lfd = new LinkedFileDescriptor(linkURL, mime); if (attributes.getValue("TIME_ORIGIN") != null) { try { long origin = Long.parseLong(attributes.getValue( "TIME_ORIGIN")); lfd.timeOrigin = origin; } catch (NumberFormatException nfe) { System.out.println("Could not parse the time origin: " + nfe.getMessage()); } } String assoc = attributes.getValue("ASSOCIATED_WITH"); if (assoc != null) { lfd.associatedWith = assoc; } linkedFileDescriptors.add(lfd); } else if (name.equals("PROPERTY")) { // transcription properties currentProperty = new PropertyImpl(); if (attributes.getValue("NAME") != null) { currentProperty.setName(attributes.getValue("NAME")); } docProperties.add(currentProperty); } else if (name.equals("TIME_ORDER")) { // nothing to be done, tierOrder ArrayList already created } else if (name.equals("TIME_SLOT")) { String timeValue = String.valueOf(TimeSlot.TIME_UNALIGNED); if (attributes.getValue("TIME_VALUE") != null) { timeValue = attributes.getValue("TIME_VALUE"); } timeSlots.put(attributes.getValue("TIME_SLOT_ID"), timeValue); timeOrder.add(attributes.getValue("TIME_SLOT_ID")); } else if (name.equals("TIER")) { currentTierId = attributes.getValue("TIER_ID"); // First check whether this tier already exists if (!tiers.containsKey(currentTierId)) { // create a record TierRecord tr = new TierRecord(); tr.setName(currentTierId); tierMap.put(currentTierId, tr); tr.setParticipant(attributes.getValue("PARTICIPANT")); tr.setAnnotator(attributes.getValue("ANNOTATOR")); tr.setLinguisticType(attributes.getValue( "LINGUISTIC_TYPE_REF")); tr.setDefaultLocale(attributes.getValue("DEFAULT_LOCALE")); tr.setParentTier(attributes.getValue("PARENT_REF")); // create entries in the tiers and tierAttributes HashMaps for annotations and attributes resp. tiers.put(currentTierId, new ArrayList()); tierNames.add(currentTierId); } } else if (name.equals("ALIGNABLE_ANNOTATION")) { currentAnnotationId = attributes.getValue("ANNOTATION_ID"); // create new "AnnotationRecord" and add to annotations HashMap for current tier //// currentAnnRecord = new AnnotationRecord(); currentAnnRecord.setAnnotationId(currentAnnotationId); String svg_ref = attributes.getValue("SVG_REF"); if (svg_ref != null) { currentAnnRecord.setAnnotationType(AnnotationRecord.ALIGNABLE_SVG); currentAnnRecord.setSvgReference(svg_ref); } else { currentAnnRecord.setAnnotationType(AnnotationRecord.ALIGNABLE); } currentAnnRecord.setBeginTimeSlotId(attributes.getValue( "TIME_SLOT_REF1")); currentAnnRecord.setEndTimeSlotId(attributes.getValue( "TIME_SLOT_REF2")); ((ArrayList) tiers.get(currentTierId)).add(currentAnnRecord); } else if (name.equals("REF_ANNOTATION")) { currentAnnotationId = attributes.getValue("ANNOTATION_ID"); // create new "AnnotationRecord" and add to annotations HashMap for current tier //// currentAnnRecord = new AnnotationRecord(); currentAnnRecord.setAnnotationId(currentAnnotationId); currentAnnRecord.setAnnotationType(AnnotationRecord.REFERENCE); currentAnnRecord.setReferredAnnotId(attributes.getValue( "ANNOTATION_REF")); if (attributes.getValue("PREVIOUS_ANNOTATION") != null) { currentAnnRecord.setPreviousAnnotId(attributes.getValue( "PREVIOUS_ANNOTATION")); } else { currentAnnRecord.setPreviousAnnotId(""); } ((ArrayList) tiers.get(currentTierId)).add(currentAnnRecord); } else if (name.equals("LINGUISTIC_TYPE")) { LingTypeRecord ltr = new LingTypeRecord(); ltr.setLingTypeId(attributes.getValue("LINGUISTIC_TYPE_ID")); String timeAlignable = "true"; if ((attributes.getValue("TIME_ALIGNABLE") != null) && (attributes.getValue("TIME_ALIGNABLE").equals("false"))) { timeAlignable = "false"; } ltr.setTimeAlignable(timeAlignable); String graphicReferences = "false"; if ((attributes.getValue("GRAPHIC_REFERENCES") != null) && (attributes.getValue("GRAPHIC_REFERENCES").equals("true"))) { graphicReferences = "true"; } ltr.setGraphicReferences(graphicReferences); String stereotype = attributes.getValue("CONSTRAINTS"); ltr.setStereoType(stereotype); ltr.setControlledVocabulary(attributes.getValue( "CONTROLLED_VOCABULARY_REF")); linguisticTypes.add(ltr); } else if (name.equals("LOCALE")) { String langCode = attributes.getValue("LANGUAGE_CODE"); String countryCode = attributes.getValue("COUNTRY_CODE"); if (countryCode == null) { countryCode = ""; } String variant = attributes.getValue("VARIANT"); if (variant == null) { variant = ""; } Locale l = new Locale(langCode, countryCode, variant); locales.add(l); } else if (name.equals("CONTROLLED_VOCABULARY")) { currentCVId = attributes.getValue("CV_ID"); ArrayList cv = new ArrayList(); String desc = attributes.getValue("DESCRIPTION"); if (desc != null) { cv.add(desc); } controlledVocabularies.put(currentCVId, cv); } else if (name.equals("CV_ENTRY")) { currentEntryRecord = new CVEntryRecord(); currentEntryRecord.setDescription(attributes.getValue( "DESCRIPTION")); ((ArrayList) controlledVocabularies.get(currentCVId)).add(currentEntryRecord); } } //startElement /** * ContentHandler method * * @param nameSpaceURI DOCUMENT ME! * @param name DOCUMENT ME! * @param rawName DOCUMENT ME! * * @throws SAXException DOCUMENT ME! */ public void endElement(String nameSpaceURI, String name, String rawName) throws SAXException { if (name.equals("ANNOTATION_VALUE")) { currentAnnRecord.setValue(content); } else if (name.equals("CV_ENTRY")) { currentEntryRecord.setValue(content); } else if (name.equals("PROPERTY")) { if (content.length() > 0) { currentProperty.setValue(content); } } } /** * ContentHandler method * * @param ch DOCUMENT ME! * @param start DOCUMENT ME! * @param end DOCUMENT ME! * * @throws SAXException DOCUMENT ME! */ public void characters(char[] ch, int start, int end) throws SAXException { content += new String(ch, start, end); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -