001/**
002 * Portions Copyright 2001 Sun Microsystems, Inc.
003 * Portions Copyright 1999-2001 Language Technologies Institute, 
004 * Carnegie Mellon University.
005 * All Rights Reserved.  Use is subject to license terms.
006 * 
007 * See the file "license.terms" for information on usage and
008 * redistribution of this file, and for a DISCLAIMER OF ALL 
009 * WARRANTIES.
010 */
011package com.sun.speech.freetts;
012
013import org.w3c.dom.Document;
014import java.io.InputStream;
015
016/**
017 * Minimal implementation of a FreeTTSSpeakable
018 */
019public class FreeTTSSpeakableImpl implements FreeTTSSpeakable {
020    private Document doc;
021    private String text;
022    private InputStream inputStream;
023    volatile boolean completed = false;
024    volatile boolean cancelled = false;
025
026    /**
027     * Constructor.
028     *
029     * @param text the text to be spoken
030     */
031    public FreeTTSSpeakableImpl(String text) {
032        this.text = text;
033    }
034
035    /**
036     * Constructor.
037     *
038     * @param doc the doc to be spoken
039     */
040    public FreeTTSSpeakableImpl(Document doc) {
041        this.doc = doc;
042    }
043
044    /**
045     * Constructor.
046     *
047     * @param is the doc to be spoken
048     */
049    public FreeTTSSpeakableImpl(InputStream is) {
050        this.inputStream = is;
051    }
052
053    /**
054     * Indicate that this speakable has been started.
055     */
056    public void started() {
057    }
058
059    /**
060     * Indicates that this speakable has been completed.
061     */
062    public synchronized void completed() {
063        completed = true;
064        notifyAll();
065    }
066
067    /**
068     * Indicates that this speakable has been cancelled.
069     */
070    public synchronized void cancelled() {
071        completed = true;
072        cancelled = true;
073        notifyAll();
074    }
075
076    /**
077     * Returns true if this queue item has been 
078     * processed.
079     *
080     * @return true if it has been processed
081     */
082    public synchronized boolean isCompleted() {
083        return completed;
084    }
085
086    /**
087     * Waits for this speakable item to be completed.
088     *
089     * @return true if the item was completed successfully, false if
090     *   the speakable  was cancelled or an error occurred.
091     */
092    public synchronized boolean waitCompleted() {
093        while  (!completed) {
094            try {
095                wait();
096            } catch (InterruptedException ie) {
097                System.err.println( "FreeTTSSpeakableImpl:Wait interrupted");
098                return false;
099            }
100        }
101        return !cancelled;
102    }
103
104   /**
105    * Returns <code>true</code> if the item contains plain text
106    * (not Java Speech Markup Language text).
107    *
108    * @return true if the item contains plain text
109    */
110    public boolean isPlainText() {
111        return text != null;
112    }
113
114    /**
115     * Returns the text corresponding to this Playable.
116     *
117     * @return the Playable text
118     */
119    public String getText() {
120        return text;
121    }
122
123    /**
124     * Gets the DOM document for this object.
125     *
126     * @return the DOM document for this object.
127     */
128    public Document getDocument() {
129        return doc;
130    }
131
132   /**
133    * Returns <code>true</code> if the item is an input stream.
134    *
135    * @return true if the item is an input stream
136    */
137    public boolean isStream() {
138        return inputStream  != null;
139    }
140
141    /**
142     * Gets the input stream.
143     *
144     * @return the input stream
145     */
146    public InputStream getInputStream() {
147        return inputStream ;
148    }
149
150    /**
151     * Returns <code>true</code> if the item is a JSML document
152     * (Java Speech Markup Language).
153     *
154     * @return true if the item is a document
155     */
156    public boolean isDocument() {
157        return doc != null;
158    }
159}