Search in sources :

Example 1 with OriginalMetadataAnnotation

use of loci.formats.meta.OriginalMetadataAnnotation 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 OriginalMetadataAnnotation

use of loci.formats.meta.OriginalMetadataAnnotation 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 3 with OriginalMetadataAnnotation

use of loci.formats.meta.OriginalMetadataAnnotation in project bioformats by openmicroscopy.

the class OMEXMLServiceImpl method getOriginalMetadata.

/**
 * @see OMEXMLService#getOriginalMetadata(loci.formats.ome.OMEXMLMetadata)
 */
@Override
public Hashtable getOriginalMetadata(OMEXMLMetadata omexmlMeta) {
    OMEXMLMetadataRoot root = (OMEXMLMetadataRoot) omexmlMeta.getRoot();
    StructuredAnnotations annotations = root.getStructuredAnnotations();
    if (annotations == null) {
        return null;
    }
    Hashtable metadata = new Hashtable();
    for (int i = 0; i < annotations.sizeOfXMLAnnotationList(); i++) {
        XMLAnnotation annotation = annotations.getXMLAnnotation(i);
        if (annotation instanceof OriginalMetadataAnnotation) {
            OriginalMetadataAnnotation original = (OriginalMetadataAnnotation) annotation;
            metadata.put(original.getKey(), original.getValueForKey());
            continue;
        }
        String xml = annotation.getValue();
        try {
            Document annotationRoot = XMLTools.parseDOM(xml);
            NodeList metadataNodes = annotationRoot.getElementsByTagName("OriginalMetadata");
            for (int meta = 0; meta < metadataNodes.getLength(); meta++) {
                Element metadataNode = (Element) metadataNodes.item(meta);
                NodeList keys = metadataNode.getElementsByTagName("Key");
                NodeList values = metadataNode.getElementsByTagName("Value");
                for (int q = 0; q < keys.getLength(); q++) {
                    Node key = keys.item(q);
                    Node value = values.item(q);
                    metadata.put(key.getTextContent(), value.getTextContent());
                }
            }
            if (metadataNodes.getLength() == 0) {
                metadataNodes = annotationRoot.getDocumentElement().getChildNodes();
                for (int meta = 0; meta < metadataNodes.getLength(); meta++) {
                    // com.sun.org.apache.xerces.internal.dom.DeferredCommentImpl.
                    if (metadataNodes.item(meta) instanceof Element) {
                        Element node = (Element) metadataNodes.item(meta);
                        String name = node.getNodeName();
                        NamedNodeMap attrs = node.getAttributes();
                        Node value = attrs.getNamedItem("Value");
                        if (value != null) {
                            metadata.put(name, value.getNodeValue());
                        }
                    }
                }
            }
        } catch (ParserConfigurationException e) {
            LOGGER.debug("Failed to parse OriginalMetadata", e);
        } catch (SAXException e) {
            LOGGER.debug("Failed to parse OriginalMetadata", e);
        } catch (IOException e) {
            LOGGER.debug("Failed to parse OriginalMetadata", e);
        }
    }
    return metadata;
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) Hashtable(java.util.Hashtable) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) StructuredAnnotations(ome.xml.model.StructuredAnnotations) IOException(java.io.IOException) OriginalMetadataAnnotation(loci.formats.meta.OriginalMetadataAnnotation) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) OMEXMLMetadataRoot(ome.xml.meta.OMEXMLMetadataRoot) XMLAnnotation(ome.xml.model.XMLAnnotation) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 4 with OriginalMetadataAnnotation

use of loci.formats.meta.OriginalMetadataAnnotation in project bioformats by openmicroscopy.

the class OMEXMLServiceTest method testOriginalMetadata.

@Test
public void testOriginalMetadata() throws ServiceException {
    OMEXMLMetadata metadata = service.createOMEXMLMetadata();
    service.populateOriginalMetadata(metadata, "testKey", "testValue");
    Hashtable metadataTable = service.getOriginalMetadata(metadata);
    assertEquals(metadataTable.size(), 1);
    assertTrue("testValue".equals(metadataTable.get("testKey")));
    OME root = (OME) metadata.getRoot();
    StructuredAnnotations annotations = root.getStructuredAnnotations();
    assertEquals(annotations.sizeOfXMLAnnotationList(), 1);
    XMLAnnotation xmlAnn = annotations.getXMLAnnotation(0);
    String txt = "<OriginalMetadata><Key>testKey</Key><Value>testValue</Value></OriginalMetadata>";
    assertEquals(txt, xmlAnn.getValue());
    OriginalMetadataAnnotation omAnn = (OriginalMetadataAnnotation) xmlAnn;
    assertEquals("testValue", omAnn.getValueForKey());
}
Also used : OMEXMLMetadata(loci.formats.ome.OMEXMLMetadata) Hashtable(java.util.Hashtable) OME(ome.xml.model.OME) StructuredAnnotations(ome.xml.model.StructuredAnnotations) XMLAnnotation(ome.xml.model.XMLAnnotation) OriginalMetadataAnnotation(loci.formats.meta.OriginalMetadataAnnotation) Test(org.testng.annotations.Test)

Aggregations

OriginalMetadataAnnotation (loci.formats.meta.OriginalMetadataAnnotation)4 StructuredAnnotations (ome.xml.model.StructuredAnnotations)4 OMEXMLMetadataRoot (ome.xml.meta.OMEXMLMetadataRoot)3 Hashtable (java.util.Hashtable)2 XMLAnnotation (ome.xml.model.XMLAnnotation)2 IOException (java.io.IOException)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 OMEXMLMetadata (loci.formats.ome.OMEXMLMetadata)1 OME (ome.xml.model.OME)1 Test (org.testng.annotations.Test)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1 NamedNodeMap (org.w3c.dom.NamedNodeMap)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1 SAXException (org.xml.sax.SAXException)1