Tundraには、まさに必要なことを行うサービスがありますtundra.document:get($document, $key)
。
$document
com.wm.data.IData
値を抽出するドキュメントです
$key
抽出するキーの名前であり、完全修飾して配列インデックスを含めることができます。たとえば、"path/to/my[1]/key"
関連する Java コードは次のとおりです。
public static final void get(IData pipeline) throws ServiceException {
IDataCursor cursor = pipeline.getCursor();
try {
IData document = IDataUtil.getIData(cursor, "$document");
String key = IDataUtil.getString(cursor, "$key");
IDataUtil.put(cursor, "$value", get(document, key));
} finally {
cursor.destroy();
}
}
// wrapper class for fully qualified IData keys
public static class Key {
public static final String SEPARATOR = "/";
public static final java.util.regex.Pattern INDEX_PATTERN = java.util.regex.Pattern.compile("\\[(-?\\d+?)\\]$");
protected boolean hasIndex = false;
protected int index = 0;
protected String key = null;
public Key(String key) {
java.util.regex.Matcher matcher = INDEX_PATTERN.matcher(key);
StringBuffer buffer = new StringBuffer();
while(matcher.find()) {
hasIndex = true;
index = Integer.parseInt(matcher.group(1));
matcher.appendReplacement(buffer, "");
}
matcher.appendTail(buffer);
this.key = buffer.toString();
}
public boolean hasIndex() {
return hasIndex;
}
public int getIndex() {
return index;
}
public String toString() {
return key;
}
public static java.util.Queue<Key> parse(String key) {
String[] parts = key.split(SEPARATOR);
java.util.Queue<Key> queue = new java.util.ArrayDeque<Key>(parts.length);
for (int i = 0; i < parts.length; i++) {
queue.add(new Key(parts[i]));
}
return queue;
}
public static boolean isFullyQualified(String key) {
return key != null && (key.contains(SEPARATOR) || INDEX_PATTERN.matcher(key).find());
}
}
// returns the value associated with the given key from the given IData document
public static Object get(IData input, String key) {
Object value = null;
if (input != null && key != null) {
// try finding a value that matches the literal key
IDataCursor cursor = input.getCursor();
try {
value = IDataUtil.get(cursor, key);
} finally {
cursor.destroy();
}
// if value wasn't found using the literal key, the key could be fully qualified
if (value == null && Key.isFullyQualified(key)) value = get(input, Key.parse(key));
}
return value;
}
// gets a value from an IData document with a fully qualified key
protected static Object get(IData input, java.util.Queue<Key> keys) {
Object value = null;
if (input != null && keys != null && keys.size() > 0) {
IDataCursor cursor = input.getCursor();
Key key = keys.remove();
if (keys.size() > 0) {
if (key.hasIndex()) {
IData[] array = IDataUtil.getIDataArray(cursor, key.toString());
value = get(get(array, key.getIndex()), keys);
} else {
value = get(IDataUtil.getIData(cursor, key.toString()), keys);
}
} else {
if (key.hasIndex()) {
Object[] array = IDataUtil.getObjectArray(cursor, key.toString());
value = get(array, key.getIndex());
} else {
value = IDataUtil.get(cursor, key.toString());
}
}
cursor.destroy();
}
return value;
}
public static <T> T get(T[] array, int index) {
T item = null;
if (array != null) {
// support reverse/tail indexing
if (index < 0) index += array.length;
item = array[index];
}
return item;
}
注: Tundracom.wm.data.IData
には、ドキュメントに値を設定するための補完的なサービスもあります: tundra.document.put($document, $key, $value)
.