Moved from t2.lang
git-svn-id: https://taverna.googlecode.com/svn/taverna/utils/net.sf.taverna.t2.lang/branches/maintenance@16888 bf327186-88b3-11dd-a302-d386e5130c1c Project: http://git-wip-us.apache.org/repos/asf/incubator-taverna-workbench/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-taverna-workbench/commit/c7cfb2ef Tree: http://git-wip-us.apache.org/repos/asf/incubator-taverna-workbench/tree/c7cfb2ef Diff: http://git-wip-us.apache.org/repos/asf/incubator-taverna-workbench/diff/c7cfb2ef Branch: refs/heads/master Commit: c7cfb2efb53719b1d57a412d79c03eb360a33033 Parents: 52f2960 Author: [email protected] <[email protected]@bf327186-88b3-11dd-a302-d386e5130c1c> Authored: Wed Mar 19 16:51:06 2014 +0000 Committer: [email protected] <[email protected]@bf327186-88b3-11dd-a302-d386e5130c1c> Committed: Wed Mar 19 16:51:06 2014 +0000 ---------------------------------------------------------------------- results/pom.xml | 54 --- .../t2/lang/baclava/BaclavaDocumentHandler.java | 133 -------- .../taverna/t2/lang/results/ResultsUtils.java | 329 ------------------- 3 files changed, 516 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-taverna-workbench/blob/c7cfb2ef/results/pom.xml ---------------------------------------------------------------------- diff --git a/results/pom.xml b/results/pom.xml deleted file mode 100644 index 4db22ee..0000000 --- a/results/pom.xml +++ /dev/null @@ -1,54 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> - <modelVersion>4.0.0</modelVersion> - <groupId>net.sf.taverna.t2.lang</groupId> - <artifactId>results</artifactId> - <parent> - <groupId>net.sf.taverna.t2</groupId> - <artifactId>lang</artifactId> - <version>1.6-SNAPSHOT</version> - </parent> - <name>Common Results handling utilities</name> - <description>Common utilities for handling results, in particular Baclava document handling and de-referencing T2References</description> - <dependencies> - <dependency> - <groupId>uk.org.mygrid.taverna.baclava</groupId> - <artifactId>baclava-core</artifactId> - <version>${taverna.version}</version> - </dependency> - <dependency> - <groupId>uk.org.mygrid.taverna.baclava</groupId> - <artifactId>baclava-core</artifactId> - <version>${taverna.version}</version> - </dependency> - <dependency> - <groupId>net.sf.taverna.t2.core</groupId> - <artifactId>reference-api</artifactId> - <version>${t2.core.version}</version> - </dependency> - <dependency> - <groupId>net.sf.taverna.t2.core</groupId> - <artifactId>workflowmodel-api</artifactId> - <version>${t2.core.version}</version> - </dependency> - <dependency> - <groupId>uk.org.mygrid.resources.mimeutil</groupId> - <artifactId>mime-util</artifactId> - <version>2.1.2-7-SNAPSHOT</version> - </dependency> - <dependency> - <groupId>uk.org.mygrid.resources.clapper</groupId> - <artifactId>ocutil</artifactId> - <version>2.5.1</version> - </dependency> - <dependency> - <groupId>log4j</groupId> - <artifactId>log4j</artifactId> - </dependency> - <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - <scope>test</scope> - </dependency> - </dependencies> -</project> http://git-wip-us.apache.org/repos/asf/incubator-taverna-workbench/blob/c7cfb2ef/results/src/main/java/net/sf/taverna/t2/lang/baclava/BaclavaDocumentHandler.java ---------------------------------------------------------------------- diff --git a/results/src/main/java/net/sf/taverna/t2/lang/baclava/BaclavaDocumentHandler.java b/results/src/main/java/net/sf/taverna/t2/lang/baclava/BaclavaDocumentHandler.java deleted file mode 100644 index 62dbe65..0000000 --- a/results/src/main/java/net/sf/taverna/t2/lang/baclava/BaclavaDocumentHandler.java +++ /dev/null @@ -1,133 +0,0 @@ -package net.sf.taverna.t2.lang.baclava; - -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.io.PrintWriter; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.Map; - -import net.sf.taverna.t2.invocation.InvocationContext; -import net.sf.taverna.t2.lang.results.ResultsUtils; -import net.sf.taverna.t2.reference.ReferenceService; -import net.sf.taverna.t2.reference.T2Reference; - -import org.embl.ebi.escience.baclava.DataThing; -import org.embl.ebi.escience.baclava.factory.DataThingFactory; -import org.embl.ebi.escience.baclava.factory.DataThingXMLFactory; -import org.jdom.Document; -import org.jdom.Element; -import org.jdom.JDOMException; -import org.jdom.Namespace; -import org.jdom.input.SAXBuilder; -import org.jdom.output.Format; -import org.jdom.output.XMLOutputter; - -/** - * Handles the loading and saving of T2Reference data as Baclava documents - * - * @author Stuart Owen - * - */ - -public class BaclavaDocumentHandler { - - private Map<String, T2Reference> chosenReferences; - - private static Namespace namespace = Namespace.getNamespace("b", - "http://org.embl.ebi.escience/baclava/0.1alpha"); - - protected ReferenceService referenceService; - - protected InvocationContext context; - - // private static Logger logger = - // Logger.getLogger(BaclavaDocumentHandler.class); - - - /** - * Reads a baclava document from an InputStream and returns a map of DataThings mapped to the portName - * - * @throws IOException, JDOMException - */ - public Map<String, DataThing> readData(InputStream inputStream) - throws IOException, JDOMException { - - SAXBuilder builder = new SAXBuilder(); - Document inputDoc; - inputDoc = builder.build(inputStream); - - return DataThingXMLFactory.parseDataDocument(inputDoc); - } - - /** - * Saves the result data to an XML Baclava file. - * - * @throws IOException - */ - public void saveData(File file) throws IOException { - // Build the string containing the XML document from the result map - Document doc = getDataDocument(); - XMLOutputter xo = new XMLOutputter(Format.getPrettyFormat()); - PrintWriter out = new PrintWriter(new FileWriter(file)); - xo.output(doc, out); - } - - /** - * Returns a org.jdom.Document from a map of port named to DataThingS - * containing the port's results. - */ - public Document getDataDocument() { - Element rootElement = new Element("dataThingMap", namespace); - Document theDocument = new Document(rootElement); - // Build the DataThing map from the chosenReferences - // First convert map of references to objects into a map of real result - // objects - for (String portName : getChosenReferences().keySet()) { - DataThing thing = DataThingFactory.bake(getObjectForName(portName)); - Element dataThingElement = new Element("dataThing", namespace); - dataThingElement.setAttribute("key", portName); - dataThingElement.addContent(thing.getElement()); - rootElement.addContent(dataThingElement); - } - return theDocument; - } - - /** - * @param referenceService - * the referenceService to set - */ - public void setReferenceService(ReferenceService referenceService) { - this.referenceService = referenceService; - } - - /** - * Sets the InvocationContext to be used to get the Reference Service to be - * used dereference the reference. - */ - public void setInvocationContext(InvocationContext context) { - this.context = context; - } - - protected Object getObjectForName(String name) { - Object result = null; - if (getChosenReferences().containsKey(name)) { - result = ResultsUtils.convertReferenceToObject( - getChosenReferences().get(name), referenceService, context); - } - if (result == null) { - result = "null"; - } - return result; - } - - private Map<String, T2Reference> getChosenReferences() { - return chosenReferences; - } - - public void setChosenReferences(Map<String, T2Reference> chosenReferences) { - this.chosenReferences = chosenReferences; - } -} http://git-wip-us.apache.org/repos/asf/incubator-taverna-workbench/blob/c7cfb2ef/results/src/main/java/net/sf/taverna/t2/lang/results/ResultsUtils.java ---------------------------------------------------------------------- diff --git a/results/src/main/java/net/sf/taverna/t2/lang/results/ResultsUtils.java b/results/src/main/java/net/sf/taverna/t2/lang/results/ResultsUtils.java deleted file mode 100644 index c99a646..0000000 --- a/results/src/main/java/net/sf/taverna/t2/lang/results/ResultsUtils.java +++ /dev/null @@ -1,329 +0,0 @@ -/******************************************************************************* - * Copyright (C) 2007 The University of Manchester - * - * Modifications to the initial code base are copyright of their - * respective authors, or their employers as appropriate. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License - * as published by the Free Software Foundation; either version 2.1 of - * the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 - ******************************************************************************/ -package net.sf.taverna.t2.lang.results; - -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Set; - -import javax.swing.tree.DefaultMutableTreeNode; - -import net.sf.taverna.t2.invocation.InvocationContext; -import net.sf.taverna.t2.reference.ErrorDocument; -import net.sf.taverna.t2.reference.ErrorDocumentService; -import net.sf.taverna.t2.reference.ExternalReferenceSPI; -import net.sf.taverna.t2.reference.IdentifiedList; -import net.sf.taverna.t2.reference.ListService; -import net.sf.taverna.t2.reference.ReferenceService; -import net.sf.taverna.t2.reference.ReferenceServiceException; -import net.sf.taverna.t2.reference.ReferenceSet; -import net.sf.taverna.t2.reference.ReferencedDataNature; -import net.sf.taverna.t2.reference.StackTraceElementBean; -import net.sf.taverna.t2.reference.T2Reference; -import net.sf.taverna.t2.reference.T2ReferenceType; -import net.sf.taverna.t2.reference.ValueCarryingExternalReference; - -import org.apache.log4j.Logger; -import org.clapper.util.misc.MIMETypeUtil; - -import eu.medsea.mimeutil.MimeType; -import eu.medsea.mimeutil.MimeUtil2; - -/** - * Convenience methods for displaying and storing workflow run results. For - * example, converting result error documents into various representations (e.g. - * StringS or JTreeS), getting MIME type of result objects, etc. - * - * @author Alex Nenadic - * - */ -public class ResultsUtils { - - private static Logger logger = Logger.getLogger(ResultsUtils.class); - - /** - * Converts a T2Reference pointing to results to - * a list of (lists of ...) dereferenced result object. - * @param context - * @param referenceService - */ - public static Object convertReferenceToObject(T2Reference reference, ReferenceService referenceService, InvocationContext context) { - - if (reference.getReferenceType() == T2ReferenceType.ReferenceSet){ - - ReferenceSet rs = referenceService.getReferenceSetService().getReferenceSet(reference); - if (rs == null) { - throw new ReferenceServiceException("Could not find ReferenceSet " + reference); - } - // Check that there are references in the set - if (rs.getExternalReferences().isEmpty()) { - throw new ReferenceServiceException( - "Can't render an empty reference set to a POJO"); - } - - ReferencedDataNature dataNature = ReferencedDataNature.UNKNOWN; - for (ExternalReferenceSPI ers : rs.getExternalReferences()) { - ReferencedDataNature erDataNature = ers.getDataNature(); - if (!erDataNature.equals(ReferencedDataNature.UNKNOWN)) { - dataNature = erDataNature; - break; - } - } - - // Dereference the object - Object dataValue; - try{ - if (dataNature.equals(ReferencedDataNature.TEXT)) { - dataValue = referenceService.renderIdentifier(reference, String.class, context); - } else { - dataValue = referenceService.renderIdentifier(reference, byte[].class, context); - } - } - catch(ReferenceServiceException rse){ - String message = "Problem rendering T2Reference in convertReferencesToObjects()."; - logger.error("BaclavaDocumentHandler Error: "+ message, rse); - throw rse; - } - return dataValue; - } - else if (reference.getReferenceType() == T2ReferenceType.ErrorDocument){ - // Dereference the ErrorDocument and convert it to some string representation - ErrorDocument errorDocument = (ErrorDocument)referenceService.resolveIdentifier(reference, null, context); - String errorString = ResultsUtils.buildErrorDocumentString(errorDocument, context); - return errorString; - } - else { // it is an IdentifiedList<T2Reference> - go recursively - IdentifiedList<T2Reference> identifiedList = referenceService.getListService().getList(reference); - List<Object> list = new ArrayList<Object>(); - - for (int j=0; j<identifiedList.size(); j++){ - T2Reference ref = identifiedList.get(j); - list.add(convertReferenceToObject(ref,referenceService,context)); - } - return list; - } - } - - /** - * Creates a string representation of the ErrorDocument. - */ - public static String buildErrorDocumentString(ErrorDocument errDocument, - InvocationContext context) { - - String errDocumentString = ""; - - String message = errDocument.getMessage(); - if (message != null && !message.isEmpty()) { - errDocumentString = message + "\n"; - } - - String exceptionMessage = errDocument.getExceptionMessage(); - if (exceptionMessage != null && !exceptionMessage.equals("")) { - errDocumentString += exceptionMessage + "\n"; - List<StackTraceElementBean> stackTrace = errDocument - .getStackTraceStrings(); - if (stackTrace.size() > 0) { - for (StackTraceElementBean stackTraceElement : stackTrace) { - errDocumentString += getStackTraceElementString(stackTraceElement) - + "\n"; - } - } - - } - - Set<T2Reference> errorReferences = errDocument.getErrorReferences(); - if (!errorReferences.isEmpty()) { - errDocumentString += "Set of ErrorDocuments to follow." + "\n"; - } - int errorCounter = 1; - int listCounter = 0; - for (T2Reference reference : errorReferences) { - if (reference.getReferenceType().equals( - T2ReferenceType.ErrorDocument)) { - ErrorDocumentService errorDocumentService = context - .getReferenceService().getErrorDocumentService(); - ErrorDocument causeErrorDocument = errorDocumentService - .getError(reference); - if (listCounter == 0) { - errDocumentString += "ErrorDocument " + (errorCounter++) - + "\n"; - } else { - errDocumentString += "ErrorDocument " + listCounter + "." - + (errorCounter++) + "\n"; - } - errDocumentString += buildErrorDocumentString( - causeErrorDocument, context) - + "\n"; - } else if (reference.getReferenceType().equals( - T2ReferenceType.IdentifiedList)) { - List<ErrorDocument> errorDocuments = getErrorDocuments( - reference, context.getReferenceService()); - errDocumentString += "ErrorDocument list " + (++listCounter) - + "\n"; - for (ErrorDocument causeErrorDocument : errorDocuments) { - errDocumentString += buildErrorDocumentString( - causeErrorDocument, context) - + "\n"; - } - } - } - - return errDocumentString; - } - - public static void buildErrorDocumentTree(DefaultMutableTreeNode node, - ErrorDocument errorDocument, ReferenceService referenceService) { - DefaultMutableTreeNode child = new DefaultMutableTreeNode(errorDocument); - String exceptionMessage = errorDocument.getExceptionMessage(); - if (exceptionMessage != null && !exceptionMessage.equals("")) { - DefaultMutableTreeNode exceptionMessageNode = new DefaultMutableTreeNode( - exceptionMessage); - child.add(exceptionMessageNode); - List<StackTraceElementBean> stackTrace = errorDocument - .getStackTraceStrings(); - if (stackTrace.size() > 0) { - for (StackTraceElementBean stackTraceElement : stackTrace) { - exceptionMessageNode.add(new DefaultMutableTreeNode( - getStackTraceElementString(stackTraceElement))); - } - } - - } - node.add(child); - - Set<T2Reference> errorReferences = errorDocument.getErrorReferences(); - for (T2Reference reference : errorReferences) { - if (reference.getReferenceType().equals( - T2ReferenceType.ErrorDocument)) { - ErrorDocumentService errorDocumentService = referenceService - .getErrorDocumentService(); - ErrorDocument causeErrorDocument = errorDocumentService - .getError(reference); - if (errorReferences.size() == 1) { - buildErrorDocumentTree(node, causeErrorDocument, referenceService); - } else { - buildErrorDocumentTree(child, causeErrorDocument, referenceService); - } - } else if (reference.getReferenceType().equals( - T2ReferenceType.IdentifiedList)) { - List<ErrorDocument> errorDocuments = getErrorDocuments( - reference, referenceService); - if (errorDocuments.size() == 1) { - buildErrorDocumentTree(node, errorDocuments.get(0), referenceService); - } else { - for (ErrorDocument errorDocument2 : errorDocuments) { - buildErrorDocumentTree(child, errorDocument2, referenceService); - } - } - } - } - } - - private static String getStackTraceElementString( - StackTraceElementBean stackTraceElement) { - StringBuilder sb = new StringBuilder(); - sb.append(stackTraceElement.getClassName()); - sb.append('.'); - sb.append(stackTraceElement.getMethodName()); - if (stackTraceElement.getFileName() == null) { - sb.append("(unknown file)"); - } else { - sb.append('('); - sb.append(stackTraceElement.getFileName()); - sb.append(':'); - sb.append(stackTraceElement.getLineNumber()); - sb.append(')'); - } - return sb.toString(); - } - - public static List<ErrorDocument> getErrorDocuments(T2Reference reference, - ReferenceService referenceService) { - List<ErrorDocument> errorDocuments = new ArrayList<ErrorDocument>(); - if (reference.getReferenceType().equals(T2ReferenceType.ErrorDocument)) { - ErrorDocumentService errorDocumentService = referenceService - .getErrorDocumentService(); - errorDocuments.add(errorDocumentService.getError(reference)); - } else if (reference.getReferenceType().equals( - T2ReferenceType.IdentifiedList)) { - ListService listService = referenceService.getListService(); - IdentifiedList<T2Reference> list = listService.getList(reference); - for (T2Reference listReference : list) { - errorDocuments - .addAll(getErrorDocuments(listReference, referenceService)); - } - } - return errorDocuments; - } - - @SuppressWarnings("unchecked") - public static List<MimeType> getMimeTypes( - ExternalReferenceSPI externalReference, InvocationContext context) { - List<MimeType> mimeList = new ArrayList<MimeType>(); - MimeUtil2 mimeUtil = new MimeUtil2(); - mimeUtil - .registerMimeDetector("eu.medsea.mimeutil.detector.ExtensionMimeDetector"); - mimeUtil - .registerMimeDetector("eu.medsea.mimeutil.detector.MagicMimeMimeDetector"); - mimeUtil - .registerMimeDetector("eu.medsea.mimeutil.detector.WindowsRegistryMimeDetector"); - mimeUtil - .registerMimeDetector("eu.medsea.mimeutil.detector.ExtraMimeTypes"); - InputStream inputStream = externalReference.openStream(context); - try { - byte[] bytes = new byte[2048]; // need to read this much if we want to detect SVG using the hack below - inputStream.read(bytes); - Collection mimeTypes2 = mimeUtil.getMimeTypes(bytes); - mimeList.addAll(mimeTypes2); - - // Hack for SVG that seems not to be recognised - String bytesString = new String(bytes, "UTF-8"); - if (bytesString.contains("http://www.w3.org/2000/svg")){ - MimeType svgMimeType = new MimeType("image/svg+xml"); - if (!mimeList.contains(svgMimeType)){ - mimeList.add(svgMimeType); - } - } - - } catch (IOException e) { - logger.error("Failed to read from stream to determine mimetype", e); - } finally { - try { - inputStream.close(); - } catch (IOException e) { - logger.error( - "Failed to close stream after determining mimetype", e); - } - } - - return mimeList; - } - - public static String getExtension(String mimeType) { - - String mimeTypeForFileExtension = MIMETypeUtil - .fileExtensionForMIMEType(mimeType); - return mimeTypeForFileExtension; - } -}
