Search in sources :

Example 1 with XMLAnnotation

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

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

the class ObjectBasedOMEModelMock method makeImage.

private Image makeImage() {
    // Create <Image/>
    Image image = new Image();
    image.setID(InOutCurrentTest.IMAGE_ID);
    ListAnnotation listAnnotation = new ListAnnotation();
    listAnnotation.setID(InOutCurrentTest.IMAGE_LIST_ANNOTATION_ID);
    listAnnotation.setNamespace(InOutCurrentTest.GENERAL_ANNOTATION_NAMESPACE);
    annotations.addListAnnotation(listAnnotation);
    BooleanAnnotation annotation = new BooleanAnnotation();
    annotation.setID(InOutCurrentTest.IMAGE_ANNOTATION_ID);
    annotation.setValue(InOutCurrentTest.IMAGE_ANNOTATION_VALUE);
    annotation.setNamespace(InOutCurrentTest.GENERAL_ANNOTATION_NAMESPACE);
    listAnnotation.linkAnnotation(annotation);
    image.linkAnnotation(listAnnotation);
    annotations.addBooleanAnnotation(annotation);
    // Create <Pixels/>
    Pixels pixels = new Pixels();
    pixels.setID(InOutCurrentTest.PIXELS_ID);
    pixels.setSizeX(new PositiveInteger(InOutCurrentTest.SIZE_X));
    pixels.setSizeY(new PositiveInteger(InOutCurrentTest.SIZE_Y));
    pixels.setSizeZ(new PositiveInteger(InOutCurrentTest.SIZE_Z));
    pixels.setSizeC(new PositiveInteger(InOutCurrentTest.SIZE_C));
    pixels.setSizeT(new PositiveInteger(InOutCurrentTest.SIZE_T));
    pixels.setDimensionOrder(InOutCurrentTest.DIMENSION_ORDER);
    pixels.setType(InOutCurrentTest.PIXEL_TYPE);
    // Create <TiffData/>
    TiffData tiffData = new TiffData();
    // Create <UUID/>
    UUID uuid = new UUID();
    uuid.setValue(InOutCurrentTest.TIFF_DATA_UUID);
    tiffData.setUUID(uuid);
    pixels.addTiffData(tiffData);
    // Create <Channel/> under <Pixels/>
    for (int i = 0; i < InOutCurrentTest.SIZE_C; i++) {
        Channel channel = new Channel();
        channel.setID("Channel:" + i);
        if (i == 0) {
            XMLAnnotation channelAnnotation = new XMLAnnotation();
            channelAnnotation.setID(InOutCurrentTest.CHANNEL_ANNOTATION_ID);
            channelAnnotation.setValue(InOutCurrentTest.CHANNEL_ANNOTATION_VALUE);
            channelAnnotation.setNamespace(InOutCurrentTest.GENERAL_ANNOTATION_NAMESPACE);
            channel.linkAnnotation(channelAnnotation);
            annotations.addXMLAnnotation(channelAnnotation);
        }
        pixels.addChannel(channel);
    }
    // Put <Pixels/> under <Image/>
    image.setPixels(pixels);
    return image;
}
Also used : BooleanAnnotation(ome.xml.model.BooleanAnnotation) PositiveInteger(ome.xml.model.primitives.PositiveInteger) TiffData(ome.xml.model.TiffData) Channel(ome.xml.model.Channel) ListAnnotation(ome.xml.model.ListAnnotation) XMLAnnotation(ome.xml.model.XMLAnnotation) Image(ome.xml.model.Image) UUID(ome.xml.model.UUID) Pixels(ome.xml.model.Pixels)

Example 3 with XMLAnnotation

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

the class XMLAnnotationTest method testValidXMLAnnotation.

@Test
public void testValidXMLAnnotation() throws EnumerationException {
    assertNotNull(ome);
    assertEquals(1, ome.sizeOfImageList());
    Image image = ome.getImage(0);
    Pixels pixels = image.getPixels();
    assertNotNull(pixels);
    assertEquals(3, pixels.sizeOfChannelList());
    Channel channel = pixels.getChannel(0);
    assertEquals(1, channel.sizeOfLinkedAnnotationList());
    Annotation annotation = channel.getLinkedAnnotation(0);
    assertEquals(XMLAnnotation.class, annotation.getClass());
    String annotationValue = ((XMLAnnotation) annotation).getValue();
    // normalize line endings if the test is run on Windows
    annotationValue = annotationValue.replaceAll("\r\n", "\n");
    assertEquals("<TestData>\n                    <key>foo</key>\n\t\t\t\t\t<value>bar</value>\n                </TestData>", annotationValue);
}
Also used : Channel(ome.xml.model.Channel) XMLAnnotation(ome.xml.model.XMLAnnotation) Image(ome.xml.model.Image) Pixels(ome.xml.model.Pixels) XMLAnnotation(ome.xml.model.XMLAnnotation) Annotation(ome.xml.model.Annotation) Test(org.testng.annotations.Test)

Example 4 with XMLAnnotation

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

the class FileWriteSPW method cleanup.

/**
 * Close the file writer.
 */
public void cleanup() {
    // No of planes expected for each image = 1 if not FLIM
    int validPlanes = 1;
    if (delays != null) {
        validPlanes = sizet;
    }
    OMEXMLMetadataRoot root = (OMEXMLMetadataRoot) omexml.getRoot();
    Plate plate = root.getPlate(0);
    StructuredAnnotations anns = root.getStructuredAnnotations();
    ArrayList<Image> invalidImages = new ArrayList<>();
    // if not record those images as being invalid
    for (int i = 0; i < expectedImages.length; i++) {
        if (expectedImages[i] < validPlanes) {
            Image im = root.getImage(i);
            invalidImages.add(im);
            // remove modulo Annotation if FLIM
            if (delays != null) {
                XMLAnnotation ann = (XMLAnnotation) im.getLinkedAnnotation(0);
                anns.removeXMLAnnotation(ann);
            }
        }
    }
    // Now remove all limked wellSnmples and then invalid images
    for (int i = 0; i < invalidImages.size(); i++) {
        Image im = invalidImages.get(i);
        List<WellSample> list = im.copyLinkedWellSampleList();
        if (!list.isEmpty()) {
            WellSample wellSample = im.getLinkedWellSample(0);
            Well well = wellSample.getWell();
            well.removeWellSample(wellSample);
        }
        root.removeImage(im);
    }
    if (writer != null) {
        try {
            writer.close();
        } catch (IOException e) {
            System.err.println("Failed to close file writer.");
        }
    }
}
Also used : OMEXMLMetadataRoot(ome.xml.meta.OMEXMLMetadataRoot) Well(ome.xml.model.Well) ArrayList(java.util.ArrayList) StructuredAnnotations(ome.xml.model.StructuredAnnotations) XMLAnnotation(ome.xml.model.XMLAnnotation) WellSample(ome.xml.model.WellSample) IOException(java.io.IOException) Image(ome.xml.model.Image) Plate(ome.xml.model.Plate)

Example 5 with XMLAnnotation

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

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