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);
}
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());
}
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());
}
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());
}
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);
}
Aggregations