Search in sources :

Example 76 with DocumentException

use of org.dom4j.DocumentException in project Spark by igniterealtime.

the class LoginDialog method checkForOldSettings.

/**
 * Checks for historic Spark settings and upgrades the user.
 *
 * @throws Exception thrown if an error occurs.
 */
private void checkForOldSettings() throws Exception {
    // Check for old settings.xml
    File settingsXML = new File(Spark.getSparkUserHome(), "/settings.xml");
    if (settingsXML.exists()) {
        SAXReader saxReader = new SAXReader();
        Document pluginXML;
        try {
            pluginXML = saxReader.read(settingsXML);
        } catch (DocumentException e) {
            Log.error(e);
            return;
        }
        List<?> plugins = pluginXML.selectNodes("/settings");
        for (Object plugin1 : plugins) {
            Element plugin = (Element) plugin1;
            String username = plugin.selectSingleNode("username").getText();
            localPref.setLastUsername(username);
            String server = plugin.selectSingleNode("server").getText();
            localPref.setServer(server);
            String autoLogin = plugin.selectSingleNode("autoLogin").getText();
            localPref.setAutoLogin(Boolean.parseBoolean(autoLogin));
            String savePassword = plugin.selectSingleNode("savePassword").getText();
            localPref.setSavePassword(Boolean.parseBoolean(savePassword));
            String password = plugin.selectSingleNode("password").getText();
            localPref.setPasswordForUser(username + "@" + server, password);
            SettingsManager.saveSettings();
        }
        // Delete settings File
        settingsXML.delete();
    }
}
Also used : SAXReader(org.dom4j.io.SAXReader) DocumentException(org.dom4j.DocumentException) Element(org.dom4j.Element) Document(org.dom4j.Document) File(java.io.File)

Example 77 with DocumentException

use of org.dom4j.DocumentException in project sagacity-sqltoy by chenrenfei.

the class TranslateManager method parseTranslate.

/**
 * @todo 解析缓存翻译的配置文件
 * @param configFile
 * @throws Exception
 */
public void parseTranslate(String configFile) throws Exception {
    if (StringUtil.isBlank(configFile))
        return;
    InputStream fileIS = null;
    InputStreamReader ir = null;
    try {
        fileIS = CommonUtils.getFileInputStream(configFile);
        if (fileIS != null) {
            SAXReader saxReader = new SAXReader();
            saxReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
            ir = new InputStreamReader(fileIS);
            Document doc = saxReader.read(ir);
            Element root = doc.getRootElement();
            List translates = root.elements("translate");
            if (translates == null || translates.isEmpty())
                return;
            Element translate;
            for (Iterator iter = translates.iterator(); iter.hasNext(); ) {
                translate = (Element) iter.next();
                TranslateCacheModel translateCacheModel = new TranslateCacheModel();
                // 对应缓存
                translateCacheModel.setCacheName(translate.attributeValue("cache"));
                // key对应的数据列
                if (translate.attribute("key-index") != null) {
                    translateCacheModel.setKeyIndex(Integer.parseInt(translate.attributeValue("key-index")));
                }
                // 查询数据的service
                if (translate.attribute("service") != null) {
                    translateCacheModel.setService(translate.attributeValue("service"));
                }
                // 查询数据的service对应的方法
                if (translate.attribute("method") != null) {
                    translateCacheModel.setServiceMethod(translate.attributeValue("method"));
                }
                // 设置缓存管理器
                if (translate.attribute("cache-manager") != null) {
                    translateCacheModel.setTranslateCacheManager(translate.attributeValue("cache-manager"));
                } else if (translate.attribute("manager") != null) {
                    translateCacheModel.setTranslateCacheManager(translate.attributeValue("manager"));
                }
                // 过期时长
                CacheConfig cacheConfig = new CacheConfig();
                cacheConfig.setExpireSeconds(SqlToyConstants.getCacheExpireSeconds());
                if (translate.attribute("expire-seconds") != null) {
                    cacheConfig.setExpireSeconds(Long.parseLong(translate.attributeValue("expire-seconds")));
                } else if (translate.attribute("keep-alive") != null) {
                    cacheConfig.setExpireSeconds(Long.parseLong(translate.attributeValue("keep-alive")));
                }
                // sql对应的dataSource
                if (translate.attribute("datasource") != null)
                    translateCacheModel.setDataSource(translate.attributeValue("datasource"));
                else if (translate.attribute("dataSource") != null)
                    translateCacheModel.setDataSource(translate.attributeValue("dataSource"));
                // 基于简单的sql查询对应的sql
                if (translate.attribute("sql") != null) {
                    translateCacheModel.setSql(translate.attributeValue("sql"));
                } else if (StringUtil.isNotBlank(translate.getTextTrim())) {
                    translateCacheModel.setSql(translate.getText());
                } else if (translate.element("sql") != null) {
                    translateCacheModel.setSql(translate.element("sql").getText());
                }
                translateMap.put(translateCacheModel.getCacheName(), translateCacheModel);
            }
        }
    } catch (DocumentException de) {
        de.printStackTrace();
        logger.error("读取translate对应的xml文件失败,对应文件={}", configFile, de);
        throw de;
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    } finally {
        if (ir != null)
            ir.close();
        if (fileIS != null)
            fileIS.close();
    }
}
Also used : TranslateCacheModel(org.sagacity.sqltoy.config.model.TranslateCacheModel) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) DocumentException(org.dom4j.DocumentException) Iterator(java.util.Iterator) List(java.util.List) Document(org.dom4j.Document) CacheConfig(org.sagacity.sqltoy.config.model.CacheConfig) DocumentException(org.dom4j.DocumentException)

Example 78 with DocumentException

use of org.dom4j.DocumentException in project sagacity-sqltoy by chenrenfei.

the class SqlXMLConfigParse method parseSingleFile.

/**
 * @todo <b>解析单个sql对应的xml文件</b>
 * @param xmlFile
 * @param cache
 * @param encoding
 * @param dialect
 * @throws Exception
 */
public static void parseSingleFile(Object xmlFile, ConcurrentHashMap<String, SqlToyConfig> cache, String encoding, String dialect) throws Exception {
    InputStream fileIS = null;
    InputStreamReader ir = null;
    try {
        if (xmlFile instanceof File) {
            File file = (File) xmlFile;
            filesLastModifyMap.put(file.getName(), new Long(file.lastModified()));
            fileIS = new FileInputStream(file);
            if (logger.isDebugEnabled())
                logger.debug("正在解析sql文件,对应文件={}", file.getName());
        } else {
            fileIS = getResourceAsStream((String) xmlFile);
            if (logger.isDebugEnabled())
                logger.debug("正在解析sql文件,对应文件={}", (String) xmlFile);
        }
        if (fileIS != null) {
            if (encoding != null)
                ir = new InputStreamReader(fileIS, encoding);
            else
                ir = new InputStreamReader(fileIS);
            SAXReader saxReader = new SAXReader();
            saxReader.setFeature(SqlToyConstants.XML_FETURE, false);
            if (StringUtil.isNotBlank(encoding))
                saxReader.setEncoding(encoding);
            Document doc = saxReader.read(ir);
            List<Element> sqlElts = doc.getRootElement().elements();
            if (sqlElts == null || sqlElts.isEmpty())
                return;
            // 解析单个sql
            SqlToyConfig sqlToyConfig;
            for (Iterator<Element> iter = sqlElts.iterator(); iter.hasNext(); ) {
                sqlToyConfig = parseSingleSql(iter.next(), dialect);
                if (sqlToyConfig != null) {
                    // 去除sql中的注释语句并放入缓存
                    if (cache.get(sqlToyConfig.getId()) != null)
                        logger.warn("发现重复的SQL语句,id={},将被覆盖!", sqlToyConfig.getId());
                    cache.put(sqlToyConfig.getId(), sqlToyConfig);
                }
            }
        }
    } catch (DocumentException de) {
        de.printStackTrace();
        logger.error("读取sql对应的xml文件失败,对应文件={}", xmlFile, de);
        throw de;
    } catch (Exception e) {
        e.printStackTrace();
        logger.error("解析xml中对应的sql失败,对应文件={},正确的配置为<sql|mql|eql id=\"\"><![CDATA[]]></sql|mql|eql>或<sql|mql|eql id=\"\"><desc></desc><value><![CDATA[]]></value></sql|mql|eql>", xmlFile, e);
        throw e;
    } finally {
        if (ir != null)
            ir.close();
        if (fileIS != null)
            fileIS.close();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SAXReader(org.dom4j.io.SAXReader) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) Element(org.dom4j.Element) Document(org.dom4j.Document) FileInputStream(java.io.FileInputStream) DocumentException(org.dom4j.DocumentException) DocumentException(org.dom4j.DocumentException) File(java.io.File)

Example 79 with DocumentException

use of org.dom4j.DocumentException in project tmdm-studio-se by Talend.

the class ResourcesUtil method parsXMLString.

private static Document parsXMLString(String responseBody) {
    SAXReader saxReader = new SAXReader();
    Document document = null;
    try {
        document = saxReader.read(new StringReader(responseBody));
    } catch (DocumentException e) {
        return null;
    }
    return document;
}
Also used : SAXReader(org.dom4j.io.SAXReader) DocumentException(org.dom4j.DocumentException) StringReader(java.io.StringReader) Document(org.dom4j.Document)

Example 80 with DocumentException

use of org.dom4j.DocumentException in project tmdm-studio-se by Talend.

the class ExtensibleXMLEditorPageContent method toXMLExpression.

public String toXMLExpression() {
    Document xmlDoc = DOMDocumentFactory.getInstance().createDocument();
    fillXMLDoc(xmlDoc);
    try {
        // $NON-NLS-1$
        return XmlUtil.formatPretty(XmlUtil.toXml(xmlDoc), "UTF-8");
    } catch (DocumentException e) {
        // $NON-NLS-1$
        return "";
    }
}
Also used : DocumentException(org.dom4j.DocumentException) Document(org.dom4j.Document)

Aggregations

DocumentException (org.dom4j.DocumentException)123 Document (org.dom4j.Document)80 SAXReader (org.dom4j.io.SAXReader)73 Element (org.dom4j.Element)51 IOException (java.io.IOException)45 File (java.io.File)27 InputStream (java.io.InputStream)21 StringReader (java.io.StringReader)15 InputStreamReader (java.io.InputStreamReader)11 ArrayList (java.util.ArrayList)10 XMLWriter (org.dom4j.io.XMLWriter)9 InputSource (org.xml.sax.InputSource)9 FileInputStream (java.io.FileInputStream)7 URL (java.net.URL)7 List (java.util.List)7 Node (org.dom4j.Node)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 FileNotFoundException (java.io.FileNotFoundException)5 Reader (java.io.Reader)5