use of com.twinsoft.convertigo.engine.util.TwsCachedXPathAPI in project convertigo by convertigo.
the class IfXpathExistsThenElseStatement method execute.
@Override
public boolean execute(Context javascriptContext, Scriptable scope) throws EngineException {
if (isEnabled()) {
if (super.execute(javascriptContext, scope)) {
HtmlConnector htmlConnector = getConnector();
Document xmlDocument = htmlConnector.getCurrentXmlDocument();
TwsCachedXPathAPI xpathApi = htmlConnector.context.getXpathApi();
if ((xmlDocument == null) || (xpathApi == null)) {
Engine.logBeans.warn((xmlDocument == null) ? "(XPath) Current DOM of HtmlConnector is Null!" : "TwsCachedXPathAPI of HtmlConnector is Null!");
return false;
}
evaluate(javascriptContext, scope, getCondition(), "xpath", false);
String jsXpath = evaluated.toString();
NodeList nodeList = null;
try {
nodeList = xpathApi.selectNodeList(xmlDocument, jsXpath);
} catch (TransformerException e) {
return false;
} catch (ClassCastException e) {
return false;
}
if (nodeList == null) {
return false;
}
if (nodeList.getLength() == 0) {
elseStatement = getElseStatement();
if (elseStatement != null) {
elseStatement.execute(javascriptContext, scope);
}
return false;
}
thenStatement = getThenStatement();
if (thenStatement != null) {
thenStatement.execute(javascriptContext, scope);
return true;
}
return false;
}
}
return false;
}
use of com.twinsoft.convertigo.engine.util.TwsCachedXPathAPI in project convertigo by convertigo.
the class BuildLocally method installCordova.
public Status installCordova() {
try {
File resourceFolder = mobilePlatform.getResourceFolder();
File configFile = new File(resourceFolder, "config.xml");
Document doc = XMLUtils.loadXml(configFile);
TwsCachedXPathAPI xpathApi = new TwsCachedXPathAPI();
Element singleElement = (Element) xpathApi.selectSingleNode(doc, "/widget/preference[@name='nodejs-version']");
String nodeVersion = (singleElement != null) ? singleElement.getAttribute("value") : ProcessUtils.getDefaultNodeVersion();
nodeDir = ProcessUtils.getNodeDir(nodeVersion, (r, t, x) -> {
Engine.logEngine.info("Downloading nodejs " + nodeVersion + ": " + Math.round((r * 100f) / t) + "%");
});
Engine.logEngine.info("Checking if nodejs and npm are installed.");
List<String> parameters = new LinkedList<String>();
parameters.add("--version");
String npmVersion = runCommand(resourceFolder, "npm", parameters, false);
Pattern pattern = Pattern.compile("^([0-9])+\\.([0-9])+\\.([0-9])+$");
Matcher matcher = pattern.matcher(npmVersion);
if (!matcher.find()) {
throw new Exception("node.js is not installed ('npm --version' returned '" + npmVersion + "')\nYou must download nodes.js from https://nodejs.org/en/download/");
}
Engine.logEngine.info("OK, nodejs (" + nodeVersion + ") and npm (" + npmVersion + ") are installed.");
singleElement = (Element) xpathApi.selectSingleNode(doc, "/widget/preference[@name='prefered-android-build-tools']");
if (singleElement != null && singleElement.hasAttribute("value")) {
preferedAndroidBuildTools = singleElement.getAttribute("value");
}
Engine.logEngine.info("Checking if this cordova version is already installed.");
singleElement = (Element) xpathApi.selectSingleNode(doc, "/widget/preference[@name='cordova-version']");
if (singleElement == null) {
singleElement = (Element) xpathApi.selectSingleNode(doc, "/widget/preference[@name='phonegap-version']");
}
if (singleElement != null) {
String cliVersion = singleElement.getAttribute("value");
if (cliVersion != null) {
pattern = Pattern.compile("^(?:cli-)?([0-9]+\\.[0-9]+\\.[0-9]+)$");
matcher = pattern.matcher(cliVersion);
if (!matcher.find()) {
throw new Exception("The cordova version is specified but its value has not the right format.");
}
// Remove 'cli-' from 'cli-x.x.x'
cliVersion = matcher.group(1);
String cordovaInstallPath = BuildLocally.cordovaInstallsPath + File.separator + "cordova" + cliVersion;
File cordovaBinFile = new File(cordovaInstallPath + File.separator + "node_modules" + File.separator + "cordova" + File.separator + "bin" + File.separator + "cordova");
// If cordova is not installed
if (!cordovaBinFile.exists()) {
Engine.logEngine.info("Installing cordova " + cliVersion + " This can take some time....");
File cordovaInstallDir = new File(cordovaInstallPath);
cordovaInstallDir.mkdir();
parameters = new LinkedList<String>();
parameters.add("--prefix");
parameters.add(cordovaInstallDir.getAbsolutePath());
parameters.add("--unsafe-perm=true");
parameters.add("install");
parameters.add("cordova@" + cliVersion);
this.runCommand(cordovaInstallDir, "npm", parameters, true);
}
Engine.logEngine.info("Cordova (" + cliVersion + ") is now installed.");
this.cordovaBinPath = cordovaBinFile.getAbsolutePath();
} else {
Engine.logEngine.info("The phonegap-version prefrence version is not specified in config.xml.");
throw new Exception("The phonegap-version prefrence version is not specified in config.xml.");
}
} else {
throw new Exception("The phonegap-version preference not found in config.xml.");
}
} catch (Throwable e) {
Engine.logEngine.info("Error when installing Cordova: " + e);
logException(e, "Error when installing Cordova");
return Status.CANCEL;
}
return Status.OK;
}
use of com.twinsoft.convertigo.engine.util.TwsCachedXPathAPI in project convertigo by convertigo.
the class BuildLocally method processConfigXMLResources.
/**
* Explore "config.xml", handle plugins and copy needed resources to appropriate platforms folders.
* @param wwwDir
* @param platform
* @param cordovaDir
*/
private void processConfigXMLResources(File wwwDir, File cordovaDir) throws Throwable {
try {
File configFile = new File(cordovaDir, "config.xml");
Document doc = XMLUtils.loadXml(configFile);
TwsCachedXPathAPI xpathApi = new TwsCachedXPathAPI();
// Changes icons and splashs src in config.xml file because it was moved to the parent folder
NodeIterator nodeIterator = xpathApi.selectNodeIterator(doc, "//*[local-name()='splash' or local-name()='icon']");
Element singleElement = (Element) nodeIterator.nextNode();
while (singleElement != null) {
String src = singleElement.getAttribute("src");
src = "www/" + src;
File file = new File(cordovaDir, src);
if (file.exists()) {
singleElement.setAttribute("src", src);
}
singleElement = (Element) nodeIterator.nextNode();
}
// ANDROID
if (mobilePlatform instanceof Android) {
singleElement = (Element) xpathApi.selectSingleNode(doc, "/widget/name");
if (singleElement != null) {
String name = singleElement.getTextContent();
name = name.replace("\\", "\\\\");
name = name.replace("'", "\\'");
name = name.replace("\"", "\\\"");
singleElement.setTextContent(name);
}
}
// WINPHONE
if (mobilePlatform instanceof WindowsPhone8) {
// Without these width and height the local build doesn't work but with these the remote build doesn't work
singleElement = (Element) xpathApi.selectSingleNode(doc, "/widget/platform[@name='wp8']/icon[not(@role)]");
if (singleElement != null) {
singleElement.setAttribute("width", "99");
singleElement.setAttribute("height", "99");
}
singleElement = (Element) xpathApi.selectSingleNode(doc, "/widget/platform[@name='wp8']/icon[@role='background']");
if (singleElement != null) {
singleElement.setAttribute("width", "159");
singleElement.setAttribute("height", "159");
}
// /widget/platform[@name='wp8']/splash
singleElement = (Element) xpathApi.selectSingleNode(doc, "/widget/platform/splash");
if (singleElement != null) {
singleElement.setAttribute("width", "768");
singleElement.setAttribute("height", "1280");
}
singleElement = (Element) xpathApi.selectSingleNode(doc, "/widget/plugin[@name='phonegap-plugin-push']/param[@name='SENDER_ID']");
if (singleElement != null) {
// Remote build needs a node named 'param' and local build needs a node named 'variable'
singleElement.getParentNode().appendChild(cloneNode(singleElement, "variable"));
singleElement.getParentNode().removeChild(singleElement);
}
}
// XMLUtils.saveXml(doc, configFile.getAbsolutePath());
// We have to add the root config.xml all our app's config.xml preferences.
// Cordova will use this file to generates the platform specific config.xml
// Get preferences from current config.xml
NodeIterator preferences = xpathApi.selectNodeIterator(doc, "//preference");
// File configFile = new File(cordovaDir, "config.xml");
// doc = XMLUtils.loadXml(configFile); // The root config.xml
NodeList preferencesList = doc.getElementsByTagName("preference");
// Remove old preferences
while (preferencesList.getLength() > 0) {
Element pathNode = (Element) preferencesList.item(0);
// Remove empty lines
Node prev = pathNode.getPreviousSibling();
if (prev != null && prev.getNodeType() == Node.TEXT_NODE && prev.getNodeValue().trim().length() == 0) {
doc.getDocumentElement().removeChild(prev);
}
doc.getDocumentElement().removeChild(pathNode);
}
for (Element preference = (Element) preferences.nextNode(); preference != null; preference = (Element) preferences.nextNode()) {
String name = preference.getAttribute("name");
String value = preference.getAttribute("value");
Element elt = doc.createElement("preference");
elt.setAttribute("name", name);
elt.setAttribute("value", value);
Engine.logEngine.info("Adding preference'" + name + "' with value '" + value + "'");
doc.getDocumentElement().appendChild(elt);
}
Engine.logEngine.trace("New config.xml is: " + XMLUtils.prettyPrintDOM(doc));
File resXmlFile = new File(cordovaDir, "config.xml");
// FileUtils.deleteQuietly(resXmlFile);
XMLUtils.saveXml(doc, resXmlFile.getAbsolutePath());
// Last part, as all resources has been copied to the correct location, we can remove
// our "www/res" directory before packaging to save build time and size...
// FileUtils.deleteDirectory(new File(wwwDir, "res"));
} catch (Exception e) {
logException(e, "Unable to process config.xml in your project, check the file's validity");
}
}
use of com.twinsoft.convertigo.engine.util.TwsCachedXPathAPI in project convertigo by convertigo.
the class GetNodesStatement method execute.
@Override
public boolean execute(Context javascriptContext, Scriptable scope) throws EngineException {
if (isEnabled()) {
if (super.execute(javascriptContext, scope)) {
HtmlConnector htmlConnector = getConnector();
Document xmlDocument = htmlConnector.getHtmlParser().getDom(htmlConnector.context);
TwsCachedXPathAPI xpathApi = htmlConnector.context.getXpathApi();
if ((xmlDocument == null) || (xpathApi == null)) {
Engine.logBeans.warn((xmlDocument == null) ? "(XPath) Current DOM of HtmlConnector is Null!" : "TwsCachedXPathAPI of HtmlConnector is Null!");
return false;
}
evaluate(javascriptContext, scope, getXpath(), "xpath", false);
String jsXpath = evaluated.toString();
NodeList nodeList = null;
try {
nodeList = xpathApi.selectNodeList(xmlDocument, jsXpath);
} catch (TransformerException e) {
return false;
} catch (ClassCastException e) {
return false;
}
if (nodeList == null)
return false;
addToScope(scope, nodeList);
return true;
}
}
return false;
}
use of com.twinsoft.convertigo.engine.util.TwsCachedXPathAPI in project convertigo by convertigo.
the class GetIconsCSS method writeResponseResult.
@SuppressWarnings("unlikely-arg-type")
@Override
protected void writeResponseResult(HttpServletRequest request, HttpServletResponse response) throws Exception {
Document pluginDocument = Get.getPluginDocument();
TwsCachedXPathAPI xpathApi = new TwsCachedXPathAPI();
List<Node> nActions = xpathApi.selectList(pluginDocument, "/plugin/extension[@point='org.eclipse.ui.popupMenus']//action[@icon]");
Set<String> actionIconAttrs = new HashSet<>();
StringBuilder sb = new StringBuilder();
for (Node nAction : nActions) {
Element eAction = (Element) nAction;
String attrIcon = eAction.getAttribute("icon");
if (!actionIconAttrs.contains(actionIconAttrs)) {
sb.append(".").append(eAction.getAttribute("id").replaceAll("\\.", "-")).append(" {background-image:url(").append("../../admin/services/studio.menu.GetIcon?iconPath=").append(attrIcon).append(") !important;").append("}\r\n");
actionIconAttrs.add(attrIcon);
}
}
response.setContentType(MimeType.Css.value());
IOUtils.write(sb.toString(), response.getOutputStream(), "UTF-8");
}
Aggregations