import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@SuppressWarnings("unchecked")
public class Dom2XmlUtils {
private static final int MAX_LEN = 300;
public Document read(String fileName) throws DocumentException {
SAXReader reader = new SAXReader();
Document document = reader.read(new File(fileName));
return document;
}
private String getBeginElementStr(Element element){
List<Attribute> attributeList = element.attributes();
StringBuffer result = new StringBuffer().append("<").append(element.getName());
for(Attribute attribute : attributeList){
result.append(" ").append(attribute.getName()).append("=\"").append(attribute.getValue()).append("\"");
}
return result.toString();
}
private String getEndElementStr(Element element){
return "</"+element.getName()+">";
}
public void format(Iterator<Node> nodeIterator, List<String> resultList, String blank, boolean maxLen){
while(nodeIterator.hasNext()){
Node node = nodeIterator.next();
if(node.getNodeType() == Node.ELEMENT_NODE){
Element element = (Element)node;
String begin = this.getBeginElementStr(element);
Iterator<Node> subNodeIterator = element.nodeIterator();
if(!subNodeIterator.hasNext()){
begin += " />";
resultList.add(blank + begin);
continue;
}else {
begin += " >";
resultList.add(blank + begin);
}
format(subNodeIterator, resultList, blank + " ", maxLen);
String end = this.getEndElementStr(element);
resultList.add(blank + end);
}else if(node.getNodeType() == Node.TEXT_NODE){
String text = node.getText().trim();
if(StringUtils.isBlank(text)){
continue;
}
text = text.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">");//.replaceAll("\"", """).replaceAll("'", "'");
String[] textArr = text.split("\n");
for(String item: textArr){
item = item.trim();
String lastLine = resultList.get(resultList.size() - 1);
if(!maxLen && !lastLine.endsWith(">")){
lastLine += item.trim();
resultList.set(resultList.size() - 1, lastLine);
continue;
}
if(maxLen){
String curLine = item.trim();
if(curLine.length() <= MAX_LEN){
resultList.add(blank + curLine);
}else {
split(curLine, resultList, blank);
}
continue;
}
resultList.add(blank + item.trim());
}
}
}
}
private static void split(String curLine, List<String> resultList, String blank){
int index = curLine.indexOf(",", MAX_LEN);
while (index != -1){
String first = curLine.substring(0, index + 1);
curLine = curLine.substring(index + 1);
index = curLine.indexOf(",", MAX_LEN);
resultList.add(blank + first);
}
resultList.add(blank + curLine);
}
public static void format(String fileName, boolean maxLen) throws DocumentException, IOException{
Dom2XmlUtils utils = new Dom2XmlUtils();
Document document = utils.read(fileName);
Iterator<Node> nodeIterator = document.nodeIterator();
List<String> resultList = new ArrayList<String>();
utils.format(nodeIterator, resultList, "", maxLen);
String first = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
String second = "<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">";
resultList.add(0, first);
resultList.add(1, second);
FileUtils.writeLines(new File(fileName), "UTF-8", resultList);
}
public static void format(String fileName) throws DocumentException, IOException{
format(fileName, false);
format(fileName, true);
}
}