Example usage for org.dom4j.io OutputFormat setIndent

List of usage examples for org.dom4j.io OutputFormat setIndent

Introduction

In this page you can find the example usage for org.dom4j.io OutputFormat setIndent.

Prototype

public void setIndent(boolean doIndent) 

Source Link

Document

Set the indent on or off.

Usage

From source file:com.faithbj.shop.action.admin.InstallAction.java

public String save() throws URISyntaxException, IOException, DocumentException {
    if (isInstalled()) {
        return ajaxJsonErrorMessage("CAIJINGLING?????");
    }//from   w w  w .  ja  v a  2 s.  c o  m
    if (StringUtils.isEmpty(databaseHost)) {
        return ajaxJsonErrorMessage("?!");
    }
    if (StringUtils.isEmpty(databasePort)) {
        return ajaxJsonErrorMessage("??!");
    }
    if (StringUtils.isEmpty(databaseUsername)) {
        return ajaxJsonErrorMessage("???!");
    }
    if (StringUtils.isEmpty(databasePassword)) {
        return ajaxJsonErrorMessage("??!");
    }
    if (StringUtils.isEmpty(databaseName)) {
        return ajaxJsonErrorMessage("???!");
    }
    if (StringUtils.isEmpty(adminUsername)) {
        return ajaxJsonErrorMessage("???!");
    }
    if (StringUtils.isEmpty(adminPassword)) {
        return ajaxJsonErrorMessage("??!");
    }
    if (StringUtils.isEmpty(installStatus)) {
        Map<String, String> jsonMap = new HashMap<String, String>();
        jsonMap.put(STATUS, "requiredCheckFinish");
        return ajaxJson(jsonMap);
    }

    String jdbcUrl = "jdbc:mysql://" + databaseHost + ":" + databasePort + "/" + databaseName
            + "?useUnicode=true&characterEncoding=UTF-8";
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    try {
        // ?
        connection = DriverManager.getConnection(jdbcUrl, databaseUsername, databasePassword);
        DatabaseMetaData databaseMetaData = connection.getMetaData();
        String[] types = { "TABLE" };
        resultSet = databaseMetaData.getTables(null, databaseName, "%", types);
        if (StringUtils.equalsIgnoreCase(installStatus, "databaseCheck")) {
            Map<String, String> jsonMap = new HashMap<String, String>();
            jsonMap.put(STATUS, "databaseCheckFinish");
            return ajaxJson(jsonMap);
        }

        // ?
        if (StringUtils.equalsIgnoreCase(installStatus, "databaseCreate")) {
            StringBuffer stringBuffer = new StringBuffer();
            BufferedReader bufferedReader = null;
            String sqlFilePath = Thread.currentThread().getContextClassLoader().getResource("").toURI()
                    .getPath() + SQL_INSTALL_FILE_NAME;
            bufferedReader = new BufferedReader(
                    new InputStreamReader(new FileInputStream(sqlFilePath), "UTF-8"));
            String line = "";
            while (null != line) {
                line = bufferedReader.readLine();
                stringBuffer.append(line);
                if (null != line && line.endsWith(";")) {
                    System.out.println("[CAIJINGLING?]SQL: " + line);
                    preparedStatement = connection.prepareStatement(stringBuffer.toString());
                    preparedStatement.executeUpdate();
                    stringBuffer = new StringBuffer();
                }
            }
            String insertAdminSql = "INSERT INTO `admin` VALUES ('402881862bec2a21012bec2bd8de0003','2010-10-10 0:0:0','2010-10-10 0:0:0','','admin@shopxx.net',b'1',b'0',b'0',b'0',NULL,NULL,0,NULL,'?','"
                    + DigestUtils.md5Hex(adminPassword) + "','" + adminUsername + "');";
            String insertAdminRoleSql = "INSERT INTO `admin_role` VALUES ('402881862bec2a21012bec2bd8de0003','402881862bec2a21012bec2b70510002');";
            preparedStatement = connection.prepareStatement(insertAdminSql);
            preparedStatement.executeUpdate();
            preparedStatement = connection.prepareStatement(insertAdminRoleSql);
            preparedStatement.executeUpdate();
        }
    } catch (SQLException e) {
        e.printStackTrace();
        return ajaxJsonErrorMessage("???!");
    } finally {
        try {
            if (resultSet != null) {
                resultSet.close();
                resultSet = null;
            }
            if (preparedStatement != null) {
                preparedStatement.close();
                preparedStatement = null;
            }
            if (connection != null) {
                connection.close();
                connection = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // ???
    String configFilePath = Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath()
            + JDBC_CONFIG_FILE_NAME;
    Properties properties = new Properties();
    properties.put("jdbc.driver", "com.mysql.jdbc.Driver");
    properties.put("jdbc.url", jdbcUrl);
    properties.put("jdbc.username", databaseUsername);
    properties.put("jdbc.password", databasePassword);
    properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
    properties.put("hibernate.show_sql", "false");
    properties.put("hibernate.format_sql", "false");
    OutputStream outputStream = new FileOutputStream(configFilePath);
    properties.store(outputStream, JDBC_CONFIG_FILE_DESCRIPTION);
    outputStream.close();

    // ??
    String backupWebConfigFilePath = Thread.currentThread().getContextClassLoader().getResource("").toURI()
            .getPath() + BACKUP_WEB_CONFIG_FILE_NAME;
    String backupApplicationContextConfigFilePath = Thread.currentThread().getContextClassLoader()
            .getResource("").toURI().getPath() + BACKUP_APPLICATION_CONTEXT_CONFIG_FILE_NAME;
    String backupCompassApplicationContextConfigFilePath = Thread.currentThread().getContextClassLoader()
            .getResource("").toURI().getPath() + BACKUP_COMPASS_APPLICATION_CONTEXT_CONFIG_FILE_NAME;
    String backupSecurityApplicationContextConfigFilePath = Thread.currentThread().getContextClassLoader()
            .getResource("").toURI().getPath() + BACKUP_SECURITY_APPLICATION_CONTEXT_CONFIG_FILE_NAME;

    String webConfigFilePath = new File(
            Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath()).getParent() + "/"
            + WEB_CONFIG_FILE_NAME;
    String applicationContextConfigFilePath = Thread.currentThread().getContextClassLoader().getResource("")
            .toURI().getPath() + APPLICATION_CONTEXT_CONFIG_FILE_NAME;
    String compassApplicationContextConfigFilePath = Thread.currentThread().getContextClassLoader()
            .getResource("").toURI().getPath() + COMPASS_APPLICATION_CONTEXT_CONFIG_FILE_NAME;
    String securityApplicationContextConfigFilePath = Thread.currentThread().getContextClassLoader()
            .getResource("").toURI().getPath() + SECURITY_APPLICATION_CONTEXT_CONFIG_FILE_NAME;

    FileUtils.copyFile(new File(backupWebConfigFilePath), new File(webConfigFilePath));
    FileUtils.copyFile(new File(backupApplicationContextConfigFilePath),
            new File(applicationContextConfigFilePath));
    FileUtils.copyFile(new File(backupCompassApplicationContextConfigFilePath),
            new File(compassApplicationContextConfigFilePath));
    FileUtils.copyFile(new File(backupSecurityApplicationContextConfigFilePath),
            new File(securityApplicationContextConfigFilePath));

    // ??
    String systemConfigFilePath = Thread.currentThread().getContextClassLoader().getResource("").toURI()
            .getPath() + SystemConfigUtil.CONFIG_FILE_NAME;
    File systemConfigFile = new File(systemConfigFilePath);
    SAXReader saxReader = new SAXReader();
    Document document = saxReader.read(systemConfigFile);
    Element rootElement = document.getRootElement();
    Element systemConfigElement = rootElement.element("systemConfig");
    Node isInstalledNode = document.selectSingleNode("/shopxx/systemConfig/isInstalled");
    if (isInstalledNode == null) {
        isInstalledNode = systemConfigElement.addElement("isInstalled");
    }
    isInstalledNode.setText("true");
    try {
        OutputFormat outputFormat = OutputFormat.createPrettyPrint();// XML?
        outputFormat.setEncoding("UTF-8");// XML?
        outputFormat.setIndent(true);// ?
        outputFormat.setIndent("   ");// TAB?
        outputFormat.setNewlines(true);// ??
        XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(systemConfigFile), outputFormat);
        xmlWriter.write(document);
        xmlWriter.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return ajaxJsonSuccessMessage("CAIJINGLING?????");
}

From source file:com.feilong.tools.dom4j.Dom4jUtil.java

License:Apache License

/**
 * ? xml.<br>/* w  ww  . j a  v a  2  s.c  om*/
 * 
 * <pre>
 * {@code
 * <wddxPacket version='1.0'><header/><data><struct><var name='TRANSACTIONID'><string></string></var><var name='ACQUIRERRESPONSECODE'><string></string></var><var name='SCRUBMESSAGE'><string>Invalid siteID</string></var><var name='AMOUNT'><string></string></var><var name='SERVICEVERSION'><string>2.0</string></var><var name='TRANSACTIONSCRUBCODE'><string></string></var><var name='MERCHANTTRANSACTIONID'><string>010003170001</string></var><var name='CURRENCY'><string></string></var><var name='TRANSACTIONSTATUS'><string></string></var><var name='SITEID'><string>Blanja2</string></var><var name='TRANSACTIONDATE'><string></string></var><var name='ACQUIRERCODE'><string></string></var><var name='SCRUBCODE'><string>50007</string></var><var name='TRANSACTIONSCRUBMESSAGE'><string></string></var><var name='ACQUIRERAPPROVALCODE'><string></string></var><var name='TRANSACTIONTYPE'><string>AUTHORIZATION</string></var></struct></data></wddxPacket>
 * }
 * </pre>
 * 
 * ?
 * 
 * <pre>
 * {@code
 * <wddxPacket version="1.0">
 *    <header/>
 *    <data>
 *       <struct>
 *          <var name="TRANSACTIONID">
 *             <string/>
 *          </var>
 *          <var name="ACQUIRERRESPONSECODE">
 *             <string/>
 *          </var>
 *          <var name="SCRUBMESSAGE">
 *             <string>Invalid siteID</string>
 *          </var>
 *          <var name="AMOUNT">
 *             <string/>
 *          </var>
 *          <var name="SERVICEVERSION">
 *             <string>2.0</string>
 *          </var>
 *          <var name="TRANSACTIONSCRUBCODE">
 *             <string/>
 *          </var>
 *          <var name="MERCHANTTRANSACTIONID">
 *             <string>010003170001</string>
 *          </var>
 *          <var name="CURRENCY">
 *             <string/>
 *          </var>
 *          <var name="TRANSACTIONSTATUS">
 *             <string/>
 *          </var>
 *          <var name="SITEID">
 *             <string>Blanja2</string>
 *          </var>
 *          <var name="TRANSACTIONDATE">
 *             <string/>
 *          </var>
 *          <var name="ACQUIRERCODE">
 *             <string/>
 *          </var>
 *          <var name="SCRUBCODE">
 *             <string>50007</string>
 *          </var>
 *          <var name="TRANSACTIONSCRUBMESSAGE">
 *             <string/>
 *          </var>
 *          <var name="ACQUIRERAPPROVALCODE">
 *             <string/>
 *          </var>
 *          <var name="TRANSACTIONTYPE">
 *             <string>AUTHORIZATION</string>
 *          </var>
 *       </struct>
 *    </data>
 * </wddxPacket>
 * 
 * }
 * </pre>
 *
 * @param xmlString
 *            the xml string
 * @param encoding
 *            the encoding
 * @param writer
 *            the writer
 * @return the string
 * @throws Dom4jException
 *             the dom4j exception
 * @since 1.0.8
 */
public static String format(String xmlString, String encoding, Writer writer) throws Dom4jException {

    OutputFormat outputFormat = OutputFormat.createPrettyPrint(); // XML?
    outputFormat.setEncoding(encoding); // XML?
    outputFormat.setSuppressDeclaration(true);
    outputFormat.setIndent(true); // ?
    outputFormat.setIndent("   "); // tab?
    outputFormat.setNewlines(true); // ??

    XMLWriter xmlWriter = new XMLWriter(writer, outputFormat);
    xmlWriter.setEscapeText(false);

    Document document = string2Document(xmlString);
    try {
        xmlWriter.write(document);
        xmlWriter.close();

        return writer.toString();
    } catch (IOException e) {
        throw new Dom4jException(e);
    }
}

From source file:com.globalsight.diplomat.util.XmlUtil.java

License:Apache License

public static String format(String xml, EntityResolver entityResolver) {
    XmlParser parser = XmlParser.hire();
    if (entityResolver != null) {
        parser.setEntityResolver(entityResolver);
    }// w  ww.j  a  v a 2s .c  o m
    org.dom4j.Document dom = parser.parseXml(xml);
    OutputFormat format = new OutputFormat();
    format.setNewlines(true);
    format.setIndent(true);

    StringWriter writer = new StringWriter();
    XMLWriter xmlWriter = new XMLWriter(writer, format);
    try {
        xmlWriter.write(dom);
        xmlWriter.close();
    } catch (IOException e) {
        return xml;
    } finally {
        try {
            xmlWriter.close();
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
    }

    return writer.getBuffer().toString();
}

From source file:com.iterzp.momo.utils.SettingUtils.java

License:Open Source License

/**
 * /*from   w ww  .ja  v  a2s. co  m*/
 * 
 * @param setting
 *            
 */
public static void set(Setting setting) {
    try {
        File shopxxXmlFile = new ClassPathResource(CommonAttributes.MOMO_XML_PATH).getFile();
        Document document = new SAXReader().read(shopxxXmlFile);
        List<Element> elements = document.selectNodes("/momo/setting");
        for (Element element : elements) {
            try {
                String name = element.attributeValue("name");
                String value = beanUtils.getProperty(setting, name);
                Attribute attribute = element.attribute("value");
                attribute.setValue(value);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
        }

        FileOutputStream fileOutputStream = null;
        XMLWriter xmlWriter = null;
        try {
            OutputFormat outputFormat = OutputFormat.createPrettyPrint();
            outputFormat.setEncoding("UTF-8");
            outputFormat.setIndent(true);
            outputFormat.setIndent("   ");
            outputFormat.setNewlines(true);
            fileOutputStream = new FileOutputStream(shopxxXmlFile);
            xmlWriter = new XMLWriter(fileOutputStream, outputFormat);
            xmlWriter.write(document);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (xmlWriter != null) {
                try {
                    xmlWriter.close();
                } catch (IOException e) {
                }
            }
            IOUtils.closeQuietly(fileOutputStream);
        }

        Ehcache cache = cacheManager.getEhcache(Setting.CACHE_NAME);
        cache.put(new net.sf.ehcache.Element(Setting.CACHE_KEY, setting));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.jiangnan.es.orm.mybatis.util.MybatisMapperXmlGenerator.java

License:Apache License

/**
 * //from   w  w w .  j  a  v a 2  s . c om
 * @param document
 * @throws IOException
 */
private void write(Document document) throws IOException {
    XMLWriter xmlWriter = null;
    try {
        FileWriter writer = new FileWriter(new File(this.exportFileName));
        OutputFormat format = new OutputFormat();
        format.setEncoding("UTF-8");
        //? 
        format.setIndent(true);
        format.setIndent("    ");
        //? 
        format.setNewlines(true);
        xmlWriter = new XMLWriter(writer, format);
        xmlWriter.write(document);
    } finally {
        if (null != xmlWriter) {
            xmlWriter.close();
        }
    }
}

From source file:com.liferay.petra.xml.Dom4jUtil.java

License:Open Source License

public static String toString(Node node, String indent, boolean expandEmptyElements, boolean trimText)
        throws IOException {

    UnsyncByteArrayOutputStream unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream();

    OutputFormat outputFormat = OutputFormat.createPrettyPrint();

    outputFormat.setExpandEmptyElements(expandEmptyElements);
    outputFormat.setIndent(indent);
    outputFormat.setLineSeparator(StringPool.NEW_LINE);
    outputFormat.setTrimText(trimText);//from  w w w  .  j av a2  s  .  com

    XMLWriter xmlWriter = new XMLWriter(unsyncByteArrayOutputStream, outputFormat);

    xmlWriter.write(node);

    String content = unsyncByteArrayOutputStream.toString(StringPool.UTF8);

    // LEP-4257

    //content = StringUtil.replace(content, "\n\n\n", "\n\n");

    if (content.endsWith("\n\n")) {
        content = content.substring(0, content.length() - 2);
    }

    if (content.endsWith("\n")) {
        content = content.substring(0, content.length() - 1);
    }

    while (content.contains(" \n")) {
        content = StringUtil.replace(content, " \n", "\n");
    }

    if (content.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")) {
        content = StringUtil.replaceFirst(content, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
                "<?xml version=\"1.0\"?>");
    }

    return content;
}

From source file:com.liferay.util.xml.XMLFormatter.java

License:Open Source License

public static String toString(Document doc, String indent) throws IOException {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    OutputFormat format = OutputFormat.createPrettyPrint();

    format.setIndent(indent);
    format.setLineSeparator("\n");

    XMLWriter writer = new XMLWriter(baos, format);

    writer.write(doc);// w  w  w.  java 2  s  .  c o m

    String content = baos.toString();

    content = StringUtil.replace(content, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
            "<?xml version=\"1.0\"?>");

    int x = content.indexOf("<!DOCTYPE");
    if (x != -1) {
        x = content.indexOf(">", x) + 1;
        content = content.substring(0, x) + "\n" + content.substring(x, content.length());
    }

    content = StringUtil.replace(content, "\n\n\n", "\n\n");

    if (content.endsWith("\n\n")) {
        content = content.substring(0, content.length() - 2);
    }

    if (content.endsWith("\n")) {
        content = content.substring(0, content.length() - 1);
    }

    return content;
}

From source file:com.liferay.util.xml.XMLMergerRunner.java

License:Open Source License

private String _documentToString(Document doc, String docType) throws IOException {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    OutputFormat format = OutputFormat.createPrettyPrint();

    format.setIndent("\t");
    format.setLineSeparator("\n");

    XMLWriter writer = new XMLWriter(baos, format);

    writer.write(doc);/*from w w  w  .j  a v a2s. c o  m*/

    String xml = baos.toString();

    int pos = xml.indexOf("<?");

    String header = xml.substring(pos, xml.indexOf("?>", pos) + 2);

    xml = StringUtil.replace(xml, header, "");
    xml = header + "\n" + docType + "\n" + xml;

    return xml;
}

From source file:com.lingxiang2014.util.SettingUtils.java

License:Open Source License

public static void set(Setting setting) {
    try {// w  ww. j a  v  a  2  s .  c  o  m
        File shopxxXmlFile = new ClassPathResource(CommonAttributes.SHOPXX_XML_PATH).getFile();
        Document document = new SAXReader().read(shopxxXmlFile);
        List<Element> elements = document.selectNodes("/shopxx/setting");
        for (Element element : elements) {
            try {
                String name = element.attributeValue("name");
                String value = beanUtils.getProperty(setting, name);
                Attribute attribute = element.attribute("value");
                attribute.setValue(value);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
        }

        FileOutputStream fileOutputStream = null;
        XMLWriter xmlWriter = null;
        try {
            OutputFormat outputFormat = OutputFormat.createPrettyPrint();
            outputFormat.setEncoding("UTF-8");
            outputFormat.setIndent(true);
            outputFormat.setIndent("   ");
            outputFormat.setNewlines(true);
            fileOutputStream = new FileOutputStream(shopxxXmlFile);
            xmlWriter = new XMLWriter(fileOutputStream, outputFormat);
            xmlWriter.write(document);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (xmlWriter != null) {
                try {
                    xmlWriter.close();
                } catch (IOException e) {
                }
            }
            IOUtils.closeQuietly(fileOutputStream);
        }

        Ehcache cache = cacheManager.getEhcache(Setting.CACHE_NAME);
        cache.put(new net.sf.ehcache.Element(Setting.CACHE_KEY, setting));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.sammyun.util.SettingUtils.java

License:Open Source License

/**
 * /*from  w  w w .  ja v  a 2 s .  com*/
 * 
 * @param setting 
 */
public static void set(Setting setting) {
    try {
        File preschoolEduXmlFile = new ClassPathResource(CommonAttributes.PRESCHOOLEDU_XML_PATH).getFile();
        Document document = new SAXReader().read(preschoolEduXmlFile);
        List<Element> elements = document.selectNodes("/preschoolEdu/setting");
        for (Element element : elements) {
            try {
                String name = element.attributeValue("name");
                String value = beanUtils.getProperty(setting, name);
                Attribute attribute = element.attribute("value");
                attribute.setValue(value);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                logger.error(e.getMessage());
            } catch (InvocationTargetException e) {
                e.printStackTrace();
                logger.error(e.getMessage());
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
                logger.error(e.getMessage());
            }
        }

        FileOutputStream fileOutputStream = null;
        XMLWriter xmlWriter = null;
        try {
            OutputFormat outputFormat = OutputFormat.createPrettyPrint();
            outputFormat.setEncoding("UTF-8");
            outputFormat.setIndent(true);
            outputFormat.setIndent("   ");
            outputFormat.setNewlines(true);
            fileOutputStream = new FileOutputStream(preschoolEduXmlFile);
            xmlWriter = new XMLWriter(fileOutputStream, outputFormat);
            xmlWriter.write(document);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (xmlWriter != null) {
                try {
                    xmlWriter.close();
                } catch (IOException e) {
                }
            }
            IOUtils.closeQuietly(fileOutputStream);
        }

        Ehcache cache = cacheManager.getEhcache(Setting.CACHE_NAME);
        cache.put(new net.sf.ehcache.Element(Setting.CACHE_KEY, setting));
    } catch (Exception e) {
        e.printStackTrace();
        logger.error(e.getMessage());
    }
}