upstream version 1.2.2
[debian/freetts] / com / sun / speech / freetts / FreeTTSSpeakableImpl.java
1 /**
2  * Portions Copyright 2001 Sun Microsystems, Inc.
3  * Portions Copyright 1999-2001 Language Technologies Institute, 
4  * Carnegie Mellon University.
5  * All Rights Reserved.  Use is subject to license terms.
6  * 
7  * See the file "license.terms" for information on usage and
8  * redistribution of this file, and for a DISCLAIMER OF ALL 
9  * WARRANTIES.
10  */
11 package com.sun.speech.freetts;
12
13 import org.w3c.dom.Document;
14 import java.io.InputStream;
15
16 /**
17  * Minimal implementation of a FreeTTSSpeakable
18  */
19 public class FreeTTSSpeakableImpl implements FreeTTSSpeakable {
20     private Document doc;
21     private String text;
22     private InputStream inputStream;
23     volatile boolean completed = false;
24     volatile boolean cancelled = false;
25
26     /**
27      * Constructor.
28      *
29      * @param text the text to be spoken
30      */
31     public FreeTTSSpeakableImpl(String text) {
32         this.text = text;
33     }
34
35     /**
36      * Constructor.
37      *
38      * @param doc the doc to be spoken
39      */
40     public FreeTTSSpeakableImpl(Document doc) {
41         this.doc = doc;
42     }
43
44     /**
45      * Constructor.
46      *
47      * @param is the doc to be spoken
48      */
49     public FreeTTSSpeakableImpl(InputStream is) {
50         this.inputStream = is;
51     }
52
53     /**
54      * Indicate that this speakable has been started.
55      */
56     public void started() {
57     }
58
59     /**
60      * Indicates that this speakable has been completed.
61      */
62     public synchronized void completed() {
63         completed = true;
64         notifyAll();
65     }
66
67     /**
68      * Indicates that this speakable has been cancelled.
69      */
70     public synchronized void cancelled() {
71         completed = true;
72         cancelled = true;
73         notifyAll();
74     }
75
76     /**
77      * Returns true if this queue item has been 
78      * processed.
79      *
80      * @return true if it has been processed
81      */
82     public synchronized boolean isCompleted() {
83         return completed;
84     }
85
86     /**
87      * Waits for this speakable item to be completed.
88      *
89      * @return true if the item was completed successfully, false if
90      *   the speakable  was cancelled or an error occurred.
91      */
92     public synchronized boolean waitCompleted() {
93         while  (!completed) {
94             try {
95                 wait();
96             } catch (InterruptedException ie) {
97                 System.err.println( "FreeTTSSpeakableImpl:Wait interrupted");
98                 return false;
99             }
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 }