Search in sources :

Example 1 with Annotation

use of ome.xml.model.Annotation 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 2 with Annotation

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

the class InOutCurrentTest method testValidInstrumentAnnotation.

@Test(dependsOnMethods = { "testValidInstrumentNode" })
public void testValidInstrumentAnnotation() {
    Annotation n = ome.getInstrument(0).getLinkedAnnotation(0);
    assertNotNull(n);
    assertEquals(INSTRUMENT_ANNOTATION_ID, n.getID());
    assertEquals(n.getNamespace(), GENERAL_ANNOTATION_NAMESPACE);
    assertTrue(n instanceof CommentAnnotation);
    CommentAnnotation string = (CommentAnnotation) n;
    assertEquals(INSTRUMENT_ANNOTATION_VALUE, string.getValue());
}
Also used : CommentAnnotation(ome.xml.model.CommentAnnotation) 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)

Example 3 with Annotation

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

the class InOutCurrentTest method testValidROIAnnotation.

@Test(dependsOnMethods = { "testValidROINode" })
public void testValidROIAnnotation() {
    Annotation n = ome.getROI(0).getLinkedAnnotation(0);
    assertNotNull(n);
    assertEquals(ROI_ANNOTATION_ID, n.getID());
    assertEquals(n.getNamespace(), GENERAL_ANNOTATION_NAMESPACE);
    assertTrue(n instanceof CommentAnnotation);
    CommentAnnotation string = (CommentAnnotation) n;
    assertEquals(ROI_ANNOTATION_VALUE, string.getValue());
}
Also used : CommentAnnotation(ome.xml.model.CommentAnnotation) 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)

Example 4 with Annotation

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

the class InOutCurrentTest method testValidDichroicAnnotation.

@Test(dependsOnMethods = { "testValidDichroicNode" })
public void testValidDichroicAnnotation() {
    Annotation n = ome.getInstrument(0).getDichroic(0).getLinkedAnnotation(0);
    assertNotNull(n);
    assertEquals(DICHROIC_ANNOTATION_ID, n.getID());
    assertEquals(n.getNamespace(), GENERAL_ANNOTATION_NAMESPACE);
    assertTrue(n instanceof CommentAnnotation);
    CommentAnnotation string = (CommentAnnotation) n;
    assertEquals(DICHROIC_ANNOTATION_VALUE, string.getValue());
}
Also used : CommentAnnotation(ome.xml.model.CommentAnnotation) 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)

Example 5 with Annotation

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

the class InOutCurrentTest method testValidImageAnnotation.

@Test(dependsOnMethods = { "testValidImageNode" })
public void testValidImageAnnotation() {
    Annotation n = ome.getImage(0).getLinkedAnnotation(0);
    assertNotNull(n);
    assertEquals(ListAnnotation.class, n.getClass());
    ListAnnotation l = (ListAnnotation) n;
    assertEquals(l.getID(), IMAGE_LIST_ANNOTATION_ID);
    assertEquals(l.getNamespace(), GENERAL_ANNOTATION_NAMESPACE);
    Annotation n2 = l.getLinkedAnnotation(0);
    assertEquals(BooleanAnnotation.class, n2.getClass());
    BooleanAnnotation b = (BooleanAnnotation) n2;
    assertEquals(b.getValue(), IMAGE_ANNOTATION_VALUE);
    assertEquals(b.getNamespace(), GENERAL_ANNOTATION_NAMESPACE);
    assertEquals(b.getID(), IMAGE_ANNOTATION_ID);
}
Also used : BooleanAnnotation(ome.xml.model.BooleanAnnotation) ListAnnotation(ome.xml.model.ListAnnotation) 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

Annotation (ome.xml.model.Annotation)17 XMLAnnotation (ome.xml.model.XMLAnnotation)17 Test (org.testng.annotations.Test)15 BooleanAnnotation (ome.xml.model.BooleanAnnotation)14 CommentAnnotation (ome.xml.model.CommentAnnotation)14 ListAnnotation (ome.xml.model.ListAnnotation)14 LongAnnotation (ome.xml.model.LongAnnotation)14 TimestampAnnotation (ome.xml.model.TimestampAnnotation)14 Image (ome.xml.model.Image)3 IOException (java.io.IOException)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 ModuloAnnotation (loci.formats.meta.ModuloAnnotation)2 OriginalMetadataAnnotation (loci.formats.meta.OriginalMetadataAnnotation)2 OMEXMLMetadataRoot (ome.xml.meta.OMEXMLMetadataRoot)2 Channel (ome.xml.model.Channel)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 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)1