Search in sources :

Example 31 with XMLElement

use of com.dexels.navajo.document.nanoimpl.XMLElement in project navajo by Dexels.

the class ScriptInheritance method extend.

private XMLElement extend(XMLElement superScript, XMLElement subScript) throws Exception {
    addMessageLevels(superScript, 0);
    addMessageLevels(subScript, 0);
    // Replace children.
    for (int levelindex = maxLevel; levelindex >= 0; levelindex--) {
        Vector<XMLElement> superMessages = new Vector<XMLElement>();
        findMessagesWithLevel(levelindex, superScript, superMessages);
        // Loop over all messages with this level...
        for (int i = 0; i < superMessages.size(); i++) {
            XMLElement childPrev = superMessages.elementAt(i);
            if (childPrev.getName().equals("message") && childPrev.getAttribute("processed") == null) {
                String messageName = (String) childPrev.getAttribute("name");
                String level = (String) childPrev.getAttribute("level");
                XMLElement found = findMessageWithLevel(messageName, level, subScript);
                if (found != null) {
                    replaceMessagesWithLevel(Integer.parseInt(level), 0, i, superScript, found);
                }
            }
        }
    }
    return superScript;
}
Also used : CaseSensitiveXMLElement(com.dexels.navajo.document.nanoimpl.CaseSensitiveXMLElement) XMLElement(com.dexels.navajo.document.nanoimpl.XMLElement) Vector(java.util.Vector)

Example 32 with XMLElement

use of com.dexels.navajo.document.nanoimpl.XMLElement in project navajo by Dexels.

the class ScriptInheritance method addMessageLevels.

private void addMessageLevels(XMLElement e, int level) {
    Vector<XMLElement> messages = new Vector<XMLElement>();
    findNearestMessageChildren(e, messages);
    for (int i = 0; i < messages.size(); i++) {
        XMLElement msg = messages.get(i);
        msg.setAttribute("level", level + "");
        if (level > maxLevel) {
            maxLevel = level;
        }
        addMessageLevels(msg, level + 1);
    }
}
Also used : CaseSensitiveXMLElement(com.dexels.navajo.document.nanoimpl.CaseSensitiveXMLElement) XMLElement(com.dexels.navajo.document.nanoimpl.XMLElement) Vector(java.util.Vector)

Example 33 with XMLElement

use of com.dexels.navajo.document.nanoimpl.XMLElement in project navajo by Dexels.

the class ScriptInheritance method doInject.

private void doInject(XMLElement subScript, XMLElement child, String scriptPath, List<String> inheritedScripts) throws Exception {
    // find inject tags.
    Vector<XMLElement> children = (child == null ? subScript.getChildren() : child.getChildren());
    if (!scriptPath.endsWith("/")) {
        scriptPath = scriptPath + "/";
    }
    for (int i = 0; i < children.size(); i++) {
        if (children.get(i).getName().equalsIgnoreCase("inject")) {
            XMLElement injectNode = children.get(i);
            String script = (String) injectNode.getAttribute("script");
            XMLElement result = null;
            if (script != null) {
                // Recursively do inheritance for inherited scripts...
                File f = new File(scriptPath + script + ".xml");
                if (!f.exists()) {
                    f = new File(scriptPath + script + ".ns");
                }
                BufferedReader br = new BufferedReader(new InputStreamReader(inherit(new FileInputStream(f), scriptPath, inheritedScripts), "UTF-8"));
                XMLElement superScript = new CaseSensitiveXMLElement();
                superScript.parseFromReader(br);
                br.close();
                inheritedScripts.add(script);
                result = extend(superScript, injectNode);
            }
            children.remove(i);
            children.add(i, result);
        } else {
            doInject(subScript, children.get(i), scriptPath, inheritedScripts);
        }
    }
}
Also used : CaseSensitiveXMLElement(com.dexels.navajo.document.nanoimpl.CaseSensitiveXMLElement) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) CaseSensitiveXMLElement(com.dexels.navajo.document.nanoimpl.CaseSensitiveXMLElement) XMLElement(com.dexels.navajo.document.nanoimpl.XMLElement) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 34 with XMLElement

use of com.dexels.navajo.document.nanoimpl.XMLElement in project navajo by Dexels.

the class ScriptInheritance method findMessageWithLevel.

private XMLElement findMessageWithLevel(String name, String level, XMLElement sub) {
    Vector<XMLElement> children = sub.getChildren();
    for (int i = 0; i < children.size(); i++) {
        XMLElement child = children.get(i);
        if (child.getName().equals("message") && (name.equals(child.getAttribute(REPLACE_MESSAGE)) || name.equals(child.getAttribute(EXTEND_MESSAGE)) || name.equals(child.getAttribute(BLOCK_ELEMENT))) && child.getAttribute("level").equals(level)) {
            return child;
        }
        XMLElement depthChild = findMessageWithLevel(name, level, child);
        if (depthChild != null) {
            return depthChild;
        }
    }
    return null;
}
Also used : CaseSensitiveXMLElement(com.dexels.navajo.document.nanoimpl.CaseSensitiveXMLElement) XMLElement(com.dexels.navajo.document.nanoimpl.XMLElement)

Example 35 with XMLElement

use of com.dexels.navajo.document.nanoimpl.XMLElement in project navajo by Dexels.

the class ScriptInheritance method replaceMessagesWithLevel.

/**
 * Replaces a message in tsl on level level and with index targetIndex on that level with insertedMessage.
 *
 * @param level
 * @param leveledIndex
 * @param targetIndex
 * @param tsl
 * @param insertedMessage
 */
