Skip to content

Instantly share code, notes, and snippets.

@naoto-ogawa
Last active June 14, 2021 03:12
Show Gist options
  • Save naoto-ogawa/54800e3375b060278583826acfe53704 to your computer and use it in GitHub Desktop.
Save naoto-ogawa/54800e3375b060278583826acfe53704 to your computer and use it in GitHub Desktop.
JOOX wrap sample
package com.example.xml;
import org.joox.Match;
import org.w3c.dom.*;
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.File;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import static org.joox.JOOX.$;
/*
https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api
https://mvnrepository.com/artifact/org.jooq/joox
*/
public class Main4 {
private Document document;
private Map<String, Integer> bindNames;
public static void main(String[] args) throws Exception {
String path = "./data/Test04.xml";
Files.walk(Paths.get(path))
.filter(Files::isRegularFile)
.map(Path::toFile)
.filter(x -> x.getName().endsWith("xml"))
.forEach(new Main4()::analyze);
}
public final Element wrap(Match base, Element newParent) {
final int size = base.size();
for (int matchIndex = 0; matchIndex < size; matchIndex++) {
Element match = base.get(matchIndex);
Node parent = match.getParentNode();
Document doc = match.getOwnerDocument();
Node wrapper = doc.importNode(newParent, true);
parent.replaceChild(wrapper, match);
wrapper.appendChild(match);
}
return newParent;
}
private void analyze(File file) {
log(file);
document = getDocument(file);
Match tags = $(document).find("body");
tags.wrap("aaaa"); // OK
//tags.wrap("<aaaa/>"); // NG
//tags.wrap("<aaaa id='abc'/>"); // NG
writeXML(System.out);
}
private void analyze1(File file) {
log(file);
document = getDocument(file);
Match tags = $(document).find("body");
Match m = $("xxxx");
m.attr("id", "abc");
wrap(tags, m.get(0));
writeXML(System.out);
}
private void writeXML(PrintStream out) {
document.setXmlStandalone(true);
try {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMImplementation domImpl = document.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(document);
StreamResult console = new StreamResult(out);
transformer.transform(source, console);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private 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 void log(Object obj) {
System.out.println(obj);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE doc PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<doc>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<xxxx id="abc">
<body>Don't forget me this weekend!</body>
</xxxx>
</note>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<xxxx id="abc">
<body>remind me this weekend!</body>
</xxxx>
</note>
</doc>
<doc>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>remind me this weekend!</body>
</note>
</doc>
<!-- from https://www.w3schools.com/xml/note.xml -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment