Search in sources :

Example 1 with MappingException

use of com.dexels.navajo.script.api.MappingException in project navajo by Dexels.

the class MappingUtils method getAttributeObject.

private static final Object getAttributeObject(MappableTreeNode o, String name, Object[] arguments) throws UserException, MappingException {
    Object result = null;
    String methodName = "";
    try {
        java.lang.reflect.Method m = o.getMethodReference(name, arguments);
        result = m.invoke(o.myObject, arguments);
    } catch (IllegalAccessException iae) {
        throw new MappingException(methodName + " illegally accessed in mappable class: " + o.myObject.getClass().getName());
    } catch (InvocationTargetException ite) {
        Throwable t = ite.getTargetException();
        if (t instanceof UserException) {
            throw (UserException) t;
        } else {
            throw new MappingException("Error getting attribute: " + name + " of object: " + o, ite);
        }
    }
    return result;
}
Also used : UserException(com.dexels.navajo.script.api.UserException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) MappingException(com.dexels.navajo.script.api.MappingException)

Example 2 with MappingException

use of com.dexels.navajo.script.api.MappingException in project navajo by Dexels.

the class MappingUtils method setProperty.

public static final Property setProperty(boolean parameter, Message msg, String name, Object value, String type, String subtype, String direction, String description, int length, Navajo outputDoc, Navajo tmlDoc, boolean remove) throws MappingException {
    Message ref = null;
    if (parameter) {
        if (msg == null) {
            msg = tmlDoc.getMessage("__parms__");
            if (msg == null) {
                // Create __parms__ message.
                msg = NavajoFactory.getInstance().createMessage(tmlDoc, "__parms__");
                tmlDoc.addMessage(msg);
            }
        }
        ref = getMessageObject(name, msg, false, tmlDoc, false, "", -1);
        if (ref == null) {
            // Can be null due to absolute param name (starting with '/'). In this case use
            // __parms__ as parent.
            ref = tmlDoc.getMessage("__parms__");
        }
    } else {
        ref = getMessageObject(name, msg, false, outputDoc, false, "", -1);
    }
    if (ref == null) {
        ref = msg;
    }
    String actualName = getStrippedPropertyName(name);
    if (ref == null) {
        throw new MappingException("Property can only be created under a message");
    }
    // with ../ constructions in the name of the new property, it is possible to create a property at the rootMessage of a NavajoDoc but that will never be serialized. Logger message to see how often it happens
    if (ref.equals(ref.getRootDoc().getRootMessage())) {
        logger.warn("WARNING - Adding property to rootMessage of NavajoDoc - property will not be findable");
    }
    Property prop = ref.getProperty(actualName);
    // Remove a parameter if remove flag is set.
    if (remove && prop != null && parameter) {
        ref.removeProperty(prop);
        return null;
    }
    if (prop == null && remove && parameter) {
        return null;
    }
    if (prop == null) {
        // Property does not exist.
        if (!parameter) {
            if (value instanceof Property) {
                // Value is a property itself!
                prop = (Property) ((Property) value).clone(name);
            } else if (Property.SELECTION_PROPERTY.equals(type)) {
                prop = ref.getRootDoc().getNavajoFactory().createProperty(outputDoc, actualName, "1", description, direction);
                if (value instanceof Selection[]) {
                    prop.setCardinality("+");
                    prop.setValue((Selection[]) value);
                }
            } else if (Property.BINARY_PROPERTY.equals(type)) {
                prop = ref.getRootDoc().getNavajoFactory().createProperty(outputDoc, actualName, type, "", length, description, direction);
                if (value instanceof Binary) {
                    prop.setValue((Binary) value);
                }
            } else {
                // Legacy mode hack, many scripts do not expect null valued string properties.
                if (Property.STRING_PROPERTY.equals(type) && value == null) {
                    value = "";
                }
                prop = ref.getRootDoc().getNavajoFactory().createProperty(outputDoc, actualName, type, "", length, description, direction);
                if ((value instanceof StringLiteral)) {
                    value = value.toString();
                }
                prop.setAnyValue(value);
                prop.setType(type);
            }
        } else {
            if (Property.EXPRESSION_LITERAL_PROPERTY.equals(type)) {
                prop = ref.getRootDoc().getNavajoFactory().createProperty(tmlDoc, actualName, type, "", length, description, direction);
                prop.setValue(new NavajoExpression(value.toString()));
                prop.setType(type);
            } else if (Property.SELECTION_PROPERTY.equals(type)) {
                prop = ref.getRootDoc().getNavajoFactory().createProperty(tmlDoc, actualName, "1", description, direction);
                if (value instanceof Selection[]) {
                    prop.setCardinality("+");
                    prop.setValue((Selection[]) value);
                }
            } else if (Property.BINARY_PROPERTY.equals(type)) {
                prop = ref.getRootDoc().getNavajoFactory().createProperty(tmlDoc, actualName, type, "", length, description, direction);
                if (value instanceof Binary) {
                    prop.setValue((Binary) value);
                }
            } else {
                prop = ref.getRootDoc().getNavajoFactory().createProperty(tmlDoc, actualName, type, "", length, description, direction);
                if ((value instanceof StringLiteral)) {
                    value = value.toString();
                }
                prop.setAnyValue(value);
                prop.setType(type);
            }
        }
        ref.addProperty(prop);
    } else {
        // Existing property.
        prop.clearValue();
        if (value instanceof Property) {
            // Value is a property itself!
            prop = (Property) ((Property) value).clone(name);
        } else if (Property.BINARY_PROPERTY.equals(type)) {
            if (value instanceof Binary) {
                prop.setValue((Binary) value);
            } else {
                prop.clearValue();
            }
        } else if (Property.SELECTION_PROPERTY.equals(type) && value != null && value instanceof Selection[]) {
            prop.setCardinality("+");
            prop.setValue((Selection[]) value);
        } else if (!Property.SELECTION_PROPERTY.equals(type)) {
            if (value != null) {
                if ((value instanceof StringLiteral)) {
                    prop.setUnCheckedStringAsValue(((StringLiteral) value).toString());
                } else {
                    prop.setAnyValue(value);
                }
            } else {
                prop.clearValue();
            }
        }
        if (Property.DIR_IN.equals(direction) || Property.DIR_OUT.equals(direction)) {
            prop.setDirection(direction);
        }
        prop.setType(type);
        // Should not matter ;)
        prop.setName(actualName);
    }
    // Set subtype if not empty.
    if (subtype != null && !subtype.equals("")) {
        prop.setSubType(subtype);
    }
    // Set description if not empty.
    if (description != null && !description.equals("")) {
        prop.setDescription(description);
    }
    // Set length if not empty ( = -1) .
    if (length != -1) {
        prop.setLength(length);
    }
    return prop;
}
Also used : NavajoExpression(com.dexels.navajo.document.types.NavajoExpression) Message(com.dexels.navajo.document.Message) Selection(com.dexels.navajo.document.Selection) Binary(com.dexels.navajo.document.types.Binary) Property(com.dexels.navajo.document.Property) MappingException(com.dexels.navajo.script.api.MappingException)

Example 3 with MappingException

use of com.dexels.navajo.script.api.MappingException in project navajo by Dexels.

the class DomainObjectMapper method setMethodReference.

