📄 eaf23encoder.java
字号:
/* * File: EAF23Encoder.java * Project: MPI Linguistic Application * Date: 02 May 2007 * * Copyright (C) 2001-2007 Max Planck Institute for Psycholinguistics * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package mpi.eudico.server.corpora.clomimpl.dobes;import mpi.eudico.server.corpora.clom.Annotation;import mpi.eudico.server.corpora.clom.AnnotationDocEncoder;import mpi.eudico.server.corpora.clom.EncoderInfo;import mpi.eudico.server.corpora.clom.Tier;import mpi.eudico.server.corpora.clom.TimeOrder;import mpi.eudico.server.corpora.clom.TimeSlot;import mpi.eudico.server.corpora.clom.Transcription;import mpi.eudico.server.corpora.clomimpl.abstr.AlignableAnnotation;import mpi.eudico.server.corpora.clomimpl.abstr.LinkedFileDescriptor;import mpi.eudico.server.corpora.clomimpl.abstr.MediaDescriptor;import mpi.eudico.server.corpora.clomimpl.abstr.RefAnnotation;import mpi.eudico.server.corpora.clomimpl.abstr.SVGAlignableAnnotation;import mpi.eudico.server.corpora.clomimpl.abstr.TierImpl;import mpi.eudico.server.corpora.clomimpl.abstr.TranscriptionImpl;import mpi.eudico.server.corpora.clomimpl.type.Constraint;import mpi.eudico.server.corpora.clomimpl.type.LinguisticType;import mpi.eudico.server.util.IoUtil;import mpi.util.CVEntry;import mpi.util.ControlledVocabulary;import org.w3c.dom.Element;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Enumeration;import java.util.Hashtable;import java.util.Iterator;import java.util.Locale;import java.util.Vector;import java.util.logging.Logger;import javax.swing.JOptionPane;/** * Encodes a Transcription to EAF 2.3 format and saves it. * * @version Aug 2005 Identity removed * @version Feb 2006 Constraint Included In added as well as LinkedFileDescriptors */public class EAF23Encoder implements AnnotationDocEncoder { /** the version string of the format / dtd */ public static final String VERSION = "2.3"; /** Holds value of property DOCUMENT ME! */ private static final Logger LOG = Logger.getLogger(EAF23Encoder.class.getName()); /** Holds value of property DOCUMENT ME! */ public static boolean debug = false; /** * Creates a DOM and saves. * * @param theTranscription the Transcription to store * @param encoderInfo additional information for encoding * @param tierOrder preferred tier ordering; should be removed * @param path the output path */ public void encodeAndSave(Transcription theTranscription, EncoderInfo encoderInfo, Vector tierOrder, String path) { Element documentElement = createDOM(theTranscription, tierOrder); save(documentElement, path); } /** * Saves a template eaf of the Transcription; everything is saved except for * the annotations * * @param theTranscription the Transcription to store * @param tierOrder preferred tier ordering; should be removed * @param path the output path */ public void encodeAsTemplateAndSave(Transcription theTranscription, Vector tierOrder, String path) { Element documentElement = createTemplateDOM(theTranscription, tierOrder); save(documentElement, path); } /** * Create the DOM tree and returns the document element. * * @param theTranscription the Transcription to save (not null) * @param tierOrder the preferred ordering of the tiers * * @return the document element */ public static Element createDOM(Transcription theTranscription, Vector tierOrder) { long beginTime = System.currentTimeMillis(); if (debug) { System.out.println("Encoder creating DOM..."); } Hashtable tierElements = new Hashtable(); // for temporary storage of created tier Elements Hashtable timeSlotIds = new Hashtable(); // for temporary storage of generated tsIds Hashtable annotationIds = new Hashtable(); // for temporary storage of generated annIds //Hashtable svgIds = new Hashtable(); // for temporary storage of generated svg ids. Vector usedLocales = new Vector(); // for storage of used locales TranscriptionImpl attisTr = (TranscriptionImpl) theTranscription; if (attisTr == null) { LOG.warning( "[[ASSERTION FAILED]] ACM23TranscriptionStore/storeTranscription: theTranscription is null"); } /* the media object is now deprecated; MediaDescriptors are used instead if (attisTr.getMediaObject() == null) { System.out.println( "[[ASSERTION FAILED]] ACM22TranscriptionStore/storeTranscription: theTranscription.getMediaObject() is null"); } */ EAF23 eafFactory = null; try { eafFactory = new EAF23(); } catch (Exception ex) { ex.printStackTrace(); } // ANNOTATION_DOCUMENT SimpleDateFormat dateFmt = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); String dateString = dateFmt.format(Calendar.getInstance().getTime()); dateString = correctDate(dateString); String author = attisTr.getAuthor(); if (author == null) { author = "unspecified"; } Element annotDocument = eafFactory.newAnnotationDocument(dateString, author, VERSION); eafFactory.appendChild(annotDocument); // HEADER Element header = eafFactory.newHeader(""); // mediaFile maintained for compat with 1.4.1 annotDocument.appendChild(header); Iterator mdIter = attisTr.getMediaDescriptors().iterator(); while (mdIter.hasNext()) { MediaDescriptor md = (MediaDescriptor) mdIter.next(); String origin = null; if (md.timeOrigin != 0) { origin = String.valueOf(md.timeOrigin); } String extrFrom = null; if ((md.extractedFrom != null) && (md.extractedFrom != "")) { extrFrom = md.extractedFrom; } Element mdElement = eafFactory.newMediaDescriptor(md.mediaURL, md.mimeType, origin, extrFrom); header.appendChild(mdElement); } Iterator lfdIt = attisTr.getLinkedFileDescriptors().iterator(); LinkedFileDescriptor lfd; while (lfdIt.hasNext()) { lfd = (LinkedFileDescriptor) lfdIt.next(); String origin = null; if (lfd.timeOrigin != 0) { origin = String.valueOf(lfd.timeOrigin); } Element lfdElement = eafFactory.newLinkedFileDescriptor(lfd.linkURL, lfd.mimeType, origin, lfd.associatedWith); header.appendChild(lfdElement); } if (debug) { System.out.println("Header creation took: " + (System.currentTimeMillis() - beginTime) + " ms"); beginTime = System.currentTimeMillis(); } // TIME_ORDER TimeOrder timeOrder = attisTr.getTimeOrder(); // HB, July 19, 2001: cleanup unused TimeSlots first timeOrder.pruneTimeSlots(); Element timeOrderElement = eafFactory.newTimeOrder(); annotDocument.appendChild(timeOrderElement); int index = 1; Enumeration tsElements = timeOrder.elements(); while (tsElements.hasMoreElements()) { TimeSlot ts = (TimeSlot) tsElements.nextElement(); Element tsElement = null; String tsId = "ts" + index; // store ts with it's id temporarily timeSlotIds.put(ts, tsId); if (ts.getTime() != TimeSlot.TIME_UNALIGNED) { tsElement = eafFactory.newTimeSlot(tsId, ts.getTime()); } else { tsElement = eafFactory.newTimeSlot(tsId); } timeOrderElement.appendChild(tsElement); index++; } if (debug) { System.out.println("TimeSlots creation took: " + (System.currentTimeMillis() - beginTime) + " ms"); beginTime = System.currentTimeMillis(); } // 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 svgIndex = 1; // used to create svg id values //int annIndex = 0; int annIndex = 1; 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++; } /* while (annotIter.hasNext()) { Annotation ann = (Annotation) annotIter.next(); //annotation has already an id if ((ann.getId() != null) && !ann.getId().equals("")) { annotationIds.put(ann, ann.getId()); } else { //create an id that isn't yet in the transcription do { annIndex++; } while (attisTr.getAnnotation("a" + annIndex) != null); annotationIds.put(ann, "a" + annIndex); } } */ } // ANNOTATIONS // second pass. Actually creates and adds Annotation Elements Iterator tierIter2 = storeOrder.iterator(); while (tierIter2.hasNext()) { TierImpl t = (TierImpl) tierIter2.next(); Vector annotations = t.getAnnotations(); Iterator annotIter2 = annotations.iterator(); while (annotIter2.hasNext()) { Annotation ann = (Annotation) annotIter2.next(); Element annElement = eafFactory.newAnnotation(); ((Element) tierElements.get(t.getName())).appendChild(annElement); Element annSubElement = null; String annId = (String) annotationIds.get(ann); if (ann instanceof AlignableAnnotation) { String beginTsId = (String) timeSlotIds.get(((AlignableAnnotation) ann).getBegin()); String endTsId = (String) timeSlotIds.get(((AlignableAnnotation) ann).getEnd()); if (ann instanceof SVGAlignableAnnotation) { if (((SVGAlignableAnnotation) ann).getShape() != null) { String svgId = "ga" + svgIndex; ((SVGAlignableAnnotation) ann).setSVGElementID(svgId); svgIndex++; //svgIds.put(ann, svgId); annSubElement = eafFactory.newAlignableAnnotation(annId, beginTsId, endTsId, svgId); } else { ((SVGAlignableAnnotation) ann).setSVGElementID(null); annSubElement = eafFactory.newAlignableAnnotation(annId, beginTsId, endTsId, null); } } else { annSubElement = eafFactory.newAlignableAnnotation(annId, beginTsId, endTsId, null); } } else if (ann instanceof RefAnnotation) { String refId = null; String prevId = null; Vector refs = ((RefAnnotation) ann).getReferences(); RefAnnotation prev = ((RefAnnotation) ann).getPrevious(); // for the moment, take the first, if it exists if (refs.size() > 0) { refId = (String) annotationIds.get((Annotation) refs.firstElement()); } if (prev != null) { prevId = (String) annotationIds.get(prev); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -