Search in sources :

Example 1 with IncludeDependency

use of com.dexels.navajo.mapping.compiler.meta.IncludeDependency 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);
}
Also used : Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) FileInputStream(java.io.FileInputStream) 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) ByteArrayInputStream(java.io.ByteArrayInputStream) IncludeDependency(com.dexels.navajo.mapping.compiler.meta.IncludeDependency) NS3ToNSXML(com.dexels.navajo.mapping.compiler.navascript.NS3ToNSXML) UserException(com.dexels.navajo.script.api.UserException) File(java.io.File)

Aggregations

TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)1 IncludeDependency (com.dexels.navajo.mapping.compiler.meta.IncludeDependency)1 KeywordException (com.dexels.navajo.mapping.compiler.meta.KeywordException)1 MetaCompileException (com.dexels.navajo.mapping.compiler.meta.MetaCompileException)1 NS3ToNSXML (com.dexels.navajo.mapping.compiler.navascript.NS3ToNSXML)1 ParseException (com.dexels.navajo.parser.compiled.ParseException)1 CompilationException (com.dexels.navajo.script.api.CompilationException)1 MappingException (com.dexels.navajo.script.api.MappingException)1 SystemException (com.dexels.navajo.script.api.SystemException)1 UserException (com.dexels.navajo.script.api.UserException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 TransformerException (javax.xml.transform.TransformerException)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1