Search in sources :

Example 1 with ParseException

use of com.dexels.navajo.parser.compiled.ParseException in project navajo by Dexels.

the class TslCompiler method propertyNode.

public String propertyNode(int ident, Element n, boolean canBeSubMapped, String className, String objectName) throws UserException, ParseException {
    StringBuilder result = new StringBuilder();
    String propertyName = n.getAttribute("name");
    String direction = n.getAttribute("direction");
    String type = n.getAttribute("type");
    String subtype = n.getAttribute("subtype");
    String lengthStr = n.getAttribute("length");
    int length = ((lengthStr != null && !lengthStr.equals("")) ? Integer.parseInt(lengthStr) : -1);
    String value = n.getAttribute("value");
    String description = n.getAttribute("description");
    String cardinality = n.getAttribute("cardinality");
    String condition = n.getAttribute("condition");
    String key = n.getAttribute("key");
    String reference = n.getAttribute("reference");
    String extendsProp = n.getAttribute("extends");
    String bindProp = n.getAttribute("bind");
    String methodProp = n.getAttribute("method");
    value = (value == null) || (value.equals("")) ? "" : value;
    type = (type == null) ? "" : type;
    subtype = (subtype == null) ? "" : subtype;
    description = (description == null) ? "" : description;
    cardinality = (cardinality == null || cardinality.equals("")) ? "1" : cardinality;
    condition = (condition == null) ? "" : condition;
    methodProp = (methodProp == null) ? "" : methodProp;
    boolean conditionClause = false;
    if (!condition.equals("")) {
        conditionClause = true;
        result.append(printIdent(ident) + "if (Condition.evaluate(" + replaceQuotes(condition) + ", access.getInDoc(), currentMap, currentInMsg, currentParamMsg,access)) { \n");
        ident += 2;
    }
    NodeList children = n.getChildNodes();
    boolean hasChildren = false;
    boolean isSelection = false;
    boolean isMapped = false;
    Element mapNode = null;
    StringBuilder optionItems = new StringBuilder();
    int exprCount = countNodes(children, "expression");
    if ("".equals(value)) {
        result.append(printIdent(ident) + "matchingConditions = false;\n");
    }
    Class localContextClass = null;
    for (int i = 0; i < children.getLength(); i++) {
        hasChildren = true;
        // Has condition
        if (children.item(i).getNodeName().equals("expression")) {
            result.append(expressionNode(ident, (Element) children.item(i), --exprCount, className, objectName));
        } else if (children.item(i).getNodeName().equals("option")) {
            isSelection = true;
            String optionCondition = ((Element) children.item(i)).getAttribute("condition");
            String optionName = ((Element) children.item(i)).getAttribute("name");
            String optionValue = ((Element) children.item(i)).getAttribute("value");
            String selectedValue = ((Element) children.item(i)).getAttribute("selected");
            type = "selection";
            String selected = selectedValue;
            if (!(selected.equals("0") || selected.equals("1"))) {
                selected = "Expression.evaluate(" + replaceQuotes(selectedValue) + ", access.getInDoc(), currentMap, currentInMsg, currentParamMsg, currentSelection, null,getEvaluationParams()).value";
            }
            // Created condition statement if condition is given!
            String conditional = (optionCondition != null && !optionCondition.equals("")) ? "if (Condition.evaluate(" + replaceQuotes(optionCondition) + ", access.getInDoc(), currentMap, currentInMsg, currentParamMsg,access))\n" : "";
            optionItems.append(conditional + "p.addSelection(NavajoFactory.getInstance().createSelection(access.getOutputDoc(), \"" + optionName + "\", \"" + optionValue + "\", " + selected + "));\n");
        } else if (children.item(i).getNodeName().equals("map")) {
            // property!!!
            if (!canBeSubMapped) {
                throw new ParseException("This property can not be submapped: " + propertyName);
            }
            if (!type.equals("selection")) {
                throw new ParseException("Only selection properties can be submapped: " + propertyName);
            }
            mapNode = (Element) children.item(i);
            isMapped = true;
            isSelection = true;
        } else if (children.item(i) instanceof Element) {
            String tagValue = "<" + n.getNodeName() + " name=\"" + propertyName + "\">";
            throw new ParseException("Illegal child tag <" + children.item(i).getNodeName() + "> in " + tagValue + " (Check your script) ");
        }
    }
    if (!hasChildren || isSelection) {
        if (!isSelection) {
            result.append(printIdent(ident) + "sValue = new StringLiteral(\"" + value + "\");\n");
            result.append(printIdent(ident) + "matchingConditions = true;\n");
        } else {
            result.append(printIdent(ident) + "sValue = new String(\"" + value + "\");\n");
        }
        result.append(printIdent(ident) + "type = \"" + type + "\";\n");
    } else {
        if (!Property.EXPRESSION_LITERAL_PROPERTY.equals(type) && !Property.EXPRESSION_PROPERTY.equals(type)) {
            result.append(printIdent(ident) + "type = (sValue != null) ? MappingUtils.determineNavajoType(sValue) : \"" + type + "\";\n");
        } else {
            result.append(printIdent(ident) + "type = \"" + type + "\";\n");
        }
    }
    result.append(printIdent(ident) + "subtype = \"" + subtype + "\";\n");
    if (n.getNodeName().equals("property")) {
        result.append(printIdent(ident) + "p = MappingUtils.setProperty(false, currentOutMsg, \"" + propertyName + "\", sValue, type, subtype, \"" + direction + "\", \"" + description + "\", " + length + ", access.getOutputDoc(), access.getInDoc(), !matchingConditions);\n");
        if (!"".equals(key)) {
            result.append(printIdent(ident) + "p.setKey(\"" + key + "\");\n");
        }
        if (!"".equals(reference)) {
            result.append(printIdent(ident) + "p.setReference(\"" + reference + "\");\n");
        }
        if (!"".equals(extendsProp)) {
            result.append(printIdent(ident) + "p.setExtends(\"" + extendsProp + "\");\n");
        }
        if (!"".equals(bindProp)) {
            result.append(printIdent(ident) + "p.setBind(\"" + bindProp + "\");\n");
        }
        result.append(printIdent(ident) + "p.setMethod(\"" + methodProp + "\");\n");
    } else {
        // parameter
        result.append(printIdent(ident) + "MappingUtils.setProperty(true, currentParamMsg, \"" + propertyName + "\", sValue, type, subtype, \"" + direction + "\", \"" + description + "\", " + length + ", access.getOutputDoc(), access.getInDoc(), !matchingConditions);\n");
    }
    if (isMapped) {
        try {
            localContextClass = Class.forName(className, false, loader);
        } catch (Exception e) {
            throw new ParseException("Could not find adapter: " + className);
        }
        addDependency("dependentObjects.add( new JavaDependency( -1, \"" + className + "\"));\n", "JAVA" + className);
        if (mapNode == null) {
            throw new IllegalStateException("Unexpected null mapNode");
        }
        String ref = mapNode.getAttribute("ref");
        if (ref != null && !"".equals(ref)) {
            // replace $ for refs.
            ref = ref.replaceAll("\\$", "");
        }
        String filter = mapNode.getAttribute("filter");
        String mappableArrayName = "mappableObject" + (objectCounter++);
        result.append(printIdent(ident + 2) + mappableArrayName + " = " + objectName + ".get" + ((ref.charAt(0) + "").toUpperCase() + ref.substring(1)) + "();\n");
        String mappableArrayDefinition = "Object [] " + mappableArrayName + " = null;\n";
        variableClipboard.add(mappableArrayDefinition);
        result.append(printIdent(ident + 2) + "for (int i" + (ident + 2) + " = 0; i" + (ident + 2) + " < " + mappableArrayName + ".length; i" + (ident + 2) + "++) {\n if (!kill) {\n");
        result.append(printIdent(ident + 4) + "treeNodeStack.push(currentMap);\n");
        result.append(printIdent(ident + 4) + "currentMap = new MappableTreeNode(access, currentMap, " + mappableArrayName + "[i" + (ident + 2) + "], true);\n");
        result.append(printIdent(ident + 4) + "currentMap.setNavajoLineNr(" + n.getAttribute("linenr") + ");\n");
        // If filter is specified, evaluate filter first (31/1/2007)
        if (!filter.equals("")) {
            result.append(printIdent(ident + 4) + "if (Condition.evaluate(" + replaceQuotes(filter) + ", access.getInDoc(), currentMap, currentInMsg, currentParamMsg,access)) {\n");
            ident += 2;
        }
        result.append(printIdent(ident + 4) + "String optionName = \"\";\n");
        result.append(printIdent(ident + 4) + "String optionValue = \"\";\n");
        result.append(printIdent(ident + 4) + "boolean optionSelected = false;\n");
        children = mapNode.getChildNodes();
        String subClassName = MappingUtils.getFieldType(localContextClass, ref);
        String subClassObjectName = "mappableObject" + (objectCounter++);
        result.append(printIdent(ident + 4) + subClassObjectName + " = (" + subClassName + ") currentMap.myObject;\n");
        String objectDefinition = subClassName + " " + subClassObjectName + " = null;\n";
        variableClipboard.add(objectDefinition);
        for (int i = 0; i < children.getLength(); i++) {
            if (children.item(i).getNodeName().equals("property")) {
                Element elt = (Element) children.item(i);
                String subPropertyName = elt.getAttribute("name");
                if (!(subPropertyName.equals("name") || subPropertyName.equals("value") || subPropertyName.equals("selected"))) {
                    throw new ParseException("Only 'name' or 'value' named properties expected when submapping a 'selection' property");
                }
                NodeList expressions = elt.getChildNodes();
                int leftOver = countNodes(expressions, "expression");
                for (int j = 0; j < expressions.getLength(); j++) {
                    if ((expressions.item(j) instanceof Element) && expressions.item(j).getNodeName().equals("expression")) {
                        result.append(expressionNode(ident + 4, (Element) expressions.item(j), --leftOver, subClassName, subClassObjectName));
                    }
                }
                if (subPropertyName.equals("name")) {
                    result.append(printIdent(ident + 4) + "optionName = (sValue != null) ? sValue + \"\" : \"\";\n");
                } else if (subPropertyName.equals("value")) {
                    result.append(printIdent(ident + 4) + "optionValue = (sValue != null) ? sValue + \"\" : \"\";\n");
                } else {
                    result.append(printIdent(ident + 4) + "optionSelected = (sValue != null) ? ((Boolean) sValue).booleanValue() : false;\n");
                }
            } else if (children.item(i).getNodeName().equals("debug")) {
                result.append(debugNode(ident, (Element) children.item(i)));
            } else if (children.item(i).getNodeName().equals("param")) {
                result.append(propertyNode(ident, (Element) children.item(i), false, className, objectName));
            } else if (children.item(i) instanceof Element) {
                throw new ParseException("<property> tag expected while sub-mapping a selection property: " + children.item(i).getNodeName());
            }
        }
        result.append(printIdent(ident + 4) + "p.addSelection(NavajoFactory.getInstance().createSelection(access.getOutputDoc(), optionName, optionValue, optionSelected));\n");
        // If filter is specified add closing bracket (31/1/2007)
        if (!filter.equals("")) {
            ident -= 2;
            result.append(printIdent(ident + 4) + "}\n");
        }
        result.append(printIdent(ident + 4) + "currentMap.setEndtime();\ncurrentMap = (MappableTreeNode) treeNodeStack.pop();\n");
        result.append(printIdent(ident + 2) + "}\n} // EOF Array map result to property\n");
    }
    if (isSelection) {
        // Set selection property stuff.
        result.append(optionItems.toString());
    }
    if (n.getNodeName().equals("property")) {
        result.append("p.setCardinality(\"" + cardinality + "\");\n");
    }
    if (conditionClause) {
        ident -= 2;
        result.append(printIdent(ident) + "} // EOF property condition \n");
    }
    return result.toString();
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ParseException(com.dexels.navajo.parser.compiled.ParseException) 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)

