diff --git a/pom.xml b/pom.xml index aed8c33..3ccae06 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ io.github.emays parent - 0.0.6 + 0.0.7 @@ -88,7 +88,6 @@ true - diff --git a/src/main/java/com/mays/util/Util.java b/src/main/java/com/mays/util/Util.java index 3955441..11aad84 100644 --- a/src/main/java/com/mays/util/Util.java +++ b/src/main/java/com/mays/util/Util.java @@ -16,11 +16,14 @@ public class Util { + private Util() { + } + public static PrintWriter newPrintWriter(Path path) throws IOException { return new PrintWriter(Files.newBufferedWriter(path)); } - public static void trFile(String fr, String to, String in_file, String out_file) throws Exception { + public static void trFile(String fr, String to, String in_file, String out_file) throws IOException { Stream lines = Files.lines(Paths.get(in_file)).map(line -> line.replace(fr, to)); Files.write(Paths.get(out_file), iterable(lines)); } @@ -47,10 +50,6 @@ public static String tabDelimitedString(List fields) { } public static String delimitedString(String delim, List fields) { -// if (fields.size() == 1) -// return "" + fields.get(0); -// return (String) fields.subList(1, fields.size()).stream().reduce(fields.get(0).toString(), -// (partial, field) -> partial + delim + field.toString()); return String.join(delim, fields); } @@ -62,7 +61,7 @@ public static String delimitedString(String delim, Object... fields) { ArrayList list = new ArrayList<>(); Arrays.stream(fields).forEach(field -> { if (field == null) - throw new RuntimeException(list.toString()); + throw new IllegalArgumentException(list.toString()); list.add(field.toString()); }); return delimitedString(delim, list); diff --git a/src/main/java/com/mays/util/html/ElementW.java b/src/main/java/com/mays/util/html/ElementW.java index 3c4b60e..02a0ab2 100644 --- a/src/main/java/com/mays/util/html/ElementW.java +++ b/src/main/java/com/mays/util/html/ElementW.java @@ -16,7 +16,7 @@ public class ElementW { /** * Creates a new {@code ElementW} wrapping the given element - * + * * @param element is the {@code Element} to wrap * @return the new {@code ElementW} */ @@ -26,17 +26,16 @@ public static ElementW of(Element element) { /** * Constructs a new {@code ElementW} wrapping the given element - * + * * @param element is the {@code Element} to wrap */ public ElementW(Element element) { - super(); this.element = element; } /** * Access the wrapped {@code Element} - * + * * @return the wrapped {@code Element} */ public Element getElement() { @@ -68,11 +67,11 @@ public ElementW addElement(String name) { * Adds a new {@code ElementW} node with a name of the given HtmlTag tag to this * element and returns the new node. Adds an attribute to the new node for each * name and value in attributes. - * + * *

*

addElement(HtmlTag.DIV, "id", "div1", "style", "font: 10pt * sans-serif;")
- * + * * @param tag is the HtmlTag name for the {@code ElementW} node * @param attributes is a sequence of attribute name and value * @return the newly added {@code ElementW} node @@ -87,7 +86,7 @@ public ElementW addElement(HtmlTag tag, String... attributes) { * Adds a new {@code ElementW} node with the given name to this element and * returns the new node. Adds an attribute to the new node for each name and * value in attributes. - * + * *

*

addElement("div", "id", "div1", "style", "font: 10pt * sans-serif;")
@@ -140,7 +139,7 @@ public ElementW addAttribute(String name, String value) { /** * Appends the value to the class attribute of this {@code ElementW} creating a * new attribute if one doesn't already exist - * + * * @param value is the value of the class attribute to append or set * @return this {@code ElementW} */ @@ -257,16 +256,6 @@ public ElementW elementById(String idValue) { return null; } - @Deprecated - public ElementW addDiv(String div_class) { - return addElement(HtmlTag.DIV, "class", div_class); - } - - @Deprecated - public ElementW addDiv(String div_class, String text) { - return addDiv(div_class).addText(text); - } - /** * Creates a deep copy of this element. The new element is detached from its * parent, and getParent() on the clone will return null. diff --git a/src/main/java/com/mays/util/html/HtmlPage.java b/src/main/java/com/mays/util/html/HtmlPage.java index 8b61d42..c28117e 100644 --- a/src/main/java/com/mays/util/html/HtmlPage.java +++ b/src/main/java/com/mays/util/html/HtmlPage.java @@ -1,10 +1,12 @@ package com.mays.util.html; +import java.io.IOException; import java.io.InputStream; import java.nio.file.Path; import java.nio.file.Paths; import org.dom4j.Document; +import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.SAXReader; @@ -29,13 +31,13 @@ public HtmlPage(String name, String stylesheet, String script) { element = addBody(stylesheet, script); } - public HtmlPage(Path path) throws Exception { + public HtmlPage(Path path) throws DocumentException { SAXReader reader = new SAXReader(); document = reader.read(path.toString()); element = new ElementW(document.getRootElement()); } - public HtmlPage(InputStream stream) throws Exception { + public HtmlPage(InputStream stream) throws DocumentException { SAXReader reader = new SAXReader(); document = reader.read(stream); element = new ElementW(document.getRootElement()); @@ -86,7 +88,7 @@ private ElementW addBody(String stylesheet, String script) { return new ElementW(html.addElement("body")); } - public void write(String outdir) throws Exception { + public void write(String outdir) throws IOException { HtmlUtil.write(document, Paths.get(outdir, name + ".html")); } diff --git a/src/main/java/com/mays/util/html/HtmlUtil.java b/src/main/java/com/mays/util/html/HtmlUtil.java index a1ece4a..adcf756 100644 --- a/src/main/java/com/mays/util/html/HtmlUtil.java +++ b/src/main/java/com/mays/util/html/HtmlUtil.java @@ -11,6 +11,9 @@ public class HtmlUtil { + private HtmlUtil() { + } + public static void write(Document document, Path file) throws IOException { OutputFormat format = OutputFormat.createPrettyPrint(); format.setTrimText(false); @@ -20,7 +23,7 @@ public static void write(Document document, Path file) throws IOException { writer.write(document); writer.close(); } - + public static String write(Document document) throws IOException { OutputFormat format = OutputFormat.createPrettyPrint(); format.setTrimText(false); diff --git a/src/main/java/com/mays/util/xml/XmlResourceResolver.java b/src/main/java/com/mays/util/xml/XmlResourceResolver.java index f1d0085..1c74a56 100644 --- a/src/main/java/com/mays/util/xml/XmlResourceResolver.java +++ b/src/main/java/com/mays/util/xml/XmlResourceResolver.java @@ -3,6 +3,7 @@ import java.io.InputStream; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.MissingResourceException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -22,10 +23,10 @@ public XmlResourceResolver(String basePath) { @Override public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) { Path path = Paths.get(basePath, systemId); - logger.info("Resolve resource: " + path.toString()); + logger.info("Resolve resource: " + path); InputStream resource = this.getClass().getClassLoader().getResourceAsStream(path.toString()); if (resource == null) - throw new RuntimeException("Could not find the specified xsd file: " + basePath + " " + systemId); + throw new MissingResourceException("Could not find the specified xsd file", basePath, systemId); return new XmlLSInputImpl(publicId, systemId, baseURI, resource, "UTF-8"); } diff --git a/src/main/java/com/mays/util/xml/XmlUtil.java b/src/main/java/com/mays/util/xml/XmlUtil.java index 81a70fe..f92c3d7 100644 --- a/src/main/java/com/mays/util/xml/XmlUtil.java +++ b/src/main/java/com/mays/util/xml/XmlUtil.java @@ -24,11 +24,13 @@ import org.slf4j.LoggerFactory; import org.xml.sax.SAXException; -@SuppressWarnings("unused") public class XmlUtil { private static final Logger logger = LoggerFactory.getLogger(XmlUtil.class); + private XmlUtil() { + } + public static void write(Document document, Path file) throws IOException { OutputFormat format = OutputFormat.createPrettyPrint(); format.setTrimText(false); @@ -37,31 +39,29 @@ public static void write(Document document, Path file) throws IOException { writer.close(); } - // TODO // https://stackoverflow.com/questions/55571046/eclipse-is-confused-by-imports-accessible-from-more-than-one-module -// public static void validate(String file, String xsd, String schema_dir) -// throws ParserConfigurationException, SAXException, IOException { -// XmlUtil.validate(new FileInputStream(file), xsd, schema_dir); -// } -// -// public static void validate(InputStream in, String xsd, String schema_dir) -// throws ParserConfigurationException, SAXException, IOException { -// // See javax.xml.validation -// DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); -// dbf.setNamespaceAware(true); -// DocumentBuilder parser = dbf.newDocumentBuilder(); -// org.w3c.dom.Document document = parser.parse(in); -// SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); -// factory.setResourceResolver(new XmlResourceResolver(schema_dir)); -// ArrayList sources = new ArrayList<>(); -// // for (String xsd : xsds) { -// logger.info("Schema: " + xsd); -// Source schema_file = new StreamSource(XmlUtil.class.getClassLoader().getResourceAsStream(xsd)); -// sources.add(schema_file); -// // } -// javax.xml.validation.Schema schema = factory.newSchema(sources.toArray(new Source[sources.size()])); -// Validator validator = schema.newValidator(); -// validator.validate(new DOMSource(document)); -// } + + public static void validate(String file, String xsd, String schema_dir) + throws ParserConfigurationException, SAXException, IOException { + XmlUtil.validate(new FileInputStream(file), xsd, schema_dir); + } + + public static void validate(InputStream in, String xsd, String schema_dir) + throws ParserConfigurationException, SAXException, IOException { + // See javax.xml.validation + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + dbf.setNamespaceAware(true); + DocumentBuilder parser = dbf.newDocumentBuilder(); + org.w3c.dom.Document document = parser.parse(in); + SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); + factory.setResourceResolver(new XmlResourceResolver(schema_dir)); + ArrayList sources = new ArrayList<>(); + logger.info("Schema: " + xsd); + Source schema_file = new StreamSource(XmlUtil.class.getClassLoader().getResourceAsStream(xsd)); + sources.add(schema_file); + javax.xml.validation.Schema schema = factory.newSchema(sources.toArray(new Source[sources.size()])); + Validator validator = schema.newValidator(); + validator.validate(new DOMSource(document)); + } }