use of ome.xml.meta.OMEXMLMetadataRoot 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 ome.xml.meta.OMEXMLMetadataRoot 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.meta.OMEXMLMetadataRoot in project bioformats by openmicroscopy.
the class OMEXMLServiceImpl method createOMEXMLMetadata.
/**
* @see OMEXMLService#createOMEXMLMetadata(java.lang.String, java.lang.String)
*/
@Override
public OMEXMLMetadata createOMEXMLMetadata(String xml, String version) throws ServiceException {
if (xml != null) {
xml = XMLTools.sanitizeXML(xml);
}
OMEXMLMetadataRoot ome = xml == null ? null : createRoot(transformToLatestVersion(xml));
OMEXMLMetadata meta = new OMEXMLMetadataImpl();
if (ome != null)
meta.setRoot(ome);
return meta;
}
use of ome.xml.meta.OMEXMLMetadataRoot 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 ome.xml.meta.OMEXMLMetadataRoot in project bioformats by openmicroscopy.
the class OMEXMLServiceImpl method createRoot.
/**
* Constructs an OME root node. <b>NOTE:</b> This method is mostly here to
* ensure type safety of return values as instances of service dependency
* classes should not leak out of the interface.
* @param xml String of XML to create the root node from.
* @return An ome.xml.model.OMEModelObject subclass root node.
* @throws IOException If there is an error reading from the string.
* @throws SAXException If there is an error parsing the XML.
* @throws ParserConfigurationException If there is an error preparing the
* parsing infrastructure.
*/
private OMEXMLMetadataRoot createRoot(String xml) throws ServiceException {
try {
OMEModel model = new OMEModelImpl();
OMEXMLMetadataRoot ome = new OMEXMLMetadataRoot(XMLTools.parseDOM(xml).getDocumentElement(), model);
model.resolveReferences();
return ome;
} catch (Exception e) {
throw new ServiceException(e);
}
}
Aggregations