Search in sources :

Example 51 with Message

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;
        }
    };
}
Also used : MappableTreeNode(com.dexels.navajo.script.api.MappableTreeNode) TipiLink(com.dexels.navajo.expression.api.TipiLink) Message(com.dexels.navajo.document.Message) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) Optional(java.util.Optional) Selection(com.dexels.navajo.document.Selection) Operand(com.dexels.navajo.document.Operand) ContextExpression(com.dexels.navajo.expression.api.ContextExpression) Access(com.dexels.navajo.script.api.Access) Navajo(com.dexels.navajo.document.Navajo)

Example 52 with Message

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, "}");
    }
}
Also used : Message(com.dexels.navajo.document.Message) Property(com.dexels.navajo.document.Property)

Example 53 with Message

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;
        }
    }
}
Also used : Message(com.dexels.navajo.document.Message)

Example 54 with Message

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);
}
Also used : StringTokenizer(java.util.StringTokenizer) Message(com.dexels.navajo.document.Message)

Example 55 with Message

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;
    }
}
Also used : Message(com.dexels.navajo.document.Message) Property(com.dexels.navajo.document.Property)

Aggregations

Message (com.dexels.navajo.document.Message)312 Property (com.dexels.navajo.document.Property)149 Navajo (com.dexels.navajo.document.Navajo)127 Test (org.junit.Test)95 NavajoException (com.dexels.navajo.document.NavajoException)39 ImmutableMessage (com.dexels.immutable.api.ImmutableMessage)35 Access (com.dexels.navajo.script.api.Access)27 UserException (com.dexels.navajo.script.api.UserException)27 TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)26 Selection (com.dexels.navajo.document.Selection)25 Operand (com.dexels.navajo.document.Operand)22 IOException (java.io.IOException)19 ArrayList (java.util.ArrayList)19 MappableException (com.dexels.navajo.script.api.MappableException)18 Ignore (org.junit.Ignore)17 SystemException (com.dexels.navajo.script.api.SystemException)16 Optional (java.util.Optional)15 ContextExpression (com.dexels.navajo.expression.api.ContextExpression)13 MappableTreeNode (com.dexels.navajo.script.api.MappableTreeNode)13 HashMap (java.util.HashMap)13