Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>io.github.emays</groupId>
<artifactId>parent</artifactId>
<version>0.0.6</version>
<version>0.0.7</version>
<relativePath />
</parent>

Expand Down Expand Up @@ -88,7 +88,6 @@
</releases>
<snapshots>
<enabled>true</enabled>
<!--<updatePolicy>always</updatePolicy>-->
</snapshots>
</repository>
</repositories>
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/com/mays/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> lines = Files.lines(Paths.get(in_file)).map(line -> line.replace(fr, to));
Files.write(Paths.get(out_file), iterable(lines));
}
Expand All @@ -47,10 +50,6 @@ public static String tabDelimitedString(List<String> fields) {
}

public static String delimitedString(String delim, List<String> 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);
}

Expand All @@ -62,7 +61,7 @@ public static String delimitedString(String delim, Object... fields) {
ArrayList<String> 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);
Expand Down
25 changes: 7 additions & 18 deletions src/main/java/com/mays/util/html/ElementW.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand All @@ -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() {
Expand Down Expand Up @@ -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.
*
*
* <p>
* <blockquote> addElement(HtmlTag.DIV, "id", "div1", "style", "font: 10pt
* sans-serif;") </blockquote>
*
*
* @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
Expand All @@ -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.
*
*
* <p>
* <blockquote> addElement("div", "id", "div1", "style", "font: 10pt
* sans-serif;") </blockquote>
Expand Down Expand Up @@ -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}
*/
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/com/mays/util/html/HtmlPage.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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());
Expand Down Expand Up @@ -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"));
}

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/mays/util/html/HtmlUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/mays/util/xml/XmlResourceResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
}

Expand Down
52 changes: 26 additions & 26 deletions src/main/java/com/mays/util/xml/XmlUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<Source> 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<Source> 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));
}

}