Merge pull request #8125 from eugenp/revert-8119-BAEL-3275-2

Revert "BAEL-3275: Using blocking queue for pub-sub"
This commit is contained in:
Eric Martin
2019-10-31 20:43:47 -05:00
committed by GitHub
parent db85c8f275
commit 3225470df5
20543 changed files with 1642750 additions and 0 deletions
@@ -0,0 +1,10 @@
package com.baeldung.xmlhtml;
import com.baeldung.xmlhtml.helpers.XMLRunner;
public class Application {
public static void main(String[] args) {
XMLRunner.doWork();
}
}
@@ -0,0 +1,22 @@
package com.baeldung.xmlhtml;
public class Constants {
public static final String XML_FILE_IN = "src/main/resources/xml/in.xml";
public static final String JAXB_FILE_OUT = "src/main/resources/xml/jaxb.html";
public static final String JAXP_FILE_OUT = "src/main/resources/xml/jaxp.html";
public static final String STAX_FILE_OUT = "src/main/resources/xml/stax.html";
public static final String EXCEPTION_ENCOUNTERED = "Generic exception! Error: ";
public static final String LINE_BREAK = System.getProperty("line.separator");
public static final String WHITE_SPACE = " ";
public static final String DOCUMENT_START = "Document parsing has begun!";
public static final String DOCUMENT_END = "Document parsing has ended!";
public static final String ELEMENT_START = "Element parsing has begun!";
public static final String ELEMENT_END = "Element parsing has ended!";
public static final String BREAK = "\n";
public static final String TAB = "\t";
}
@@ -0,0 +1,41 @@
package com.baeldung.xmlhtml.freemarker;
import com.baeldung.xmlhtml.stax.StaxTransformer;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import javax.xml.stream.XMLStreamException;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
public class FreemarkerTransformer {
private StaxTransformer staxTransformer;
private String templateDirectory;
private String templateFile;
public FreemarkerTransformer(StaxTransformer staxTransformer, String templateDirectory, String templateFile) {
this.staxTransformer = staxTransformer;
this.templateDirectory = templateDirectory;
this.templateFile = templateFile;
}
public String html() throws IOException, TemplateException {
Configuration cfg = new Configuration(Configuration.VERSION_2_3_29);
cfg.setDirectoryForTemplateLoading(new File(templateDirectory));
cfg.setDefaultEncoding(StandardCharsets.UTF_8.toString());
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
cfg.setLogTemplateExceptions(false);
cfg.setWrapUncheckedExceptions(true);
cfg.setFallbackOnNullLoopVariable(false);
Template temp = cfg.getTemplate(templateFile);
try (Writer output = new StringWriter()) {
temp.process(staxTransformer.getMap(), output);
return output.toString();
}
}
}
@@ -0,0 +1,12 @@
package com.baeldung.xmlhtml.helpers;
import com.baeldung.xmlhtml.helpers.jaxb.JAXBHelper;
import com.baeldung.xmlhtml.stax.StaxTransformer;
public class XMLRunner {
public static void doWork() {
JAXBHelper.example();
}
}
@@ -0,0 +1,77 @@
package com.baeldung.xmlhtml.helpers.jaxb;
import com.baeldung.xmlhtml.pojo.jaxb.html.ExampleHTML;
import com.baeldung.xmlhtml.pojo.jaxb.html.elements.Body;
import com.baeldung.xmlhtml.pojo.jaxb.html.elements.CustomElement;
import com.baeldung.xmlhtml.pojo.jaxb.html.elements.Meta;
import com.baeldung.xmlhtml.pojo.jaxb.html.elements.NestedElement;
import com.baeldung.xmlhtml.pojo.jaxb.xml.XMLExample;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.File;
import static com.baeldung.xmlhtml.Constants.*;
public class JAXBHelper {
private static void print(String xmlContent) {
System.out.println(xmlContent);
}
private static Unmarshaller getContextUnmarshaller(Class clazz) {
Unmarshaller unmarshaller = null;
try {
JAXBContext context = JAXBContext.newInstance(clazz);
unmarshaller = context.createUnmarshaller();
} catch (Exception ex) {
System.out.println(EXCEPTION_ENCOUNTERED + ex);
}
return unmarshaller;
}
private static Marshaller getContextMarshaller(Class clazz) {
Marshaller marshaller = null;
try {
JAXBContext context = JAXBContext.newInstance(clazz);
marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty("jaxb.fragment", Boolean.TRUE);
} catch (Exception ex) {
System.out.println(EXCEPTION_ENCOUNTERED + ex);
}
return marshaller;
}
public static void example() {
try {
XMLExample xml = (XMLExample) JAXBHelper.getContextUnmarshaller(XMLExample.class).unmarshal(new File(XML_FILE_IN));
JAXBHelper.print(xml.toString());
ExampleHTML html = new ExampleHTML();
Body body = new Body();
CustomElement customElement = new CustomElement();
NestedElement nested = new NestedElement();
CustomElement child = new CustomElement();
customElement.setValue("descendantOne: " + xml.getAncestor().getDescendantOne().getValue());
child.setValue("descendantThree: " + xml.getAncestor().getDescendantTwo().getDescendantThree().getValue());
nested.setCustomElement(child);
body.setCustomElement(customElement);
body.setNestedElement(nested);
Meta meta = new Meta();
meta.setTitle("example");
html.getHead().add(meta);
html.setBody(body);
JAXBHelper.getContextMarshaller(ExampleHTML.class).marshal(html, new File(JAXB_FILE_OUT));
} catch (Exception ex) {
System.out.println(EXCEPTION_ENCOUNTERED + ex);
}
}
}
@@ -0,0 +1,103 @@
package com.baeldung.xmlhtml.jaxp;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
public class JaxpTransformer {
private Document input;
private DocumentBuilderFactory factory;
public JaxpTransformer(String resourcePath) throws ParserConfigurationException, IOException, SAXException {
// 1- Build the doc from the XML file
factory = DocumentBuilderFactory.newInstance();
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
input = factory
.newDocumentBuilder()
.parse(resourcePath);
}
public String html() throws ParserConfigurationException, TransformerException, IOException {
Element xml = input.getDocumentElement();
Document doc = factory
.newDocumentBuilder()
.newDocument();
//Build Map
Map<String, String> map = buildMap(xml);
//Head
Element html = doc.createElement("html");
html.setAttribute("lang", "en");
Element head = buildHead(map, doc);
html.appendChild(head);
//Body
Element body = buildBody(map, doc);
html.appendChild(body);
doc.appendChild(html);
return String.format("<!DOCTYPE html>%n%s", applyTransformation(doc));
}
private String applyTransformation(Document doc) throws TransformerException, IOException {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformerFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
try (Writer output = new StringWriter()) {
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(new DOMSource(doc), new StreamResult(output));
return output.toString();
}
}
private Map<String, String> buildMap(Element xml) {
Map<String, String> map = new HashMap<>();
map.put("heading", xml
.getElementsByTagName("heading")
.item(0)
.getTextContent());
map.put("from", String.format("from: %s", xml
.getElementsByTagName("from")
.item(0)
.getTextContent()));
map.put("content", xml
.getElementsByTagName("content")
.item(0)
.getTextContent());
return map;
}
private Element buildHead(Map<String, String> map, Document doc) {
Element head = doc.createElement("head");
Element title = doc.createElement("title");
title.setTextContent(map.get("heading"));
head.appendChild(title);
return head;
}
private Element buildBody(Map<String, String> map, Document doc) {
Element body = doc.createElement("body");
Element from = doc.createElement("p");
from.setTextContent(map.get("from"));
Element success = doc.createElement("p");
success.setTextContent(map.get("content"));
body.appendChild(from);
body.appendChild(success);
return body;
}
}
@@ -0,0 +1,31 @@
package com.baeldung.xmlhtml.mustache;
import com.baeldung.xmlhtml.stax.StaxTransformer;
import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache;
import com.github.mustachejava.MustacheFactory;
import javax.xml.stream.XMLStreamException;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
public class MustacheTransformer {
private StaxTransformer staxTransformer;
private String templateFile;
public MustacheTransformer(StaxTransformer staxTransformer, String templateFile) {
this.staxTransformer = staxTransformer;
this.templateFile = templateFile;
}
public String html() throws IOException {
MustacheFactory mf = new DefaultMustacheFactory();
Mustache mustache = mf.compile(templateFile);
try (Writer output = new StringWriter()) {
mustache.execute(output, staxTransformer.getMap());
output.flush();
return output.toString();
}
}
}
@@ -0,0 +1,38 @@
package com.baeldung.xmlhtml.pojo.jaxb.html;
import com.baeldung.xmlhtml.pojo.jaxb.html.elements.Body;
import com.baeldung.xmlhtml.pojo.jaxb.html.elements.Meta;
import javax.xml.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@XmlType(propOrder = {"head", "body"})
@XmlRootElement(name = "html")
public class ExampleHTML {
private List<Meta> head = new ArrayList<>();
private Body body;
public ExampleHTML() { }
public List<Meta> getHead() {
return head;
}
@XmlElementWrapper(name = "head")
@XmlElement(name = "meta")
public void setHead(List<Meta> head) {
this.head = head;
}
public Body getBody() {
return body;
}
@XmlElement(name = "body")
public void setBody(Body body) {
this.body = body;
}
}
@@ -0,0 +1,29 @@
package com.baeldung.xmlhtml.pojo.jaxb.html.elements;
import javax.xml.bind.annotation.XmlElement;
public class Body {
private CustomElement customElement;
public CustomElement getCustomElement() {
return customElement;
}
@XmlElement(name = "p")
public void setCustomElement(CustomElement customElement) {
this.customElement = customElement;
}
private NestedElement nestedElement;
public NestedElement getNestedElement() {
return nestedElement;
}
@XmlElement(name = "div")
public void setNestedElement(NestedElement nestedElement) {
this.nestedElement = nestedElement;
}
}
@@ -0,0 +1,18 @@
package com.baeldung.xmlhtml.pojo.jaxb.html.elements;
import javax.xml.bind.annotation.XmlValue;
public class CustomElement {
private String value;
@XmlValue
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
@@ -0,0 +1,30 @@
package com.baeldung.xmlhtml.pojo.jaxb.html.elements;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlValue;
public class Meta {
private String title;
public String getTitle() {
return title;
}
@XmlAttribute(name = "title")
public void setTitle(String title) {
this.title = title;
}
private String value;
@XmlValue
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
@@ -0,0 +1,17 @@
package com.baeldung.xmlhtml.pojo.jaxb.html.elements;
import javax.xml.bind.annotation.XmlElement;
public class NestedElement {
private CustomElement customElement;
public CustomElement getCustomElement() {
return customElement;
}
@XmlElement(name = "p")
public void setCustomElement(CustomElement customElement) {
this.customElement = customElement;
}
}
@@ -0,0 +1,21 @@
package com.baeldung.xmlhtml.pojo.jaxb.xml;
import com.baeldung.xmlhtml.pojo.jaxb.xml.elements.Ancestor;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "xmlexample")
public class XMLExample {
private Ancestor ancestor;
@XmlElement(name = "ancestor")
public void setAncestor(Ancestor ancestor) {
this.ancestor = ancestor;
}
public Ancestor getAncestor() {
return ancestor;
}
}
@@ -0,0 +1,28 @@
package com.baeldung.xmlhtml.pojo.jaxb.xml.elements;
import javax.xml.bind.annotation.XmlElement;
public class Ancestor {
private DescendantOne descendantOne;
private DescendantTwo descendantTwo;
public DescendantOne getDescendantOne() {
return descendantOne;
}
@XmlElement(name = "descendantOne")
public void setDescendantOne(DescendantOne descendantOne) {
this.descendantOne = descendantOne;
}
public DescendantTwo getDescendantTwo() {
return descendantTwo;
}
@XmlElement(name = "descendantTwo")
public void setDescendantTwo(DescendantTwo descendantTwo) {
this.descendantTwo = descendantTwo;
}
}
@@ -0,0 +1,19 @@
package com.baeldung.xmlhtml.pojo.jaxb.xml.elements;
import javax.xml.bind.annotation.XmlValue;
public class DescendantOne {
private String value;
@XmlValue
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
@@ -0,0 +1,18 @@
package com.baeldung.xmlhtml.pojo.jaxb.xml.elements;
import javax.xml.bind.annotation.XmlValue;
public class DescendantThree {
private String value;
@XmlValue
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
@@ -0,0 +1,17 @@
package com.baeldung.xmlhtml.pojo.jaxb.xml.elements;
import javax.xml.bind.annotation.XmlElement;
public class DescendantTwo {
private DescendantThree descendantThree;
public DescendantThree getDescendantThree() {
return descendantThree;
}
@XmlElement(name = "descendantThree")
public void setDescendant(DescendantThree descendantThree) {
this.descendantThree = descendantThree;
}
}
@@ -0,0 +1,99 @@
package com.baeldung.xmlhtml.stax;
import javax.xml.stream.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
public class StaxTransformer {
private Map<String, String> map;
public StaxTransformer(String resourcePath) throws IOException, XMLStreamException {
XMLInputFactory factory = XMLInputFactory.newInstance();
factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
factory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
XMLStreamReader input = null;
try (FileInputStream file = new FileInputStream(resourcePath)) {
input = factory.createXMLStreamReader(file);
map = buildMap(input);
} finally {
if (input != null) {
input.close();
}
}
}
public String html() throws XMLStreamException, IOException {
try (Writer output = new StringWriter()) {
XMLStreamWriter writer = XMLOutputFactory
.newInstance()
.createXMLStreamWriter(output);
//Head
writer.writeDTD("<!DOCTYPE html>");
writer.writeCharacters(String.format("%n"));
writer.writeStartElement("html");
writer.writeAttribute("lang", "en");
writer.writeCharacters(String.format("%n"));
writer.writeStartElement("head");
writer.writeCharacters(String.format("%n"));
writer.writeDTD("<META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
writer.writeCharacters(String.format("%n"));
writer.writeStartElement("title");
writer.writeCharacters(map.get("heading"));
writer.writeEndElement();
writer.writeCharacters(String.format("%n"));
writer.writeEndElement();
writer.writeCharacters(String.format("%n"));
//Body
writer.writeStartElement("body");
writer.writeCharacters(String.format("%n"));
writer.writeStartElement("p");
writer.writeCharacters(map.get("from"));
writer.writeEndElement();
writer.writeCharacters(String.format("%n"));
writer.writeStartElement("p");
writer.writeCharacters(map.get("content"));
writer.writeEndElement();
writer.writeCharacters(String.format("%n"));
writer.writeEndElement();
writer.writeCharacters(String.format("%n"));
writer.writeEndDocument();
writer.writeCharacters(String.format("%n"));
writer.flush();
return output.toString();
}
}
private Map<String, String> buildMap(XMLStreamReader input) throws XMLStreamException {
Map<String, String> tempMap = new HashMap<>();
while (input.hasNext()) {
input.next();
if (input.isStartElement()) {
if (input
.getLocalName()
.equals("heading")) {
tempMap.put("heading", input.getElementText());
}
if (input
.getLocalName()
.equals("from")) {
tempMap.put("from", String.format("from: %s", input.getElementText()));
}
if (input
.getLocalName()
.equals("content")) {
tempMap.put("content", input.getElementText());
}
}
}
return tempMap;
}
public Map<String, String> getMap() {
return map;
}
}