/**
 ################################################################################
 #                  Copyright 2012 The SCAPE Project Consortium
 #
 #   This software is copyrighted by the SCAPE Project Consortium. 
 #   The SCAPE project is co-funded by the European Union under
 #   FP7 ICT-2009.4.1 (Grant Agreement number 270137).
 #
 #   Licensed under the Apache License, Version 2.0 (the "License");
 #   you may not use this file except in compliance with the License.
 #   You may obtain a copy of the License at
 #
 #                   http://www.apache.org/licenses/LICENSE-2.0              
 #
 #   Unless required by applicable law or agreed to in writing, software
 #   distributed under the License is distributed on an "AS IS" BASIS,
 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   
 #   See the License for the specific language governing permissions and
 #   limitations under the License.
 ################################################################################
 */
package eu.scape_project.tool.toolwrapper.data.tool_spec.utils;

import java.io.File;
import java.io.IOException;

import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import org.apache.commons.io.FileUtils;
import org.xml.sax.SAXException;

import eu.scape_project.tool.toolwrapper.data.tool_spec.Tool;

/**
 * Utility class with static methods useful to interact with toolspec info
 * */

public final class Utils {
	private static final String TOOLSPEC_FILENAME_IN_RESOURCES = "/tool-1.1_draft.xsd";

	private Utils() {
	}

	/**
	 * Method that creates a {@link Tool} instance from the provided toolspec
	 * file, validating it against toolspec XML Schema
	 * 
	 * @param toolspecFilePath
	 *            path to the toolspec file
	 * @throws JAXBException
	 * @throws IOException
	 * @throws SAXException
	 */
	public static Tool createTool(String toolspecFilePath)
			throws JAXBException, IOException, SAXException {
		File schemaFile = null;

		JAXBContext context = JAXBContext.newInstance(Tool.class);
		Unmarshaller unmarshaller = context.createUnmarshaller();

		// copy XML Schema from resources to a temporary location
		schemaFile = File.createTempFile("schema", null);
		FileUtils
				.copyInputStreamToFile(Utils.class
						.getResourceAsStream(TOOLSPEC_FILENAME_IN_RESOURCES),
						schemaFile);
		Schema schema = SchemaFactory.newInstance(
				XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(schemaFile);

		// validate provided toolspec against XML Schema
		unmarshaller.setSchema(schema);

		// unmarshal it
		return (Tool) unmarshaller.unmarshal(new File(toolspecFilePath));

	}
}

@@ -1129,32 +1129,60 @@
 .io.IOException;
+%0Aimport java.io.InputStream;
 %0A%0Aimport javax.x
@@ -1233,16 +1233,51 @@
 ontext;%0A
+import javax.xml.bind.JAXBElement;%0A
 import j
@@ -1341,16 +1341,64 @@
 haller;%0A
+import javax.xml.transform.stream.StreamSource;%0A
 import j
@@ -1687,20 +1687,17 @@
  info%0A *
- */%0A
+/
 %0Apublic 
@@ -1802,16 +1802,358 @@
 .xsd%22;%0A%0A
+    private static JAXBContext jc;%0A    %0A    static %7B%0A        try %7B%0A            jc  = createContext();%0A        %7D catch (JAXBException e) %7B%0A            throw new ExceptionInInitializerError(e);%0A        %7D%0A    %7D%0A%0A    private static JAXBContext createContext() throws JAXBException %7B%0A        return JAXBContext.newInstance(Tool.class);%0A    %7D%0A    %0A
 %09private
@@ -2166,16 +2166,17 @@
 () %7B%0A%09%7D%0A
+%09
 %0A%09/**%0A%09 
@@ -2378,83 +2378,8 @@
 ile%0A
-%09 * @throws JAXBException%0A%09 * @throws IOException%0A%09 * @throws SAXException%0A
 %09 */
@@ -2494,96 +2494,538 @@
 %7B%0A%09%09
-File schemaFile = null;%0A%0A%09%09JAXBContext context = JAXBContext.newInstance(Tool.class);%0A%09%09
+Unmarshaller unmarshaller = createUnmarshaller();%0A%09%09return (Tool) unmarshaller.unmarshal(new File(toolspecFilePath));%0A%09%7D%0A%0A    /**%0A     * Unmarshals an input stream of xml data to a Tool.%0A     */%0A%09public static Tool fromInputStream(InputStream input) throws JAXBException, IOException, SAXException %7B%0A        Unmarshaller unmarshaller = Utils.createUnmarshaller();%0A        JAXBElement%3CTool%3E unmarshalled = unmarshaller.unmarshal(new StreamSource(input), Tool.class);%0A        return unmarshalled.getValue();%0A    %7D%0A%09%0A%09private static 
 Unma
@@ -3025,33 +3025,39 @@
 ic Unmarshaller 
-u
+createU
 nmarshaller = co
@@ -3055,18 +3055,99 @@
 ller
- = context
+() throws JAXBException, IOException, SAXException %7B%0A        Unmarshaller unmarshaller = jc
 .cre
@@ -3165,19 +3165,187 @@
 ller();%0A
-%0A
 %09%09
+setSchemaTo(unmarshaller);%0A        return unmarshaller;%0A    %7D%0A%0A    private static void setSchemaTo(Unmarshaller unmarshaller) throws IOException, SAXException %7B%0A        
 // copy 
@@ -3396,16 +3396,21 @@
 ation%0A%09%09
+File 
 schemaFi
@@ -3462,21 +3462,16 @@
 ileUtils
-%0A%09%09%09%09
 .copyInp
@@ -3774,97 +3774,12 @@
 a);%0A
-%0A%09%09// unmarshal it%0A%09%09return (Tool) unmarshaller.unmarshal(new File(toolspecFilePath));%0A%0A%09
+    
 %7D%0A%7D%0A

add another helper method to ToolSpec Utils helper