Search in sources :

Example 56 with Message

use of com.dexels.navajo.document.Message in project navajo by Dexels.

the class BaseMessageImpl method isEqual.

@Override
public final boolean isEqual(Message o, String skipProperties) {
    BaseMessageImpl other = (BaseMessageImpl) o;
    if (!other.getName().equals(this.getName())) {
        return false;
    }
    // Check sub message structure.
    List<Message> allOther = other.messageList;
    if (allOther != null && messageList == null) {
        return false;
    }
    if (messageList != null && allOther == null) {
        return false;
    }
    if (allOther != null && allOther.size() != messageList.size()) {
        return false;
    }
    if (allOther != null) {
        for (int i = 0; i < allOther.size(); i++) {
            Message otherMsg = allOther.get(i);
            boolean match = false;
            for (int j = 0; j < messageList.size(); j++) {
                Message myMsg = messageList.get(j);
                if (myMsg.isEqual(otherMsg, skipProperties)) {
                    match = true;
                    j = messageList.size() + 1;
                }
            }
            if (!match) {
                return false;
            }
        }
    }
    // Check property structure.
    ArrayList<Property> allOtherProps = other.getAllProperties();
    ArrayList<Property> allMyProps = this.getAllProperties();
    if (allOtherProps.size() != allMyProps.size()) {
        return false;
    }
    for (int i = 0; i < allOtherProps.size(); i++) {
        Property otherProp = allOtherProps.get(i);
        boolean match = false;
        // Check whether property name exists in skipProperties list.
        if (skipProperties.indexOf(otherProp.getName()) != -1) {
            match = true;
        } else {
            for (int j = 0; j < allMyProps.size(); j++) {
                Property myProp = allMyProps.get(j);
                if (myProp.isEqual(otherProp)) {
                    match = true;
                    j = allMyProps.size() + 1;
                }
            }
        }
        if (!match) {
            return false;
        }
    }
    return true;
}
Also used : Message(com.dexels.navajo.document.Message) Property(com.dexels.navajo.document.Property)

Example 57 with Message

use of com.dexels.navajo.document.Message in project navajo by Dexels.

the class BaseMessageImpl method getMessages.

/**
 * Return all messages that match a given regular expression. Regular expression
 * may include sub-messages and even absolute message references starting at the
 * root level.
 */
