public Object getData() { if (type.isXMLType()) { return xmlDocument; } else if (type.isTextType()) { return plainText; } else { // audio return audio; } }
/** * Read data from input stream <code>is</code>, in the appropriate way as determined by our <code> * type</code>. * * @param is the InputStream from which to read. * @param endMarker a string marking end of file. If this is null, read until end-of-file; if it * is non-null, read up to (and including) the first line containing the end marker string. * This will be ignored for audio data. * @throws ParserConfigurationException ParserConfigurationException * @throws SAXException SAXException * @throws IOException IOException * @throws TransformerConfigurationException TransformerConfigurationException * @throws TransformerException TransformerException */ public void readFrom(InputStream is, String endMarker) throws ParserConfigurationException, SAXException, IOException, TransformerConfigurationException, TransformerException { if (type.isXMLType() || type.isTextType()) readFrom(new InputStreamReader(is, "UTF-8"), endMarker); else { // audio // ignore endMarker setAudio((AudioInputStream) is); } }
/** * Write our internal representation to writer <code>w</code>, in the appropriate way as * determined by our <code>type</code>. Only XML and Text data can be written to a writer, audio * data cannot. "Helpers" needed to read the data, such as XML parser objects, are created when * they are needed. * * @param w w * @throws TransformerConfigurationException TransformerConfigurationException * @throws FileNotFoundException FileNotFoundException * @throws TransformerException TransformerException * @throws IOException IOException * @throws Exception Exception */ public void writeTo(Writer w) throws TransformerConfigurationException, FileNotFoundException, TransformerException, IOException, Exception { if (type.isXMLType()) { throw new IOException("Better write XML data to an OutputStream, not to a Writer"); } else if (type.isTextType()) { // caution: XML types are text types! w.write(plainText); w.flush(); logger.debug("Writing Text output:\n" + plainText); } else { // audio - cannot write this to a writer throw new Exception("Illegal attempt to write audio data to a character Writer"); } }
public void append(MaryData md) { if (md == null) throw new NullPointerException("Received null marydata"); if (!md.getType().equals(this.getType())) throw new IllegalArgumentException( "Cannot append mary data of type `" + md.getType().name() + "' to mary data of type `" + this.getType().name() + "'"); if (getType().isXMLType()) { NodeList kids = md.getDocument().getDocumentElement().getChildNodes(); logger.debug("Appending " + kids.getLength() + " nodes to MaryXML structure"); Element docEl = this.getDocument().getDocumentElement(); for (int i = 0; i < kids.getLength(); i++) { docEl.appendChild(this.getDocument().importNode(kids.item(i), true)); } } else if (getType().isTextType()) { // Attention: XML type is a text type! if (this.plainText == null) { this.plainText = md.getPlainText(); } else { this.plainText = this.plainText + "\n\n" + md.getPlainText(); } } else if (getType().equals(MaryDataType.get("AUDIO"))) { appendAudio(md.getAudio()); } else { throw new UnsupportedOperationException( "Cannot append two mary data items of type `" + getType() + "'"); } }
/** * Set the content data of this MaryData object from the given String. For XML data ({@link * MaryDataType#isXMLType()}), parse the String representation of the data into a DOM tree. * * @param dataString string representation of the input data. * @throws ParserConfigurationException ParserConfigurationException * @throws IOException IOException * @throws SAXException SAXException * @throws IllegalArgumentException if this method is called for MaryDataTypes that are neither * text nor XML. */ public void setData(String dataString) throws ParserConfigurationException, SAXException, IOException { // First, some data cleanup: dataString = StringUtils.purgeNonBreakingSpaces(dataString); // Now, deal with it. if (type.isXMLType()) { logger.debug( "Parsing XML input (" + (doValidate ? "" : "non-") + "validating): " + dataString); xmlDocument = DomUtils.parseDocument(dataString, doValidate); } else if (type.isTextType()) { logger.debug("Setting text input: " + dataString); plainText = dataString; } else { throw new IllegalArgumentException("Cannot set data of type " + type + " from a string"); } }
public MaryData(MaryDataType type, Locale locale, boolean createStubDocument) { if (type == null) throw new NullPointerException("Received null type for MaryData"); this.type = type; this.locale = locale; // The following is the default setting for module output (we suppose // that for the input data, setValidating() is called as appropriate): doValidate = MaryProperties.getBoolean("maryxml.validate.modules", false); if (createStubDocument && type.isMaryXML()) { xmlDocument = MaryXML.newDocument(); } }
/** * Write our internal representation to output stream <code>os</code>, in the appropriate way as * determined by our <code>type</code>. * * @param os os * @throws TransformerConfigurationException TransformerConfigurationException * @throws FileNotFoundException FileNotFoundException * @throws TransformerException TransformerException * @throws IOException IOException * @throws Exception Exception */ public void writeTo(OutputStream os) throws TransformerConfigurationException, FileNotFoundException, TransformerException, IOException, Exception { if (type.isXMLType()) { if (writer == null) writer = new MaryNormalisedWriter(); if (logger.getEffectiveLevel().equals(Level.DEBUG)) { ByteArrayOutputStream debugOut = new ByteArrayOutputStream(); writer.output(xmlDocument, debugOut); logger.debug(debugOut.toString()); } writer.output(xmlDocument, new BufferedOutputStream(os)); } else if (type.isTextType()) { // caution: XML types are text types! writeTo(new OutputStreamWriter(os, "UTF-8")); } else { // audio logger.debug("Writing audio output, frame length " + audio.getFrameLength()); AudioSystem.write(audio, audioFileFormat.getType(), os); os.flush(); os.close(); } }