use of com.dexels.navajo.mapping.compiler.navascript.NS3ToNSXML 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.navascript.NS3ToNSXML in project navajo by Dexels.
the class TslPreCompiler method getAllDependencies.
public void getAllDependencies(File script, String scriptFolder, List<Dependency> deps, String scriptTenant) throws XPathExpressionException, UserException {
Document tslDoc = null;
InputStream is = null;
try {
if (script.getAbsolutePath().endsWith(".ns")) {
// Check for NS3 type script
NS3ToNSXML ns3toxml = new NS3ToNSXML();
ns3toxml.initialize();
String content = ns3toxml.read(script.getAbsolutePath());
InputStream metais = ns3toxml.parseNavascript(content);
MapMetaData mmd = MapMetaData.getInstance();
String intermed = mmd.parse(script.getAbsolutePath(), metais);
metais.close();
is = new ByteArrayInputStream(intermed.getBytes());
} else if (MapMetaData.isMetaScript(script.getAbsolutePath())) {
// Check for navascript type script
MapMetaData mmd = MapMetaData.getInstance();
InputStream metais = inputStreamReader.getResource(script.getAbsolutePath());
String intermed = mmd.parse(script.getAbsolutePath(), metais);
metais.close();
is = new ByteArrayInputStream(intermed.getBytes());
} else {
// Normal tsl type script
is = inputStreamReader.getResource(script.getAbsolutePath());
}
tslDoc = XMLDocumentUtils.createDocument(is, false);
} catch (Exception e) {
throw new UserException(-1, "Exception on pre-compiling script: " + script, e);
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
logger.error("Error: ", e);
}
}
}
getAllDependencies(script.getAbsolutePath(), scriptTenant, scriptFolder, deps, tslDoc);
}
use of com.dexels.navajo.mapping.compiler.navascript.NS3ToNSXML in project navajo by Dexels.
the class TslCompiler method compileScript.
public void compileScript(String script, String scriptPath, String workingPath, String packagePath, Writer outputWriter, List<Dependency> deps, String tenant, boolean hasTenantSpecificScript, boolean forceTenant) throws SystemException, SkipCompilationException {
final String extension = ".xml";
String fullScriptPath = scriptPath + "/" + packagePath + "/" + script + extension;
String ns3ScriptPath = scriptPath + "/" + packagePath + "/" + script + ".ns";
List<String> inheritedScripts = new ArrayList<>();
List<String> extendEntities = new ArrayList<>();
InputStream is = null;
boolean isNavascript = false;
try {
if (new File(ns3ScriptPath).exists()) {
NS3ToNSXML ns3toxml = new NS3ToNSXML();
ns3toxml.initialize();
scriptType = "navascript";
String content = ns3toxml.read(ns3ScriptPath);
InputStream metais = ns3toxml.parseNavascript(content);
MapMetaData mmd = MapMetaData.getInstance();
String intermed = mmd.parse(fullScriptPath, metais);
metais.close();
is = new ByteArrayInputStream(intermed.getBytes());
isNavascript = true;
} else if (MapMetaData.isMetaScript(fullScriptPath)) {
// Check for metascript.
scriptType = "navascript";
MapMetaData mmd = MapMetaData.getInstance();
InputStream metais = navajoIOConfig.getScript(packagePath + "/" + script, tenant, extension);
String intermed = mmd.parse(fullScriptPath, metais);
metais.close();
is = new ByteArrayInputStream(intermed.getBytes());
} else {
is = navajoIOConfig.getScript(packagePath + "/" + script, tenant, extension);
}
if (!isNavascript) {
// NS3 does NOT support inheritance at this moment.
InputStream sis = navajoIOConfig.getScript(packagePath + "/" + script, tenant, extension);
logger.debug("Getting script: {}/{}", packagePath, script);
if (ScriptInheritance.containsInject(sis)) {
// Inheritance preprocessor before compiling.
InputStream ais = null;
ais = ScriptInheritance.inherit(is, scriptPath, inheritedScripts);
is.close();
is = ais;
}
sis.close();
}
for (int i = 0; i < inheritedScripts.size(); i++) {
File inheritedFile = new File(fetchScriptFileName(scriptPath + "/" + inheritedScripts.get(i)));
addDependency("dependentObjects.add( new InheritDependency( Long.valueOf(\"" + IncludeDependency.getFileTimeStamp(inheritedFile) + "\"), \"" + inheritedScripts.get(i) + "\"));\n", "INHERIT" + inheritedScripts.get(i));
}
compileScript(is, packagePath, script, scriptPath, outputWriter, deps, tenant, forceTenant);
} catch (SkipCompilationException e) {
throw e;
} catch (Exception e) {
throw new SystemException(-1, "Error while generating Java code for script: " + script, e);
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
}
}
}
}
Aggregations