Skip to content

Instantly share code, notes, and snippets.

@naoto-ogawa
Last active November 25, 2018 07:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naoto-ogawa/ccb115dd1777e00c24d88d8b180d7d9e to your computer and use it in GitHub Desktop.
Save naoto-ogawa/ccb115dd1777e00c24d88d8b180d7d9e to your computer and use it in GitHub Desktop.
joox example
package com.example.xml;
import org.joox.Match;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Element;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.joox.JOOX.*;
public class Main {
public static void main(String[] args) throws Exception {
String path = "./data/";
Files.walk(Paths.get(path))
.filter(Files::isRegularFile)
.map(x -> x.toFile())
.forEach(Main::analyze);
}
private static void analyze(File f) {
log(f);
Document document = getDocument(f);
log($(document));
log("-------------------------");
log($(document).find("select"));
log("-------------------------");
log($(document).find("resultMap"));
log("-------------------------");
log($(document).find("#insert"));
log("-------------------------");
log($(document).find("#insert").contents());
log("-------------------------");
log($(document).find("#insert").attr("id"));
log("-------------------------");
log($(document).find("[parameterType='java.util.List']").attr("parameterType"));
log("-------------------------");
log($(document).find("[parameterType$='Short']").attr("parameterType"));
log("-------------------------");
log($(document).find("[resultMap^='Dept']").attr("resultMap"));
log("-------------------------");
log($(document).find("if").attr("test"));
log("-------------------------");
log($(document).find("if[test$='null']"));
// log("-------------------------");
// log($(document).find("if[test*=\"!= '' \"]")); できないっぽい
log("-------------------------");
log($(document).find("if").attrs("test"));
log("-------------------------");
Match foo01 = $(document).find("if").parent("sql");
addBindTag(foo01);
Iterator<Element> iter = foo01.children("if").iterator();
while(iter.hasNext()) {
changeIfTag(iter.next());
}
log(foo01);
// 宣言やDOCTYPEが出力されない
// try {
// $(document).write(System.out);
// } catch (IOException e) {
// throw new RuntimeException(e);
// }
File f2 = f.getAbsoluteFile();
f.renameTo(new File(f2.getAbsolutePath() + ".tmp"));
Document document1 = foo01.document();
// PrintStream out = System.out;
// writeXML(document1, out);
try {
writeXML(document1, new PrintStream(f2));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private static Match addBindTag(Match foo01) {
Match m1 = makeBindTag("x_n_1","x_v_1");
Match m2 = makeBindTag("x_n_2","x_v_2");
Match m3 = makeBindTag("x_n_3","x_v_3");
return foo01.prepend(m3).prepend(m2).prepend(m1);
}
private static Match changeIfTag(Element e) {
Match ret = $(e);
String attr = ret.attr("test");
if (contains("[!][=] *''", attr)) {
changeOneItem(ret);
}
if (contains("size[(][)] *[>] *0", attr)) {
changeListItem(ret);
}
return ret;
}
private static boolean contains(String regrex, String str) {
return Pattern.compile(regrex).matcher(str).find();
}
private static void changeListItem(Match ret) {
// TODO
}
private static void changeOneItem(Match ret) {
String texttext = ret.text();
String ref = getRef(texttext);
String var = getVar(texttext);
ret.attr("test", "#exists " + var);
ret.text(texttext.replace("#{" + ref + "}", "#{" + var + "}"));
Match m3 = $("bind").attr("name",var).attr("value", ref);
ret.before(m3);
}
private static String getRef(String str) {
Matcher m = VAR.matcher(str);
m.find();
return m.group(1);
}
private static Pattern VAR = Pattern.compile("#[{](.*)[}]");
private static Pattern TBL_COL = Pattern.compile("[.]([a-zA-Z0-9_]*) *= ");
private static Pattern COL = Pattern.compile(" *([a-zA-Z0-9_]*) *=");
private static String getVar(String str) {
Matcher m = TBL_COL.matcher(str);
if(m.find()) {
return m.group(1);
}
Matcher m1 = COL.matcher(str);
m1.find();
return m1.group(1);
}
private static Match makeBindTag(String name, String value) {
return $("bind").attr("name", name).attr("value",value);
}
private static void writeXML(Document document1, PrintStream out) {
document1.setXmlStandalone(true);
try {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMImplementation domImpl = document1.getImplementation();
DocumentType doctype = domImpl.createDocumentType("doctype",
"-//mybatis.org//DTD Mapper 3.0//EN",
"http://mybatis.org/dtd/mybatis-3-mapper.dtd");
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, doctype.getPublicId());
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, doctype.getSystemId());
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
DOMSource source = new DOMSource(document1);
StreamResult console = new StreamResult(out);
transformer.transform(source, console);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static Document getDocument(File f) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(f);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static void log(Object obj) {
System.out.println(obj);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment