📄 eaf21transcriptionstore.java
字号:
/* * File: EAF21TranscriptionStore.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.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.clom.TranscriptionStore;import mpi.eudico.server.corpora.clomimpl.abstr.AlignableAnnotation;import mpi.eudico.server.corpora.clomimpl.abstr.MediaDescriptor;import mpi.eudico.server.corpora.clomimpl.abstr.Parser;import mpi.eudico.server.corpora.clomimpl.abstr.ParserFactory;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.TimeSlotImpl;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.corpora.clomimpl.type.SymbolicAssociation;import mpi.eudico.server.corpora.clomimpl.type.SymbolicSubdivision;import mpi.eudico.server.corpora.clomimpl.type.TimeSubdivision;import mpi.eudico.server.util.IoUtil;import org.w3c.dom.Element;import java.io.File;import java.io.IOException;import java.io.PrintWriter;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Calendar;import java.util.Enumeration;import java.util.HashMap;import java.util.Hashtable;import java.util.Iterator;import java.util.Locale;import java.util.Vector;import javax.swing.JOptionPane;/** * DOCUMENT ME! $Id: EAF21TranscriptionStore.java,v 1.1.1.1 2004/03/25 16:23:20 * wouthuij Exp $ * * @author $Author: hasloe $ * @version $Revision: 1.17 $ * @version Aug 2005 Identity removed */public class EAF21TranscriptionStore implements TranscriptionStore { // we want the XML to be saved to a file // HS 19-11-2002: "private final" changed to "public"to enable automatic // backup (if fileToWirteXMLinto is not null, then the transcription will // be written to that file /** Holds value of property DOCUMENT ME! */ public java.io.File fileToWriteXMLinto = null; //currently final (not used) /** * Creates a new EAF21TranscriptionStore instance */ public EAF21TranscriptionStore() { super(); } // File lacks println: here we add it. // TO DO: put this somewhere in mpi.alt public PrintWriter addPrintln(File f) { try { java.io.FileWriter fileWriter1 = new java.io.FileWriter(f); return new PrintWriter(fileWriter1, true); } catch (IOException ioe) { // return null is all we say. } return null; } /** * DOCUMENT ME! * * @param theTranscription DOCUMENT ME! * @param tierOrder DOCUMENT ME! */ public void storeTranscription(Transcription theTranscription, EncoderInfo encoderInfo, Vector tierOrder, int format) { if (theTranscription instanceof TranscriptionImpl) { String pathName = ((TranscriptionImpl) theTranscription).getPathName(); if (!pathName.substring(pathName.length() - 4, pathName.length() - 3).equals(".")) { pathName += ".eaf"; } else { //always give it extension eaf pathName = pathName.substring(0, pathName.length() - 3); pathName = pathName + "eaf"; } storeTranscriptionIn(theTranscription, tierOrder, pathName); } } //M_P public void storeTranscription(Transcription theTranscription, EncoderInfo encoderInfo, Vector tierOrder, String pathName, int format) { if (theTranscription instanceof TranscriptionImpl) { //String pathName = ((DobesTranscription) theTranscription).getPathName(); if (!pathName.substring(pathName.length() - 4, pathName.length() - 3).equals(".")) { pathName += ".eaf"; } else { //always give it extension eaf pathName = pathName.substring(0, pathName.length() - 3); pathName = pathName + "eaf"; } storeTranscriptionIn(theTranscription, tierOrder, pathName); } } /** * Writes to original file if this.fileToWriteXMLinto is null * * @param theTranscription MUST NOT BE NULL * @param tierOrder DOCUMENT ME! * @param path DOCUMENT ME! */ public void storeTranscriptionIn(Transcription theTranscription, Vector tierOrder, String path) { // System.out.println("EAF21TranscriptionStore.storeTranscription called"); // System.out.println("theTranscription == " + theTranscription.getName()); Element documentElement = createDOM(theTranscription, tierOrder); if (this.fileToWriteXMLinto != null) { save(documentElement, fileToWriteXMLinto.getAbsolutePath(), true); } else { save(documentElement, path, false); } } private static void save(Element documentElement, String path, boolean backup) { System.out.println(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); //Don't show message for backup // if(!backup){ // JOptionPane.showMessageDialog(null, "Your file has been written to " + path); // } } catch (Exception eee) { String txt = "Sorry: unable to save this file. (" + eee.getMessage() + ")"; JOptionPane.showMessageDialog(null, txt, txt, JOptionPane.ERROR_MESSAGE); } } //make a string that will validate 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; } /** * DOCUMENT ME! * * @param theTranscription DOCUMENT ME! * @param tierOrder DOCUMENT ME! * * @return DOCUMENT ME! */ public static Element createDOM(Transcription theTranscription, Vector tierOrder) { 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) { System.out.println( "[[ASSERTION FAILED]] EAF21TranscriptionStore/storeTranscription: theTranscription is null"); } ; if (attisTr.getMediaObject() == null) { System.out.println( "[[ASSERTION FAILED]] EAF21TranscriptionStore/storeTranscription: theTranscription.getMediaObject() is null"); } ; EAF21 eafFactory = null; try { eafFactory = new EAF21(); } 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"; } String version = "2.1"; // version of DTD/format, not of file. That is handled by 'date' 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); } // 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++; } // 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 int svgIndex = 1; // used to create svg id values Iterator tierIter = storeOrder.iterator();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -