use of ome.xml.model.StructuredAnnotations in project bioformats by openmicroscopy.
the class MapAnnotationTest method setUp.
@BeforeClass
public void setUp() throws Exception {
// Add an Image/Pixels
Image image = new Image();
image.setID("Image:0");
Pixels pixels = new Pixels();
pixels.setID("Pixels:0");
image.setPixels(pixels);
// Add a Map Annotation
List<MapPair> map = new ArrayList<MapPair>();
map.add(new MapPair("a", "1"));
map.add(new MapPair("d", "2"));
map.add(new MapPair("c", "3"));
map.add(new MapPair("b", "4"));
map.add(new MapPair("e", "5"));
map.add(new MapPair("c", "6"));
MapAnnotation mapAnnotation = new MapAnnotation();
mapAnnotation.setID("Annotation:0");
mapAnnotation.setValue(map);
StructuredAnnotations structuredAnnotations = new StructuredAnnotations();
structuredAnnotations.addMapAnnotation(mapAnnotation);
ome.setStructuredAnnotations(structuredAnnotations);
image.linkAnnotation(mapAnnotation);
ome.addImage(image);
}
use of ome.xml.model.StructuredAnnotations 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;
}
use of ome.xml.model.StructuredAnnotations 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());
}
Aggregations