📄 eaf22encoder.java
字号:
Vector lTypes = attisTr.getLinguisticTypes(); if (lTypes != null) { Iterator typeIter = lTypes.iterator(); while (typeIter.hasNext()) { // HB, april 24, 2002: for the moment, just store lt name LinguisticType lt = (LinguisticType) typeIter.next(); String stereotype = null; if (lt.hasConstraints()) { stereotype = Constraint.stereoTypes[lt.getConstraints() .getStereoType()]; stereotype = stereotype.replace(' ', '_'); } Element typeElement = eafFactory.newLinguisticType(lt.getLinguisticTypeName(), lt.isTimeAlignable(), lt.hasGraphicReferences(), stereotype, lt.getControlledVocabylaryName()); annotDocument.appendChild(typeElement); } } // LOCALES Iterator locIter = usedLocales.iterator(); while (locIter.hasNext()) { Locale l = (Locale) locIter.next(); Element locElement = eafFactory.newLocale(l); annotDocument.appendChild(locElement); } // HB, 18 jul 02: for the moment manually add relevant Constraints // CONSTRAINTS Element timeSubdivision = eafFactory.newConstraint(Constraint.stereoTypes[Constraint.TIME_SUBDIVISION].replace( ' ', '_'), "Time subdivision of parent annotation's time interval, no time gaps allowed within this interval"); Element symbSubdivision = eafFactory.newConstraint(Constraint.stereoTypes[Constraint.SYMBOLIC_SUBDIVISION].replace( ' ', '_'), "Symbolic subdivision of a parent annotation. Annotations refering to the same parent are ordered"); Element symbAssociation = eafFactory.newConstraint(Constraint.stereoTypes[Constraint.SYMBOLIC_ASSOCIATION].replace( ' ', '_'), "1-1 association with a parent annotation"); annotDocument.appendChild(timeSubdivision); annotDocument.appendChild(symbSubdivision); annotDocument.appendChild(symbAssociation); //CONTROLLED VOCABULARIES Vector conVocs = attisTr.getControlledVocabularies(); if (conVocs.size() > 0) { ControlledVocabulary cv; CVEntry entry; Element cvElement; Element entryElement; for (int i = 0; i < conVocs.size(); i++) { cv = (ControlledVocabulary) conVocs.get(i); cvElement = eafFactory.newControlledVocabulary(cv.getName(), cv.getDescription()); CVEntry[] entries = cv.getEntries(); for (int j = 0; j < entries.length; j++) { entry = entries[j]; entryElement = eafFactory.newCVEntry(entry.getValue(), entry.getDescription()); cvElement.appendChild(entryElement); } annotDocument.appendChild(cvElement); } } return eafFactory.getDocumentElement(); } /** * Create the DOM tree containing only elements that need to be stored in the * template; i.e. everything except timeorder, time slots, annotations and media * descriptors. * * @param theTranscription the Transcription to save (not null) * @param tierOrder the preferred ordering of the tiers * * @return the document element */ public static Element createTemplateDOM(Transcription theTranscription, Vector tierOrder) { Hashtable tierElements = new Hashtable(); // for temporary storage of created tier Elements //Hashtable annotationIds = new Hashtable(); // for temporary storage of generated annIds Vector usedLocales = new Vector(); // for storage of used locales TranscriptionImpl attisTr = (TranscriptionImpl) theTranscription; if (attisTr == null) { LOG.warning( "[[ASSERTION FAILED]] ACM22TranscriptionStore/storeTranscription: theTranscription is null"); } /* the media object is deprecated if (attisTr.getMediaObject() == null) { System.out.println( "[[ASSERTION FAILED]] ACM22TranscriptionStore/storeTranscription: theTranscription.getMediaObject() is null"); } */ EAF22 eafFactory = null; try { eafFactory = new EAF22(); } catch (Exception ex) { ex.printStackTrace(); } // ANNOTATION_DOCUMENT SimpleDateFormat dateFmt = new SimpleDateFormat("yyyy.MM.dd HH:mm z"); String dateString = dateFmt.format(Calendar.getInstance().getTime()); String author = attisTr.getAuthor(); //always set author to empty in template author = ""; Element annotDocument = eafFactory.newAnnotationDocument(dateString, author, VERSION); eafFactory.appendChild(annotDocument); // HEADER //always set header to empty in template //Element header = eafFactory.newHeader(attisTr.getMediaObject().getMediaURL().toString()); Element header = eafFactory.newHeader(""); annotDocument.appendChild(header); // TIERS Vector tiers = attisTr.getTiers(); Vector storeOrder = new Vector(tierOrder); // start with tiers in specified order Iterator tIter = tiers.iterator(); while (tIter.hasNext()) { // add other tiers in document order Tier t = (Tier) tIter.next(); if (!storeOrder.contains(t)) { storeOrder.add(t); } } int annIndex = 1; // used to create annotation id values Iterator tierIter = storeOrder.iterator(); while (tierIter.hasNext()) { TierImpl t = (TierImpl) tierIter.next(); String id = t.getName(); String participant = (String) t.getMetadataValue("PARTICIPANT"); String lingType = t.getLinguisticType().getLinguisticTypeName(); if (lingType == null) { lingType = "not specified"; } Locale lang = (Locale) t.getMetadataValue("DEFAULT_LOCALE"); if (lang == null) { lang = new Locale("not specified", "", ""); } // check is quick solution, TreeSet would do this but compareTo causes ClassCastException if (!usedLocales.contains(lang)) { usedLocales.add(lang); } String parentName = null; if (t.getParentTier() != null) { parentName = t.getParentTier().getName(); } Element tierElement = eafFactory.newTier(id, participant, lingType, lang, parentName); annotDocument.appendChild(tierElement); tierElements.put(t.getName(), tierElement); // store for later use /* Vector annotations = t.getAnnotations(); Iterator annotIter = annotations.iterator(); while (annotIter.hasNext()) { Annotation ann = (Annotation) annotIter.next(); annotationIds.put(ann, "a" + annIndex); annIndex++; } */ } // LINGUISTIC_TYPES Vector lTypes = attisTr.getLinguisticTypes(); if (lTypes != null) { Iterator typeIter = lTypes.iterator(); while (typeIter.hasNext()) { // HB, april 24, 2002: for the moment, just store lt name LinguisticType lt = (LinguisticType) typeIter.next(); String stereotype = null; if (lt.hasConstraints()) { stereotype = Constraint.stereoTypes[lt.getConstraints() .getStereoType()]; stereotype = stereotype.replace(' ', '_'); } Element typeElement = eafFactory.newLinguisticType(lt.getLinguisticTypeName(), lt.isTimeAlignable(), lt.hasGraphicReferences(), stereotype, lt.getControlledVocabylaryName()); annotDocument.appendChild(typeElement); } } // LOCALES Iterator locIter = usedLocales.iterator(); while (locIter.hasNext()) { Locale l = (Locale) locIter.next(); Element locElement = eafFactory.newLocale(l); annotDocument.appendChild(locElement); } // HB, 18 jul 02: for the moment manually add relevant Constraints // CONSTRAINTS Element timeSubdivision = eafFactory.newConstraint(Constraint.stereoTypes[Constraint.TIME_SUBDIVISION].replace( ' ', '_'), "Time subdivision of parent annotation's time interval, no time gaps allowed within this interval"); Element symbSubdivision = eafFactory.newConstraint(Constraint.stereoTypes[Constraint.SYMBOLIC_SUBDIVISION].replace( ' ', '_'), "Symbolic subdivision of a parent annotation. Annotations refering to the same parent are ordered"); Element symbAssociation = eafFactory.newConstraint(Constraint.stereoTypes[Constraint.SYMBOLIC_ASSOCIATION].replace( ' ', '_'), "1-1 association with a parent annotation"); annotDocument.appendChild(timeSubdivision); annotDocument.appendChild(symbSubdivision); annotDocument.appendChild(symbAssociation); // CONTROLLED VOCABULARIES Vector conVocs = attisTr.getControlledVocabularies(); if (conVocs.size() > 0) { ControlledVocabulary cv; CVEntry entry; Element cvElement; Element entryElement; for (int i = 0; i < conVocs.size(); i++) { cv = (ControlledVocabulary) conVocs.get(i); cvElement = eafFactory.newControlledVocabulary(cv.getName(), cv.getDescription()); CVEntry[] entries = cv.getEntries(); for (int j = 0; j < entries.length; j++) { entry = entries[j]; entryElement = eafFactory.newCVEntry(entry.getValue(), entry.getDescription()); cvElement.appendChild(entryElement); } annotDocument.appendChild(cvElement); } } return eafFactory.getDocumentElement(); } /** * Creates a validating date string. * * @param strIn the date string to correct * @return a validating date string */ private static String correctDate(String strIn) { String strResult = new String(strIn); try { int offsetGMT = Calendar.getInstance().getTimeZone().getRawOffset() / (60 * 60 * 1000); String strOffset = "+"; if (offsetGMT < 0) { strOffset = "-"; } offsetGMT = Math.abs(offsetGMT); if (offsetGMT < 10) { strOffset += "0"; } strOffset += (offsetGMT + ":00"); strResult += strOffset; int indexSpace = strResult.indexOf(" "); if (indexSpace != -1) { String strEnd = strResult.substring(indexSpace + 1); strResult = strResult.substring(0, indexSpace); strResult += "T"; strResult += strEnd; } strResult = strResult.replace('.', '-'); } catch (Exception ex) { return strIn; } return strResult; } private static void save(Element documentElement, String path) { LOG.info(path + " <----XML output\n"); try { // test for errors if (("" + documentElement).length() == 0) { String txt = "Sorry: unable to save this file (zero length)."; JOptionPane.showMessageDialog(null, txt, txt, JOptionPane.ERROR_MESSAGE); //bla return; } //IoUtil.writeEncodedFile("UTF-8", path, documentElement); IoUtil.writeEncodedEAFFile("UTF-8", path, documentElement); } catch (Exception eee) { String txt = "Sorry: unable to save this file. (" + eee.getMessage() + ")"; JOptionPane.showMessageDialog(null, txt, txt, JOptionPane.ERROR_MESSAGE); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -