use of com.dexels.navajo.document.Message in project navajo by Dexels.
the class SimpleNode method lazyBiFunction.
public ContextExpression lazyBiFunction(List<String> problems, String expression, BinaryOperator<Operand> func, BiFunction<Optional<String>, Optional<String>, Boolean> acceptTypes, BiFunction<Optional<String>, Optional<String>, Optional<String>> returnTypeResolver, Function<String, FunctionClassification> functionClassifier, Function<String, Optional<Node>> mapResolver) {
ContextExpression expA = jjtGetChild(0).interpretToLambda(problems, expression, functionClassifier, mapResolver);
ContextExpression expB = jjtGetChild(1).interpretToLambda(problems, expression, functionClassifier, mapResolver);
Optional<String> aType = expA.returnType();
Optional<String> bType = expB.returnType();
boolean inputTypesValid = acceptTypes.apply(aType, bType);
if (!inputTypesValid) {
problems.add("Invalid input types in node: " + aType.orElse("unknown") + " and " + bType.orElse("unknown") + " in node type: " + this.getClass());
}
Optional<String> returnType = returnTypeResolver.apply(aType, bType);
return new ContextExpression() {
@Override
public Operand apply(Navajo doc, Message parentMsg, Message parentParamMsg, Selection parentSel, MappableTreeNode mapNode, TipiLink tipiLink, Access access, Optional<ImmutableMessage> immutableMessage, Optional<ImmutableMessage> paramMessage) {
Operand a = expA.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage);
Operand b = expB.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage);
return func.apply(a, b);
}
@Override
public boolean isLiteral() {
return expA.isLiteral() && expB.isLiteral();
}
@Override
public Optional<String> returnType() {
return returnType;
}
@Override
public String expression() {
return expression;
}
};
}
use of com.dexels.navajo.document.Message in project navajo by Dexels.
the class BaseMessageImpl method printElementJSONTypeless.
private void printElementJSONTypeless(String name, final Writer sw, String[] propertyFilter) throws IOException {
ArrayList<Message> messages = getAllMessages();
ArrayList<Property> properties = getAllProperties();
if (getType().equals(Message.MSG_TYPE_ARRAY)) {
writeElement(sw, "\"" + name + "\" : [");
int cnt = 0;
if (propertyFilter != null) {
for (Message m : messages) {
if (cnt > 0) {
writeElement(sw, ", ");
}
((BaseMessageImpl) m).printElementJSONTypeless(name, sw, propertyFilter);
cnt++;
}
} else {
for (Message m : messages) {
if (cnt > 0) {
writeElement(sw, ", ");
}
((BaseNode) m).printElementJSONTypeless(sw);
cnt++;
}
}
writeElement(sw, "]");
} else if (getType().equals(Message.MSG_TYPE_ARRAY_ELEMENT)) {
writeElement(sw, "{");
int cnt = 0;
if (propertyFilter != null) {
for (String s : propertyFilter) {
if (cnt > 0) {
writeElement(sw, ", ");
}
Property p = getProperty(s);
if (p != null) {
((BaseNode) p).printElementJSONTypeless(sw);
cnt++;
}
}
} else {
for (Property p : properties) {
if (cnt > 0) {
writeElement(sw, ", ");
}
((BaseNode) p).printElementJSONTypeless(sw);
cnt++;
}
}
writeElement(sw, "}");
} else if (getType().equals(Message.MSG_TYPE_SIMPLE)) {
writeElement(sw, "\"" + getName() + "\" : {");
int cnt = 0;
if (propertyFilter != null) {
for (String s : propertyFilter) {
if (cnt > 0) {
writeElement(sw, ", ");
}
Property p = getProperty(s);
((BaseNode) p).printElementJSONTypeless(sw);
cnt++;
}
} else {
for (Property p : properties) {
if (cnt > 0) {
writeElement(sw, ", ");
}
((BaseNode) p).printElementJSONTypeless(sw);
cnt++;
}
}
writeElement(sw, "}");
cnt = 0;
for (Message m : messages) {
if (cnt > 0) {
writeElement(sw, ", ");
}
((BaseNode) m).printElementJSONTypeless(sw);
cnt++;
}
writeElement(sw, "}");
}
}
use of com.dexels.navajo.document.Message in project navajo by Dexels.
the class BaseMessageImpl method getByPath.
public final Message getByPath(String path) {
if (path.startsWith("../")) {
return getParentMessage().getMessage(path.substring(3));
// I THINK! It did not make sense at all
}
if (path.length() > 0 && path.charAt(0) == '/') {
path = path.substring(1);
}
int slash = path.indexOf("/");
if (slash < 0) {
return getMessage(path);
} else {
String messagename = path.substring(0, slash);
Message m = getMessage(messagename);
if (m != null) {
return m.getMessage(path.substring(slash + 1));
} else {
return null;
}
}
}
use of com.dexels.navajo.document.Message in project navajo by Dexels.
the class BaseMessageImpl method getMessage.
@Override
public Message getMessage(String name) {
// Check self reference.
if (name.equals(".")) {
return this;
} else if (name.startsWith("../")) {
// Check parent reference.
if (getParentMessage() == null) {
return null;
} else {
return getParentMessage().getMessage(name.substring(3));
}
} else // Check starting with self reference.
if (name.startsWith("./")) {
name = name.substring(2);
}
if (name.length() > 0 && name.charAt(0) == '/') {
return getRootDoc().getMessage(name.substring(1));
}
if (name.indexOf("/") >= 0) {
return getByPath(name);
}
if (name.indexOf("@") >= 0) {
StringTokenizer arEl = new StringTokenizer(name, "@");
String realName = arEl.nextToken();
Message array = getMessage(realName);
if (array != null) {
if ((array.getType() != null) && (array.getType().equals(Message.MSG_TYPE_ARRAY))) {
if (arEl.hasMoreTokens()) {
String index = arEl.nextToken();
if (index.indexOf("=") >= 0) {
String propertyName = index.split("=")[0];
String propertyValue = index.split("=")[1];
// Find array element.
for (int x = 0; x < array.getArraySize(); x++) {
Message am = array.getMessage(x);
if (am.getProperty(propertyName) != null) {
if (am.getProperty(propertyName).getValue().equals(propertyValue)) {
return am;
}
}
}
} else {
int i = 0;
try {
i = Integer.parseInt(index);
} catch (NumberFormatException ex) {
logger.error("Error: ", ex);
}
return array.getMessage(i);
}
}
}
}
}
if (messageMap == null) {
return null;
}
return messageMap.get(name);
}
use of com.dexels.navajo.document.Message in project navajo by Dexels.
the class BaseMessageImpl method getPropertyByPath.
public final Property getPropertyByPath(String pth) {
String path = null;
if (pth.length() > 0 && pth.charAt(0) == '/') {
path = pth.substring(1);
} else {
path = pth;
}
if (path.startsWith("..")) {
if (getParentMessage() == null) {
return null;
}
return getParentMessage().getProperty(path.substring(3));
}
int slash = path.indexOf("/");
if (slash < 0) {
if (propertyList == null || propertyMap == null) {
return null;
}
if (propertyList.size() != propertyMap.size()) {
logger.info("Warning: Propertymap sizE: " + propertyMap.size() + " listsize: " + propertyList.size());
}
Property pp = propertyMap.get(path);
if (pp == null && !Message.MSG_TYPE_DEFINITION.equals(getType())) {
// check for definition messages (except if I'm a definition
// message myself)
Message arrayP = getArrayParentMessage();
if (arrayP != null) {
Message def = arrayP.getDefinitionMessage();
if (def != null && def.getProperty(path) != null) {
Property res = def.getProperty(path).copy(getRootDoc());
if (def.getType() == null || "".equals(def.getType())) {
throw new IllegalStateException("DEFINITION PROPERTY FOUND WITHOUT TYPE!");
}
res.setType(res.getType());
addProperty(res);
return res;
}
}
}
return pp;
} else {
String msgname = path.substring(0, slash);
String propname = path.substring(slash, path.length());
BaseMessageImpl ms = (BaseMessageImpl) getMessage(msgname);
if (ms != null) {
return ms.getPropertyByPath(propname);
}
return null;
}
}
Aggregations