Search in sources :

Example 6 with XMLAnnotation

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

the class OMEXMLServiceImpl method getModuloAlong.

/**
 * Create a {@link loci.formats.Modulo} corresponding to the given ModuloAlong* tag.
 * @param omexml the OMEXMLMetadata from which to retrieve the ModuloAlong* tag
 * @param tag the tag name (e.g. "ModuloAlongC")
 * @param image the Image index within the OMEXMLMetadata
 * @return the corresponding Modulo object
 */
private Modulo getModuloAlong(OMEXMLMetadata omexml, String tag, int image) {
    OMEXMLMetadataRoot root = (OMEXMLMetadataRoot) omexml.getRoot();
    Image img = root.getImage(image);
    if (img == null) {
        return null;
    }
    for (int i = 0; i < img.sizeOfLinkedAnnotationList(); i++) {
        Annotation annotation = img.getLinkedAnnotation(i);
        if (!(annotation instanceof XMLAnnotation)) {
            continue;
        }
        String xml = ((XMLAnnotation) annotation).getValue();
        try {
            Document annotationRoot = XMLTools.parseDOM(xml);
            NodeList nodes = annotationRoot.getElementsByTagName(tag);
            if (nodes.getLength() > 0) {
                Element modulo = (Element) nodes.item(0);
                NamedNodeMap attrs = modulo.getAttributes();
                Modulo m = new Modulo(tag.substring(tag.length() - 1));
                Node start = attrs.getNamedItem("Start");
                Node end = attrs.getNamedItem("End");
                Node step = attrs.getNamedItem("Step");
                Node type = attrs.getNamedItem("Type");
                Node typeDescription = attrs.getNamedItem("TypeDescription");
                Node unit = attrs.getNamedItem("Unit");
                if (start != null) {
                    m.start = Double.parseDouble(start.getNodeValue());
                }
                if (end != null) {
                    m.end = Double.parseDouble(end.getNodeValue());
                }
                if (step != null) {
                    m.step = Double.parseDouble(step.getNodeValue());
                }
                if (type != null) {
                    m.type = type.getNodeValue();
                }
                if (typeDescription != null) {
                    m.typeDescription = typeDescription.getNodeValue();
                }
                if (unit != null) {
                    m.unit = unit.getNodeValue();
                }
                NodeList labels = modulo.getElementsByTagName("Label");
                if (labels != null && labels.getLength() > 0) {
                    m.labels = new String[labels.getLength()];
                    for (int q = 0; q < labels.getLength(); q++) {
                        m.labels[q] = labels.item(q).getTextContent();
                    }
                }
                return m;
            }
        } catch (ParserConfigurationException e) {
            LOGGER.debug("Failed to parse ModuloAlong", e);
        } catch (SAXException e) {
            LOGGER.debug("Failed to parse ModuloAlong", e);
        } catch (IOException e) {
            LOGGER.debug("Failed to parse ModuloAlong", e);
        }
    }
    return null;
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) Modulo(loci.formats.Modulo) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) IOException(java.io.IOException) Image(ome.xml.model.Image) Document(org.w3c.dom.Document) 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) OMEXMLMetadataRoot(ome.xml.meta.OMEXMLMetadataRoot) XMLAnnotation(ome.xml.model.XMLAnnotation) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 7 with XMLAnnotation

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

Example 8 with XMLAnnotation

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

the class InOutCurrentTest method testValidChannelAnnotation.

@Test(dependsOnMethods = { "testValidChannelNode" })
public void testValidChannelAnnotation() {
    Channel c = ome.getImage(0).getPixels().getChannel(0);
    Annotation n = c.getLinkedAnnotation(0);
    assertNotNull(n);
    assertTrue(n instanceof XMLAnnotation);
    assertEquals(CHANNEL_ANNOTATION_ID, n.getID());
    assertEquals(n.getNamespace(), GENERAL_ANNOTATION_NAMESPACE);
    assertEquals(CHANNEL_ANNOTATION_VALUE, ((XMLAnnotation) n).getValue());
}
Also used : Channel(ome.xml.model.Channel) XMLAnnotation(ome.xml.model.XMLAnnotation) ListAnnotation(ome.xml.model.ListAnnotation) CommentAnnotation(ome.xml.model.CommentAnnotation) Annotation(ome.xml.model.Annotation) LongAnnotation(ome.xml.model.LongAnnotation) TimestampAnnotation(ome.xml.model.TimestampAnnotation) XMLAnnotation(ome.xml.model.XMLAnnotation) BooleanAnnotation(ome.xml.model.BooleanAnnotation) Test(org.testng.annotations.Test)

Aggregations

XMLAnnotation (ome.xml.model.XMLAnnotation)8 Image (ome.xml.model.Image)5 IOException (java.io.IOException)4 OriginalMetadataAnnotation (loci.formats.meta.OriginalMetadataAnnotation)4 OMEXMLMetadataRoot (ome.xml.meta.OMEXMLMetadataRoot)4 Annotation (ome.xml.model.Annotation)4 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)3 Channel (ome.xml.model.Channel)3 StructuredAnnotations (ome.xml.model.StructuredAnnotations)3 Test (org.testng.annotations.Test)3 Document (org.w3c.dom.Document)3 Node (org.w3c.dom.Node)3 NodeList (org.w3c.dom.NodeList)3 SAXException (org.xml.sax.SAXException)3 Hashtable (java.util.Hashtable)2 ModuloAnnotation (loci.formats.meta.ModuloAnnotation)2 OMEXMLMetadata (loci.formats.ome.OMEXMLMetadata)2 BooleanAnnotation (ome.xml.model.BooleanAnnotation)2 ListAnnotation (ome.xml.model.ListAnnotation)2 Pixels (ome.xml.model.Pixels)2