/** 
 * Copyright (c) 2007-2008, Regents of the University of Colorado 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 
 * Neither the name of the University of Colorado at Boulder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE. 
 */
package org.cleartk.classifier;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.jar.JarFile;

/**
 * <br>
 * Copyright (c) 2007-2008, Regents of the University of Colorado <br>
 * All rights reserved.
 */

public class ClassifierFactory {

	/**
	 * Every classifier should be able to instantiated from a jar file. This
	 * factory method reads the class of the classifier from manifest of the jar
	 * file from the attribute "classifier". e.g.:
	 * <p>
	 * <code>classifier: org.cleartk.classifier.OpenNLPMaxentClassifier</code>
	 * </p>
	 * The value of the classifier attribute of the manifest is used to
	 * instantiate a classifier using reflection and the constructor that takes
	 * a jar file.
	 * 
	 * @param jarFileName
	 *            the name of a jar file
	 * @return a classifier defined by the contents of the jar file.
	 * @throws IOException
	 */
	public static Classifier<?> createClassifierFromJar(String jarFileName) throws IOException {
		return (Classifier<?>) _createClassifierFromJar(jarFileName);
	}

	public static SequentialClassifier<?> createSequentialClassifierFromJar(String jarFileName) throws IOException {
		return (SequentialClassifier<?>) _createClassifierFromJar(jarFileName);
	}

	private static Object _createClassifierFromJar(String jarFileName) throws IOException {
		// get the jar file manifest
		JarFile modelFile = new JarFile(jarFileName);
		ClassifierManifest manifest = new ClassifierManifest(modelFile);

		// get the classifier class
		ClassifierBuilder<?> builder = manifest.getClassifierBuilder();
		Class<?> classifierClass = builder.getClassifierClass();

		// create the classifier, passing in the jar file
		try {
			return classifierClass.getConstructor(JarFile.class).newInstance(modelFile);
		}
		catch (InstantiationException e) {
			throw newIOException(e);
		}
		catch (IllegalAccessException e) {
			throw newIOException(e);
		}
		catch (InvocationTargetException e) {
			throw newIOException(e);
		}
		catch (NoSuchMethodException e) {
			throw newIOException(e);
		}
		finally {
			modelFile.close();
		}
	}

	private static IOException newIOException(Throwable cause) {
		IOException exception = new IOException();
		exception.initCause(cause);
		return exception;
	}

}

@@ -1679,61 +1679,8 @@
 n;%0D%0A
-import java.lang.reflect.InvocationTargetException;%0D%0A
 impo
@@ -2607,25 +2607,8 @@
 urn 
-(Classifier%3C?%3E) _
 crea
@@ -2630,32 +2630,50 @@
 mJar(jarFileName
+, Classifier.class
 );%0D%0A%09%7D%0D%0A%0D%0A%09publi
@@ -2794,35 +2794,8 @@
 urn 
-(SequentialClassifier%3C?%3E) _
 crea
@@ -2821,24 +2821,52 @@
 (jarFileName
+, SequentialClassifier.class
 );%0D%0A%09%7D%0D%0A%0D%0A%09p
@@ -2883,16 +2883,14 @@
 tic 
-Object _
+%3CT%3E T 
 crea
@@ -2919,32 +2919,46 @@
 ring jarFileName
+, Class%3CT%3E cls
 ) throws IOExcep
@@ -3223,16 +3223,26 @@
 %09Class%3C?
+ extends T
 %3E classi
@@ -3281,16 +3281,32 @@
 rClass()
+.asSubclass(cls)
 ;%0D%0A%0D%0A%09%09/
@@ -3452,32 +3452,16 @@
 %0A%09%09%7D
-%0D%0A%09%09
+ 
 catch (
-Instantiation
 Exce
@@ -3471,33 +3471,24 @@
 on e) %7B%0D%0A%09%09%09
-throw new
 IOException(
@@ -3490,69 +3490,25 @@
 tion
-(e);%0D%0A%09%09%7D%0D%0A%09%09catch (IllegalAccessException e) %7B%0D%0A%09%09%09throw
+ exception =
  new
+ 
 IOEx
@@ -3519,128 +3519,38 @@
 ion(
-e
 );%0D%0A%09%09
-%7D%0D%0A%09%09catch (InvocationTargetException e) %7B%0D%0A%09%09%09throw newIOException(e);%0D%0A%09%09%7D%0D%0A%09%09catch (NoSuchMethodException e) %7B
+%09exception.initCause(e);
 %0D%0A%09%09
@@ -3556,30 +3556,25 @@
 %09%09throw 
-newIOE
+e
 xception
 (e);%0D%0A%09%09
@@ -3569,21 +3569,15 @@
 tion
-(e)
 ;%0D%0A%09%09%7D
-%0D%0A%09%09
+ 
 fina
@@ -3619,176 +3619,7 @@
 %09%7D%0D%0A
-%0D%0A%09private static IOException newIOException(Throwable cause) %7B%0D%0A%09%09IOException exception = new IOException();%0D%0A%09%09exception.initCause(cause);%0D%0A%09%09return exception;%0D%0A%09%7D%0D%0A%0D%0A
 %7D%0D%0A

Clean up casts in ClassifierFactory using .asSubclass().