Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

7856 flim ann #820

Merged
merged 15 commits into from Dec 12, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
76 changes: 76 additions & 0 deletions components/scifio-devel/src/ome/scifio/xml/XMLTools.java
Expand Up @@ -38,13 +38,15 @@

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
Expand All @@ -62,6 +64,7 @@
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.ErrorListener;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Templates;
Expand All @@ -84,6 +87,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
Expand All @@ -105,6 +109,9 @@ public final class XMLTools {

static final Logger LOGGER = LoggerFactory.getLogger(XMLTools.class);

private static final String XSI_NS =
"http://www.w3.org/2001/XMLSchema-instance";

private static final String XML_SCHEMA_PATH =
"http://www.w3.org/2001/XMLSchema";

Expand All @@ -127,6 +134,28 @@ private XMLTools() { }

// -- XML to/from DOM --

/**
* Creates a new {@link DocumentBuilder} via {@link DocumentBuilderFactory}
* or logs and throws a {@link RuntimeException}.
*/
public static DocumentBuilder createBuilder() {
try {
return DocumentBuilderFactory.newInstance().newDocumentBuilder();
}
catch (ParserConfigurationException e) {
LOGGER.error("Cannot create DocumentBuilder", e);
throw new RuntimeException(e);
}
}

/**
* Calls {@link DocumentBuilder#newDocument()} on a
* {@link #createBuilder() new builder}.
*/
public static Document createDocument() {
return createBuilder().newDocument();
}

/** Parses a DOM from the given XML file on disk. */
public static Document parseDOM(File file)
throws ParserConfigurationException, SAXException, IOException
Expand Down Expand Up @@ -184,6 +213,42 @@ public static String getXML(Document doc)
return stringWriter.getBuffer().toString();
}

/**
* Dumps the given OME-XML DOM tree to a string.
* @param schemaLocation if null, no xmlns attribute will be added.
* @return OME-XML as a string.
*/
public static String dumpXML(String schemaLocation, Document doc, Element r) {
return dumpXML(schemaLocation, doc, r, true);
}

/**
* Dumps the given OME-XML DOM tree to a string.
* @param schemaLocation if null, no xmlns attribute will be added.
* @return OME-XML as a string.
*/
public static String dumpXML(String schemaLocation, Document doc, Element r,
boolean includeXMLDeclaration) {
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
if (schemaLocation != null) {
r.setAttribute("xmlns:xsi", XSI_NS);
r.setAttribute("xsi:schemaLocation", schemaLocation);
}
doc.appendChild(r);
writeXML(os, doc, includeXMLDeclaration);
return os.toString(Constants.ENCODING);
}
catch (TransformerException exc) {
LOGGER.warn("Failed to create XML", exc);
throw new RuntimeException(exc);
}
catch (UnsupportedEncodingException exc) {
LOGGER.warn("Failed to create XML", exc);
throw new RuntimeException(exc);
}
}

// -- Filtering --

/** Remove invalid characters from an XML string. */
Expand Down Expand Up @@ -383,9 +448,20 @@ public static void parseXML(InputStream xml, DefaultHandler handler)
/** Writes the specified DOM to the given output stream. */
public static void writeXML(OutputStream os, Document doc)
throws TransformerException
{
writeXML(os, doc, true);
}

