upstream version 1.2.2
[debian/freetts] / demo / JSAPI / Player / Playable.java
1 /**
2  * Copyright 2001 Sun Microsystems, Inc.
3  * 
4  * See the file "license.terms" for information on usage and
5  * redistribution of this file, and for a DISCLAIMER OF ALL 
6  * WARRANTIES.
7  */
8 import java.io.File;
9
10 /**
11  * An object that can be played by the Player. Contains type information,
12  * and stores actual object that will be played.
13  */
14 public class Playable {
15
16     private PlayableType type;
17     private Object data = null;
18     private String name;
19     
20     /**
21      * Creates a JSML text Playable object with the given text.
22      *
23      * @param jsmlText the JSML text of the Playable object
24      *
25      * @return a JSML text Playable object 
26      */
27     public static Playable createJSMLPlayable(String jsmlText) {
28         return new Playable(PlayableType.JSML, jsmlText, jsmlText);
29     }
30
31     /**
32      * Creates a JSML file Playable object with the given File.
33      *
34      * @param jsmlFile the JSML file
35      *
36      * @return a JSML file Playable object
37      */
38     public static Playable createJSMLFilePlayable(File jsmlFile) {
39         return new Playable
40             (PlayableType.JSML_FILE, jsmlFile, jsmlFile.getName());
41     }
42     
43     /**
44      * Creates an ASCII text Playable object with the given text.
45      *
46      * @param text the ASCII text
47      *
48      * @return an ASCII text JSML file Playable object
49      */
50     public static Playable createTextPlayable(String text) {
51         return new Playable(PlayableType.TEXT, text, text);
52     }
53
54     /**
55      * Creates an ASCII file Playable object with the given text file.
56      *
57      * @param textFile the ASCII text file
58      *
59      * @return an ASCII text file Playable object
60      */
61     public static Playable createTextFilePlayable(File textFile) {
62         return new Playable
63             (PlayableType.TEXT_FILE, textFile, textFile.getName());
64     }
65
66     /**
67      * Creates a URL Playable object with the given URL.
68      *
69      * @param url the URL
70      *
71      * @return a URL Playable object
72      */
73     public static Playable createURLPlayable(String url) {
74         return new Playable(PlayableType.URL, url, url.toString());
75     }
76     
77     
78     /**
79      * Constructs a Playable object of the given type and data.
80      *
81      * @param type the Playable type
82      * @param data the object containing the Playable data
83      */
84     private Playable(PlayableType type, Object data, String name) {
85         this.type = type;
86         this.data = data;
87         this.name = name;
88     }
89
90     /**
91      * Returns the Playable type.
92      *
93      * @return the PlayableType
94      */
95     public PlayableType getType() {
96         return type;
97     }
98     
99     /**
100      * Returns the File corresponding to this Playable.
101      * 
102      * @return the Playable File
103      */
104     public File getFile() {
105         if (type == PlayableType.TEXT_FILE ||
106             type == PlayableType.JSML_FILE) {
107             return (File) data;
108         } else {
109             return null;
110         }
111     }
112
113     /**
114      * Returns the text corresponding to this Playable.
115      *
116      * @return the Playable text
117      */
118     public String getText() {
119         if (type == PlayableType.JSML || type == PlayableType.TEXT) {
120             return (String) data;
121         } else {
122             return null;
123         }
124     }
125
126     /**
127      * Returns the name of this Playable
128      *
129      * @return the name of this Playable
130      */
131     public String getName() {
132         return name;
133     }
134
135     /**
136      * Returns a String describing the type and name of this Playable, e.g.,
137      * <p><code>[JSML file] example1.jsml</code>
138      *
139      * @return the type and name of this Playable
140      */
141     public String toString() {
142         String typeName = "[" + type.toString() + "] ";
143         return typeName + name;
144     }
145     
146 }