use of org.apache.drill.exec.store.image.ImageBatchReader.ColumnDefn in project drill by apache.
the class ImageDirectoryProcessor method processXmpDirectory.
protected static void processXmpDirectory(final MapColumnDefn writer, final XmpDirectory directory) {
XMPMeta xmpMeta = directory.getXMPMeta();
if (xmpMeta != null) {
try {
IteratorOptions iteratorOptions = new IteratorOptions().setJustLeafnodes(true);
for (final XMPIterator i = xmpMeta.iterator(iteratorOptions); i.hasNext(); ) {
try {
XMPPropertyInfo prop = (XMPPropertyInfo) i.next();
String path = prop.getPath();
String value = prop.getValue();
if (path != null && value != null) {
// handling lang-alt array items
if (prop.getOptions().getHasLanguage()) {
XMPPropertyInfo langProp = (XMPPropertyInfo) i.next();
if (langProp.getPath().endsWith("/xml:lang")) {
String lang = langProp.getValue();
path = path.replaceFirst("\\[\\d+\\]$", "") + (lang.equals("x-default") ? "" : "_" + lang);
}
}
ColumnDefn rootColumn = writer;
ColumnWriter subColumn = null;
String[] elements = path.replaceAll("/\\w+:", "/").split(":|/|(?=\\[)");
// 1. lookup and create nested structure
for (int j = 1; j < elements.length; j++) {
String parent = elements[j - 1];
boolean isList = elements[j].startsWith("[");
if (!parent.startsWith("[")) {
// skipped. such as parent is [1] but not the last element
final String formatName = ImageMetadataUtils.formatName(parent);
if (isList) {
if (j + 1 == elements.length) {
// for list
subColumn = rootColumn.addList(formatName);
} else {
// for list-map
subColumn = rootColumn.addListMap(formatName);
}
rootColumn = new ListColumnDefn(formatName).builder((ArrayWriter) subColumn);
} else {
// for map
subColumn = ((MapColumnDefn) rootColumn).addMap(formatName);
// set up the current writer in nested structure
rootColumn = new MapColumnDefn(formatName).builder((TupleWriter) subColumn);
}
}
}
// 2. set up the value for writer
String parent = elements[elements.length - 1];
if (parent.startsWith("[")) {
subColumn.setObject(new String[] { value });
} else {
rootColumn.addText(ImageMetadataUtils.formatName(parent)).setString(value);
if (subColumn instanceof ArrayWriter) {
((ArrayWriter) subColumn).save();
}
}
}
} catch (Exception skipped) {
// simply skip this property
logger.warn("Error in written xmp metadata : {}", skipped.getMessage());
}
}
} catch (XMPException ignored) {
logger.warn("Error in processing xmp directory : {}", ignored.getMessage());
}
}
}
Aggregations