Search in sources :

Example 6 with TwsCachedXPathAPI

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;
}
Also used : HtmlConnector(com.twinsoft.convertigo.beans.connectors.HtmlConnector) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) TransformerException(javax.xml.transform.TransformerException) TwsCachedXPathAPI(com.twinsoft.convertigo.engine.util.TwsCachedXPathAPI)

Example 7 with TwsCachedXPathAPI

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;
}
Also used : TwsCachedXPathAPI(com.twinsoft.convertigo.engine.util.TwsCachedXPathAPI) IOs(com.twinsoft.convertigo.beans.mobileplatforms.IOs) Attr(org.w3c.dom.Attr) Matcher(java.util.regex.Matcher) Document(org.w3c.dom.Document) Node(org.w3c.dom.Node) MobileApplication(com.twinsoft.convertigo.beans.core.MobileApplication) NamedNodeMap(org.w3c.dom.NamedNodeMap) LinkedList(java.util.LinkedList) NodeList(org.w3c.dom.NodeList) Windows(com.twinsoft.convertigo.beans.mobileplatforms.Windows) Engine(com.twinsoft.convertigo.engine.Engine) JSONObject(org.codehaus.jettison.json.JSONObject) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) FileInputStream(java.io.FileInputStream) InputStreamReader(java.io.InputStreamReader) WindowsPhone8(com.twinsoft.convertigo.beans.mobileplatforms.WindowsPhone8) File(java.io.File) Android(com.twinsoft.convertigo.beans.mobileplatforms.Android) List(java.util.List) Element(org.w3c.dom.Element) NodeIterator(org.w3c.dom.traversal.NodeIterator) MobileResourceHelper(com.twinsoft.convertigo.engine.admin.services.mobiles.MobileResourceHelper) XMLUtils(com.twinsoft.convertigo.engine.util.XMLUtils) MobilePlatform(com.twinsoft.convertigo.beans.core.MobilePlatform) BufferedReader(java.io.BufferedReader) Pattern(java.util.regex.Pattern) EngineException(com.twinsoft.convertigo.engine.EngineException) Collections(java.util.Collections) ProcessUtils(com.twinsoft.convertigo.engine.util.ProcessUtils) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) LinkedList(java.util.LinkedList) IOException(java.io.IOException) EngineException(com.twinsoft.convertigo.engine.EngineException) File(java.io.File) TwsCachedXPathAPI(com.twinsoft.convertigo.engine.util.TwsCachedXPathAPI)

Example 8 with TwsCachedXPathAPI

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");
    }
}
Also used : NodeIterator(org.w3c.dom.traversal.NodeIterator) WindowsPhone8(com.twinsoft.convertigo.beans.mobileplatforms.WindowsPhone8) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) File(java.io.File) IOException(java.io.IOException) EngineException(com.twinsoft.convertigo.engine.EngineException) TwsCachedXPathAPI(com.twinsoft.convertigo.engine.util.TwsCachedXPathAPI) Android(com.twinsoft.convertigo.beans.mobileplatforms.Android)

Example 9 with TwsCachedXPathAPI

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;
}
Also used : HtmlConnector(com.twinsoft.convertigo.beans.connectors.HtmlConnector) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) TransformerException(javax.xml.transform.TransformerException) TwsCachedXPathAPI(com.twinsoft.convertigo.engine.util.TwsCachedXPathAPI)

Example 10 with TwsCachedXPathAPI

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");
}
Also used : Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) TwsCachedXPathAPI(com.twinsoft.convertigo.engine.util.TwsCachedXPathAPI) HashSet(java.util.HashSet)

Aggregations

TwsCachedXPathAPI (com.twinsoft.convertigo.engine.util.TwsCachedXPathAPI)18 Document (org.w3c.dom.Document)13 NodeList (org.w3c.dom.NodeList)11 Element (org.w3c.dom.Element)10 Node (org.w3c.dom.Node)8 TransformerException (javax.xml.transform.TransformerException)7 LinkedList (java.util.LinkedList)5 HtmlConnector (com.twinsoft.convertigo.beans.connectors.HtmlConnector)4 Android (com.twinsoft.convertigo.beans.mobileplatforms.Android)4 WindowsPhone8 (com.twinsoft.convertigo.beans.mobileplatforms.WindowsPhone8)4 EngineException (com.twinsoft.convertigo.engine.EngineException)4 File (java.io.File)4 IOException (java.io.IOException)4 List (java.util.List)4 MobileApplication (com.twinsoft.convertigo.beans.core.MobileApplication)3 MobilePlatform (com.twinsoft.convertigo.beans.core.MobilePlatform)3 IOs (com.twinsoft.convertigo.beans.mobileplatforms.IOs)3 DatabaseObject (com.twinsoft.convertigo.beans.core.DatabaseObject)2 Project (com.twinsoft.convertigo.beans.core.Project)2 Windows (com.twinsoft.convertigo.beans.mobileplatforms.Windows)2