use of loci.formats.meta.OriginalMetadataAnnotation in project bioformats by openmicroscopy.
the class OMEXMLServiceImpl method populateOriginalMetadata.
/**
* @see OMEXMLService#populateOriginalMetadata(loci.formats.ome.OMEXMLMetadata, Hashtable)
*/
@Override
public void populateOriginalMetadata(OMEXMLMetadata omexmlMeta, Hashtable<String, Object> metadata) {
omexmlMeta.resolveReferences();
if (metadata.size() == 0) {
return;
}
OMEXMLMetadataRoot root = (OMEXMLMetadataRoot) omexmlMeta.getRoot();
StructuredAnnotations annotations = root.getStructuredAnnotations();
if (annotations == null)
annotations = new StructuredAnnotations();
int annotationIndex = annotations.sizeOfXMLAnnotationList();
if (annotationIndex > 0) {
String lastAnnotationID = omexmlMeta.getXMLAnnotationID(annotationIndex - 1);
String lastIndex = lastAnnotationID.substring(lastAnnotationID.lastIndexOf(":") + 1);
try {
int index = Integer.parseInt(lastIndex);
while (index >= annotationIndex) {
annotationIndex++;
}
} catch (NumberFormatException e) {
}
}
for (String key : metadata.keySet()) {
OriginalMetadataAnnotation annotation = new OriginalMetadataAnnotation();
annotation.setID(MetadataTools.createLSID("Annotation", annotationIndex));
annotation.setKeyValue(key, metadata.get(key).toString());
annotations.addXMLAnnotation(annotation);
annotationIndex++;
}
root.setStructuredAnnotations(annotations);
omexmlMeta.setRoot(root);
}
use of loci.formats.meta.OriginalMetadataAnnotation in project bioformats by openmicroscopy.
the class OMEXMLServiceImpl method populateOriginalMetadata.
/**
* @see OMEXMLService#populateOriginalMetadata(loci.formats.ome.OMEXMLMetadata, java.lang.String, java.lang.String)
*/
@Override
public void populateOriginalMetadata(OMEXMLMetadata omexmlMeta, String key, String value) {
omexmlMeta.resolveReferences();
OMEXMLMetadataRoot root = (OMEXMLMetadataRoot) omexmlMeta.getRoot();
StructuredAnnotations annotations = root.getStructuredAnnotations();
if (annotations == null)
annotations = new StructuredAnnotations();
int annotationIndex = annotations.sizeOfXMLAnnotationList();
if (annotationIndex > 0) {
String lastAnnotationID = omexmlMeta.getXMLAnnotationID(annotationIndex - 1);
String lastIndex = lastAnnotationID.substring(lastAnnotationID.lastIndexOf(":") + 1);
try {
int index = Integer.parseInt(lastIndex);
while (index >= annotationIndex) {
annotationIndex++;
}
} catch (NumberFormatException e) {
}
}
OriginalMetadataAnnotation annotation = new OriginalMetadataAnnotation();
annotation.setID(MetadataTools.createLSID("Annotation", annotationIndex));
annotation.setKeyValue(key, value);
annotations.addXMLAnnotation(annotation);
root.setStructuredAnnotations(annotations);
omexmlMeta.setRoot(root);
}
use of loci.formats.meta.OriginalMetadataAnnotation 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 loci.formats.meta.OriginalMetadataAnnotation 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