Skip to content

Commit 297b0be

Browse files
authored
Merge pull request #8 from emays/feature/20251119-develop
Feature/20251119 develop
2 parents 37a8f9e + 5333c68 commit 297b0be

File tree

7 files changed

+51
-58
lines changed

7 files changed

+51
-58
lines changed

pom.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>io.github.emays</groupId>
77
<artifactId>parent</artifactId>
8-
<version>0.0.6</version>
8+
<version>0.0.7</version>
99
<relativePath />
1010
</parent>
1111

@@ -88,7 +88,6 @@
8888
</releases>
8989
<snapshots>
9090
<enabled>true</enabled>
91-
<!--<updatePolicy>always</updatePolicy>-->
9291
</snapshots>
9392
</repository>
9493
</repositories>

src/main/java/com/mays/util/Util.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616

1717
public class Util {
1818

19+
private Util() {
20+
}
21+
1922
public static PrintWriter newPrintWriter(Path path) throws IOException {
2023
return new PrintWriter(Files.newBufferedWriter(path));
2124
}
2225

23-
public static void trFile(String fr, String to, String in_file, String out_file) throws Exception {
26+
public static void trFile(String fr, String to, String in_file, String out_file) throws IOException {
2427
Stream<String> lines = Files.lines(Paths.get(in_file)).map(line -> line.replace(fr, to));
2528
Files.write(Paths.get(out_file), iterable(lines));
2629
}
@@ -47,10 +50,6 @@ public static String tabDelimitedString(List<String> fields) {
4750
}
4851

4952
public static String delimitedString(String delim, List<String> fields) {
50-
// if (fields.size() == 1)
51-
// return "" + fields.get(0);
52-
// return (String) fields.subList(1, fields.size()).stream().reduce(fields.get(0).toString(),
53-
// (partial, field) -> partial + delim + field.toString());
5453
return String.join(delim, fields);
5554
}
5655

@@ -62,7 +61,7 @@ public static String delimitedString(String delim, Object... fields) {
6261
ArrayList<String> list = new ArrayList<>();
6362
Arrays.stream(fields).forEach(field -> {
6463
if (field == null)
65-
throw new RuntimeException(list.toString());
64+
throw new IllegalArgumentException(list.toString());
6665
list.add(field.toString());
6766
});
6867
return delimitedString(delim, list);

src/main/java/com/mays/util/html/ElementW.java

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class ElementW {
1616

1717
/**
1818
* Creates a new {@code ElementW} wrapping the given element
19-
*
19+
*
2020
* @param element is the {@code Element} to wrap
2121
* @return the new {@code ElementW}
2222
*/
@@ -26,17 +26,16 @@ public static ElementW of(Element element) {
2626

2727
/**
2828
* Constructs a new {@code ElementW} wrapping the given element
29-
*
29+
*
3030
* @param element is the {@code Element} to wrap
3131
*/
3232
public ElementW(Element element) {
33-
super();
3433
this.element = element;
3534
}
3635

3736
/**
3837
* Access the wrapped {@code Element}
39-
*
38+
*
4039
* @return the wrapped {@code Element}
4140
*/
4241
public Element getElement() {
@@ -68,11 +67,11 @@ public ElementW addElement(String name) {
6867
* Adds a new {@code ElementW} node with a name of the given HtmlTag tag to this
6968
* element and returns the new node. Adds an attribute to the new node for each
7069
* name and value in attributes.
71-
*
70+
*
7271
* <p>
7372
* <blockquote> addElement(HtmlTag.DIV, "id", "div1", "style", "font: 10pt
7473
* sans-serif;") </blockquote>
75-
*
74+
*
7675
* @param tag is the HtmlTag name for the {@code ElementW} node
7776
* @param attributes is a sequence of attribute name and value
7877
* @return the newly added {@code ElementW} node
@@ -87,7 +86,7 @@ public ElementW addElement(HtmlTag tag, String... attributes) {
8786
* Adds a new {@code ElementW} node with the given name to this element and
8887
* returns the new node. Adds an attribute to the new node for each name and
8988
* value in attributes.
90-
*
89+
*
9190
* <p>
9291
* <blockquote> addElement("div", "id", "div1", "style", "font: 10pt
9392
* sans-serif;") </blockquote>
@@ -140,7 +139,7 @@ public ElementW addAttribute(String name, String value) {
140139
/**
141140
* Appends the value to the class attribute of this {@code ElementW} creating a
142141
* new attribute if one doesn't already exist
143-
*
142+
*
144143
* @param value is the value of the class attribute to append or set
145144
* @return this {@code ElementW}
146145
*/
@@ -257,16 +256,6 @@ public ElementW elementById(String idValue) {
257256
return null;
258257
}
259258

260-
@Deprecated
261-
public ElementW addDiv(String div_class) {
262-
return addElement(HtmlTag.DIV, "class", div_class);
263-
}
264-
265-
@Deprecated
266-
public ElementW addDiv(String div_class, String text) {
267-
return addDiv(div_class).addText(text);
268-
}
269-
270259
/**
271260
* Creates a deep copy of this element. The new element is detached from its
272261
* parent, and getParent() on the clone will return null.

src/main/java/com/mays/util/html/HtmlPage.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.mays.util.html;
22

3+
import java.io.IOException;
34
import java.io.InputStream;
45
import java.nio.file.Path;
56
import java.nio.file.Paths;
67

78
import org.dom4j.Document;
9+
import org.dom4j.DocumentException;
810
import org.dom4j.DocumentHelper;
911
import org.dom4j.Element;
1012
import org.dom4j.io.SAXReader;
@@ -29,13 +31,13 @@ public HtmlPage(String name, String stylesheet, String script) {
2931
element = addBody(stylesheet, script);
3032
}
3133

32-
public HtmlPage(Path path) throws Exception {
34+
public HtmlPage(Path path) throws DocumentException {
3335
SAXReader reader = new SAXReader();
3436
document = reader.read(path.toString());
3537
element = new ElementW(document.getRootElement());
3638
}
3739

38-
public HtmlPage(InputStream stream) throws Exception {
40+
public HtmlPage(InputStream stream) throws DocumentException {
3941
SAXReader reader = new SAXReader();
4042
document = reader.read(stream);
4143
element = new ElementW(document.getRootElement());
@@ -86,7 +88,7 @@ private ElementW addBody(String stylesheet, String script) {
8688
return new ElementW(html.addElement("body"));
8789
}
8890

89-
public void write(String outdir) throws Exception {
91+
public void write(String outdir) throws IOException {
9092
HtmlUtil.write(document, Paths.get(outdir, name + ".html"));
9193
}
9294

src/main/java/com/mays/util/html/HtmlUtil.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
public class HtmlUtil {
1313

14+
private HtmlUtil() {
15+
}
16+
1417
public static void write(Document document, Path file) throws IOException {
1518
OutputFormat format = OutputFormat.createPrettyPrint();
1619
format.setTrimText(false);
@@ -20,7 +23,7 @@ public static void write(Document document, Path file) throws IOException {
2023
writer.write(document);
2124
writer.close();
2225
}
23-
26+
2427
public static String write(Document document) throws IOException {
2528
OutputFormat format = OutputFormat.createPrettyPrint();
2629
format.setTrimText(false);

src/main/java/com/mays/util/xml/XmlResourceResolver.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.InputStream;
44
import java.nio.file.Path;
55
import java.nio.file.Paths;
6+
import java.util.MissingResourceException;
67

78
import org.slf4j.Logger;
89
import org.slf4j.LoggerFactory;
@@ -22,10 +23,10 @@ public XmlResourceResolver(String basePath) {
2223
@Override
2324
public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
2425
Path path = Paths.get(basePath, systemId);
25-
logger.info("Resolve resource: " + path.toString());
26+
logger.info("Resolve resource: " + path);
2627
InputStream resource = this.getClass().getClassLoader().getResourceAsStream(path.toString());
2728
if (resource == null)
28-
throw new RuntimeException("Could not find the specified xsd file: " + basePath + " " + systemId);
29+
throw new MissingResourceException("Could not find the specified xsd file", basePath, systemId);
2930
return new XmlLSInputImpl(publicId, systemId, baseURI, resource, "UTF-8");
3031
}
3132

src/main/java/com/mays/util/xml/XmlUtil.java

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@
2424
import org.slf4j.LoggerFactory;
2525
import org.xml.sax.SAXException;
2626

27-
@SuppressWarnings("unused")
2827
public class XmlUtil {
2928

3029
private static final Logger logger = LoggerFactory.getLogger(XmlUtil.class);
3130

31+
private XmlUtil() {
32+
}
33+
3234
public static void write(Document document, Path file) throws IOException {
3335
OutputFormat format = OutputFormat.createPrettyPrint();
3436
format.setTrimText(false);
@@ -37,31 +39,29 @@ public static void write(Document document, Path file) throws IOException {
3739
writer.close();
3840
}
3941

40-
// TODO
4142
// https://stackoverflow.com/questions/55571046/eclipse-is-confused-by-imports-accessible-from-more-than-one-module
42-
// public static void validate(String file, String xsd, String schema_dir)
43-
// throws ParserConfigurationException, SAXException, IOException {
44-
// XmlUtil.validate(new FileInputStream(file), xsd, schema_dir);
45-
// }
46-
//
47-
// public static void validate(InputStream in, String xsd, String schema_dir)
48-
// throws ParserConfigurationException, SAXException, IOException {
49-
// // See javax.xml.validation
50-
// DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
51-
// dbf.setNamespaceAware(true);
52-
// DocumentBuilder parser = dbf.newDocumentBuilder();
53-
// org.w3c.dom.Document document = parser.parse(in);
54-
// SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
55-
// factory.setResourceResolver(new XmlResourceResolver(schema_dir));
56-
// ArrayList<Source> sources = new ArrayList<>();
57-
// // for (String xsd : xsds) {
58-
// logger.info("Schema: " + xsd);
59-
// Source schema_file = new StreamSource(XmlUtil.class.getClassLoader().getResourceAsStream(xsd));
60-
// sources.add(schema_file);
61-
// // }
62-
// javax.xml.validation.Schema schema = factory.newSchema(sources.toArray(new Source[sources.size()]));
63-
// Validator validator = schema.newValidator();
64-
// validator.validate(new DOMSource(document));
65-
// }
43+
44+
public static void validate(String file, String xsd, String schema_dir)
45+
throws ParserConfigurationException, SAXException, IOException {
46+
XmlUtil.validate(new FileInputStream(file), xsd, schema_dir);
47+
}
48+
49+
public static void validate(InputStream in, String xsd, String schema_dir)
50+
throws ParserConfigurationException, SAXException, IOException {
51+
// See javax.xml.validation
52+
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
53+
dbf.setNamespaceAware(true);
54+
DocumentBuilder parser = dbf.newDocumentBuilder();
55+
org.w3c.dom.Document document = parser.parse(in);
56+
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
57+
factory.setResourceResolver(new XmlResourceResolver(schema_dir));
58+
ArrayList<Source> sources = new ArrayList<>();
59+
logger.info("Schema: " + xsd);
60+
Source schema_file = new StreamSource(XmlUtil.class.getClassLoader().getResourceAsStream(xsd));
61+
sources.add(schema_file);
62+
javax.xml.validation.Schema schema = factory.newSchema(sources.toArray(new Source[sources.size()]));
63+
Validator validator = schema.newValidator();
64+
validator.validate(new DOMSource(document));
65+
}
6666

6767
}

0 commit comments

Comments
 (0)