Dom4j 批量修改xsl resource引用路径
程序员文章站
2022-05-29 23:21:34
...
项目用到了xsl,但是这个项目已经是几百个人经历了10年的垃圾般的工程,这些个xsl里的相互引用盘根错杂,想使用eclipse的xml编辑器快速定位一些变量和模板,经过了两天的摸索发现我错了.
Eclipse的xml编辑器使用的相对目录定位resource,结果项目的xsl全部使用的无根目录(通过项目内部的自定义xml扩展定位容器的目录再结合resource路径定位).
我承认这帮人xml处理很牛,但是他们不提供源码.所以通过代码把里面的无根路径改成了相对目录
工具类
package com.tr.longlh; import java.net.URL; import java.util.Iterator; import java.util.List; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.Node; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; public class Util { public Document parse(URL url) throws DocumentException { SAXReader reader = new SAXReader(); Document document = reader.read(url); return document; } public Document parse(String path) throws DocumentException { SAXReader reader = new SAXReader(); Document document = reader.read(path); return document; } public void findLinks(Document document) throws DocumentException { List<Node> list = document.selectNodes("//a/@href"); for (Iterator<Node> iter = list.iterator(); iter.hasNext();) { Attribute attribute = (Attribute) iter.next(); String url = attribute.getValue(); } } public void treeWalk(Document document) { treeWalk(document.getRootElement()); } public void treeWalk(Element element) { for (int i = 0, size = element.nodeCount(); i < size; i++) { Node node = element.node(i); if (node instanceof Element) { treeWalk((Element) node); } else { // do something… } } } public void barbar(Document document) { List<Node> list = document.selectNodes("//foo/bar"); Node node = document.selectSingleNode("//foo/bar/author"); String name = node.valueOf("@name"); } public void bar(Document document) throws DocumentException { Element root = document.getRootElement(); // iterate through child elements of root for (Iterator<Element> it = root.elementIterator(); it.hasNext();) { Element element = it.next(); // do something } // iterate through child elements of root with element name "foo" for (Iterator<Element> it = root.elementIterator("foo"); it.hasNext();) { Element foo = it.next(); // do something } // iterate through attributes of root for (Iterator<Attribute> it = root.attributeIterator(); it.hasNext();) { Attribute attribute = it.next(); // do something } } public Document createDocument() { Document document = DocumentHelper.createDocument(); Element root = document.addElement("root"); Element author1 = root.addElement("author").addAttribute("name", "James").addAttribute("location", "UK") .addText("James Strachan"); Element author2 = root.addElement("author").addAttribute("name", "Bob").addAttribute("location", "US") .addText("Bob McWhirter"); return document; } // { // Document document = null; // FileWriter out; // try { // out = new FileWriter("foo.xml"); // document.write(out); // } catch (IOException e) { // e.printStackTrace(); // } // } public void prettyPrint(Document document) { // lets write to a file try { // FileWriter fileWriter = new FileWriter("output.xml"); // XMLWriter writer = new XMLWriter(fileWriter); // writer.write(document); // writer.close(); // Pretty print the document to System.out OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter(System.out, format); writer.write(document); // Compact format to System.out format = OutputFormat.createCompactFormat(); writer = new XMLWriter(System.out, format); writer.write(document); } catch (Exception e) { e.printStackTrace(); } } }
XML修改处理类
package com.tr.longlh; import java.io.File; import java.io.FileWriter; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; public class XMLUtil extends Util { public Document parse(File f) throws DocumentException { SAXReader reader = new SAXReader(); reader.setValidation(false); reader.setIncludeInternalDTDDeclarations(true); reader.setIncludeExternalDTDDeclarations(true); Document document = reader.read(f); return document; } public void prettyWrite(String dir, String name, Document document) { try { File file = new File(dir, name); file.getParentFile().mkdirs(); if (file.getParentFile().exists()) { OutputFormat format = OutputFormat.createPrettyPrint(); FileWriter fileWriter = new FileWriter(file); XMLWriter writer = new XMLWriter(fileWriter, format); writer.write(document); writer.close(); } } catch (Exception e) { e.printStackTrace(); } } }
文件遍历
package com.tr.longlh; import java.io.File; import java.net.MalformedURLException; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.function.Supplier; import java.util.stream.Stream; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Node; public class XSLExtrenalResChangeMain extends XMLUtil { private static final String OUTPUT_DIR_REDNDING = "IBE"; private static final String INPUT_DIR_REDNDING = "E:\\work\\projects\\IBE\\xRez\\ibe"; public void dirWalkIBERendering() { File dir = new File(INPUT_DIR_REDNDING); dirWalk(dir, "rendering"); } public void dirWalk(File f, String folder) { if (f.isFile()) return; File[] listFiles = f.listFiles(); for (File file : listFiles) { if (file.isDirectory() && file.getName().equals(folder)) { // do xmlFilesWalk(file); } else { dirWalk(file, folder); } } } public void xmlFilesWalk(File f) { if (f.isFile()) return; File[] listFiles = f.listFiles(); for (File file : listFiles) { if (file.isDirectory()) { xmlFilesWalk(file); } if (file.getName().endsWith(".xsl")) { try { String pathTail = file.getPath().replace(INPUT_DIR_REDNDING, ""); String[] splitBySplash = pathTail.split("\\\\"); Document relativeDiretoryAddingDoc = doRelativeDiretoryAdding(file, splitBySplash.length-2); prettyPrint(relativeDiretoryAddingDoc); prettyWrite(OUTPUT_DIR_REDNDING, pathTail, relativeDiretoryAddingDoc); } catch (DocumentException e) { e.printStackTrace(); } } } } public Document doRelativeDiretoryAdding(File f, int splashCount) throws DocumentException { Stream<String> stream = Stream.generate(new Supplier<String>() { @Override public String get() { return "../"; } }).limit(splashCount); String directAdding = String.join("", stream.toArray(String[]::new)); Map<String, String> nsmap = new HashMap<String, String>(); nsmap.put("xsldoc", "http://www.bacman.net/XSLdoc"); nsmap.put("itemList", "http://xml.apache.org/xslt/java/com.openjawx.xRez.jsp.ItemListConfigBean"); nsmap.put("config", "http://xml.apache.org/xslt/java/com.openjawx.xRez.rendering.RenderingConfigBean"); nsmap.put("datalist", "http://xml.apache.org/xslt/java/com.openjawx.xRez.jsp.DataListConfigBean"); nsmap.put("ojd", "http://www.openjawtech.com/datalist"); nsmap.put("i18n", "http://xml.apache.org/xslt/java/com.openjawx.xRez.jsp.xRezLanguageConfigBean"); Document document = parse(f); // System.out.println(document.getRootElement().getNamespacePrefix()); // System.out.println(document.getRootElement().getNamespaceURI()); nsmap.put(document.getRootElement().getNamespacePrefix(), document.getRootElement().getNamespaceURI()); org.dom4j.XPath xImport = document.createXPath("//xsl:import/@href"); org.dom4j.XPath xInclude = document.createXPath("//xsl:include/@href"); xImport.setNamespaceURIs(nsmap); xInclude.setNamespaceURIs(nsmap); List<Node> xImports = xImport.selectNodes(document); for (Node node : xImports) { System.out.println(node.getStringValue()); node.setText(directAdding + node.getStringValue()); } List<Node> xIncludes = xInclude.selectNodes(document); for (Node node : xIncludes) { System.out.println(node.getStringValue()); node.setText(directAdding + node.getStringValue()); } return document; } public static void main(String[] args) throws MalformedURLException, DocumentException { XSLExtrenalResChangeMain extrenalResChangeMain = new XSLExtrenalResChangeMain(); extrenalResChangeMain.dirWalkIBERendering(); } }