Search in sources :

Example 6 with StructuredAnnotations

use of ome.xml.model.StructuredAnnotations in project bioformats by openmicroscopy.

the class MapAnnotationTest method setUp.

@BeforeClass
public void setUp() throws Exception {
    // Add an Image/Pixels
    Image image = new Image();
    image.setID("Image:0");
    Pixels pixels = new Pixels();
    pixels.setID("Pixels:0");
    image.setPixels(pixels);
    // Add a Map Annotation
    List<MapPair> map = new ArrayList<MapPair>();
    map.add(new MapPair("a", "1"));
    map.add(new MapPair("d", "2"));
    map.add(new MapPair("c", "3"));
    map.add(new MapPair("b", "4"));
    map.add(new MapPair("e", "5"));
    map.add(new MapPair("c", "6"));
    MapAnnotation mapAnnotation = new MapAnnotation();
    mapAnnotation.setID("Annotation:0");
    mapAnnotation.setValue(map);
    StructuredAnnotations structuredAnnotations = new StructuredAnnotations();
    structuredAnnotations.addMapAnnotation(mapAnnotation);
    ome.setStructuredAnnotations(structuredAnnotations);
    image.linkAnnotation(mapAnnotation);
    ome.addImage(image);
}
Also used : MapPair(ome.xml.model.MapPair) MapAnnotation(ome.xml.model.MapAnnotation) ArrayList(java.util.ArrayList) StructuredAnnotations(ome.xml.model.StructuredAnnotations) Image(ome.xml.model.Image) Pixels(ome.xml.model.Pixels) BeforeClass(org.testng.annotations.BeforeClass)

Example 7 with StructuredAnnotations

use of ome.xml.model.StructuredAnnotations 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 8 with StructuredAnnotations

use of ome.xml.model.StructuredAnnotations 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

StructuredAnnotations (ome.xml.model.StructuredAnnotations)8 OMEXMLMetadataRoot (ome.xml.meta.OMEXMLMetadataRoot)5 OriginalMetadataAnnotation (loci.formats.meta.OriginalMetadataAnnotation)4 IOException (java.io.IOException)3 Image (ome.xml.model.Image)3 XMLAnnotation (ome.xml.model.XMLAnnotation)3 ArrayList (java.util.ArrayList)2 Hashtable (java.util.Hashtable)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 Test (org.testng.annotations.Test)2 Document (org.w3c.dom.Document)2 Node (org.w3c.dom.Node)2 NodeList (org.w3c.dom.NodeList)2 SAXException (org.xml.sax.SAXException)2 HashSet (java.util.HashSet)1 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)1 TransformerException (javax.xml.transform.TransformerException)1 ServiceException (loci.common.services.ServiceException)1 OMEXMLMetadata (loci.formats.ome.OMEXMLMetadata)1 CommentAnnotation (ome.xml.model.CommentAnnotation)1