Dom4j batch modify xsl resource reference path

       The project uses xsl, but this project has been a rubbish project that hundreds of people have experienced for 10 years. The mutual references in these xsl are complicated. I want to use the xml editor of eclipse to quickly locate some variables and templates. After two days of groping, I found that I was wrong.

       The relative directory used by Eclipse's xml editor locates the resource, and the resulting xsl of the project uses a rootless directory (the directory of the container is located through the custom xml extension inside the project and then combined with the resource path to locate).

      I admit that these people are very good at xml processing, but they do not provide source code. So through the code, the rootless path inside is changed to a relative directory

Tools

 

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 modification processing class

 

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 ();
		}
	}
}

 

file traversal

 

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();
	}
}

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326939434&siteId=291194637