use of com.dexels.navajo.article.command.ArticleCommand in project navajo by Dexels.
the class BaseRuntimeImpl method execute.
@Override
public void execute(ArticleContext context) throws APIException, NoJSONOutputException {
verifyScopes();
List<XMLElement> children = article.getChildren();
try {
List<JsonNode> elements = new ArrayList<JsonNode>();
for (XMLElement e : children) {
String name = e.getName();
if (name.startsWith("_")) {
// at runtime
continue;
}
ArticleCommand ac = context.getCommand(name);
if (ac == null) {
throw new APIException("Unknown command: " + name, null, APIErrorCode.InternalError);
}
Map<String, String> parameters = new HashMap<String, String>();
for (Iterator<String> iterator = e.enumerateAttributeNames(); iterator.hasNext(); ) {
String attributeName = iterator.next();
parameters.put(attributeName, e.getStringAttribute(attributeName));
}
JsonNode node = ac.execute(this, context, parameters, e);
if (node != null) {
elements.add(node);
}
}
if (elements.size() == 0) {
writeNode(rootNode);
} else if (elements.size() == 1) {
// HUH?
writeNode(elements.iterator().next());
} else {
ArrayNode an = getObjectMapper().createArrayNode();
for (JsonNode jsonNode : elements) {
an.add(jsonNode);
}
writeNode(an);
}
// mapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
// mapper.writeValue(getOutputWriter(), rootNode);
// commit();
} catch (IOException e1) {
logger.error("Error: ", e1);
}
}
use of com.dexels.navajo.article.command.ArticleCommand in project navajo by Dexels.
the class BaseContextImpl method interpretMeta.
public void interpretMeta(XMLElement article, ObjectMapper mapper, ObjectNode articleNode, boolean extended) throws APIException {
String outputType = article.getStringAttribute("output");
if (outputType != null) {
articleNode.put("output", outputType);
}
String scopes = article.getStringAttribute("scopes");
if (scopes != null) {
String[] scopeArray = scopes.split(",");
ArrayNode scopeArgs = mapper.createArrayNode();
for (String scope : scopeArray) {
scopeArgs.add(scope);
}
articleNode.set("scopes", scopeArgs);
}
String description = article.getStringAttribute("description");
if (extended && description != null && description.length() != 0) {
articleNode.put("description", description);
}
XMLElement argTag = article.getChildByTagName("_arguments");
ArrayNode inputArgs = mapper.createArrayNode();
articleNode.set("input", inputArgs);
if (argTag != null) {
List<XMLElement> args = argTag.getChildren();
for (XMLElement xmlElement : args) {
// name="aantalregels" description="Maximum aantal regels"
// type="integer" optional="true" default="5"
ObjectNode input = mapper.createObjectNode();
input.put("name", xmlElement.getStringAttribute("name"));
input.put("description", xmlElement.getStringAttribute("description"));
input.put("type", xmlElement.getStringAttribute("type"));
final boolean optional = xmlElement.getBooleanAttribute("optional", "true", "false", false);
input.put("optional", optional);
final String defaultValue = xmlElement.getStringAttribute("default");
if (defaultValue != null) {
input.put("default", defaultValue);
}
final String sourcearticle = xmlElement.getStringAttribute("sourcearticle");
if (sourcearticle != null) {
input.put("sourcearticle", sourcearticle);
}
final String sourcekey = xmlElement.getStringAttribute("sourcekey");
if (sourcekey != null) {
input.put("sourcekey", sourcekey);
}
inputArgs.add(input);
}
}
ArrayNode outputArgs = mapper.createArrayNode();
articleNode.set("output", outputArgs);
List<XMLElement> children = article.getChildren();
for (XMLElement e : children) {
String name = e.getName();
if (name.startsWith("_")) {
continue;
}
ArticleCommand ac = getCommand(name);
if (ac == null) {
throw new APIException("Unknown command: " + name, null, APIErrorCode.InternalError);
}
Map<String, String> parameters = new HashMap<String, String>();
for (Iterator<String> iterator = e.enumerateAttributeNames(); iterator.hasNext(); ) {
String attributeName = iterator.next();
parameters.put(attributeName, e.getStringAttribute(attributeName));
}
ac.writeMetadata(e, outputArgs, mapper);
}
}
Aggregations