Search in sources :

Example 1 with OMEXMLMetadataRoot

use of ome.xml.meta.OMEXMLMetadataRoot in project bioformats by openmicroscopy.

the class OMEXMLServiceImpl method populateOriginalMetadata.

/**
 * @see OMEXMLService#populateOriginalMetadata(loci.formats.ome.OMEXMLMetadata, Hashtable)
 */
@Override
public void populateOriginalMetadata(OMEXMLMetadata omexmlMeta, Hashtable<String, Object> metadata) {
    omexmlMeta.resolveReferences();
    if (metadata.size() == 0) {
        return;
    }
    OMEXMLMetadataRoot root = (OMEXMLMetadataRoot) omexmlMeta.getRoot();
    StructuredAnnotations annotations = root.getStructuredAnnotations();
    if (annotations == null)
        annotations = new StructuredAnnotations();
    int annotationIndex = annotations.sizeOfXMLAnnotationList();
    if (annotationIndex > 0) {
        String lastAnnotationID = omexmlMeta.getXMLAnnotationID(annotationIndex - 1);
        String lastIndex = lastAnnotationID.substring(lastAnnotationID.lastIndexOf(":") + 1);
        try {
            int index = Integer.parseInt(lastIndex);
            while (index >= annotationIndex) {
                annotationIndex++;
            }
        } catch (NumberFormatException e) {
        }
    }
    for (String key : metadata.keySet()) {
        OriginalMetadataAnnotation annotation = new OriginalMetadataAnnotation();
        annotation.setID(MetadataTools.createLSID("Annotation", annotationIndex));
        annotation.setKeyValue(key, metadata.get(key).toString());
        annotations.addXMLAnnotation(annotation);
        annotationIndex++;
    }
    root.setStructuredAnnotations(annotations);
    omexmlMeta.setRoot(root);
}
Also used : OMEXMLMetadataRoot(ome.xml.meta.OMEXMLMetadataRoot) StructuredAnnotations(ome.xml.model.StructuredAnnotations) OriginalMetadataAnnotation(loci.formats.meta.OriginalMetadataAnnotation)

Example 2 with OMEXMLMetadataRoot

use of ome.xml.meta.OMEXMLMetadataRoot in project bioformats by openmicroscopy.

the class OMEXMLServiceImpl method validateOMEXML.

/**
 * @see OMEXMLService#validateOMEXML(java.lang.String, boolean)
 */
@Override
public boolean validateOMEXML(String xml, boolean pixelsHack) {
    // HACK: Inject a TiffData element beneath any childless Pixels elements.
    if (pixelsHack) {
        // convert XML string to DOM
        Document doc = null;
        Exception exception = null;
        try {
            doc = XMLTools.parseDOM(xml);
        } catch (ParserConfigurationException exc) {
            exception = exc;
        } catch (SAXException exc) {
            exception = exc;
        } catch (IOException exc) {
            exception = exc;
        }
        if (exception != null) {
            LOGGER.info("Malformed OME-XML", exception);
            return false;
        }
        // inject TiffData elements as needed
        NodeList list = doc.getElementsByTagName("Pixels");
        for (int i = 0; i < list.getLength(); i++) {
            Node node = list.item(i);
            NodeList children = node.getChildNodes();
            boolean needsTiffData = true;
            for (int j = 0; j < children.getLength(); j++) {
                Node child = children.item(j);
                String name = child.getLocalName();
                if ("TiffData".equals(name) || "BinData".equals(name)) {
                    needsTiffData = false;
                    break;
                }
            }
            if (needsTiffData) {
                // inject TiffData element
                Node tiffData = doc.createElement("TiffData");
                node.insertBefore(tiffData, node.getFirstChild());
            }
        }
        // convert tweaked DOM back to XML string
        try {
            xml = XMLTools.getXML(doc);
        } catch (TransformerConfigurationException exc) {
            exception = exc;
        } catch (TransformerException exc) {
            exception = exc;
        }
        if (exception != null) {
            LOGGER.info("Internal XML conversion error", exception);
            return false;
        }
    }
    try {
        OMEXMLMetadata omexml = createOMEXMLMetadata(xml);
        OMEXMLMetadataRoot root = (OMEXMLMetadataRoot) omexml.getRoot();
        for (int image = 0; image < root.sizeOfImageList(); image++) {
            Image img = root.getImage(image);
            for (int i = 0; i < img.sizeOfLinkedAnnotationList(); i++) {
                Annotation annotation = img.getLinkedAnnotation(i);
                if (!(annotation instanceof XMLAnnotation)) {
                    continue;
                }
                String annotationXML = ((XMLAnnotation) annotation).getValue();
                Document annotationRoot = XMLTools.parseDOM(annotationXML);
                NodeList nodes = annotationRoot.getElementsByTagName("ModuloAlongZ");
                if (nodes.getLength() > 0) {
                    ((XMLAnnotation) annotation).setValue("");
                }
                nodes = annotationRoot.getElementsByTagName("ModuloAlongC");
                if (nodes.getLength() > 0) {
                    ((XMLAnnotation) annotation).setValue("");
                }
                nodes = annotationRoot.getElementsByTagName("ModuloAlongT");
                if (nodes.getLength() > 0) {
                    ((XMLAnnotation) annotation).setValue("");
                }
            }
        }
        omexml.setRoot(root);
        xml = getOMEXML(omexml);
    } catch (ServiceException | ParserConfigurationException | SAXException | IOException e) {
        LOGGER.warn("Could not remove Modulo annotations", e);
    }
    return XMLTools.validateXML(xml, "OME-XML", SCHEMA_CLASSPATH_READER);
}
Also used : TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) IOException(java.io.IOException) Document(org.w3c.dom.Document) Image(ome.xml.model.Image) ServiceException(loci.common.services.ServiceException) SAXException(org.xml.sax.SAXException) TransformerException(javax.xml.transform.TransformerException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Annotation(ome.xml.model.Annotation) ModuloAnnotation(loci.formats.meta.ModuloAnnotation) OriginalMetadataAnnotation(loci.formats.meta.OriginalMetadataAnnotation) XMLAnnotation(ome.xml.model.XMLAnnotation) SAXException(org.xml.sax.SAXException) ServiceException(loci.common.services.ServiceException) OMEXMLMetadata(loci.formats.ome.OMEXMLMetadata) OMEXMLMetadataRoot(ome.xml.meta.OMEXMLMetadataRoot) XMLAnnotation(ome.xml.model.XMLAnnotation) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TransformerException(javax.xml.transform.TransformerException)

Example 3 with OMEXMLMetadataRoot

use of ome.xml.meta.OMEXMLMetadataRoot in project bioformats by openmicroscopy.

the class OMEXMLServiceImpl method createOMEXMLMetadata.

/**
 * @see OMEXMLService#createOMEXMLMetadata(java.lang.String, java.lang.String)
 */
@Override
public OMEXMLMetadata createOMEXMLMetadata(String xml, String version) throws ServiceException {
    if (xml != null) {
        xml = XMLTools.sanitizeXML(xml);
    }
    OMEXMLMetadataRoot ome = xml == null ? null : createRoot(transformToLatestVersion(xml));
    OMEXMLMetadata meta = new OMEXMLMetadataImpl();
    if (ome != null)
        meta.setRoot(ome);
    return meta;
}
Also used : OMEXMLMetadata(loci.formats.ome.OMEXMLMetadata) OMEXMLMetadataRoot(ome.xml.meta.OMEXMLMetadataRoot) OMEXMLMetadataImpl(loci.formats.ome.OMEXMLMetadataImpl)

Example 4 with OMEXMLMetadataRoot

use of ome.xml.meta.OMEXMLMetadataRoot in project bioformats by openmicroscopy.

the class OMEXMLServiceImpl method populateOriginalMetadata.

/**
 * @see OMEXMLService#populateOriginalMetadata(loci.formats.ome.OMEXMLMetadata, java.lang.String, java.lang.String)
 */
@Override
public void populateOriginalMetadata(OMEXMLMetadata omexmlMeta, String key, String value) {
    omexmlMeta.resolveReferences();
    OMEXMLMetadataRoot root = (OMEXMLMetadataRoot) omexmlMeta.getRoot();
    StructuredAnnotations annotations = root.getStructuredAnnotations();
    if (annotations == null)
        annotations = new StructuredAnnotations();
    int annotationIndex = annotations.sizeOfXMLAnnotationList();
    if (annotationIndex > 0) {
        String lastAnnotationID = omexmlMeta.getXMLAnnotationID(annotationIndex - 1);
        String lastIndex = lastAnnotationID.substring(lastAnnotationID.lastIndexOf(":") + 1);
        try {
            int index = Integer.parseInt(lastIndex);
            while (index >= annotationIndex) {
                annotationIndex++;
            }
        } catch (NumberFormatException e) {
        }
    }
    OriginalMetadataAnnotation annotation = new OriginalMetadataAnnotation();
    annotation.setID(MetadataTools.createLSID("Annotation", annotationIndex));
    annotation.setKeyValue(key, value);
    annotations.addXMLAnnotation(annotation);
    root.setStructuredAnnotations(annotations);
    omexmlMeta.setRoot(root);
}
Also used : OMEXMLMetadataRoot(ome.xml.meta.OMEXMLMetadataRoot) StructuredAnnotations(ome.xml.model.StructuredAnnotations) OriginalMetadataAnnotation(loci.formats.meta.OriginalMetadataAnnotation)

Example 5 with OMEXMLMetadataRoot

use of ome.xml.meta.OMEXMLMetadataRoot in project bioformats by openmicroscopy.

the class OMEXMLServiceImpl method createRoot.

/**
 * Constructs an OME root node. <b>NOTE:</b> This method is mostly here to
 * ensure type safety of return values as instances of service dependency
 * classes should not leak out of the interface.
 * @param xml String of XML to create the root node from.
 * @return An ome.xml.model.OMEModelObject subclass root node.
 * @throws IOException If there is an error reading from the string.
 * @throws SAXException If there is an error parsing the XML.
 * @throws ParserConfigurationException If there is an error preparing the
 * parsing infrastructure.
 */
private OMEXMLMetadataRoot createRoot(String xml) throws ServiceException {
    try {
        OMEModel model = new OMEModelImpl();
        OMEXMLMetadataRoot ome = new OMEXMLMetadataRoot(XMLTools.parseDOM(xml).getDocumentElement(), model);
        model.resolveReferences();
        return ome;
    } catch (Exception e) {
        throw new ServiceException(e);
    }
}
Also used : ServiceException(loci.common.services.ServiceException) OMEXMLMetadataRoot(ome.xml.meta.OMEXMLMetadataRoot) OMEModel(ome.xml.model.OMEModel) OMEModelImpl(ome.xml.model.OMEModelImpl) ServiceException(loci.common.services.ServiceException) SAXException(org.xml.sax.SAXException) TransformerException(javax.xml.transform.TransformerException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

OMEXMLMetadataRoot (ome.xml.meta.OMEXMLMetadataRoot)27 ServiceException (loci.common.services.ServiceException)11 Image (ome.xml.model.Image)11 IOException (java.io.IOException)8 FormatException (loci.formats.FormatException)8 OMEXMLMetadata (loci.formats.ome.OMEXMLMetadata)7 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)6 DependencyException (loci.common.services.DependencyException)6 ServiceFactory (loci.common.services.ServiceFactory)6 OMEXMLService (loci.formats.services.OMEXMLService)6 Pixels (ome.xml.model.Pixels)6 Location (loci.common.Location)5 OriginalMetadataAnnotation (loci.formats.meta.OriginalMetadataAnnotation)5 Document (org.w3c.dom.Document)5 Node (org.w3c.dom.Node)5 NodeList (org.w3c.dom.NodeList)5 SAXException (org.xml.sax.SAXException)5 StructuredAnnotations (ome.xml.model.StructuredAnnotations)4 XMLAnnotation (ome.xml.model.XMLAnnotation)4 PositiveInteger (ome.xml.model.primitives.PositiveInteger)4