/**
 * Gets the appropriate 'setter' for an attribute name with parameters signature.
 *
 * @param myClass the class to introspect
 * @param name name of the attribute
 * @param parameters type signature for method parameters
 * @return
 * @throws MappingException
 */
public final Method setMethodReference(Class myClass, String name, Class[] parameters) throws MappingException {
    String key = constructParameterSignature(name, parameters);
    java.lang.reflect.Method m = methods.get(key);
    if (m == null) {
        String methodName = "set" + (name.charAt(0) + "").toUpperCase() + name.substring(1, name.length());
        Class c = this.myObject.getClass();
        java.lang.reflect.Method[] all = c.getMethods();
        for (int i = 0; i < all.length; i++) {
            if (all[i].getName().equalsIgnoreCase(methodName) && equalsParameterTypes(all[i].getParameterTypes(), parameters)) {
                m = all[i];
                break;
            }
        }
        if (m == null) {
            throw new MappingException("Could not find setter in class " + myClass.getCanonicalName() + " for attribute: " + name);
        }
        methods.put(key, m);
    }
    return m;
}
Also used : Method(java.lang.reflect.Method) Method(java.lang.reflect.Method) MappingException(com.dexels.navajo.script.api.MappingException)

Example 4 with MappingException

use of com.dexels.navajo.script.api.MappingException in project navajo by Dexels.

the class TslCompiler method messageNode.