private boolean replaceMessagesWithLevel(int level, int leveledIndex, int targetIndex, XMLElement tsl, XMLElement insertedMessage) throws Exception {
    Vector<XMLElement> children = tsl.getChildren();
    for (int i = 0; i < children.size(); i++) {
        XMLElement child = children.get(i);
        if (child.getName().equals("message") && child.getAttribute("level").equals(level + "")) {
            if (leveledIndex == targetIndex) {
                XMLElement parent = child.getParent();
                // Find current child index..
                Vector<XMLElement> allChildrenOfParent = parent.getChildren();
                for (int j = 0; j < allChildrenOfParent.size(); j++) {
                    if (allChildrenOfParent.get(j).equals(child)) {
                        String operation = "";
                        String nameOfInsertedMessage = (String) insertedMessage.getAttribute(REPLACE_MESSAGE);
                        insertedMessage.removeAttribute(REPLACE_MESSAGE);
                        operation = REPLACE_MESSAGE;
                        if (nameOfInsertedMessage == null) {
                            nameOfInsertedMessage = (String) insertedMessage.getAttribute(EXTEND_MESSAGE);
                            insertedMessage.removeAttribute(EXTEND_MESSAGE);
                            operation = EXTEND_MESSAGE;
                        }
                        if (nameOfInsertedMessage == null) {
                            nameOfInsertedMessage = (String) insertedMessage.getAttribute(BLOCK_ELEMENT);
                            insertedMessage.removeAttribute(BLOCK_ELEMENT);
                            operation = BLOCK_ELEMENT;
                        }
                        String newName = (String) insertedMessage.getAttribute("name");
                        if (newName == null && nameOfInsertedMessage == null) {
                            throw new Exception("Unknown inheritance operation @line: " + insertedMessage.getLineNr());
                        }
                        insertedMessage.setAttribute("name", (newName != null ? newName : nameOfInsertedMessage));
                        if (operation.equals(BLOCK_ELEMENT)) {
                            allChildrenOfParent.remove(j);
                        } else if (operation.equals(REPLACE_MESSAGE)) {
                            // simply replace.
                            allChildrenOfParent.remove(j);
                            allChildrenOfParent.add(j, insertedMessage);
                        } else {
                            // Add all children of insertedMessage.
                            XMLElement orig = allChildrenOfParent.get(j);
                            orig.setAttribute("name", (newName != null ? newName : nameOfInsertedMessage));
                            Vector<XMLElement> allChildrenOfInsertedMessage = insertedMessage.getChildren();
                            // replace 'overlap' with 'new' and delete 'blocked'.
                            for (int aci = 0; aci < allChildrenOfInsertedMessage.size(); aci++) {
                                boolean added = false;
                                for (int oci = 0; oci < orig.getChildren().size(); oci++) {
                                    // Remove elements that have a blocked attribute.
                                    if (orig.getChildren().get(oci).getAttribute(BLOCK_ELEMENT) != null) {
                                        orig.getChildren().remove(oci);
                                    }
                                    if (equalElements(orig.getChildren().get(oci), allChildrenOfInsertedMessage.get(aci))) {
                                        orig.getChildren().remove(oci);
                                        if (allChildrenOfInsertedMessage.get(aci).getAttribute(BLOCK_ELEMENT) == null) {
                                            // Only add if blocked attribute was NOT specified.
                                            orig.getChildren().add(oci, allChildrenOfInsertedMessage.get(aci));
                                        }
                                        oci = orig.getChildren().size() + 1;
                                        added = true;
                                    }
                                }
                                if (!added) {
                                    orig.addChild(allChildrenOfInsertedMessage.get(aci));
                                }
                            }
                        }
                        // Tag parent message 1 level up, such that it is not replaced.
                        if (level > 0) {
                            XMLElement parentMessage = null;
                            while (parentMessage == null) {
                                if (parent.getName().equals("message")) {
                                    parentMessage = parent;
                                } else {
                                    parent = parent.getParent();
                                }
                            }
                            parentMessage.setAttribute("processed", "0");
                        }
                        return true;
                    }
                }
                return false;
            } else {
                leveledIndex++;
            }
        } else {
            if (replaceMessagesWithLevel(level, leveledIndex, targetIndex, child, insertedMessage)) {
                return true;
            }
        }
    }
    return false;
}
Also used : CaseSensitiveXMLElement(com.dexels.navajo.document.nanoimpl.CaseSensitiveXMLElement) XMLElement(com.dexels.navajo.document.nanoimpl.XMLElement) Vector(java.util.Vector)

Aggregations

XMLElement (com.dexels.navajo.document.nanoimpl.XMLElement)120 CaseSensitiveXMLElement (com.dexels.navajo.document.nanoimpl.CaseSensitiveXMLElement)109 MapTag (com.dexels.navajo.document.navascript.tags.MapTag)12 IOException (java.io.IOException)12 NS3Compatible (com.dexels.navajo.document.navascript.tags.NS3Compatible)10 ArrayList (java.util.ArrayList)8 ParamTag (com.dexels.navajo.document.navascript.tags.ParamTag)7 InputStreamReader (java.io.InputStreamReader)7 HashMap (java.util.HashMap)7 APIException (com.dexels.navajo.article.APIException)6 ExpressionTag (com.dexels.navajo.document.navascript.tags.ExpressionTag)5 FieldTag (com.dexels.navajo.document.navascript.tags.FieldTag)5 FileInputStream (java.io.FileInputStream)5 FileReader (java.io.FileReader)5 Property (com.dexels.navajo.document.Property)4 IncludeTag (com.dexels.navajo.document.navascript.tags.IncludeTag)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 File (java.io.File)4 Message (com.dexels.navajo.document.Message)3 BlockTag (com.dexels.navajo.document.navascript.tags.BlockTag)3