@Override
public List<Message> getMessages(String regularExpression) {
    List<Message> sub = null;
    List<Message> sub2 = null;
    if (regularExpression.startsWith(Navajo.PARENT_MESSAGE + Navajo.MESSAGE_SEPARATOR)) {
        regularExpression = regularExpression.substring((Navajo.PARENT_MESSAGE + Navajo.MESSAGE_SEPARATOR).length());
        return getParentMessage().getMessages(regularExpression);
    } else if (regularExpression.startsWith(Navajo.MESSAGE_SEPARATOR)) {
        return myDocRoot.getMessages(regularExpression);
    } else // Contains submessages.
    if (regularExpression.indexOf(Navajo.MESSAGE_SEPARATOR) != -1) {
        // contains
        // a path, descent it first
        StringTokenizer tok = new StringTokenizer(regularExpression, Navajo.MESSAGE_SEPARATOR);
        Message m = null;
        while (tok.hasMoreElements()) {
            String msgName = tok.nextToken();
            if (sub == null) {
                // First message in path.
                sub = getMessages(msgName);
            } else {
                // Subsequent submessages in path.
                ArrayList<Message> messages = new ArrayList<Message>();
                for (int i = 0; i < sub.size(); i++) {
                    m = sub.get(i);
                    sub2 = m.getMessages(msgName);
                    messages.addAll(sub2);
                }
                sub = messages;
            }
        }
        return sub;
    } else {
        ArrayList<Message> result = new ArrayList<>();
        try {
            String index = null;
            if (regularExpression.indexOf("@") != -1) {
                StringTokenizer arEl = new StringTokenizer(regularExpression, "@");
                regularExpression = arEl.nextToken();
                index = arEl.nextToken();
            }
            Pattern pattern = Pattern.compile(regularExpression);
            if (messageList != null) {
                for (int i = 0; i < messageList.size(); i++) {
                    BaseMessageImpl m = (BaseMessageImpl) messageList.get(i);
                    String name = m.getName();
                    String type = m.getType();
                    if (type.equals(Message.MSG_TYPE_ARRAY) && pattern.matcher(name).matches()) {
                        // message is array type add all children.
                        if (index == null) {
                            if (m.messageList != null) {
                                result.addAll(m.messageList);
                            }
                        } else {
                            if (index.indexOf("=") >= 0) {
                                String propertyName = index.split("=")[0];
                                String propertyValue = index.split("=")[1];
                                // Find array element.
                                for (int x = 0; x < m.getArraySize(); x++) {
                                    Message am = m.getMessage(x);
                                    if (am.getProperty(propertyName) != null) {
                                        if (am.getProperty(propertyName).getValue().equals(propertyValue)) {
                                            result.add(am);
                                        }
                                    }
                                }
                            } else {
                                try {
                                    if (m.getMessage(Integer.parseInt(index)) != null) {
                                        result.add(m.getMessage(Integer.parseInt(index)));
                                    }
                                } catch (Exception pe) {
                                    throw new NavajoExceptionImpl("Could not parse array index: " + index);
                                }
                            }
                        }
                    } else {
                        if (pattern.matcher(name).matches()) {
                            result.add(messageList.get(i));
                        }
                    }
                }
            }
        } catch (Exception re) {
            throw new NavajoExceptionImpl(re);
        }
        return result;
    }
}
Also used : Pattern(java.util.regex.Pattern) StringTokenizer(java.util.StringTokenizer) Message(com.dexels.navajo.document.Message) ArrayList(java.util.ArrayList) NavajoException(com.dexels.navajo.document.NavajoException) ExpressionChangedException(com.dexels.navajo.document.ExpressionChangedException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 58 with Message

use of com.dexels.navajo.document.Message in project navajo by Dexels.

the class BaseMessageImpl method copy.

@Override
public final Message copy() {
    Navajo empty = NavajoFactory.getInstance().createNavajo();
    Message result = copy(empty);
    empty.addMessage(result);
    return result;
}
Also used : Message(com.dexels.navajo.document.Message) Navajo(com.dexels.navajo.document.Navajo)

Example 59 with Message

use of com.dexels.navajo.document.Message in project navajo by Dexels.

the class JSONTMLImpl method formatMessages.

private void formatMessages(JsonGenerator jg, List<Message> messages) throws Exception {
    for (Message m : messages) {
        String origName = TOP_LEVEL_MSG;
        try {
            if (skipTopLevelMessage && !m.isArrayMessage()) {
                origName = m.getName();
                m.setName(TOP_LEVEL_MSG);
            }
            format(jg, m, false);
        } finally {
            if (skipTopLevelMessage) {
                m.setName(origName);
            }
        }
    }
}
Also used : Message(com.dexels.navajo.document.Message)

Example 60 with Message

use of com.dexels.navajo.document.Message in project navajo by Dexels.

the class JSONTMLImpl method getEntityTemplateProperty.

private Property getEntityTemplateProperty(Property prop) {
    // message in the entity template
    if (entityTemplate == null) {
        return null;
    }
    String fullPath = prop.getFullPropertyName();
    if (!fullPath.contains("@")) {
        return entityTemplate.getProperty(prop.getFullPropertyName());
    }
    // We have an array message somewhere. Lets try to get the correct property from the entity template
    StringTokenizer st = new StringTokenizer(prop.getParentMessage().getPath(), "/");
    Message next = entityTemplate.getMessage(st.nextToken());
    while (next != null && st.hasMoreTokens()) {
        String subpath = st.nextToken();
        if (subpath.contains("@")) {
            // Strip the @ part
            subpath = subpath.substring(0, subpath.indexOf('@'));
            next = next.getMessage(subpath).getDefinitionMessage();
        } else {
            next = next.getMessage(subpath);
        }
    }
    if (next != null) {
        return next.getProperty(prop.getName());
    }
    return null;
}
Also used : StringTokenizer(java.util.StringTokenizer) Message(com.dexels.navajo.document.Message)

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