@SuppressWarnings("unchecked")
public String messageNode(int ident, Element n, String className, String objectName, List<Dependency> deps, String tenant) throws MappingException, ClassNotFoundException, UserException, IOException, MetaCompileException, ParseException {
    StringBuilder result = new StringBuilder();
    String messageName = n.getAttribute("name");
    String condition = n.getAttribute("condition");
    String type = n.getAttribute("type");
    String mode = n.getAttribute("mode");
    String count = n.getAttribute("count");
    String startindex = n.getAttribute("start_index");
    String orderby = n.getAttribute("orderby");
    String extendsMsg = n.getAttribute("extends");
    String scopeMsg = n.getAttribute("scope");
    String method = n.getAttribute("method");
    String subType = n.getAttribute("subtype");
    type = (type == null) ? "" : type;
    mode = (mode == null) ? "" : mode;
    condition = (condition == null) ? "" : condition;
    count = (count == null || count.equals("")) ? "1" : count;
    method = (method == null) ? "" : method;
    subType = (subType == null) ? "" : subType;
    int startIndex = (startindex == null || startindex.equals("")) ? -1 : Integer.parseInt(startindex);
    boolean conditionClause = false;
    // If <message> node is conditional:
    if (!condition.equals("")) {
        conditionClause = true;
        result.append(printIdent(ident) + "if (Condition.evaluate(" + replaceQuotes(condition) + ", access.getInDoc(), currentMap, currentInMsg, currentParamMsg,access)) { \n");
        ident += 2;
    }
    Element nextElt = getNextElement(n);
    String ref = "";
    String filter = "";
    String startElement = "";
    String elementOffset = "";
    boolean isArrayAttr = false;
    boolean isSubMapped = false;
    boolean forceArray = false;
    boolean isIterator = false;
    boolean isMappedMessage = false;
    String mapPath = null;
    // Check if <message> is mapped to an object attribute:
    if (nextElt != null && nextElt.getNodeName().equals("map") && nextElt.getAttribute("ref") != null && !nextElt.getAttribute("ref").equals("")) {
        String refOriginal = null;
        isSubMapped = true;
        isMappedMessage = false;
        refOriginal = nextElt.getAttribute("ref");
        // Check if ref contains [ char, if it does, an array message of a selection property is mapped.
        if (refOriginal.length() > 0 && refOriginal.charAt(0) == '[') {
            refOriginal = refOriginal.replaceAll("\\[", "");
            refOriginal = refOriginal.replaceAll("\\]", "");
            isMappedMessage = true;
            isArrayAttr = true;
            type = Message.MSG_TYPE_ARRAY;
        } else if (refOriginal.indexOf('$') != -1) {
            // Remove leading $ (if present).
            refOriginal = refOriginal.replaceAll("\\$", "");
        }
        if (!isMappedMessage && refOriginal.indexOf('/') != -1) {
            ref = refOriginal.substring(refOriginal.lastIndexOf('/') + 1, refOriginal.length());
            mapPath = refOriginal.substring(0, refOriginal.lastIndexOf('/'));
        } else {
            ref = refOriginal;
        }
        forceArray = type.equals(Message.MSG_TYPE_ARRAY);
        filter = nextElt.getAttribute("filter");
        if (filter != null) {
            filter = filter.replace('\r', ' ');
            filter = filter.replace('\n', ' ');
        }
        startElement = nextElt.getAttribute("start_element");
        elementOffset = nextElt.getAttribute("element_offset");
        startElement = ((startElement == null || startElement.equals("")) ? "" : startElement);
        elementOffset = ((elementOffset == null || elementOffset.equals("")) ? "" : elementOffset);
        if (!isMappedMessage) {
            try {
                if (mapPath != null) {
                    contextClass = locateContextClass(mapPath, 0);
                    className = contextClass.getName();
                } else {
                    contextClass = Class.forName(className, false, loader);
                }
            } catch (Exception e) {
                throw new MappingException("Error line " + nextElt.getAttribute("linenr") + ": Could not find field: " + className + "/" + mapPath, e);
            }
            addDependency("dependentObjects.add( new JavaDependency( -1, \"" + className + "\"));\n", "JAVA" + className);
            if (DomainObjectMapper.class.isAssignableFrom(contextClass)) {
                isArrayAttr = forceArray;
                type = Message.MSG_TYPE_ARRAY;
            } else {
                try {
                    isArrayAttr = MappingUtils.isArrayAttribute(contextClass, ref);
                } catch (Exception e) {
                    throw new MappingException("Error line " + nextElt.getAttribute("linenr") + ": " + e.getMessage());
                }
                isIterator = MappingUtils.isIteratorAttribute(contextClass, ref);
                if (isIterator) {
                    isArrayAttr = true;
                }
                if (isArrayAttr) {
                    type = Message.MSG_TYPE_ARRAY;
                }
            }
        }
    }
    // Create the message(s). Multiple messages are created if count > 1.
    result.append(printIdent(ident) + "count = " + (count.equals("1") ? "1" : "((Integer) Expression.evaluate(\"" + count + "\", access.getInDoc(), currentMap, currentInMsg, currentParamMsg,null,null,getEvaluationParams()).value).intValue()") + ";\n");
    String messageList = "messageList" + (messageListCounter++);
    result.append(printIdent(ident) + "Message [] " + messageList + " = null;\n");
    String orderbyExpression = ("".equals(orderby) ? "\"\"" : "(String) Expression.evaluate(" + replaceQuotes(orderby) + ", access.getInDoc(), currentMap, currentInMsg, currentParamMsg,null,null,getEvaluationParams()).value");
    if (n.getNodeName().equals("message")) {
        result.append(printIdent(ident) + messageList + " = MappingUtils.addMessage(access.getOutputDoc(), currentOutMsg, \"" + messageName + "\", \"\", count, \"" + type + "\", \"" + mode + "\", " + orderbyExpression + ");\n");
        result.append("");
    } else if (n.getNodeName().equals("loop")) {
        // create a message array of size 1 as placeholder.
        result.append(printIdent(ident) + messageList + " = new Message[1];\n");
        result.append("");
    } else {
        // must be parammessage.
        result.append(printIdent(ident) + messageList + " = MappingUtils.addMessage(access.getInDoc(), currentParamMsg, \"" + messageName + "\", \"\", count, \"" + type + "\", \"" + mode + "\");\n");
    }
    result.append(printIdent(ident) + "for (int messageCount" + (ident) + " = 0; messageCount" + (ident) + " < " + messageList + ".length; messageCount" + (ident) + "++) {\n if (!kill) {\n");
    if (n.getNodeName().equals("message")) {
        result.append(printIdent(ident + 2) + "outMsgStack.push(currentOutMsg);\n");
        result.append(printIdent(ident + 2) + "currentOutMsg = " + messageList + "[messageCount" + (ident) + "];\n");
        if (subType != null && !subType.equals("")) {
            result.append(printIdent(ident + 2) + "currentOutMsg.setSubType(\"" + subType + "\");\n");
            String[] subTypeElements = subType.split(",");
            for (String subTypeElement : subTypeElements) {
                if (subTypeElement.startsWith("interface=")) {
                    for (String iface : subTypeElement.replace("interface=", "").split(";")) {
                        String version = "0";
                        if (iface.indexOf(".") != -1) {
                            version = iface.substring(iface.indexOf(".") + 1, iface.indexOf("?") == -1 ? iface.length() : iface.indexOf("?"));
                        }
                        String replace = "." + version;
                        iface = iface.replace(replace, "");
                        String options = null;
                        if (iface.indexOf('?') > 0) {
                            options = iface.split("\\?")[1];
                            iface = iface.split("\\?")[0];
                        }
                        addDependency("dependentObjects.add( new ExtendDependency( Long.valueOf(\"" + ExtendDependency.getScriptTimeStamp(iface) + "\"), \"" + iface + "\"));\n", "EXTEND" + iface);
                        deps.add(new ExtendDependency(ExtendDependency.getScriptTimeStamp(iface), iface));
                    }
                }
            }
        }
        if (extendsMsg != null && !extendsMsg.equals("")) {
            result.append(printIdent(ident + 2) + "currentOutMsg.setExtends(\"" + extendsMsg + "\");\n");
            if (extendsMsg.startsWith("navajo://")) {
                String ext = extendsMsg.substring(9);
                String version = "0";
                if (ext.indexOf('.') != -1) {
                    version = ext.substring(ext.indexOf('.') + 1, ext.indexOf('?') == -1 ? ext.length() : ext.indexOf('?'));
                }
                String rep = "." + version;
                ext = ext.replace(rep, "");
                String[] superEntities = ext.split(",");
                for (String superEntity : superEntities) {
                    if (superEntity.indexOf('?') > 0) {
                        superEntity = superEntity.split("\\?")[0];
                    }
                    addDependency("dependentObjects.add( new ExtendDependency( Long.valueOf(\"" + ExtendDependency.getScriptTimeStamp(superEntity) + "\"), \"" + superEntity + "\"));\n", "EXTEND" + superEntity);
                    deps.add(new ExtendDependency(ExtendDependency.getScriptTimeStamp(superEntity), superEntity));
                }
            }
        }
        if (scopeMsg != null) {
            result.append(printIdent(ident + 2) + "currentOutMsg.setScope(\"" + scopeMsg + "\");\n");
        }
        result.append(printIdent(ident + 2) + "currentOutMsg.setMethod(\"" + method + "\");\n");
    } else if (n.getNodeName().equals("loop")) {
    // do nothing.
    } else {
        // must be parammessage.
        result.append(printIdent(ident + 2) + "paramMsgStack.push(currentParamMsg);\n");
        result.append(printIdent(ident + 2) + "currentParamMsg = " + messageList + "[messageCount" + (ident) + "];\n");
    }
    result.append(printIdent(ident + 2) + "access.setCurrentOutMessage(currentOutMsg);\n");
    if (isSubMapped && isMappedMessage) {
        boolean isParam = false;
        result.append(printIdent(ident + 2) + "// Map message(s) to message\n");
        String messageListName = "messages" + ident;
        result.append(printIdent(ident + 2) + "List " + messageListName + " = null;\n");
        result.append(printIdent(ident + 2) + "inSelectionRef = MappingUtils.isSelection(currentInMsg, access.getInDoc(), \"" + ref + "\");\n");
        result.append(printIdent(ident + 2) + "if (!inSelectionRef)\n");
        result.append(printIdent(ident + 4) + messageListName + " = MappingUtils.getMessageList(currentInMsg, access.getInDoc(), \"" + ref + "\", \"" + "" + "\", currentMap, currentParamMsg,access);\n");
        result.append(printIdent(ident + 2) + "else\n");
        result.append(printIdent(ident + 4) + messageListName + " = MappingUtils.getSelectedItems(currentInMsg, access.getInDoc(), \"" + ref + "\");\n");
        String loopCounterName = "j" + subObjectCounter++;
        variableClipboard.add("int " + loopCounterName + ";\n");
        result.append(printIdent(ident + 2) + "for (" + loopCounterName + " = 0; " + loopCounterName + " < " + messageListName + ".size(); " + loopCounterName + "++) {\n if (!kill){\n");
        // currentInMsg, inMsgStack
        ident += 4;
        result.append(printIdent(ident) + "inMsgStack.push(currentInMsg);\n");
        if (isParam) {
            result.append(printIdent(ident) + "paramMsgStack.push(currentParamMsg);\n");
        }
        result.append(printIdent(ident) + "inSelectionRefStack.push(new Boolean(inSelectionRef));\n");
        if (isParam) {
            result.append(printIdent(ident) + "if (!inSelectionRef)\n");
            result.append(printIdent(ident + 2) + "currentParamMsg = (Message) " + messageListName + ".get(" + loopCounterName + ");\n");
        }
        result.append(printIdent(ident) + "if (!inSelectionRef)\n");
        result.append(printIdent(ident + 2) + "currentInMsg = (Message) " + messageListName + ".get(" + loopCounterName + ");\n");
        result.append(printIdent(ident) + "else \n");
        // currentSelection.
        result.append(printIdent(ident + 2) + "currentSelection = (Selection) " + messageListName + ".get(" + loopCounterName + ");\n");
        // If filter is specified, evaluate filter first:
        if (!filter.equals("")) {
            result.append(printIdent(ident + 4) + "if (inSelectionRef || Condition.evaluate(" + replaceQuotes(filter) + ", access.getInDoc(), currentMap, currentInMsg, currentParamMsg,access)) {\n");
            ident += 2;
        }
        if (n.getNodeName().equals("message")) {
            result.append(printIdent(ident + 4) + "outMsgStack.push(currentOutMsg);\n");
            result.append(printIdent(ident + 4) + "currentOutMsg = MappingUtils.getMessageObject(\"" + MappingUtils.getBaseMessageName(messageName) + "\", currentOutMsg, true, access.getOutputDoc(), false, \"\", " + "-1" + ");\n");
            result.append(printIdent(ident + 4) + "access.setCurrentOutMessage(currentOutMsg);\n");
        } else if (n.getNodeName().equals("loop")) {
        // do nothing.
        } else {
            // parammessage.
            result.append(printIdent(ident + 4) + "paramMsgStack.push(currentParamMsg);\n");
            result.append(printIdent(ident + 4) + "currentParamMsg = MappingUtils.getMessageObject(\"" + MappingUtils.getBaseMessageName(messageName) + "\", currentParamMsg, true, access.getInDoc(), false, \"\", " + "-1" + ");\n");
        }
        result.append(printIdent(ident) + "try {\n");
        ident = ident + 2;
        NodeList children = nextElt.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            if (children.item(i) instanceof Element) {
                result.append(compile(ident + 4, children.item(i), className, objectName, deps, tenant));
            }
        }
        ident = ident - 2;
        result.append(printIdent(ident) + "} catch (Exception e" + ident + ") {\n");
        result.append(printIdent(ident) + "}\n");
        if (n.getNodeName().equals("message")) {
            result.append(printIdent(ident + 2) + "currentOutMsg = (Message) outMsgStack.pop();\n");
            result.append(printIdent(ident + 2) + "access.setCurrentOutMessage(currentOutMsg);\n");
        } else if (n.getNodeName().equals("loop")) {
        // do  nothing.
        } else {
            result.append(printIdent(ident) + "currentParamMsg = (Message) paramMsgStack.pop();\n");
        }
        if (filter != null && !filter.equals("")) {
            ident -= 2;
            result.append(printIdent(ident + 4) + "}\n");
        }
        result.append(printIdent(ident) + "currentInMsg = (Message) inMsgStack.pop();\n");
        if (isParam) {
            result.append(printIdent(ident) + "currentParamMsg = (Message) paramMsgStack.pop();\n");
        }
        result.append(printIdent(ident) + "inSelectionRef = ((Boolean) inSelectionRefStack.pop()).booleanValue();\n");
        result.append(printIdent(ident) + "currentSelection = null;\n");
        ident -= 4;
        result.append(printIdent(ident + 2) + "}\n} // FOR loop for " + loopCounterName + "\n");
    } else if (isSubMapped && isArrayAttr) {
        type = Message.MSG_TYPE_ARRAY_ELEMENT;
        String lengthName = "length" + (lengthCounter++);
        String mappableArrayName = "mappableObject" + (objectCounter++);
        boolean isDomainObjectMapper = false;
        contextClassStack.push(contextClass);
        String subClassName = null;
        NodeList children = nextElt.getChildNodes();
        try {
            subClassName = MappingUtils.getFieldType(contextClass, ref);
            contextClass = Class.forName(subClassName, false, loader);
        } catch (Exception e) {
            isDomainObjectMapper = contextClass.isAssignableFrom(DomainObjectMapper.class);
            subClassName = "com.dexels.navajo.mapping.bean.DomainObjectMapper";
            contextClass = com.dexels.navajo.mapping.bean.DomainObjectMapper.class;
            if (isDomainObjectMapper) {
                type = "java.lang.Object";
            } else {
                throw new MappingException("Could not find adapter: " + subClassName);
            }
        }
        addDependency("dependentObjects.add( new JavaDependency( -1, \"" + subClassName + "\"));\n", "JAVA" + subClassName);
        // Extract ref....
        if (mapPath == null) {
            if (isDomainObjectMapper) {
                result.append(printIdent(ident + 2) + "try {\n");
                result.append(printIdent(ident + 2) + mappableArrayName + " = com.dexels.navajo.mapping.bean.DomainObjectMapper.createArray( (Object []) ((" + className + ") currentMap.myObject).getDomainObjectAttribute(\"" + ref + "\", null) ) ;\n");
                result.append(printIdent(ident + 2) + "} catch (Exception e) {\n");
                result.append(printIdent(ident + 2) + "  String subtype = ((" + className + ") currentMap.myObject).getDomainObjectAttribute(\"" + ref + "\", null).getClass().getName();\n");
                result.append(printIdent(ident + 2) + "  throw new Exception(\" Could not cast " + ref + "(type = \" + subtype + \") to an array\");\n");
                result.append(printIdent(ident + 2) + "}\n");
            } else {
                result.append(printIdent(ident + 2) + mappableArrayName + " = ((" + className + ") currentMap.myObject).get" + ((ref.charAt(0) + "").toUpperCase() + ref.substring(1)) + "();\n");
            }
        } else {
            if (isDomainObjectMapper) {
                result.append(printIdent(ident + 2) + "try {\n");
                result.append(printIdent(ident + 2) + mappableArrayName + " = com.dexels.navajo.mapping.bean.DomainObjectMapper.createArray( (Object []) ((" + className + ") findMapByPath( \"" + mapPath + "\")).getDomainObjectAttribute(\"" + ref + "\", null) ) ;\n");
                result.append(printIdent(ident + 2) + "} catch (Exception e) {\n");
                result.append(printIdent(ident + 2) + "  String subtype = ((" + className + ") findMapByPath( \"" + mapPath + "\")).getDomainObjectAttribute(\"" + ref + "\", null).getClass().getName();\n");
                result.append(printIdent(ident + 2) + "  throw new Exception(\" Could not cast " + ref + "(type = \" + subtype + \") to an array\");\n");
                result.append(printIdent(ident + 2) + "}\n");
            } else {
                result.append(printIdent(ident + 2) + mappableArrayName + " = ((" + className + ") findMapByPath( \"" + mapPath + "\")).get" + ((ref.charAt(0) + "").toUpperCase() + ref.substring(1)) + "();\n");
            }
        }
        String mappableArrayDefinition = (isIterator ? "java.util.Iterator<" + subClassName + "> " + mappableArrayName + " = null;\n" : "Object [] " + mappableArrayName + " = null;\n");
        variableClipboard.add(mappableArrayDefinition);
        if (!isIterator) {
            result.append(printIdent(ident + 2) + "int " + lengthName + " = " + "(" + mappableArrayName + " == null ? 0 : " + mappableArrayName + ".length);\n");
        }
        String startIndexVar = "startIndex" + (startIndexCounter++);
        result.append(printIdent(ident + 2) + "int " + startIndexVar + " = " + startIndex + ";\n");
        String startElementVar = "startWith" + (startElementCounter);
        String offsetElementVar = "offset" + (startElementCounter++);
        // Use a different than 0 as start for for loop.
        // result.append(printIdent(ident) + "count = " +
        result.append(printIdent(ident + 2) + "int " + startElementVar + " = " + (startElement.equals("") ? "0" : "((Integer) Expression.evaluate(\"" + startElement + "\", access.getInDoc(), currentMap, currentInMsg, currentParamMsg,null,null,getEvaluationParams()).value).intValue()") + ";\n");
        result.append(printIdent(ident + 2) + "int " + offsetElementVar + " = " + (elementOffset.equals("") ? "1" : "((Integer) Expression.evaluate(\"" + elementOffset + "\", access.getInDoc(), currentMap, currentInMsg, currentParamMsg,null,null,getEvaluationParams()).value).intValue()") + ";\n");
        if (!isIterator) {
            result.append(printIdent(ident + 2) + "for (int i" + (ident + 2) + " = " + startElementVar + "; i" + (ident + 2) + " < " + lengthName + "; i" + (ident + 2) + " = i" + (ident + 2) + "+" + offsetElementVar + ") {\n if (!kill) {\n");
        } else {
            result.append(printIdent(ident + 2) + "while (" + mappableArrayName + ".hasNext() ) {\n if (!kill) {\n");
        }
        result.append(printIdent(ident + 4) + "treeNodeStack.push(currentMap);\n");
        if (!isIterator) {
            result.append(printIdent(ident + 4) + "currentMap = new MappableTreeNode(access, currentMap, " + mappableArrayName + "[i" + (ident + 2) + "], true);\n");
        } else {
            result.append(printIdent(ident + 4) + "currentMap = new MappableTreeNode(access, currentMap, " + mappableArrayName + ".next(), true);\n");
        }
        result.append(printIdent(ident + 4) + "currentMap.setNavajoLineNr(" + n.getAttribute("linenr") + ");\n");
        // If filter is specified, evaluate filter first:
        if (filter != null && !filter.equals("")) {
            result.append(printIdent(ident + 4) + "if (Condition.evaluate(" + replaceQuotes(filter) + ", access.getInDoc(), currentMap, currentInMsg, currentParamMsg,access)) {\n");
            ident += 2;
        }
        if (n.getNodeName().equals("message")) {
            result.append(printIdent(ident + 4) + "outMsgStack.push(currentOutMsg);\n");
            result.append(printIdent(ident + 4) + "currentOutMsg = MappingUtils.getMessageObject(\"" + MappingUtils.getBaseMessageName(messageName) + "\", currentOutMsg, true, access.getOutputDoc(), false, \"\", " + ((startIndex == -1) ? "-1" : startIndexVar + "++") + ");\n");
            result.append(printIdent(ident + 4) + "access.setCurrentOutMessage(currentOutMsg);\n");
        } else if (n.getNodeName().equals("loop")) {
        // do nothing.
        } else {
            // parammessage.
            result.append(printIdent(ident + 4) + "paramMsgStack.push(currentParamMsg);\n");
            result.append(printIdent(ident + 4) + "currentParamMsg = MappingUtils.getMessageObject(\"" + MappingUtils.getBaseMessageName(messageName) + "\", currentParamMsg, true, access.getInDoc(), false, \"\", " + ((startIndex == -1) ? "-1" : startIndexVar + "++") + ");\n");
        }
        result.append(printIdent(ident) + "if ( currentMap.myObject instanceof Mappable ) {  ((Mappable) currentMap.myObject).load(access);}\n");
        String subObjectName = "mappableObject" + (objectCounter++);
        result.append(printIdent(ident + 4) + subObjectName + " = (" + subClassName + ") currentMap.myObject;\n");
        String objectDefinition = subClassName + " " + subObjectName + " = null;\n";
        variableClipboard.add(objectDefinition);
        for (int i = 0; i < children.getLength(); i++) {
            if (children.item(i) instanceof Element) {
                result.append(compile(ident + 4, children.item(i), subClassName, subObjectName, deps, tenant));
            }
        }
        contextClass = contextClassStack.pop();
        result.append(printIdent(ident + 2) + "MappingUtils.callStoreMethod(currentMap.myObject);\n");
        if (n.getNodeName().equals("message")) {
            result.append(printIdent(ident + 2) + "currentOutMsg = (Message) outMsgStack.pop();\n");
            result.append(printIdent(ident + 2) + "access.setCurrentOutMessage(currentOutMsg);\n");
        } else if (n.getNodeName().equals("loop")) {
        // do nothing.
        } else {
            result.append(printIdent(ident) + "currentParamMsg = (Message) paramMsgStack.pop();\n");
        }
        if (filter != null && !filter.equals("")) {
            ident -= 2;
            result.append(printIdent(ident + 4) + "}\n");
        }
        result.append(printIdent(ident + 2) + "currentMap.setEndtime();\n" + "currentMap = (MappableTreeNode) treeNodeStack.pop();\n");
        result.append(printIdent(ident + 2) + "}\n} // EOF Array map result from contextMap \n");
    } else if (isSubMapped) {
        if (n.getNodeName().equals("loop")) {
            throw new MappingException("Can only loop over arrays");
        }
        if (mapPath == null) {
            result.append(printIdent(ident + 2) + "treeNodeStack.push(currentMap);\n");
            if (className.equals("com.dexels.navajo.mapping.bean.DomainObjectMapper")) {
                result.append(printIdent(ident + 2) + "currentMap = new MappableTreeNode(access, currentMap, new com.dexels.navajo.mapping.bean.DomainObjectMapper( ((" + className + ") currentMap.myObject).getDomainObjectAttribute(\"" + ref + "\", null) ), false);\n");
            } else {
                result.append(printIdent(ident + 2) + "currentMap = new MappableTreeNode(access, currentMap, ((" + className + ") currentMap.myObject).get" + ((ref.charAt(0) + "").toUpperCase() + ref.substring(1)) + "(), false);\n");
            }
            result.append(printIdent(ident + 4) + "currentMap.setNavajoLineNr(" + n.getAttribute("linenr") + ");\n");
        } else {
            String localObjectName = "mappableObject" + (objectCounter++);
            result.append(printIdent(ident + 2) + "Object " + localObjectName + " = findMapByPath( \"" + mapPath + "\");\n");
            result.append(printIdent(ident + 2) + "treeNodeStack.push(currentMap);\n");
            if (className.equals("com.dexels.navajo.mapping.bean.DomainObjectMapper")) {
                result.append(printIdent(ident + 2) + "currentMap = new MappableTreeNode(access, currentMap,new com.dexels.navajo.mapping.bean.DomainObjectMapper( ((" + className + ")" + localObjectName + ").getDomainObjectAttribute(\"" + ref + "\", null) ), false);\n");
            } else {
                result.append(printIdent(ident + 2) + "currentMap = new MappableTreeNode(access, currentMap, ((" + className + ")" + localObjectName + ").get" + ((ref.charAt(0) + "").toUpperCase() + ref.substring(1)) + "(), false);\n");
            }
            result.append(printIdent(ident + 4) + "currentMap.setNavajoLineNr(" + n.getAttribute("linenr") + ");\n");
        }
        result.append(printIdent(ident + 2) + "if (currentMap.myObject != null) {\n");
        result.append(printIdent(ident) + "if ( currentMap.myObject instanceof Mappable ) {  ((Mappable) currentMap.myObject).load(access);}\n");
        contextClassStack.push(contextClass);
        String subClassName = null;
        if (DomainObjectMapper.class.isAssignableFrom(contextClass)) {
            subClassName = "com.dexels.navajo.mapping.bean.DomainObjectMapper";
            contextClass = DomainObjectMapper.class;
        } else {
            subClassName = MappingUtils.getFieldType(contextClass, ref);
            contextClass = null;
            try {
                contextClass = Class.forName(subClassName, false, loader);
            } catch (Exception e) {
                throw new MappingException("Could not find adapter " + subClassName);
            }
        }
        addDependency("dependentObjects.add( new JavaDependency( -1, \"" + subClassName + "\"));\n", "JAVA" + subClassName);
        String subObjectName = "mappableObject" + (objectCounter++);
        result.append(printIdent(ident + 4) + subObjectName + " = (" + subClassName + ") currentMap.myObject;\n");
        String objectDefinition = subClassName + " " + subObjectName + " = null;\n";
        variableClipboard.add(objectDefinition);
        NodeList children = nextElt.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            result.append(compile(ident + 4, children.item(i), subClassName, subObjectName, deps, tenant));
        }
        contextClass = contextClassStack.pop();
        result.append(printIdent(ident + 2) + "}\n");
        result.append(printIdent(ident + 2) + "currentMap.setEndtime();\n" + "MappingUtils.callStoreMethod(currentMap.myObject);\n" + "currentMap = (MappableTreeNode) treeNodeStack.pop();\n");
    } else {
        if (n.getNodeName().equals("loop")) {
            throw new MappingException("Can only loop over arrays");
        }
        NodeList children = n.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            result.append(compile(ident + 2, children.item(i), className, objectName, deps, tenant));
        }
    }
    if (n.getNodeName().equals("message")) {
        result.append(printIdent(ident) + "currentOutMsg = (Message) outMsgStack.pop();\n");
        result.append(printIdent(ident) + "access.setCurrentOutMessage(currentOutMsg);\n");
    } else if (n.getNodeName().equals("loop")) {
    // do nothing.
    } else {
        result.append(printIdent(ident) + "currentParamMsg = (Message) paramMsgStack.pop();\n");
    }
    result.append(printIdent(ident) + "}\n } // EOF messageList for \n");
    if (conditionClause) {
        ident -= 2;
        result.append(printIdent(ident) + "} // EOF message condition \n");
    }
    return result.toString();
}
Also used : ExtendDependency(com.dexels.navajo.mapping.compiler.meta.ExtendDependency) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) DomainObjectMapper(com.dexels.navajo.mapping.bean.DomainObjectMapper) UserException(com.dexels.navajo.script.api.UserException) TransformerException(javax.xml.transform.TransformerException) MappingException(com.dexels.navajo.script.api.MappingException) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) ParseException(com.dexels.navajo.parser.compiled.ParseException) KeywordException(com.dexels.navajo.mapping.compiler.meta.KeywordException) MetaCompileException(com.dexels.navajo.mapping.compiler.meta.MetaCompileException) IOException(java.io.IOException) SystemException(com.dexels.navajo.script.api.SystemException) CompilationException(com.dexels.navajo.script.api.CompilationException) MappingException(com.dexels.navajo.script.api.MappingException)

Example 5 with MappingException

use of com.dexels.navajo.script.api.MappingException in project navajo by Dexels.

the class TslCompiler method fieldNode.

@SuppressWarnings("unchecked")
public String fieldNode(int ident, Element n, String className, String objectName, List<Dependency> dependencies, String tenant) throws UserException, MappingException, ClassNotFoundException, KeywordException, IOException, MetaCompileException, ParseException {
    StringBuilder result = new StringBuilder();
    String attributeOriginal = n.getAttribute("name");
    String condition = n.getAttribute("condition");
    String attribute = null;
    String mapPath = null;
    if (attributeOriginal.indexOf('/') != -1) {
        attribute = attributeOriginal.substring(attributeOriginal.lastIndexOf('/') + 1, attributeOriginal.length());
        mapPath = attributeOriginal.substring(0, attributeOriginal.lastIndexOf('/'));
    } else {
        attribute = attributeOriginal;
    }
    if (attribute == null || attribute.equals(""))
        throw new UserException("Name attribute is required for field tags");
    condition = (condition == null) ? "" : condition;
    String totalMethodName = "set" + (attribute.charAt(0) + "").toUpperCase() + attribute.substring(1, attribute.length());
    String methodName = null;
    if (totalMethodName.indexOf('/') != -1) {
        methodName = totalMethodName.substring(totalMethodName.lastIndexOf('/') + 1, totalMethodName.length());
    } else {
        methodName = totalMethodName;
    }
    NodeList children = n.getChildNodes();
    if (!condition.equals("")) {
        result.append(printIdent(ident) + "if (Condition.evaluate(" + replaceQuotes(condition) + ", access.getInDoc(), currentMap, currentInMsg, currentParamMsg,access)) { \n");
    } else {
        result.append(printIdent(ident) + "if (true) {\n");
    }
    // Expression nodes.
    boolean isMapped = false;
    Element mapNode = null;
    int exprCount = countNodes(children, "expression");
    List<String> exprValues = new ArrayList<>();
    for (int i = 0; i < children.getLength(); i++) {
        // Has condition
        if (children.item(i).getNodeName().equals("expression")) {
            result.append(expressionNode(ident + 2, (Element) children.item(i), --exprCount, className, objectName));
            Boolean b = false;
            exprValues.add((String) getExpressionValue(((Element) children.item(i)), b)[0]);
        } else if (children.item(i).getNodeName().equals("map")) {
            isMapped = true;
            mapNode = (Element) children.item(i);
        }
    }
    if (!isMapped) {
        String castedValue = "";
        boolean isDomainObjectMapper = false;
        try {
            Class localContextClass = null;
            try {
                if (mapPath != null) {
                    localContextClass = locateContextClass(mapPath, 0);
                } else {
                    if (Version.osgiActive()) {
                        localContextClass = resolveClassUsingService(className);
                    } else {
                        localContextClass = Class.forName(className, false, loader);
                    }
                }
            } catch (Exception e) {
                throw new UserException("Could not find adapter: " + className, e);
            }
            addDependency("dependentObjects.add( new JavaDependency( -1, \"" + className + "\"));\n", "JAVA" + className);
            dependencies.add(new JavaDependency(-1, className));
            String type = null;
            try {
                type = MappingUtils.getFieldType(localContextClass, attribute);
                checkDependentFieldResource(localContextClass, attribute, exprValues, dependencies);
            } catch (Exception e) {
                isDomainObjectMapper = localContextClass.isAssignableFrom(DomainObjectMapper.class);
                if (isDomainObjectMapper) {
                    type = "java.lang.Object";
                } else {
                    throw new UserException("Could not find field: " + attribute + " in adapter " + localContextClass.getName(), e);
                }
            }
            if (type.equals("java.lang.String")) {
                castedValue = "(String) sValue";
            } else if (type.equals("com.dexels.navajo.document.types.ClockTime")) {
                castedValue = "(com.dexels.navajo.document.types.ClockTime) sValue";
            } else if (type.equals("int")) {
                castedValue = "((Integer) sValue).intValue()";
            } else if (type.equals("double")) {
                castedValue = "((Double) sValue).doubleValue()";
            } else if (type.equals("java.util.Date")) {
                castedValue = "((java.util.Date) sValue)";
            } else if (type.equals("boolean")) {
                castedValue = "((Boolean) sValue).booleanValue()";
            } else if (type.equals("float")) {
                // sValue is never float,
                // internally always Double!
                castedValue = "(new Float(sValue+\"\")).floatValue()";
            } else if (type.equals("com.dexels.navajo.document.types.Binary")) {
                castedValue = "((com.dexels.navajo.document.types.Binary) sValue)";
            } else if (type.equals("com.dexels.navajo.document.types.Money")) {
                castedValue = "((com.dexels.navajo.document.types.Money) sValue)";
            } else if (type.equals("com.dexels.navajo.document.types.Percentage")) {
                castedValue = "((com.dexels.navajo.document.types.Percentage) sValue)";
            } else if (type.equals("java.lang.Integer")) {
                castedValue = "((Integer) sValue)";
            } else if (type.equals("java.lang.Long")) {
                castedValue = "((Long) sValue)";
            } else if (type.equals("java.lang.Float")) {
                // sValue is never
                // float,
                // internally
                // always
                // Double!
                castedValue = "new Float(sValue+\"\")";
            } else if (type.equals("java.lang.Double")) {
                castedValue = "((Double) sValue)";
            } else if (type.equals("java.lang.Boolean")) {
                castedValue = "((Boolean) sValue)";
            } else if (type.equals("java.util.List")) {
                castedValue = "((List<Object>) sValue)";
            } else {
                castedValue = "sValue";
            }
        } catch (UserException e) {
            throw new UserException(-1, "Error in script: could not find mappable object: " + className, e);
        }
        if (mapPath != null) {
            if (!isDomainObjectMapper) {
                result.append(printIdent(ident + 2) + "((" + locateContextClass(mapPath, 0).getName() + ")findMapByPath(\"" + mapPath + "\"))." + methodName + "(" + castedValue + ");\n");
            } else {
                result.append(printIdent(ident + 2) + "((" + locateContextClass(mapPath, 0).getName() + ")findMapByPath(\"" + mapPath + "\")).setDomainObjectAttribute(\"" + attribute + "\"," + castedValue + ");\n");
            }
        } else {
            if (!isDomainObjectMapper) {
                result.append(printIdent(ident + 2) + objectName + "." + methodName + "(" + castedValue + ");\n");
            } else {
                // set attribute in excluded fields.
                // USE INTROSPECTION METHOD TO CALL METHOD ON PROXIED
                // DOMAIN OBJECT...
                result.append(printIdent(ident + 2) + objectName + ".setDomainObjectAttribute(\"" + attribute + "\"," + castedValue + ");\n");
            }
        }
    } else {
        // Mappable)
        if (mapNode == null) {
            throw new IllegalStateException("Unexpected null mapNode");
        }
        String ref = mapNode.getAttribute("ref");
        boolean isParam = false;
        if (ref.indexOf("[") != -1) {
            // remove square brackets...
            ref = ref.replace('[', ' ');
            ref = ref.replace(']', ' ');
            ref = ref.trim();
        }
        if (ref.startsWith("/@")) {
            // replace @ with __parms__ 'parameter'
            // message indication.
            ref = ref.replaceAll("@", "__parms__/");
            isParam = true;
        }
        String filter = mapNode.getAttribute("filter");
        filter = (filter == null) ? "" : filter;
        result.append(printIdent(ident + 2) + "// And by the way, my filter is " + filter + "\n");
        result.append(printIdent(ident + 2) + "// Map message(s) to field\n");
        String messageListName = "messages" + ident;
        result.append(printIdent(ident + 2) + "List " + messageListName + " = null;\n");
        result.append(printIdent(ident + 2) + "inSelectionRef = MappingUtils.isSelection(currentInMsg, access.getInDoc(), \"" + ref + "\");\n");
        result.append(printIdent(ident + 2) + "if (!inSelectionRef)\n");
        result.append(printIdent(ident + 4) + messageListName + " = MappingUtils.getMessageList(currentInMsg, access.getInDoc(), \"" + ref + "\", \"" + "" + "\", currentMap, currentParamMsg,access);\n");
        result.append(printIdent(ident + 2) + "else\n");
        result.append(printIdent(ident + 4) + messageListName + " = MappingUtils.getSelectedItems(currentInMsg, access.getInDoc(), \"" + ref + "\");\n");
        contextClassStack.push(contextClass);
        Class localContextClass = null;
        try {
            if (mapPath != null) {
                localContextClass = locateContextClass(mapPath, 1);
            } else {
                localContextClass = contextClass;
            }
        } catch (Exception e) {
            throw new UserException("Could not find adapter: " + className, e);
        }
        String type = null;
        try {
            type = MappingUtils.getFieldType(localContextClass, attribute);
        } catch (Exception e) {
            throw new UserException("Could not find field: " + attribute + " in adapter " + localContextClass.getName() + ", mappath = " + mapPath);
        }
        /**
         * END.
         */
        boolean isArray = MappingUtils.isArrayAttribute(localContextClass, attribute);
        try {
            contextClass = Class.forName(type, false, loader);
        } catch (Exception e) {
            throw new UserException("Could not find adapter: " + type);
        }
        addDependency("dependentObjects.add( new JavaDependency( -1, \"" + type + "\"));\n", "JAVA" + type);
        if (isArray) {
            String subObjectsName = "subObject" + subObjectCounter;
            String loopCounterName = "j" + subObjectCounter;
            subObjectCounter++;
            String objectDefinition = type + " [] " + subObjectsName + " = null;\n";
            variableClipboard.add(objectDefinition);
            variableClipboard.add("int " + loopCounterName + ";\n");
            result.append(printIdent(ident + 2) + subObjectsName + " = new " + type + "[" + messageListName + ".size()];\n");
            result.append(printIdent(ident + 2) + "for (" + loopCounterName + " = 0; " + loopCounterName + " < " + messageListName + ".size(); " + loopCounterName + "++) {\n if (!kill){\n");
            // currentInMsg, inMsgStack
            ident += 4;
            result.append(printIdent(ident) + "inMsgStack.push(currentInMsg);\n");
            if (isParam) {
                result.append(printIdent(ident) + "paramMsgStack.push(currentParamMsg);\n");
            }
            result.append(printIdent(ident) + "inSelectionRefStack.push(new Boolean(inSelectionRef));\n");
            if (isParam) {
                result.append(printIdent(ident) + "if (!inSelectionRef)\n");
                result.append(printIdent(ident + 2) + "currentParamMsg = (Message) " + messageListName + ".get(" + loopCounterName + ");\n");
            }
            result.append(printIdent(ident) + "if (!inSelectionRef)\n");
            result.append(printIdent(ident + 2) + "currentInMsg = (Message) " + messageListName + ".get(" + loopCounterName + ");\n");
            result.append(printIdent(ident) + "else\n");
            // currentSelection.
            result.append(printIdent(ident + 2) + "currentSelection = (Selection) " + messageListName + ".get(" + loopCounterName + ");\n");
            if (!filter.equals("")) {
                result.append(printIdent(ident + 4) + "if (inSelectionRef || Condition.evaluate(" + replaceQuotes(filter) + ", access.getInDoc(), currentMap, currentInMsg, currentParamMsg,access)) {\n");
                ident += 2;
            }
            result.append(printIdent(ident) + "treeNodeStack.push(currentMap);\n");
            result.append(printIdent(ident) + "currentMap = new MappableTreeNode(access, currentMap, classLoader.getClass(\"" + type + "\").newInstance(), false);\n");
            result.append(printIdent(ident + 4) + "currentMap.setNavajoLineNr(" + n.getAttribute("linenr") + ");\n");
            result.append(printIdent(ident) + "if ( currentMap.myObject instanceof Mappable ) {  ((Mappable) currentMap.myObject).load(access);}\n");
            result.append(printIdent(ident) + subObjectsName + "[" + loopCounterName + "] = (" + type + ") currentMap.myObject;\n");
            result.append(printIdent(ident) + "try {\n");
            ident = ident + 2;
            children = mapNode.getChildNodes();
            for (int i = 0; i < children.getLength(); i++) {
                result.append(compile(ident + 2, children.item(i), type, subObjectsName + "[" + loopCounterName + "]", dependencies, tenant));
            }
            ident = ident - 2;
            result.append(printIdent(ident) + "} catch (Exception e" + ident + ") {\n");
            result.append(printIdent(ident + 2) + "MappingUtils.callKillOrStoreMethod( " + subObjectsName + "[" + loopCounterName + "], e" + ident + ");\n");
            result.append(printIdent(ident + 2) + "throw e" + ident + ";\n");
            result.append(printIdent(ident) + "}\n");
            result.append(printIdent(ident) + "MappingUtils.callStoreMethod(" + subObjectsName + "[" + loopCounterName + "]);\n");
            result.append(printIdent(ident) + "currentMap.setEndtime();\ncurrentMap = (MappableTreeNode) treeNodeStack.pop();\n");
            if (!filter.equals("")) {
                ident -= 2;
                result.append(printIdent(ident + 4) + "}\n");
            }
            result.append(printIdent(ident) + "currentInMsg = (Message) inMsgStack.pop();\n");
            if (isParam) {
                result.append(printIdent(ident) + "currentParamMsg = (Message) paramMsgStack.pop();\n");
            }
            result.append(printIdent(ident) + "inSelectionRef = ((Boolean) inSelectionRefStack.pop()).booleanValue();\n");
            result.append(printIdent(ident) + "currentSelection = null;\n");
            ident -= 4;
            result.append(printIdent(ident + 2) + "}\n} // FOR loop for " + loopCounterName + "\n");
            if (mapPath == null) {
                result.append(printIdent(ident + 2) + objectName + "." + methodName + "(" + subObjectsName + ");\n");
            } else {
                result.append(printIdent(ident + 2) + "((" + locateContextClass(mapPath, 1).getName() + ") findMapByPath(\"" + mapPath + "\"))." + methodName + "(" + subObjectsName + ");\n");
            }
        } else {
            // Not an array type field, but single Mappable object.
            // Push current mappable object on stack.
            result.append(printIdent(ident) + "treeNodeStack.push(currentMap);\n");
            // Create instance of object.
            result.append(printIdent(ident) + "currentMap = new MappableTreeNode(access, currentMap, classLoader.getClass(\"" + type + "\").newInstance(), false);\n");
            result.append(printIdent(ident + 4) + "currentMap.setNavajoLineNr(" + n.getAttribute("linenr") + ");\n");
            // Create local variable to address new object.
            String subObjectsName = "subObject" + subObjectCounter;
            subObjectCounter++;
            // push currentInMsg, currentParamMsg and inSelectionRef
            ident += 4;
            result.append(printIdent(ident) + "inMsgStack.push(currentInMsg);\n");
            if (isParam) {
                result.append(printIdent(ident) + "paramMsgStack.push(currentParamMsg);\n");
            }
            result.append(printIdent(ident) + "inSelectionRefStack.push(new Boolean(inSelectionRef));\n");
            if (isParam) {
                result.append(printIdent(ident) + "if (!inSelectionRef)\n");
                result.append(printIdent(ident + 2) + "currentParamMsg = (Message) " + messageListName + ".get(0);\n");
            }
            result.append(printIdent(ident) + "if (!inSelectionRef)\n");
            result.append(printIdent(ident + 2) + "currentInMsg = (Message) " + messageListName + ".get(0);\n");
            result.append(printIdent(ident) + "else\n");
            // currentSelection.
            result.append(printIdent(ident + 2) + "currentSelection = (Selection) " + messageListName + ".get(0);\n");
            // Call load on object.
            result.append(printIdent(ident) + "if ( currentMap.myObject instanceof Mappable) { ((Mappable) currentMap.myObject).load(access);}\n");
            // Assign local variable reference.
            result.append(printIdent(ident) + type + " " + subObjectsName + " = (" + type + ") currentMap.myObject;\n");
            result.append(printIdent(ident) + "try {\n");
            ident = ident + 2;
            // Recursively dive into children.
            children = mapNode.getChildNodes();
            for (int i = 0; i < children.getLength(); i++) {
                result.append(compile(ident + 2, children.item(i), type, subObjectsName, dependencies, tenant));
            }
            ident = ident - 2;
            result.append(printIdent(ident) + "} catch (Exception e" + ident + ") {\n");
            result.append(printIdent(ident + 2) + "MappingUtils.callKillOrStoreMethod( " + subObjectsName + ", e" + ident + ");\n");
            result.append(printIdent(ident + 2) + "throw e" + ident + ";\n");
            result.append(printIdent(ident) + "}\n");
            result.append(printIdent(ident) + "MappingUtils.callStoreMethod(" + subObjectsName + ");\n");
            result.append(printIdent(ident) + "currentInMsg = (Message) inMsgStack.pop();\n");
            if (isParam) {
                result.append(printIdent(ident) + "currentParamMsg = (Message) paramMsgStack.pop();\n");
            }
            result.append(printIdent(ident) + "inSelectionRef = ((Boolean) inSelectionRefStack.pop()).booleanValue();\n");
            result.append(printIdent(ident) + "currentSelection = null;\n");
            result.append(printIdent(ident) + "currentMap.setEndtime();\ncurrentMap = (MappableTreeNode) treeNodeStack.pop();\n");
            if (mapPath == null) {
                result.append(printIdent(ident + 2) + objectName + "." + methodName + "(" + subObjectsName + ");\n");
            } else {
                result.append(printIdent(ident + 2) + "((" + locateContextClass(mapPath, 1).getName() + ") findMapByPath(\"" + mapPath + "\"))." + methodName + "(" + subObjectsName + ");\n");
            }
        }
        contextClass = contextClassStack.pop();
    }
    result.append(printIdent(ident) + "}\n");
    return result.toString();
}
Also used : JavaDependency(com.dexels.navajo.mapping.compiler.meta.JavaDependency) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) UserException(com.dexels.navajo.script.api.UserException) TransformerException(javax.xml.transform.TransformerException) MappingException(com.dexels.navajo.script.api.MappingException) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) ParseException(com.dexels.navajo.parser.compiled.ParseException) KeywordException(com.dexels.navajo.mapping.compiler.meta.KeywordException) MetaCompileException(com.dexels.navajo.mapping.compiler.meta.MetaCompileException) IOException(java.io.IOException) SystemException(com.dexels.navajo.script.api.SystemException) CompilationException(com.dexels.navajo.script.api.CompilationException) UserException(com.dexels.navajo.script.api.UserException)

Aggregations

MappingException (com.dexels.navajo.script.api.MappingException)10 UserException (com.dexels.navajo.script.api.UserException)4 Method (java.lang.reflect.Method)4 SystemException (com.dexels.navajo.script.api.SystemException)3 TransformerException (javax.xml.transform.TransformerException)3 Element (org.w3c.dom.Element)3 NodeList (org.w3c.dom.NodeList)3 Message (com.dexels.navajo.document.Message)2 TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)2 KeywordException (com.dexels.navajo.mapping.compiler.meta.KeywordException)2 MetaCompileException (com.dexels.navajo.mapping.compiler.meta.MetaCompileException)2 ParseException (com.dexels.navajo.parser.compiled.ParseException)2 CompilationException (com.dexels.navajo.script.api.CompilationException)2 IOException (java.io.IOException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ArrayList (java.util.ArrayList)2 NavajoException (com.dexels.navajo.document.NavajoException)1 Property (com.dexels.navajo.document.Property)1 Selection (com.dexels.navajo.document.Selection)1 Binary (com.dexels.navajo.document.types.Binary)1