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