Example 2 with ParseException

use of com.dexels.navajo.parser.compiled.ParseException in project navajo by Dexels.

the class TslCompiler method mapNode.

public String mapNode(int ident, Element n, List<Dependency> deps, String tenant) throws ParseException, ClassNotFoundException, KeywordException, UserException, IOException, MetaCompileException, MappingException {
    StringBuilder result = new StringBuilder();
    String object = n.getAttribute("object");
    String condition = n.getAttribute("condition");
    // If name, is specified it could be an AsyncMap.
    String name = n.getAttribute("name");
    boolean asyncMap = false;
    condition = (condition == null) ? "" : condition;
    boolean conditionClause = false;
    if (!condition.equals("")) {
        conditionClause = true;
        result.append(printIdent(ident) + "if (Condition.evaluate(" + replaceQuotes(condition) + ", access.getInDoc(), currentMap, currentInMsg, currentParamMsg,access)) { \n");
        ident += 2;
    }
    String className = object;
    if (className.equals("")) {
        throw new ParseException("Error in reading Map xml - found map with empty object! Line " + n.getAttribute("linenr") + ":" + n.getAttribute("startoffset"));
    }
    if (contextClass != null) {
        contextClassStack.push(contextClass);
    }
    contextClass = Class.forName(className, false, loader);
    addDependency("dependentObjects.add( new JavaDependency( -1, \"" + className + "\"));\n", "JAVA" + className);
    if (!name.equals("")) {
        // We have a potential async mappable object.
        if (contextClass.getSuperclass().getName().equals("com.dexels.navajo.mapping.AsyncMappable")) {
            asyncMap = true;
        } else {
            asyncMap = false;
        }
    }
    if (asyncMap) {
        String aoName = "ao" + asyncMapCounter;
        String headerName = "h" + asyncMapCounter;
        String asyncMapFinishedName = "asyncMapFinished" + asyncMapCounter;
        String callbackRefName = "callbackRef" + asyncMapCounter;
        String interruptTypeName = "interruptType" + asyncMapCounter;
        String asyncMapName = "asyncMap" + asyncMapCounter;
        String asyncStatusName = "asyncStatus" + asyncMapCounter;
        String resumeAsyncName = "resumeAsync" + asyncMapCounter;
        asyncMapCounter++;
        variableClipboard.add("boolean " + asyncMapName + ";\n");
        variableClipboard.add("Header " + headerName + ";\n");
        variableClipboard.add("String " + callbackRefName + ";\n");
        variableClipboard.add(className + " " + aoName + ";\n");
        variableClipboard.add("boolean " + asyncMapFinishedName + ";\n");
        variableClipboard.add("boolean " + resumeAsyncName + ";\n");
        variableClipboard.add("String " + asyncStatusName + ";\n");
        variableClipboard.add("String " + interruptTypeName + ";\n");
        result.append(printIdent(ident) + asyncMapName + " = true;\n");
        result.append(printIdent(ident) + headerName + " = access.getInDoc().getHeader();\n");
        result.append(printIdent(ident) + callbackRefName + " = " + headerName + ".getCallBackPointer(\"" + name + "\");\n");
        result.append(printIdent(ident) + aoName + " = null;\n");
        result.append(printIdent(ident) + asyncMapFinishedName + " = false;\n");
        result.append(printIdent(ident) + resumeAsyncName + " = false;\n");
        result.append(printIdent(ident) + asyncStatusName + " = \"request\";\n\n");
        result.append(printIdent(ident) + "if (" + callbackRefName + " != null) {\n");
        ident += 2;
        result.append(printIdent(ident) + aoName + " = (" + className + ") DispatcherFactory.getInstance().getNavajoConfig().getAsyncStore().getInstance(" + callbackRefName + ");\n");
        result.append(printIdent(ident) + interruptTypeName + " = " + headerName + ".getCallBackInterupt(\"" + name + "\");\n");
        result.append(printIdent(ident) + " if (" + aoName + " == null) {\n " + "  throw new UserException( -1, \"Asynchronous object reference instantiation error: no sych instance (perhaps cleaned up?)\");\n}\n");
        result.append(printIdent(ident) + "if (" + interruptTypeName + ".equals(\"kill\")) { // Kill thread upon client request.\n" + "   " + aoName + ".stop();\n" + "   DispatcherFactory.getInstance().getNavajoConfig().getAsyncStore().removeInstance(" + callbackRefName + ");\n" + "   return;\n" + "} else if ( " + aoName + ".isKilled() ) " + "{ " + "     DispatcherFactory.getInstance().getNavajoConfig().getAsyncStore().removeInstance(" + callbackRefName + ");\n" + "     throw new UserException(-1, " + aoName + ".getException().getMessage()," + aoName + ".getException());\n" + "} else if (" + interruptTypeName + ".equals(\"interrupt\")) {\n" + "   " + aoName + ".interrupt();\n " + "   return;\n" + "} else if (" + interruptTypeName + ".equals(\"resume\")) { " + "  " + aoName + ".resume();\n" + "return;\n" + "}\n");
        ident -= 2;
        result.append(printIdent(ident) + "} else { // New instance!\n");
        result.append(printIdent(ident) + aoName + " = (" + className + ") classLoader.getClass(\"" + object + "\").newInstance();\n" + "  // Call load method for async map in advance:\n" + "  " + aoName + ".load(access);\n" + "  " + callbackRefName + " = DispatcherFactory.getInstance().getNavajoConfig().getAsyncStore().addInstance( " + aoName + ", access );\n" + "}\n");
        result.append(printIdent(ident) + "treeNodeStack.push(currentMap);\n");
        result.append(printIdent(ident) + "currentMap = new MappableTreeNode(access, currentMap, " + aoName + ", false);\n");
        result.append(printIdent(ident) + "currentMap.name = \"" + name + "\";\n");
        result.append(printIdent(ident) + "currentMap.ref = " + callbackRefName + ";\n");
        result.append(printIdent(ident) + aoName + ".afterReload(\"" + name + "\", " + callbackRefName + ");\n");
        result.append(printIdent(ident) + "try {\n");
        ident += 2;
        result.append(printIdent(ident) + asyncMapFinishedName + " = " + aoName + ".isFinished(access.getOutputDoc(), access);\n");
        NodeList response = n.getElementsByTagName("response");
        boolean hasResponseNode = false;
        if (response.getLength() > 0) {
            hasResponseNode = true;
        }
        NodeList running = n.getElementsByTagName("running");
        boolean hasRunningNode = false;
        if (running.getLength() > 0) {
            hasRunningNode = true;
        }
        NodeList request = n.getElementsByTagName("request");
        boolean whileRunning = ((Element) response.item(0)).getAttribute("while_running").equals("true");
        result.append(printIdent(ident) + "if (" + asyncMapFinishedName + " || (" + aoName + ".isActivated() && " + hasResponseNode + " && " + whileRunning + ")) {\n");
        result.append(printIdent(ident) + "  " + asyncStatusName + " = \"response\";\n");
        result.append(printIdent(ident) + "  " + aoName + ".beforeResponse(access);\n");
        result.append(printIdent(ident) + "  if (" + aoName + ".isActivated() && " + whileRunning + ") {\n");
        result.append(printIdent(ident) + "     " + resumeAsyncName + " = true;\n");
        result.append(printIdent(ident) + "  }\n");
        result.append(printIdent(ident) + "} else if (!" + aoName + ".isActivated()) {\n");
        result.append(printIdent(ident) + "  " + asyncStatusName + " = \"request\";\n");
        result.append(printIdent(ident) + "} else if (" + hasRunningNode + ") {\n");
        result.append(printIdent(ident) + "  " + asyncStatusName + " = \"running\";\n");
        result.append(printIdent(ident) + "  " + resumeAsyncName + " = true;\n");
        result.append(printIdent(ident) + "}\n");
        NodeList children = null;
        result.append(printIdent(ident) + "if (" + asyncStatusName + ".equals(\"response\")) {\n");
        children = response.item(0).getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            if (children.item(i) instanceof Element) {
                result.append(compile(ident + 2, children.item(i), className, aoName, deps, tenant));
            }
        }
        result.append(printIdent(ident) + "} else if (" + asyncStatusName + ".equals(\"request\")) {\n");
        children = request.item(0).getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            if (children.item(i) instanceof Element) {
                result.append(compile(ident + 2, children.item(i), className, aoName, deps, tenant));
            }
        }
        result.append(printIdent(ident) + "} else if (" + asyncStatusName + ".equals(\"running\")) {\n");
        children = running.item(0).getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            if (children.item(i) instanceof Element) {
                result.append(compile(ident + 2, children.item(i), className, aoName, deps, tenant));
            }
        }
        result.append(printIdent(ident) + "}\n");
        result.append(printIdent(ident) + "if ((currentMap.myObject != null)) {\n");
        result.append(printIdent(ident + 2) + "if (!" + asyncMapFinishedName + ") {\n");
        result.append(printIdent(ident + 4) + "if (" + resumeAsyncName + ") { " + aoName + ".afterResponse(); } else { " + aoName + ".afterRequest(); " + aoName + ".runThread(); }\n");
        result.append(printIdent(ident + 2) + "} else {\n");
        result.append(printIdent(ident + 4) + "MappingUtils.callStoreMethod(currentMap.myObject);\n");
        result.append(printIdent(ident + 4) + "DispatcherFactory.getInstance().getNavajoConfig().getAsyncStore().removeInstance(currentMap.ref);\n");
        result.append(printIdent(ident + 2) + "}\n");
        result.append(printIdent(ident) + "}\n");
        result.append(printIdent(ident) + "} catch (Exception e" + ident + ") {\n");
        result.append(printIdent(ident) + " MappingUtils.callKillOrStoreMethod(currentMap.myObject, e" + ident + ");\n");
        result.append(printIdent(ident) + " DispatcherFactory.getInstance().getNavajoConfig().getAsyncStore().removeInstance(currentMap.ref);\n");
        result.append(printIdent(ident) + "  throw e" + ident + ";\n");
        result.append(printIdent(ident) + "}\n");
    } else {
        result.append(printIdent(ident) + "treeNodeStack.push(currentMap);\n");
        result.append(printIdent(ident) + "currentMap = new MappableTreeNode(access, currentMap, classLoader.getClass(\"" + object + "\").newInstance(), false);\n");
        result.append("currentMap.setNavajoLineNr(" + n.getAttribute("linenr") + ");\n");
        n.getAttribute("navajoScript");
        String objectName = "mappableObject" + (objectCounter++);
        result.append(printIdent(ident) + objectName + " = (" + className + ") currentMap.myObject;\n");
        boolean objectMappable = false;
        try {
            objectMappable = MappingUtils.isObjectMappable(className);
        } catch (UserException e) {
            objectMappable = MappingUtils.isObjectMappable(className, loader);
        }
        if (objectMappable) {
            result.append(printIdent(ident) + objectName + ".load(access);\n");
        }
        String objectDefinition = className + " " + objectName + " = null;\n";
        variableClipboard.add(objectDefinition);
        result.append(printIdent(ident) + "try {\n");
        NodeList children = n.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            result.append(compile(ident + 2, children.item(i), className, objectName, deps, tenant));
        }
        result.append(printIdent(ident) + "} catch (Exception e" + ident + ") {\n");
        result.append(printIdent(ident) + "MappingUtils.callKillOrStoreMethod( " + objectName + ", e" + ident + ");\n");
        result.append(printIdent(ident) + "  throw e" + ident + ";\n");
        result.append(printIdent(ident) + "}\n");
        result.append(printIdent(ident) + "MappingUtils.callStoreMethod(" + objectName + ");\n");
        result.append(printIdent(ident) + "currentMap.setEndtime();\ncurrentMap = (MappableTreeNode) treeNodeStack.pop();\n");
    }
    if (conditionClause) {
        ident -= 2;
        result.append(printIdent(ident) + "} // EOF map condition \n");
    }
    if (!contextClassStack.isEmpty()) {
        contextClass = contextClassStack.pop();
    } else {
        contextClass = null;
    }
    return result.toString();
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ParseException(com.dexels.navajo.parser.compiled.ParseException) UserException(com.dexels.navajo.script.api.UserException)

Example 3 with ParseException

use of com.dexels.navajo.parser.compiled.ParseException 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 4 with ParseException

use of com.dexels.navajo.parser.compiled.ParseException 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)

Example 5 with ParseException

use of com.dexels.navajo.parser.compiled.ParseException in project navajo by Dexels.

the class ExpressionCache method parse.

public ContextExpression parse(List<String> problems, String expression, Function<String, FunctionClassification> functionClassifier, Function<String, Optional<Node>> mapResolver) {
    Optional<ContextExpression> cachedParsedExpression = exprCache.getUnchecked(expression);
    if (cachedParsedExpression.isPresent()) {
        hitCount.incrementAndGet();
        return cachedParsedExpression.get();
    }
    CompiledParser cp;
    try {
        StringReader sr = new StringReader(expression);
        cp = new CompiledParser(sr);
        cp.Expression();
        ContextExpression parsed = cp.getJJTree().rootNode().interpretToLambda(problems, expression, functionClassifier, mapResolver);
        parsedCount.incrementAndGet();
        if (parsed.isLiteral()) {
            Operand result = parsed.apply();
            exprCache.put(expression, Optional.ofNullable(parsed));
            if (result != null) {
                expressionValueCache.put(expression, Optional.of(result));
            }
            return new ContextExpression() {

                @Override
                public boolean isLiteral() {
                    return true;
                }

                @Override
                public Operand apply(Navajo doc, Message parentMsg, Message parentParamMsg, Selection parentSel, MappableTreeNode mapNode, TipiLink tipiLink, Access access, Optional<ImmutableMessage> immutableMessage, Optional<ImmutableMessage> paramMessage) {
                    return result;
                }

                @Override
                public Optional<String> returnType() {
                    return Optional.ofNullable(result.type);
                }

                @Override
                public String expression() {
                    return expression;
                }
            };
        } else {
            exprCache.put(expression, Optional.ofNullable(parsed));
            return parsed;
        }
    } catch (ParseException e) {
        throw new TMLExpressionException("Error parsing expression: " + expression, e);
    } catch (Throwable e) {
        throw new TMLExpressionException("Unexpected error parsing expression: " + expression, e);
    }
}
Also used : MappableTreeNode(com.dexels.navajo.script.api.MappableTreeNode) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) Message(com.dexels.navajo.document.Message) Optional(java.util.Optional) Operand(com.dexels.navajo.document.Operand) Selection(com.dexels.navajo.document.Selection) ContextExpression(com.dexels.navajo.expression.api.ContextExpression) Access(com.dexels.navajo.script.api.Access) Navajo(com.dexels.navajo.document.Navajo) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) CompiledParser(com.dexels.navajo.parser.compiled.CompiledParser) TipiLink(com.dexels.navajo.expression.api.TipiLink) StringReader(java.io.StringReader) ParseException(com.dexels.navajo.parser.compiled.ParseException)

Aggregations

ParseException (com.dexels.navajo.parser.compiled.ParseException)5 TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)4 UserException (com.dexels.navajo.script.api.UserException)4 Element (org.w3c.dom.Element)4 NodeList (org.w3c.dom.NodeList)4 KeywordException (com.dexels.navajo.mapping.compiler.meta.KeywordException)3 MetaCompileException (com.dexels.navajo.mapping.compiler.meta.MetaCompileException)3 CompilationException (com.dexels.navajo.script.api.CompilationException)3 MappingException (com.dexels.navajo.script.api.MappingException)3 SystemException (com.dexels.navajo.script.api.SystemException)3 IOException (java.io.IOException)3 TransformerException (javax.xml.transform.TransformerException)3 ImmutableMessage (com.dexels.immutable.api.ImmutableMessage)1 Message (com.dexels.navajo.document.Message)1 Navajo (com.dexels.navajo.document.Navajo)1 Operand (com.dexels.navajo.document.Operand)1 Selection (com.dexels.navajo.document.Selection)1 ContextExpression (com.dexels.navajo.expression.api.ContextExpression)1 TipiLink (com.dexels.navajo.expression.api.TipiLink)1 DomainObjectMapper (com.dexels.navajo.mapping.bean.DomainObjectMapper)1