Commit 289cda54 authored by redmitry@list.ru's avatar redmitry@list.ru

moved from github

parents
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>es.bsc.inb.elixir.elixibilitas</groupId>
<artifactId>elixibilitas-meta</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>elixibilitas-meta</name>
<description>
Elixibilitas Metrics Metadata auxiliary classes
</description>
<organization>
<name>Barcelona Supercomputing Center</name>
<url>https://www.bsc.es/</url>
</organization>
<developers>
<developer>
<id>redmitry</id>
<name>Dmitry Repchevsky</name>
<email>redmitry@list.ru</email>
</developer>
</developers>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.1</version>
<type>jar</type>
</dependency>
</dependencies>
</project>
/**
* *****************************************************************************
* Copyright (C) 2020 ELIXIR ES, Spanish National Bioinformatics Institute (INB)
* and Barcelona Supercomputing Center (BSC)
*
* Modifications to the initial code base are copyright of their respective
* authors, or their employers as appropriate.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*****************************************************************************
*/
package es.bsc.inb.elixir.elixibilitas.meta;
import java.util.Map;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonValue;
/**
* @author Dmitry Repchevsky
*/
public abstract class AbstractMeta {
/**
* Traverses the JSON value and updates the parent element (object or array).
*
* @param id tool's identifier
* @param path JSON path to the value
* @param name value's name (null if parent is an array)
* @param value currently traversed JSON value
* @param parent parent of traversed value
* @param map the map to keep JSON paths and values
*
* @return an updated parent element
*/
JsonValue travers(final String id,
final String path,
final String name,
final JsonValue value,
final JsonValue parent,
final Map<String, JsonValue> map) {
final JsonValue.ValueType type = value.getValueType();
if (type == JsonValue.ValueType.OBJECT) {
if (parent.getValueType() == JsonValue.ValueType.OBJECT) {
JsonValue object = map.get(path);
if (object == null) {
object = Json.createObjectBuilder().build();
}
for (Map.Entry<String, JsonValue> entry : value.asJsonObject().entrySet()) {
object = travers(id, path + entry.getKey() + "/", entry.getKey(), entry.getValue(), object, map);
}
map.put(path, object);
return name == null ? Json.createObjectBuilder(object.asJsonObject()).build() :
Json.createObjectBuilder(parent.asJsonObject()).add(name, object).build();
}
final JsonArrayBuilder ab = Json.createArrayBuilder(parent.asJsonArray());
JsonValue object = Json.createObjectBuilder().build();
for (Map.Entry<String, JsonValue> entry : value.asJsonObject().entrySet()) {
object = travers(id, path + entry.getKey() + "/", entry.getKey(), entry.getValue(), object, map);
}
ab.add(object);
return ab.build();
}
JsonValue array = map.get(path);
if (type == JsonValue.ValueType.ARRAY) {
if (array == null) {
array = Json.createArrayBuilder().build();
} else {
array = Json.createArrayBuilder(array.asJsonArray()).build();
}
int i = 0;
for (JsonValue v : value.asJsonArray()) {
array = travers(id, path + i++ + "/", null, v, array, map);
}
} else if (name != null && name.startsWith("@")) {
return parent;
} else if (array == null) {
array = Json.createArrayBuilder().add(id + path).build();
} else {
array = Json.createArrayBuilder().add(id + path).build();
}
map.put(path, array);
if (parent != null) {
switch(parent.getValueType()) {
case OBJECT: return Json.createObjectBuilder(parent.asJsonObject()).add(name, array).build();
case ARRAY: return Json.createArrayBuilder(parent.asJsonArray()).add(array).build();
}
}
return parent;
}
}
package es.bsc.inb.elixir.elixibilitas.meta;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonValue;
import javax.json.stream.JsonGenerator;
/**
* @author Dmitry Repchevsky
*/
public class MetricsMetaWriter extends AbstractMeta implements AutoCloseable {
private final JsonGenerator generator;
private String label;
final Map<String, JsonValue> map = new LinkedHashMap<>();
public MetricsMetaWriter(final JsonGenerator generator) {
this.generator = generator;
}
public void write(final JsonValue value) {
if (JsonValue.ValueType.OBJECT == value.getValueType()) {
final JsonObject o = value.asJsonObject();
String id = o.getJsonString("@id").getString();
id = id.substring(id.indexOf("metrics") + 8); // "metrics/".length()
final String label = getLabel(id);
if (this.label == null) {
this.label = label;
} else if (!this.label.equals(label)) {
this.label = label;
final JsonValue meta = map.get("/");
map.clear();
travers(id, "/", null, o, Json.createObjectBuilder().build(), map);
generator.write(meta);
}
travers(id, "/", null, o, Json.createObjectBuilder().build(), map);
}
}
private String getLabel(String id) {
final String[] nodes = id.split("/");
if (nodes.length > 0) {
final String[] _id = nodes[0].split(":");
return _id[_id.length > 1 ? 1 : 0];
}
return "";
}
@Override
public void close() throws IOException {
final JsonValue meta = map.get("/");
generator.write(meta);
generator.close();
}
}
/**
* *****************************************************************************
* Copyright (C) 2020 ELIXIR ES, Spanish National Bioinformatics Institute (INB)
* and Barcelona Supercomputing Center (BSC)
*
* Modifications to the initial code base are copyright of their respective
* authors, or their employers as appropriate.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*****************************************************************************
*/
package es.bsc.inb.elixir.elixibilitas.meta;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Stream;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonValue;
/**
* @author Dmitry Repchevsky
*/
public class ToolsMetaIterator extends AbstractMeta implements Iterator<JsonValue> {
private final Iterator<JsonValue> iterator;
private String label;
private final Map<String, JsonValue> map = new LinkedHashMap<>();
public ToolsMetaIterator(final Stream<JsonValue> stream) {
iterator = stream.iterator();
}
@Override
public boolean hasNext() {
return iterator.hasNext() || !map.isEmpty();
}
@Override
public JsonValue next() {
while (iterator.hasNext()) {
JsonValue value = iterator.next();
if (JsonValue.ValueType.OBJECT == value.getValueType()) {
final JsonObject o = value.asJsonObject();
String id = o.getJsonString("@id").getString();
id = id.substring(id.indexOf("tool") + 5); // "tool/".length()
final String label = o.getJsonString("@label").getString();
if (this.label == null) {
this.label = label;
} else if (!this.label.equals(label)) {
this.label = label;
final JsonValue meta = map.get("/");
map.clear();
travers(id, "/", null, o, Json.createObjectBuilder().build(), map);
return meta;
}
travers(id, "/", null, o, Json.createObjectBuilder().build(), map);
}
}
final JsonValue meta = map.get("/");
map.clear();
return meta;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment