use of com.dexels.navajo.mapping.compiler.meta.MetaCompileException in project navajo by Dexels.
the class TslCompiler method includeNode.
/**
* Resolve include nodes in the script: <include
* script="[name of script to be included]"/>
*
* @param scriptPath
* @param n
* @param parent
* @param deps
* @throws MetaCompileException
* @throws IOException
* @throws KeywordException
* @throws ClassNotFoundException
* @throws Exception
*/
private final void includeNode(String scriptPath, Node n, Document parent, String tenant, List<Dependency> deps) throws UserException, ClassNotFoundException, KeywordException, IOException, MetaCompileException {
included++;
if (included > 1000) {
throw new UserException(-1, "Too many included scripts!!!");
}
String script = ((Element) n).getAttribute("script");
if (script == null || script.equals("")) {
throw new UserException(-1, "No script name found in include tag (" + "missing or empty script attribute): " + n);
}
// Construct scriptName:
// First try if applicationGroup specific script exists.
String fileName = script + "_" + tenant;
Document includeDoc = null;
String includeFileName = fetchScriptFileName(scriptPath + "/" + fileName);
File includedFile = null;
if (includeFileName != null) {
includedFile = new File(includeFileName);
includeDoc = XMLDocumentUtils.createDocument(new FileInputStream(includeFileName), false);
} else {
// no tenant specific include found. Try non-tenant include instead.
fileName = script;
includeFileName = fetchScriptFileName(scriptPath + "/" + fileName);
if (includeFileName != null) {
includedFile = new File(includeFileName);
if (includeFileName.endsWith(".ns")) {
// It's an NS3 based script
NS3ToNSXML nstoxml = new NS3ToNSXML();
nstoxml.initialize();
try {
String content = nstoxml.read(includeFileName);
String tslResult = MapMetaData.getInstance().parse(scriptPath + "/" + fileName + ".ns", nstoxml.parseNavascript(content));
includeDoc = XMLDocumentUtils.createDocument(new ByteArrayInputStream(tslResult.getBytes()), false);
} catch (Exception e) {
throw new UserException(e.getLocalizedMessage(), e);
}
} else {
// It's an XML based script
includeDoc = XMLDocumentUtils.createDocument(new FileInputStream(includedFile), false);
}
}
}
if (includedFile == null) {
logger.error("Could not file include file: {}", script);
throw new UserException("Could not find include file for script: " + script);
}
// Add dependency.
addDependency("dependentObjects.add( new IncludeDependency( Long.valueOf(\"" + IncludeDependency.getFileTimeStamp(includedFile) + "\"), \"" + fileName + "\"));\n", "INCLUDE" + script);
deps.add(new IncludeDependency(IncludeDependency.getFileTimeStamp(includedFile), fileName, fileName));
if (includeDoc.getElementsByTagName("tsl").item(0) == null) {
// Maybe
// it is
// navascript??
String tslResult = MapMetaData.getInstance().parse(scriptPath + "/" + fileName + ".xml");
includeDoc = XMLDocumentUtils.createDocument(new ByteArrayInputStream(tslResult.getBytes()), false);
}
NodeList content = includeDoc.getElementsByTagName("tsl").item(0).getChildNodes();
Node nextNode = n.getNextSibling();
while (nextNode != null && !(nextNode instanceof Element)) {
nextNode = nextNode.getNextSibling();
}
if (nextNode == null || !(nextNode instanceof Element)) {
nextNode = n;
}
Node parentNode = nextNode.getParentNode();
for (int i = 0; i < content.getLength(); i++) {
Node child = content.item(i);
Node imported = parent.importNode(child.cloneNode(true), true);
parentNode.insertBefore(imported, nextNode);
}
parentNode.removeChild(n);
}
use of com.dexels.navajo.mapping.compiler.meta.MetaCompileException 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();
}
use of com.dexels.navajo.mapping.compiler.meta.MetaCompileException 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();
}
Aggregations