📄 voicemanager.java
字号:
* Gets the list of voice jarfiles in a specific directory. * * @return a vector of URLs refering to the voice jarfiles * @see getVoiceJarURLs() */ private UniqueVector getVoiceJarURLsFromDir(String dirName) throws FileNotFoundException { try { UniqueVector voiceJarURLs = new UniqueVector(); File dir = new File(new URI("file://" + dirName)); if (!dir.isDirectory()) { throw new FileNotFoundException("File is not a directory: " + dirName); } File[] files = dir.listFiles(); for (int i = 0; i < files.length; i++) { //System.out.println("TEST: checking url " + files[i].getName()); if (files[i].isFile() && (!files[i].isHidden()) && files[i].getName().endsWith(".jar")) { URL jarURL = files[i].toURL(); jarURL = new URL("jar", "", "file:"+ jarURL.getPath() + "!/"); //System.out.println("TEST: reading url " + jarURL); JarURLConnection jarConnection = (JarURLConnection) jarURL.openConnection(); // if it is not a real jar file, we will end up // with a null set of attributes. Attributes attributes = jarConnection.getMainAttributes(); if (attributes != null) { String isVoice = attributes.getValue("FreeTTSVoiceDefinition"); if (isVoice != null && isVoice.trim().equals("true")) { voiceJarURLs.add(jarURL); } } } } return voiceJarURLs; } catch (java.net.URISyntaxException e) { throw new Error("Error reading directory name '" + dirName + "'."); } catch (MalformedURLException e) { throw new Error("Error reading jars from directory " + dirName + ". "); } catch (IOException e) { throw new Error("Error reading jars from directory " + dirName + ". "); } } /** * Provides a string representation of all voices available to * FreeTTS. * * @return a String which is a space-delimited list of voice * names. If there is more than one voice, then the word "or" * appears before the last one. */ public String toString() { String names = ""; Voice[] voices = getVoices(); for (int i = 0; i < voices.length; i++) { if (i == voices.length - 1) { if (i == 0) { names = voices[i].getName(); } else { names += "or " + voices[i].getName(); } } else { names += voices[i].getName() + " "; } } return names; } /** * Check if there is a voice provides with the given name. * * @param voiceName the name of the voice to check * * @return <b>true</b> if FreeTTS has a voice available with the * name <b>voiceName</b>, else <b>false</b>. */ public boolean contains(String voiceName) { return (getVoice(voiceName) != null); } /** * Get a Voice with a given name. * * @param voiceName the name of the voice to get. * * @return the Voice that has the same name as <b>voiceName</b> * if one exists, else <b>null</b> */ public Voice getVoice(String voiceName) { Voice[] voices = getVoices(); for (int i = 0; i < voices.length; i++) { if (voices[i].getName().equals(voiceName)) { return voices[i]; } } return null; } /** * Get the directory that the jar file containing this class * resides in. * * @return the name of the directory with a trailing "/" (or * equivalent for the particular operating system), or "" if * unable to determin. (For example this class does not reside * inside a jar file). */ private String getBaseDirectory() { String name = this.getClass().getName(); int lastdot = name.lastIndexOf('.'); if (lastdot != -1) { // remove package information name = name.substring(lastdot+1); } URL url = this.getClass().getResource(name + ".class"); return getURLDirName(url); } /** * Gets the directory name from a URL * * @param url the url to parse * @return the String representation of the directory name in a * URL */ private String getURLDirName(URL url) { String urlFileName = url.getPath(); int i = urlFileName.lastIndexOf('!'); if (i == -1) { i = urlFileName.length(); } int dir = urlFileName.lastIndexOf("/", i); if (!urlFileName.startsWith("file:")) { return ""; } return urlFileName.substring(5,dir) + "/"; } /** * Get the names of the voice directories from a voices file. * Blank lines and lines beginning with "#" are ignored. * Beginning and trailing whitespace is ignored. * * @param fileName the name of the voices file to read from * * @return a vector of the names of the VoiceDirectory subclasses * @throws FileNotFoundException * @throws IOException */ private UniqueVector getVoiceDirectoryNamesFromFile(String fileName) throws FileNotFoundException, IOException { InputStream is = new FileInputStream(fileName); if (is == null) { throw new IOException(); } else { return getVoiceDirectoryNamesFromInputStream(is); } } /** * Get the names of the voice directories from an input stream. * Blank lines and lines beginning with "#" are ignored. * Beginning and trailing whitespace is ignored. * * @param is the input stream to read from * * @return a vector of the names of the VoiceDirectory subclasses * @throws IOException */ private UniqueVector getVoiceDirectoryNamesFromInputStream(InputStream is) throws IOException { UniqueVector names = new UniqueVector(); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); while (true) { String line = reader.readLine(); if (line == null) { break; } line = line.trim(); if (!line.startsWith("#") && !line.equals("")) { //System.out.println("TEST Adding vd " + line); names.add(line); } } return names; } /** * Gets the class loader used for loading dynamically detected * jars. This is useful to get resources out of jars that may be * in the class path of this class loader but not in the class * path of the system class loader. * * @return the class loader */ public static URLClassLoader getVoiceClassLoader() { return classLoader; }}/** * The DynamicClassLoader provides a means to add urls to the * classpath after the class loader has already been instantiated. */class DynamicClassLoader extends URLClassLoader { private java.util.HashSet classPath; /** * Constructs a new URLClassLoader for the specified URLs using * the default delegation parent ClassLoader. The URLs will be * searched in the order specified for classes and resources * after first searching in the parent class loader. Any URL * that ends with a '/' is assumed to refer to a directory. * Otherwise, the URL is assumed to refer to a JAR file which * will be downloaded and opened as needed. * * If there is a security manager, this method first calls the * security manager's checkCreateClassLoader method to ensure * creation of a class loader is allowed. * * @param urls the URLs from which to load classes and resources * * @throws SecurityException if a security manager exists and * its checkCreateClassLoader method doesn't allow creation of a * class loader. */ public DynamicClassLoader(URL[] urls) { super(urls); classPath = new java.util.HashSet(urls.length); for (int i = 0; i < urls.length; i++) { classPath.add(urls[i]); } } /** * Constructs a new URLClassLoader for the given URLs. The * URLs will be searched in the order specified for classes * and resources after first searching in the specified parent * class loader. Any URL that ends with a '/' is assumed to refer * to a directory. Otherwise, the URL is assumed to refer to a * JAR file which will be downloaded and opened as needed. * * If there is a security manager, this method first calls the * security manager's checkCreateClassLoader method to ensure * creation of a class loader is allowed. * * @param urls the URLs from which to load classes and resources * @param parent the parent class loader for delegation * * @throws SecurityException if a security manager exists and * its checkCreateClassLoader method doesn't allow creation of a * class loader. */ public DynamicClassLoader(URL[] urls, ClassLoader parent) { super(urls, parent); classPath = new java.util.HashSet(urls.length); for (int i = 0; i < urls.length; i++) { classPath.add(urls[i]); } } /** * Constructs a new URLClassLoader for the specified URLs, parent * class loader, and URLStreamHandlerFactory. The parent argument * will be used as the parent class loader for delegation. The * factory argument will be used as the stream handler factory to * obtain protocol handlers when creating new URLs. * * If there is a security manager, this method first calls the * security manager's checkCreateClassLoader method to ensure * creation of a class loader is allowed. * * @param urls the URLs from which to load classes and resources * @param parent the parent class loader for delegation * @param factory the URLStreamHandlerFactory to use when creating URLs * * @throws SecurityException if a security manager exists and * its checkCreateClassLoader method doesn't allow creation of a * class loader. */ public DynamicClassLoader(URL[] urls, ClassLoader parent, URLStreamHandlerFactory factory) { super(urls, parent, factory); classPath = new java.util.HashSet(urls.length); for (int i = 0; i < urls.length; i++) { classPath.add(urls[i]); } } /** * Add a URL to a class path only if has not already been added. * * @param url the url to add to the class path */ public synchronized void addUniqueURL(URL url) { if (!classPath.contains(url)) { super.addURL(url); classPath.add(url); } }}/** * Provides a vector whose elements are always unique. The * advantage over a Set is that the elements are still ordered * in the way they were added. If an element is added that * already exists, then nothing happens. */class UniqueVector { private java.util.HashSet elementSet; private java.util.Vector elementVector; /** * Creates a new vector */ public UniqueVector() { elementSet = new java.util.HashSet(); elementVector = new java.util.Vector(); } /** * Add an object o to the vector if it is not already present as * defined by the function HashSet.contains(o) * * @param o the object to add */ public void add(Object o) { if (!contains(o)) { elementSet.add(o); elementVector.add(o); } } /** * Appends all elements of a vector to this vector. * Only unique elements are added. * * @param v the vector to add */ public void addVector(UniqueVector v) { for (int i = 0; i < v.size(); i++) { add(v.get(i)); } } /** * Appends all elements of an array to this vector. * Only unique elements are added. * * @param a the array to add */ public void addArray(Object[] a) { for (int i = 0; i < a.length; i++) { add(a[i]); } } /** * Gets the number of elements currently in vector. * * @return the number of elements in vector */ public int size() { return elementVector.size(); } /** * Checks if an element is present in the vector. The check * follows the convention of HashSet contains() function, so * performance can be expected to be a constant factor. * * @param o the object to check * * @return true if element o exists in the vector, else false. */ public boolean contains(Object o) { return elementSet.contains(o); } /** * Gets an element from a vector. * * @param index the index into the vector from which to retrieve * the element * * @return the object at index <b>index</b> */ public Object get(int index) { return elementVector.get(index); } /** * Creates an array of the elements in the vector. Follows * conventions of Vector.toArray(). * * @return an array representation of the object */ public Object[] toArray() { return elementVector.toArray(); } /** * Creates an array of the elements in the vector. Follows * conventions of Vector.toArray(Object[]). * * @return an array representation of the object */ public Object[] toArray(Object[] a) { return elementVector.toArray(a); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -