Example usage for org.dom4j.io OutputFormat createPrettyPrint

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

Introduction

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

Prototype

public static OutputFormat createPrettyPrint() 

Source Link

Document

A static helper method to create the default pretty printing format.

Usage

From source file:com.taobao.android.builder.tools.xml.XmlHelper.java

License:Apache License

public static void saveDocument(Document document, File file) throws IOException {

    file.getParentFile().mkdirs();/*from  w  w w.  j a v a2  s . c  o m*/

    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding("UTF-8");

    saveFile(document, format, file);
}

From source file:com.taobao.datax.engine.tools.JobConfGenDriver.java

License:Open Source License

private static int genXmlFile(String filename, ClassNode reader, ClassNode writer) throws IOException {

    Document document = DocumentHelper.createDocument();
    Element jobsElement = document.addElement("jobs");
    Element jobElement = jobsElement.addElement("job");
    String id = reader.getName() + "_to_" + writer.getName() + "_job";
    jobElement.addAttribute("id", id);

    /**/* w  w w.j a  v a2 s . co  m*/
     * ?readerxml
     */
    Element readerElement = jobElement.addElement("reader");
    Element plugin_Element = readerElement.addElement("plugin");
    plugin_Element.setText(reader.getName());

    ClassNode readerNode = reader;
    Element tempElement = null;

    List<ClassMember> members = readerNode.getAllMembers();
    for (ClassMember member : members) {
        StringBuilder command = new StringBuilder("\n");

        Set<String> set = member.getAllKeys();
        String value = "";
        for (String key : set) {
            value = member.getAttr("default");
            command.append(key).append(":").append(member.getAttr(key)).append("\n");
        }
        readerElement.addComment(command.toString());

        String keyName = member.getName();
        keyName = keyName.substring(1, keyName.length() - 1);
        tempElement = readerElement.addElement("param");
        tempElement.addAttribute("key", keyName);

        if (value == null || "".equals(value)) {
            value = "?";
        }
        tempElement.addAttribute("value", value);
    }

    /**
     * ?writerxml
     */
    Element writerElement = jobElement.addElement("writer");
    plugin_Element = writerElement.addElement("plugin");
    plugin_Element.setText(writer.getName());

    members = writer.getAllMembers();
    for (ClassMember member : members) {
        StringBuilder command = new StringBuilder("\n");
        Set<String> set = member.getAllKeys();

        String value = "";
        for (String key : set) {
            value = member.getAttr("default");
            command.append(key).append(":").append(member.getAttr(key)).append("\n");
        }
        writerElement.addComment(command.toString());

        String keyName = member.getName();
        keyName = keyName.substring(1, keyName.length() - 1);
        tempElement = writerElement.addElement("param");
        tempElement.addAttribute("key", keyName);

        if (value == null || "".equals(value)) {
            value = "?";
        }
        tempElement.addAttribute("value", value);
    }

    try {
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("UTF-8");
        XMLWriter writerOfXML = new XMLWriter(new FileWriter(new File(filename)), format);
        writerOfXML.write(document);
        writerOfXML.close();
    } catch (Exception ex) {
        throw new IOException(ex.getCause());
    }

    return 0;
}

From source file:com.taobao.osceola.demo.acount.persist.service.impl.AccountPersistServiceImpl.java

License:Open Source License

/**
 * @param doc/*w w  w . ja  v a  2s.  com*/
 */
private void writeDocment(Document doc) throws AccountPersistException {
    Writer out = null;
    try {
        out = new OutputStreamWriter(new FileOutputStream(file), "utf-8");
        XMLWriter writer = new XMLWriter(out, OutputFormat.createPrettyPrint());
        writer.write(doc);
    } catch (Exception e) {
        throw new AccountPersistException("", e);
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
            throw new AccountPersistException("", e);
        }
    }
}

From source file:com.tedi.engine.XMLOutput.java

License:Open Source License

/**
 * Handles functionality of formatting and writing document.
 * /*  www .  j  av  a  2  s  . c  om*/
 * @param doc
 *            The document
 * @return the cleaned document.
 * @throws Exception
 */
private String cleanDocument(Document doc) throws Exception {
    if (logger.isDebugEnabled()) {
        logger.debug("Cleaning the document object.");
    }
    String docStr = "";
    // --COMMENTED OUT 10-10-2005
    // JBG----------------------------------------------------------------------------------
    // if (dtd != null && dtd.length()>0) {
    // xmlUtils.setDocument(doc);
    // try {
    // AbstractSchemaDescriptor schema =
    // AbstractSchemaDescriptor.createDescriptor(absoluteDTD_URL.toString());
    // schema.processSchema(doc.getRootElement().getName());
    // xmlUtils.setSchema(schema);
    // xmlUtils.cleanDocument();
    // }
    // catch (Exception e) {
    // execResults.addMessage(ExecutionResults.M_WARNING,
    // ExecutionResults.J2EE_TARGET_ERR,
    // "Error removing optional attributes/elements: " + e.getMessage());
    // }
    // }
    // ----------------------------------------------------------------------------------------------------------------
    cleanElement(doc.getRootElement());
    StringWriter sw = new StringWriter();
    OutputFormat format = isCompact ? OutputFormat.createCompactFormat() : OutputFormat.createPrettyPrint();
    format.setExpandEmptyElements(false);
    XMLWriter writer = new XMLWriter(sw, format);
    writer.setMaximumAllowedCharacter(127);
    writer.write(doc);
    writer.close();
    docStr = sw.toString();
    if (isSuppressDocType && doc.getDocType() != null) {
        int ndx = docStr.indexOf("<" + doc.getRootElement().getName());
        docStr = docStr.substring(ndx);
    }
    return docStr;
}

From source file:com.thinkberg.moxo.dav.WebdavHandler.java

License:Apache License

void log(Node element) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {/*  ww w .j  av a  2  s.c  om*/
        XMLWriter xmlWriter = new XMLWriter(bos, OutputFormat.createPrettyPrint());
        xmlWriter.write(element);
        System.out.print(bos.toString());
    } catch (IOException e) {
        servlet.log("!! " + e.getMessage());
    }
}

From source file:com.thinkberg.webdav.LockHandler.java

License:Apache License

protected void logXml(Node element) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {//from  w ww. ja  va 2s  . c o  m
        XMLWriter xmlWriter = new XMLWriter(bos, OutputFormat.createPrettyPrint());
        xmlWriter.write(element);
        LOG.debug(bos.toString());
    } catch (IOException e) {
        LOG.debug("ERROR writing XML log: " + e.getMessage());
    }
}

From source file:com.thinkberg.webdav.WebdavHandler.java

License:Apache License

void logXml(Node element) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {//from   www.j  a va  2  s. co m
        XMLWriter xmlWriter = new XMLWriter(bos, OutputFormat.createPrettyPrint());
        xmlWriter.write(element);
        LogFactory.getLog(this.getClass()).debug(bos.toString());
    } catch (IOException e) {
        LogFactory.getLog(this.getClass()).error(e.getMessage());
    }
}

From source file:com.thoughtworks.xstream.io.xml.Dom4JDriver.java

License:Open Source License

/**
 * @since 1.4//from  ww  w. j a v a  2s  . com
 */
public Dom4JDriver(NameCoder nameCoder) {
    this(new DocumentFactory(), OutputFormat.createPrettyPrint(), nameCoder);
    outputFormat.setTrimText(false);
}

From source file:com.tmount.util.FileUtils.java

License:Open Source License

 /**
 * ?// ww  w.  j ava2s  .c  o  m
 * @param fileName ??
 */
private static void initFile(String fileName){
      
   Document document = DocumentHelper.createDocument();
   Element root = document.addElement("auth");
   Element billdata = root.addElement("balance");
   billdata.setText("100");
   OutputFormat format = OutputFormat.createPrettyPrint();
   format.setEncoding("GBK");
   XMLWriter writer;
   try {
      writer = new XMLWriter(new FileWriter(new File(
            fileName)), format);
      writer.write(document); // 
      writer.close();
   } catch (IOException e) {
      log.info(" *** IOException *** ",e);
   }
      
}

From source file:com.uletian.ultcrm.business.service.CustomerInfoMessageService.java

public void handlerCustomerInfo(String message) throws SAXException, DocumentException, IOException {
    StringWriter writer = new StringWriter();
    OutputFormat format = OutputFormat.createPrettyPrint();
    Document doc;/*  w ww  . jav  a 2 s .c  o m*/
    doc = DocumentHelper.parseText(message);
    XMLWriter xmlwriter = new XMLWriter(writer, format);
    xmlwriter.write(doc);
    logger.info("??\n" + writer.toString());

    Element element = doc.getRootElement();
    String action = element.elementText("action");
    String sourceSys = element.elementText("sourceSys");
    String ultcrmid = element.elementText("ultcrmid");
    String crmid = element.elementText("crmid");
    String name = element.elementText("name");
    String sexy = element.elementText("sexy");
    String telephone = element.elementText("telephone");
    String country = element.elementText("country");
    String province = element.elementText("province");
    String city = element.elementText("city");
    String address = element.elementText("address");
    String postcode = element.elementText("postcode");

    if (!"UPDATE".equals(action)) {
        logger.debug("?\ncrmid:" + crmid + "\nultcrmid:" + ultcrmid);
        return;
    }
    Customer customer = customerRepository.findOne(Long.decode(ultcrmid));
    if (customer == null) {
        logger.warn("?" + ultcrmid);
        return;
    }
    customer.setPhone(telephone);
    customer.setSyncid(crmid);
    customer.setName(name);
    customer.setSex(sexy);
    customer.setCountry(country);
    customer.setProvince(province);
    customer.setCity(city);
    customer.setAddress(address);
    customer.setPostcode(postcode);
    customer.setCrmCustomerId(crmid);
    customerRepository.save(customer);

    Element elements = element.element("techs");
    Iterator<Element> iterator = elements.elementIterator("tech");
    ArrayList<Tech> techs = new ArrayList<Tech>(0);

    while (iterator.hasNext()) {
        Element techElement = iterator.next();
        String code = techElement.elementText("code");
        String techlevelno = techElement.elementText("techlevelno");
        String techerno = techElement.elementText("techerno");
        String techname = techElement.elementText("techname");
        String coursetime = techElement.elementText("coursetime");
        String trainExpireDate = techElement.elementText("trainExpireDate");
        String trainCompany = techElement.elementText("trainCompany");
        String crmTechId = techElement.elementText("crmtechid");
        String courseCode = techElement.elementText("courseCode");
        String techColor = techElement.elementText("techColor");
        String registerDate = techElement.elementText("registerDate");
        String courseLicense = techElement.elementText("courseLicense");
        String checkExpireDate = techElement.elementText("checkExpireDate");
        String memberLevel = techElement.elementText("memberLevel");

        String nextMaintCoursetime = techElement.elementText("nextMaintCoursetime");
        String nextMaintDate = techElement.elementText("nextMaintDate");
        String lastConsumeDate = techElement.elementText("lastConsumeDate");

        Tech tech = techRepository.findTechByTechlevelno(techlevelno);
        if (tech == null) {
            tech = new Tech();
        }
        TechModel techModel = null;
        try {
            techModel = techModelRepository.findModelByCode(code);
        } catch (Exception e) {
        }
        if (techModel == null) {
            logger.warn("??\n" + code);
            techModel = techModelRepository.findModelByCode("OTHERS");
        }
        tech.setCrmTechId(crmTechId);
        tech.setTechlevelno(techlevelno);
        tech.setTechModel(techModel);
        tech.setTechSery(techModel.getTechSery());
        tech.setTechCourse(techModel.getTechSery().getTechCourse());
        tech.setCustomer(customer);
        tech.setCode(code);
        tech.setTechlevelno(techlevelno);
        tech.setTecherno(techerno);
        tech.setTechname(techname);
        tech.setCoursetime(coursetime);

        try {
            tech.setTrainExpireDate(sdf.parse(trainExpireDate));
        } catch (ParseException e) {
            logger.warn("???\n" + trainExpireDate, e);
        }
        tech.setTrainCompany(trainCompany);
        tech.setCourseCode(courseCode);
        tech.setMemberLevel(memberLevel);
        tech.setColor(techColor);
        try {
            tech.setRegisterDate(sdf.parse(registerDate));
        } catch (ParseException e) {
            logger.warn("??\n" + registerDate, e);
        }
        tech.setCourseLicense(courseLicense);
        try {
            tech.setCheckExpireDate(sdf.parse(checkExpireDate));
        } catch (ParseException e) {
            logger.warn("??\n" + checkExpireDate, e);
        }

        // ?by xiecheng 2015-11-19
        tech.setNextMaintCoursetime(nextMaintCoursetime);
        try {
            tech.setNextMaintDate(sdf.parse(nextMaintDate));
        } catch (ParseException e) {
            logger.warn("??\n" + nextMaintDate, e);
        }

        try {
            tech.setLastConsumeDate(sdf.parse(lastConsumeDate));
        } catch (ParseException e) {
            logger.warn("??\n" + lastConsumeDate, e);
        }

        techs.add(tech);
    }
    techRepository.save(techs);
}