/** Writes the specified DOM to the given output stream. */
public static void writeXML(OutputStream os, Document doc,
boolean includeXMLDeclaration)
throws TransformerException
{
TransformerFactory transformFactory = TransformerFactory.newInstance();
Transformer idTransform = transformFactory.newTransformer();
if (!includeXMLDeclaration) {
idTransform.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
}
Source input = new DOMSource(doc);
Result output = new StreamResult(os);
idTransform.transform(input, output);
Expand Down
110 changes: 110 additions & 0 deletions components/scifio/src/loci/formats/meta/ModuloAnnotation.java
@@ -0,0 +1,110 @@
/*
* #%L
* OME SCIFIO package for reading and converting scientific file formats.
* %%
* Copyright (C) 2005 - 2013 Open Microscopy Environment:
* - Board of Regents of the University of Wisconsin-Madison
* - Glencoe Software, Inc.
* - University of Dundee
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. 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.
*
* 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 HOLDERS 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.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of any organization.
* #L%
*/

package loci.formats.meta;

import loci.formats.Modulo;
import loci.formats.ome.OMEXMLMetadata;
import ome.scifio.xml.XMLTools;
import ome.xml.model.XMLAnnotation;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class ModuloAnnotation extends XMLAnnotation {

public static final String MODULO_NS;
static {
MODULO_NS = "openmicroscopy.org/omero/dimension/modulo";
}

private Modulo modulo;

public void setModulo(OMEXMLMetadata meta, Modulo m) {
modulo = m;
setNamespace(MODULO_NS);
Document doc = XMLTools.createDocument();
Element r = makeModuloElement(doc);
setValue(XMLTools.dumpXML(null, doc, r, false));
}

protected Element makeModuloElement(Document document) {
Element mtop = document.createElement("Modulo");
mtop.setAttribute("namespace", "http://www.openmicroscopy.org/Schemas/Additions/2011-09");
// TODO: the above should likely NOT be hard-coded
Element m = document.createElement("ModuloAlong" + modulo.parentDimension);
mtop.appendChild(m);

String type = modulo.type;
String typeDescription = modulo.typeDescription;
if (type != null) {
type = type.toLowerCase();
}
// Handle CZI files for the moment.
// TODO: see http://trac.openmicroscopy.org.uk/ome/ticket/11720
if (type.equals("rotation")) {
type = "angle";
}
if (type == null || (!type.equals("angle") && !type.equals("phase") &&
!type.equals("tile") && !type.equals("lifetime") &&
!type.equals("lambda")))
{
if (typeDescription == null) {
typeDescription = type;
}
type = "other";
}

m.setAttribute("Type", type);
m.setAttribute("TypeDescription", typeDescription);
if (modulo.unit != null) {
m.setAttribute("Unit", modulo.unit);
}
if (modulo.end > modulo.start) {
m.setAttribute("Start", String.valueOf(modulo.start));
m.setAttribute("Step", String.valueOf(modulo.step));
m.setAttribute("End", String.valueOf(modulo.end));
}
if (modulo.labels != null) {
for (String label : modulo.labels) {
Element labelNode = document.createElement("Label");
labelNode.setTextContent(label);
m.appendChild(labelNode);
}
}
return mtop;
}
}
@@ -0,0 +1,96 @@
/*
* #%L
* OME SCIFIO package for reading and converting scientific file formats.
* %%
* Copyright (C) 2005 - 2013 Open Microscopy Environment:
* - Board of Regents of the University of Wisconsin-Madison
* - Glencoe Software, Inc.
* - University of Dundee
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. 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.
*
* 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 HOLDERS 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.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of any organization.
* #L%
*/

package loci.formats.meta;

import ome.scifio.xml.XMLTools;
import ome.xml.model.XMLAnnotation;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class OriginalMetadataAnnotation extends XMLAnnotation {

public static final String ORIGINAL_METADATA_NS;
static {
ORIGINAL_METADATA_NS = "openmicroscopy.org/OriginalMetadata";
}

private String key, value;

// -- OriginalMetadataAnnotation methods --

public void setKeyValue(String key, String value) {
setNamespace(ORIGINAL_METADATA_NS);
this.key = key;
this.value = value; // Not XML value
Document doc = XMLTools.createDocument();
Element r = makeOriginalMetadata(doc);
super.setValue(XMLTools.dumpXML(null, doc, r, false));
}

// -- XMLAnnotation methods --

public String getKey() {
return key;
}

/**
* Return just the value (i.e. v in k=v) as opposed to the XML
* value which contains the entire block (e.g. <originalmetadata>...)
*/
public String getValueForKey() {
return value;
}

protected Element makeOriginalMetadata(Document document) {

Element keyElement =
document.createElement("Key");
Element valueElement =
document.createElement("Value");
keyElement.setTextContent(key);
valueElement.setTextContent(value);

Element originalMetadata =
document.createElement("OriginalMetadata");
originalMetadata.appendChild(keyElement);
originalMetadata.appendChild(valueElement);

return originalMetadata;
}

}
30 changes: 7 additions & 23 deletions components/scifio/src/loci/formats/ome/AbstractOMEXMLMetadata.java
Expand Up @@ -111,34 +111,18 @@ public String dumpXML() {
root = (OMEModelObject) getRoot();
if (root == null) return null;
}
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
Document doc = createNewDocument();
Element r = root.asXMLElement(doc);
r.setAttribute("xmlns:xsi", XSI_NS);
r.setAttribute("xsi:schemaLocation", OME.NAMESPACE + " " + SCHEMA);
doc.appendChild(r);
XMLTools.writeXML(os, doc);

return os.toString(Constants.ENCODING);
}
catch (TransformerException exc) {
LOGGER.warn("Failed to create OME-XML", exc);
}
catch (UnsupportedEncodingException exc) {
LOGGER.warn("Failed to create OME-XML", exc);
}
return null;
Document doc = createNewDocument();
Element r = root.asXMLElement(doc);
String schemaLocation = OME.NAMESPACE + " " + SCHEMA;
return XMLTools.dumpXML(schemaLocation, doc, r);
}


// -- Helper methods --

private Document createNewDocument() {
public Document createNewDocument() {
if (builder == null) {
try {
builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
}
catch (ParserConfigurationException e) { }
builder = XMLTools.createBuilder();
}
return builder.newDocument();
}
Expand Down