Search in sources :

Example 66 with SAXReader

use of org.dom4j.io.SAXReader in project tdq-studio-se by Talend.

the class AliasManager method loadAliases.

/**
 * Loads Aliases from the users preferences
 */
public void loadAliases() throws ExplorerException {
    aliases.clear();
    try {
        SAXReader reader = new SAXReader();
        File file = new File(ApplicationFiles.USER_ALIAS_FILE_NAME);
        if (file.exists()) {
            Element root = reader.read(file).getRootElement();
            if (root.getName().equals("Beans")) {
                root = convertToV350(root);
            }
            List<Element> list = root.elements(Alias.ALIAS);
            // MOD mzhao bug:19539 Judge if the alias need to save again because it was not encripted in previouse
            // workspace. (before
            // 4.2)
            Boolean needToSave = false;
            if (list != null) {
                for (Element elem : list) {
                    Alias alias = new Alias(elem);
                    Boolean needToSaveUnit = getDecryptUser(alias.getDefaultUser());
                    if (!needToSave) {
                        needToSave = needToSaveUnit;
                    }
                    addAlias(alias);
                }
            }
            if (needToSave) {
                saveAliases();
            }
        // ~ 19539
        }
    } catch (DocumentException e) {
        throw new ExplorerException(e);
    }
}
Also used : ExplorerException(net.sourceforge.sqlexplorer.ExplorerException) SAXReader(org.dom4j.io.SAXReader) DefaultElement(org.dom4j.tree.DefaultElement) Element(org.dom4j.Element) DocumentException(org.dom4j.DocumentException) File(java.io.File)

Example 67 with SAXReader

use of org.dom4j.io.SAXReader in project tdq-studio-se by Talend.

the class DriverManager method loadDrivers.

/**
 * Loads driver definition from a given location
 * @param input
 * @throws ExplorerException
 */
protected void loadDrivers(InputStream input) throws ExplorerException {
    try {
        SAXReader reader = new SAXReader();
        Document doc = reader.read(input);
        Element root = doc.getRootElement();
        if (root.getName().equals("Beans"))
            root = convertFromV3(root);
        for (Element driverElem : root.elements(DRIVER)) {
            ManagedDriver driver = new ManagedDriver(driverElem);
            addDriver(driver);
        }
    } catch (Exception e) {
        throw new ExplorerException(e);
    }
}
Also used : ExplorerException(net.sourceforge.sqlexplorer.ExplorerException) SAXReader(org.dom4j.io.SAXReader) DefaultElement(org.dom4j.tree.DefaultElement) Element(org.dom4j.Element) Document(org.dom4j.Document) IOException(java.io.IOException) ExplorerException(net.sourceforge.sqlexplorer.ExplorerException)

Example 68 with SAXReader

use of org.dom4j.io.SAXReader in project tdq-studio-se by Talend.

the class XmlPreviewer method getXml.

private Element getXml(Object data) throws ExplorerException {
    try {
        if (data == null)
            return null;
        if (data instanceof XmlDataType)
            return ((XmlDataType) data).getRootElement();
        String text = data.toString();
        if (text == null)
            return null;
        SAXReader reader = new SAXReader();
        return reader.read(new StringReader(text)).getRootElement();
    } catch (DocumentException e) {
        throw new ExplorerException(e);
    }
}
Also used : ExplorerException(net.sourceforge.sqlexplorer.ExplorerException) XmlDataType(net.sourceforge.sqlexplorer.dataset.XmlDataType) SAXReader(org.dom4j.io.SAXReader) DocumentException(org.dom4j.DocumentException) StringReader(java.io.StringReader)

Example 69 with SAXReader

use of org.dom4j.io.SAXReader in project ma-core-public by infiniteautomation.

the class DOM4JConverter method convertInbound.

/* (non-Javadoc)
     * @see org.directwebremoting.Converter#convertInbound(java.lang.Class, org.directwebremoting.InboundVariable, org.directwebremoting.InboundContext)
     */
public Object convertInbound(Class paramType, InboundVariable iv, InboundContext inctx) throws MarshallException {
    String value = LocalUtil.decode(iv.getValue());
    try {
        SAXReader xmlReader = new SAXReader();
        Document doc = xmlReader.read(new StringReader(value));
        if (paramType == Document.class) {
            return doc;
        } else if (paramType == Element.class) {
            return doc.getRootElement();
        }
        throw new MarshallException(paramType);
    } catch (MarshallException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new MarshallException(paramType, ex);
    }
}
Also used : SAXReader(org.dom4j.io.SAXReader) MarshallException(org.directwebremoting.extend.MarshallException) Element(org.dom4j.Element) StringReader(java.io.StringReader) Document(org.dom4j.Document) MarshallException(org.directwebremoting.extend.MarshallException)

Example 70 with SAXReader

use of org.dom4j.io.SAXReader in project jMiniLang by bajdcc.

the class ModuleNet method buildRemoteMethods.

private void buildRemoteMethods(IRuntimeDebugInfo info) {
    info.addExternalFunc("g_net_get", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "HTTP GET";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return new RuntimeObjectType[] { RuntimeObjectType.kString };
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
            String text = "";
            String txt = String.valueOf(args.get(0).getObj());
            logger.debug("Request url: " + txt);
            try {
                URL url = new URL(txt);
                // 打开连接
                URLConnection urlConnection = url.openConnection();
                // 获取输入流
                BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "utf-8"));
                String line = null;
                StringBuilder sb = new StringBuilder();
                while ((line = br.readLine()) != null) {
                    sb.append(line).append("\n");
                }
                text = sb.toString();
                br.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return new RuntimeObject(text);
        }
    });
    info.addExternalFunc("g_net_get_json", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "HTTP GET - json";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return new RuntimeObjectType[] { RuntimeObjectType.kString };
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
            String text = "";
            String txt = String.valueOf(args.get(0).getObj());
            logger.debug("Request url(json): " + txt);
            try {
                URL url = new URL(txt);
                // 打开连接
                URLConnection urlConnection = url.openConnection();
                // 获取输入流
                BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "utf-8"));
                String line = null;
                StringBuilder sb = new StringBuilder();
                while ((line = br.readLine()) != null) {
                    sb.append(line).append("\n");
                }
                text = sb.toString();
                br.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return parseJson(text);
        }
    });
    // -----------------------------------
    // server
    info.addExternalFunc("g_net_msg_create_server_internal", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "MSG SERVER";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return new RuntimeObjectType[] { RuntimeObjectType.kInt };
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
            if (getServer() != null || getClient() != null)
                return new RuntimeObject(false);
            int port = ((BigInteger) args.get(0).getObj()).intValue();
            setServer(new ModuleNetServer(port));
            getServer().start();
            return new RuntimeObject(true);
        }
    });
    info.addExternalFunc("g_net_msg_shutdown_server", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "MSG SERVER SHUTDOWN";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return null;
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
            if (getServer() == null)
                return new RuntimeObject(false);
            getServer().exit();
            return new RuntimeObject(true);
        }
    });
    info.addExternalFunc("g_net_msg_get_server_status", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "MSG SERVER GET STATUS";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return null;
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
            if (getServer() != null) {
                ModuleNetServer.Status s = getServer().getStatus();
                if (s == ModuleNetServer.Status.ERROR) {
                    lastError = getServer().getError();
                    setServer(null);
                }
                status.getService().getProcessService().sleep(status.getPid(), MSG_QUERY_TIME);
                return new RuntimeObject(BigInteger.valueOf(s.ordinal()));
            }
            return new RuntimeObject(BigInteger.valueOf(ModuleNetServer.Status.NULL.ordinal()));
        }
    });
    // server
    // -----------------------------------
    // client
    info.addExternalFunc("g_net_msg_create_client_internal", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "MSG CLIENT";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return new RuntimeObjectType[] { RuntimeObjectType.kString };
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
            if (getServer() != null || getClient() != null)
                return new RuntimeObject(false);
            String addr = String.valueOf(args.get(0).getObj());
            setClient(new ModuleNetClient(addr));
            getClient().start();
            return new RuntimeObject(true);
        }
    });
    info.addExternalFunc("g_net_msg_shutdown_client", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "MSG CLIENT SHUTDOWN";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return null;
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
            if (getClient() == null)
                return new RuntimeObject(false);
            getClient().exit();
            return new RuntimeObject(true);
        }
    });
    info.addExternalFunc("g_net_msg_get_client_status", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "MSG CLIENT GET STATUS";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return null;
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
            if (getClient() != null) {
                ModuleNetClient.Status s = getClient().getStatus();
                if (s == ModuleNetClient.Status.ERROR) {
                    lastError = getClient().getError();
                    setClient(null);
                }
                status.getService().getProcessService().sleep(status.getPid(), MSG_QUERY_TIME);
                return new RuntimeObject(BigInteger.valueOf(s.ordinal()));
            }
            return new RuntimeObject(BigInteger.valueOf(ModuleNetClient.Status.NULL.ordinal()));
        }
    });
    info.addExternalFunc("g_net_msg_client_send", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "MSG CLIENT SEND";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return new RuntimeObjectType[] { RuntimeObjectType.kString };
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
            if (getClient() != null) {
                ModuleNetClient.Status s = getClient().getStatus();
                if (s == ModuleNetClient.Status.RUNNING) {
                    status.getService().getProcessService().sleep(status.getPid(), MSG_SEND_TIME);
                    getClient().send(String.valueOf(args.get(0).getObj()));
                    return new RuntimeObject(true);
                }
            }
            return new RuntimeObject(false);
        }
    });
    info.addExternalFunc("g_net_msg_client_send_with_origin", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "MSG CLIENT SEND(ORIGIN)";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return new RuntimeObjectType[] { RuntimeObjectType.kString, RuntimeObjectType.kString };
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
            if (getClient() != null) {
                ModuleNetClient.Status s = getClient().getStatus();
                if (s == ModuleNetClient.Status.RUNNING) {
                    status.getService().getProcessService().sleep(status.getPid(), MSG_SEND_TIME);
                    getClient().send(String.valueOf(args.get(0).getObj()), String.valueOf(args.get(1).getObj()));
                    return new RuntimeObject(true);
                }
            }
            return new RuntimeObject(false);
        }
    });
    info.addExternalFunc("g_net_msg_server_send", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "MSG SERVER SEND";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return new RuntimeObjectType[] { RuntimeObjectType.kString };
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
            if (getServer() != null) {
                ModuleNetServer.Status s = getServer().getStatus();
                if (s == ModuleNetServer.Status.RUNNING) {
                    status.getService().getProcessService().sleep(status.getPid(), MSG_SEND_TIME);
                    getServer().send(String.valueOf(args.get(0).getObj()));
                    return new RuntimeObject(true);
                }
            }
            return new RuntimeObject(false);
        }
    });
    info.addExternalFunc("g_net_msg_server_send_error", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "MSG SERVER SEND ERROR";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return new RuntimeObjectType[] { RuntimeObjectType.kString };
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
            if (getServer() != null) {
                ModuleNetServer.Status s = getServer().getStatus();
                if (s == ModuleNetServer.Status.RUNNING) {
                    status.getService().getProcessService().sleep(status.getPid(), MSG_SEND_TIME);
                    getServer().send_error(String.valueOf(args.get(0).getObj()));
                    return new RuntimeObject(true);
                }
            }
            return new RuntimeObject(false);
        }
    });
    info.addExternalFunc("g_net_msg_server_send_with_origin", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "MSG SERVER SEND(ORIGIN)";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return new RuntimeObjectType[] { RuntimeObjectType.kString, RuntimeObjectType.kString };
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
            if (getServer() != null) {
                ModuleNetServer.Status s = getServer().getStatus();
                if (s == ModuleNetServer.Status.RUNNING) {
                    status.getService().getProcessService().sleep(status.getPid(), MSG_SEND_TIME);
                    getServer().send(String.valueOf(args.get(0).getObj()), String.valueOf(args.get(1).getObj()));
                    return new RuntimeObject(true);
                }
            }
            return new RuntimeObject(false);
        }
    });
    // client
    // -----------------------------------
    info.addExternalFunc("g_net_msg_get_error", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "MSG SERVER GET ERROR";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return null;
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
            return new RuntimeObject(lastError);
        }
    });
    info.addExternalFunc("g_net_msg_get_server_msg", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "MSG SERVER GET MESSAGE";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return null;
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
            return new RuntimeObject(getServer() == null ? null : getServer().getMessage());
        }
    });
    info.addExternalFunc("g_net_msg_get_client_msg", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "MSG CLIENT GET MESSAGE";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return null;
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
            return new RuntimeObject(getClient() == null ? null : getClient().getMessage());
        }
    });
    info.addExternalFunc("g_net_msg_get_client_addr", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "MSG CLIENT GET ADDRESS";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return null;
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
            return new RuntimeObject(getClient() == null ? null : getClient().getAddr());
        }
    });
    info.addExternalFunc("g_net_parse_json", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "Parse json";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return new RuntimeObjectType[] { RuntimeObjectType.kString };
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
            return parseJsonSafe(String.valueOf(args.get(0).getObj()));
        }
    });
    info.addExternalFunc("g_net_get_rss", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "RSS GET(BAIDU)";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return new RuntimeObjectType[] { RuntimeObjectType.kString };
        }

        @SuppressWarnings("unchecked")
        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
            String text = "";
            String txt = String.valueOf(args.get(0).getObj());
            logger.debug("Request url(rss): " + txt);
            RuntimeArray array = new RuntimeArray();
            final Pattern pattern = Pattern.compile("<br>(.*)<br />");
            try {
                SAXReader reader = new SAXReader();
                Document document = reader.read(txt);
                Node title = document.selectSingleNode("//title");
                array.add(new RuntimeObject(title.getText()));
                List<Node> list = document.selectNodes("//item");
                for (Node item : list) {
                    String itemTitle = item.valueOf("title");
                    array.add(new RuntimeObject(itemTitle));
                    String itemDescription = item.valueOf("description");
                    Matcher matcher = pattern.matcher(itemDescription);
                    if (matcher.find()) {
                        array.add(new RuntimeObject(matcher.group(1)));
                    } else {
                        array.add(new RuntimeObject(itemDescription.replace("<br />", "")));
                    }
                }
                return new RuntimeObject(array);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return new RuntimeObject(array);
        }
    });
}
Also used : Pattern(java.util.regex.Pattern) RuntimeArray(priv.bajdcc.LALR1.grammar.runtime.data.RuntimeArray) InputStreamReader(java.io.InputStreamReader) ModuleNetClient(priv.bajdcc.LALR1.interpret.module.net.ModuleNetClient) Matcher(java.util.regex.Matcher) SAXReader(org.dom4j.io.SAXReader) Node(org.dom4j.Node) Document(org.dom4j.Document) URL(java.net.URL) URLConnection(java.net.URLConnection) BufferedReader(java.io.BufferedReader) List(java.util.List) ModuleNetServer(priv.bajdcc.LALR1.interpret.module.net.ModuleNetServer)

Aggregations

SAXReader (org.dom4j.io.SAXReader)322 Document (org.dom4j.Document)256 Element (org.dom4j.Element)195 StringReader (java.io.StringReader)120 DocumentException (org.dom4j.DocumentException)74 Test (org.junit.jupiter.api.Test)74 File (java.io.File)54 List (java.util.List)49 IOException (java.io.IOException)48 InputStream (java.io.InputStream)48 ArrayList (java.util.ArrayList)47 Node (org.dom4j.Node)28 FileInputStream (java.io.FileInputStream)25 HashMap (java.util.HashMap)24 XMLWriter (org.dom4j.io.XMLWriter)22 ByteArrayInputStream (java.io.ByteArrayInputStream)20 URL (java.net.URL)18 OutputFormat (org.dom4j.io.OutputFormat)18 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)17 Test (org.junit.Test)14