Added closeable interface on content writers

This commit is contained in:
Willem Cazander 2025-01-19 18:57:19 +01:00
parent d77f9dfc57
commit 13171b39ab
7 changed files with 118 additions and 150 deletions

View file

@ -27,6 +27,7 @@
package love.distributedrebirth.nx01.warp.manifestor.scopic.iomf5;
import java.io.IOException;
import java.util.Objects;
import org.x4o.o2o.io.sax3.ContentWriterAdapter;
@ -52,6 +53,10 @@ public class ScopicManifest5ContentParser extends ContentWriterAdapter {
this.handler = Objects.requireNonNull(handler);
}
@Override
public void close() throws IOException {
}
@Override
public void startDocument() throws SAXException {
handler.strobeManifestStart();

View file

@ -23,6 +23,7 @@
package org.x4o.o2o.io.sax3;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
@ -35,6 +36,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.PrimitiveIterator;
import java.util.Set;
import java.util.Stack;
@ -52,10 +54,10 @@ import org.xml.sax.SAXException;
* @author Willem Cazander
* @version 1.0 May 3, 2013
*/
public class AbstractContentWriterHandler implements ContentHandler {
public class AbstractContentWriterHandler implements ContentHandler, Closeable {
private final PropertyConfig propertyConfig;
private Writer out = null;
private final Writer out;
private int indent = 0;
private Map<String,String> prefixMapping = null;
private List<String> printedMappings = null;
@ -109,37 +111,22 @@ public class AbstractContentWriterHandler implements ContentHandler {
* @param out The writer to print the xml to.
*/
public AbstractContentWriterHandler(Writer out) {
if (out==null) {
throw new NullPointerException("Can't write on null writer.");
}
this.out = out;
prefixMapping = new HashMap<String,String>(15);
printedMappings = new ArrayList<String>(15);
elements = new Stack<String>();
propertyConfig = new PropertyConfig(DEFAULT_PROPERTY_CONFIG,PROPERTY_CONTEXT_PREFIX);
this.out = Objects.requireNonNull(out, "Can't write on null writer.");
this.prefixMapping = new HashMap<String,String>(15);
this.printedMappings = new ArrayList<String>(15);
this.elements = new Stack<String>();
this.propertyConfig = new PropertyConfig(DEFAULT_PROPERTY_CONFIG,PROPERTY_CONTEXT_PREFIX);
}
public PropertyConfig getPropertyConfig() {
return propertyConfig;
}
@Deprecated
public void closeWriter() throws IOException {
if (out==null) {
return;
}
@Override
public void close() throws IOException {
out.close();
}
@Deprecated
public void closeWriterSafe() {
try {
closeWriter();
} catch (IOException e) {
e.getMessage(); // discard exception
}
}
/**
* @see org.xml.sax.ContentHandler#startDocument()
*/

View file

@ -22,6 +22,8 @@
*/
package org.x4o.o2o.io.sax3;
import java.io.Closeable;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
@ -34,7 +36,7 @@ import org.xml.sax.ext.LexicalHandler;
* @author Willem Cazander
* @version 1.0 Apr 30, 2013
*/
public interface ContentWriter extends ContentHandler,LexicalHandler {
public interface ContentWriter extends ContentHandler, LexicalHandler, Closeable {
/**
* Starts and ends an element in one call.

View file

@ -22,7 +22,9 @@
*/
package org.x4o.o2o.io.sax3;
import java.io.Closeable;
import java.io.IOException;
import java.util.Objects;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
@ -34,7 +36,7 @@ import org.xml.sax.helpers.AttributesImpl;
* @author Willem Cazander
* @version 1.0 May 3, 2013
*/
public class ContentWriterTagWrapper<TAG extends Enum<?>,TAG_WRITER extends ContentWriter> implements ContentWriterTag<TAG> {
public class ContentWriterTagWrapper<TAG extends Enum<?>,TAG_WRITER extends ContentWriter> implements ContentWriterTag<TAG>, Closeable {
private final Attributes EMPTY_ATTRIBUTES = new AttributesImpl();
private final TAG_WRITER contentWriter;
@ -46,18 +48,14 @@ public class ContentWriterTagWrapper<TAG extends Enum<?>,TAG_WRITER extends Cont
}
public ContentWriterTagWrapper(TAG_WRITER contentWriter, String tagNamespaceUri, String tagNamespacePrefix) {
if (contentWriter == null) {
throw new NullPointerException("Can't create wrapper on null ContentWriter");
}
if (tagNamespaceUri == null) {
throw new NullPointerException("Can't create wrapper with null tagNamespaceUri");
}
if (tagNamespacePrefix == null) {
throw new NullPointerException("Can't create wrapper with null tagNamespacePrefix");
}
this.contentWriter=contentWriter;
this.tagNamespaceUri=tagNamespaceUri;
this.tagNamespacePrefix=tagNamespacePrefix;
this.contentWriter = Objects.requireNonNull(contentWriter, "Can't create wrapper on null ContentWriter");
this.tagNamespaceUri = Objects.requireNonNull(tagNamespaceUri, "Can't create wrapper with null tagNamespaceUri");
this.tagNamespacePrefix = Objects.requireNonNull(tagNamespacePrefix, "Can't create wrapper with null tagNamespacePrefix");
}
@Override
public void close() throws IOException {
contentWriter.close();
}
public TAG_WRITER getContentWriterWrapped() {

View file

@ -23,6 +23,7 @@
package org.x4o.o2o.io.sax3.xdbx;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
@ -36,6 +37,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.PrimitiveIterator;
import java.util.Set;
import java.util.Stack;
@ -54,10 +56,10 @@ import org.xml.sax.SAXException;
* @author Willem Cazander
* @version 1.0 Dec 19, 2024
*/
public class AbstractXDBXWriterHandler implements ContentHandler {
public class AbstractXDBXWriterHandler implements ContentHandler, Closeable {
private final PropertyConfig propertyConfig;
private OutputStream out = null;
private final OutputStream out;
private Map<String,String> prefixMapping = null;
private List<String> printedMappings = null;
private Stack<String> elements = null;
@ -98,37 +100,23 @@ public class AbstractXDBXWriterHandler implements ContentHandler {
* @param out The stream to print the xml to.
*/
public AbstractXDBXWriterHandler(OutputStream out) {
if (out==null) {
throw new NullPointerException("Can't write on null OutputStream.");
}
this.out = out;
prefixMapping = new HashMap<String,String>(15);
printedMappings = new ArrayList<String>(15);
elements = new Stack<String>();
propertyConfig = new PropertyConfig(DEFAULT_PROPERTY_CONFIG,PROPERTY_CONTEXT_PREFIX);
stringIdx = new HashMap<>();
this.out = Objects.requireNonNull(out, "Can't write on null OutputStream.");
this.prefixMapping = new HashMap<String,String>(15);
this.printedMappings = new ArrayList<String>(15);
this.elements = new Stack<String>();
this.propertyConfig = new PropertyConfig(DEFAULT_PROPERTY_CONFIG,PROPERTY_CONTEXT_PREFIX);
this.stringIdx = new HashMap<>();
}
public PropertyConfig getPropertyConfig() {
return propertyConfig;
}
// TODO: check location of this. (add to api?)
public void closeWriter() throws IOException {
if (out==null) {
return;
}
@Override
public void close() throws IOException {
out.close();
}
public void closeWriterSafe() {
try {
closeWriter();
} catch (IOException e) {
e.getMessage(); // discard exception
}
}
protected boolean xdbxStringId(String data) {
Integer result = stringIdx.get(data);
return result != null;

View file

@ -124,61 +124,60 @@ public class ApiDocGenerator implements ApiDocContentPrinter {
pathPrefix += "../";
}
File outputFile = createOutputPathFile(basePath,path.toArray(new String[]{}));
ApiDocContentWriter writer = createContentWriter(outputFile);
doc.getNodeData().clearGroupTypeLinks();
doc.getNodeData().setNavSelected(concept.getId());
configNodeData(pathPrefix,outputFile);
configActiveNavConceptLinks(node,concept,"/..");
configNextPrevLinks(node);
configSubNavLinks(node);
configData(node);
ApiDocWriteEvent<ApiDocNode> bodyEvent = new ApiDocWriteEvent<ApiDocNode>(doc,writer,node);
String titleNode = node.getName();
String titleNodeSub = null;
if (node.getParent()!=null) {
titleNodeSub = node.getParent().getId()+":"+node.getId();
}
boolean isNodePageMode = isNodePageMode(node);
String titleContent = titleNode;
if (doc.isPrintConceptTitle()) {
String conceptTitle = concept.getName();
ApiDocConcept childConcept = doc.findConceptChildByNode(node);
if (childConcept!=null) {
conceptTitle = childConcept.getName();
try (ApiDocContentWriter writer = createContentWriter(outputFile)) {
doc.getNodeData().clearGroupTypeLinks();
doc.getNodeData().setNavSelected(concept.getId());
configNodeData(pathPrefix,outputFile);
configActiveNavConceptLinks(node,concept,"/..");
configNextPrevLinks(node);
configSubNavLinks(node);
configData(node);
ApiDocWriteEvent<ApiDocNode> bodyEvent = new ApiDocWriteEvent<ApiDocNode>(doc,writer,node);
String titleNode = node.getName();
String titleNodeSub = null;
if (node.getParent()!=null) {
titleNodeSub = node.getParent().getId()+":"+node.getId();
}
titleContent = conceptTitle +" "+titleNode;
}
String titleHtml = titleNode;
if (doc.getDocPageSubTitle()!=null) {
titleHtml = titleNode+" ("+doc.getDocPageSubTitle()+")";
if (node.getParent()==null) {
titleContent = doc.getDocPageSubTitle();
titleHtml = "Overview ("+doc.getDocPageSubTitle()+")";
boolean isNodePageMode = isNodePageMode(node);
String titleContent = titleNode;
if (doc.isPrintConceptTitle()) {
String conceptTitle = concept.getName();
ApiDocConcept childConcept = doc.findConceptChildByNode(node);
if (childConcept!=null) {
conceptTitle = childConcept.getName();
}
titleContent = conceptTitle +" "+titleNode;
}
}
// Write node file
writer.docHtmlStart(titleHtml, doc.getDocKeywords(),doc.getNodeData().getPrefixPath());
docNavBar(writer,true,concept,node);
if (isNodePageMode) {
writer.docPageClassStart(titleContent, null,Tag.h1);
} else {
writer.docPageClassStart(titleContent, titleNodeSub,Tag.h2);
String titleHtml = titleNode;
if (doc.getDocPageSubTitle()!=null) {
titleHtml = titleNode+" ("+doc.getDocPageSubTitle()+")";
if (node.getParent()==null) {
titleContent = doc.getDocPageSubTitle();
titleHtml = "Overview ("+doc.getDocPageSubTitle()+")";
}
}
writer.docPageContentStart();
if (!isNodePageMode) {
writeNodeTreePath(bodyEvent);
}
writeNodeDescription(bodyEvent,isNodePageMode);
writeNodeSummary(bodyEvent,isNodePageMode);
writeNodeDetails(bodyEvent);
writer.docPageContentEnd();
writer.docPageClassEnd();
docNavBar(writer,false,concept,node);
writer.docHtmlEnd(doc.getDocCopyright(),doc.getDocStatsJS());
writer.getContentWriterWrapped().closeWriterSafe();
// Write node file
writer.docHtmlStart(titleHtml, doc.getDocKeywords(),doc.getNodeData().getPrefixPath());
docNavBar(writer,true,concept,node);
if (isNodePageMode) {
writer.docPageClassStart(titleContent, null,Tag.h1);
} else {
writer.docPageClassStart(titleContent, titleNodeSub,Tag.h2);
}
writer.docPageContentStart();
if (!isNodePageMode) {
writeNodeTreePath(bodyEvent);
}
writeNodeDescription(bodyEvent,isNodePageMode);
writeNodeSummary(bodyEvent,isNodePageMode);
writeNodeDetails(bodyEvent);
writer.docPageContentEnd();
writer.docPageClassEnd();
docNavBar(writer,false,concept,node);
writer.docHtmlEnd(doc.getDocCopyright(),doc.getDocStatsJS());
}
// Writer other files
writeAllFrameNavNode(node);
@ -242,7 +241,7 @@ public class ApiDocGenerator implements ApiDocContentPrinter {
if (i+1<bodyWriterDescriptionNode.size()) {
writer.printTagStartEnd(Tag.br);
}
}
}
writer.docPageBlockEnd();
writer.printTagEnd(Tag.div); // description
}
@ -266,7 +265,7 @@ public class ApiDocGenerator implements ApiDocContentPrinter {
if (!isPageMode) { writer.printTagCharacters(Tag.h3, "Summary"); }
nodeWriter.writeNodeContent(event);
if (!isPageMode) { writer.docPageBlockEnd(); }
if (isPageMode) { writer.printTagStartEnd(Tag.br); } // mm .. mm
if (isPageMode) { writer.printTagStartEnd(Tag.br); }
}
if (!isPageMode) {
writer.docPageBlockEnd();
@ -638,8 +637,7 @@ public class ApiDocGenerator implements ApiDocContentPrinter {
public void writeIndex() throws IOException {
File outputFile = createOutputPathFile(basePath,"index.html");
ApiDocContentWriter writer = createContentWriter(outputFile);
try {
try (ApiDocContentWriter writer = createContentWriter(outputFile)) {
writer.printDocType(DocType.HTML_4_FRAMESET);
writer.printComment("NewPage");
writer.printHtmlStart("en");
@ -694,8 +692,6 @@ public class ApiDocGenerator implements ApiDocContentPrinter {
writer.printTagEnd(Tag.frameset);
writer.printHtmlEnd();
} finally {
writer.getContentWriterWrapped().closeWriterSafe();
}
}
@ -715,8 +711,7 @@ public class ApiDocGenerator implements ApiDocContentPrinter {
findNodeByUserDataClass(doc.getRootNode(),conceptParent.getConceptClass(),nodes);
File outputFile = createOutputPathFile(basePath,"overview-frame.html");
ApiDocContentWriter writer = createContentWriter(outputFile);
try {
try (ApiDocContentWriter writer = createContentWriter(outputFile)) {
String conceptPlural = concept.getName()+"s";
String conceptParentPlural = conceptParent.getName()+"s";
@ -762,8 +757,6 @@ public class ApiDocGenerator implements ApiDocContentPrinter {
writer.printTagEnd(Tag.body);
writer.printHtmlEnd();
} finally {
writer.getContentWriterWrapped().closeWriterSafe();
}
}
@ -799,8 +792,7 @@ public class ApiDocGenerator implements ApiDocContentPrinter {
findNodeByUserDataClass(doc.getRootNode(),concept.getConceptClass(),nodes);
File outputFile = createOutputPathFile(basePath,fileName);
ApiDocContentWriter writer = createContentWriter(outputFile);
try {
try (ApiDocContentWriter writer = createContentWriter(outputFile)) {
String conceptPlural = concept.getName()+"s";
//String conceptParentPlural = conceptParent.getName()+"s";
@ -873,16 +865,13 @@ public class ApiDocGenerator implements ApiDocContentPrinter {
writer.printTagEnd(Tag.body);
writer.printHtmlEnd();
} finally {
writer.getContentWriterWrapped().closeWriterSafe();
}
}
private void writePage(ApiDocPage page) throws IOException {
File outputFile = createOutputPathFile(basePath,page.getId()+".html");
ApiDocContentWriter writer = createContentWriter(outputFile);
String pathPrefix = "";
try {
try (ApiDocContentWriter writer = createContentWriter(outputFile)) {
configNodeData(pathPrefix,outputFile);
doc.getNodeData().setNavSelected(page.getId());
String title = page.getName();
@ -901,8 +890,6 @@ public class ApiDocGenerator implements ApiDocContentPrinter {
writer.docPageClassEnd();
docNavBar(writer,false,null,null);
writer.docHtmlEnd(doc.getDocCopyright(),doc.getDocStatsJS());
} finally {
writer.getContentWriterWrapped().closeWriterSafe();
}
}

View file

@ -31,7 +31,6 @@ import java.io.BufferedWriter;
import java.io.IOException;
import java.util.List;
import org.x4o.o2o.io.sax3.ContentWriter;
import org.x4o.o2o.io.sax3.ContentWriterTagWrapper;
import org.x4o.o2o.io.sax3.ContentWriterXml;
import org.x4o.o2o.io.sax3.xdbx.XDBXWriterXml;
@ -57,19 +56,21 @@ public class WarpCorePlasmaInspectorServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
if ("true".equalsIgnoreCase(request.getParameter("___xdbx"))) {
response.setContentType("application/octet-stream");
ContentWriterInspector writer = new ContentWriterInspector(new XDBXWriterXml(response.getOutputStream()));
printReport(writer, request);
try (ContentWriterTagWrapper<Tag,?> writer = new ContentWriterTagWrapper<Tag,XDBXWriterXml>(new XDBXWriterXml(response.getOutputStream()))) {
printReport(writer, request);
}
return;
}
response.setContentType("text/xml");
response.setCharacterEncoding("utf-8");
BufferedWriter out = new BufferedWriter(response.getWriter());
ContentWriterInspector writer = new ContentWriterInspector(new ContentWriterXml(out, response.getCharacterEncoding()));
printReport(writer, request);
out.flush();
try (ContentWriterTagWrapper<Tag,?> writer = new ContentWriterTagWrapper<Tag,ContentWriterXml>(new ContentWriterXml(out, response.getCharacterEncoding()))) {
printReport(writer, request);
out.flush();
}
}
private void printReport(ContentWriterInspector writer, HttpServletRequest request) {
private void printReport(ContentWriterTagWrapper<Tag,?> writer, HttpServletRequest request) {
WarpCoreReactor reactor = ZeroFungus.INSTANCE.getWarpCore();
List<WarpReactPlasma> slots = reactor.listChilds(null);
try {
@ -93,7 +94,7 @@ public class WarpCorePlasmaInspectorServlet extends HttpServlet {
}
}
private void printSlotContract(ContentWriterInspector writer, WarpCorePlasmaIntermixChamber contract) throws IOException {
private void printSlotContract(ContentWriterTagWrapper<Tag,?> writer, WarpCorePlasmaIntermixChamber contract) throws IOException {
AttributesImpl atts = new AttributesImpl();
atts.addAttribute("", "path", "", "", contract.getSlot().getSlotPath());
writer.printTagStart(Tag.slot, atts);
@ -104,7 +105,7 @@ public class WarpCorePlasmaInspectorServlet extends HttpServlet {
writer.printTagEnd(Tag.slot);
}
private void printClaims(ContentWriterInspector writer, Tag tag, List<Class<?>> clazzes) throws IOException {
private void printClaims(ContentWriterTagWrapper<Tag,?> writer, Tag tag, List<Class<?>> clazzes) throws IOException {
if (clazzes.isEmpty()) {
return;
}
@ -118,7 +119,7 @@ public class WarpCorePlasmaInspectorServlet extends HttpServlet {
writer.printTagEnd(tag);
}
private void printRequireServices(ContentWriterInspector writer, List<Class<?>> clazzes) throws IOException {
private void printRequireServices(ContentWriterTagWrapper<Tag,?> writer, List<Class<?>> clazzes) throws IOException {
if (clazzes.isEmpty()) {
return;
}
@ -129,7 +130,7 @@ public class WarpCorePlasmaInspectorServlet extends HttpServlet {
writer.printTagEnd(Tag.requireServices);
}
private void printRequireSlots(ContentWriterInspector writer, List<WarpReactPlasma> slots) throws IOException {
private void printRequireSlots(ContentWriterTagWrapper<Tag,?> writer, List<WarpReactPlasma> slots) throws IOException {
if (slots.isEmpty()) {
return;
}
@ -140,12 +141,12 @@ public class WarpCorePlasmaInspectorServlet extends HttpServlet {
writer.printTagEnd(Tag.requireSlots);
}
public class ContentWriterInspector extends ContentWriterTagWrapper<Tag, ContentWriter> {
public ContentWriterInspector(ContentWriter writer) {
super(writer);
}
}
// public class ContentWriterInspector extends ContentWriterTagWrapper<Tag, ContentWriter> {
//
// public ContentWriterInspector(ContentWriter writer) {
// super(writer);
// }
// }
public enum Tag {
reactor, slot, requireSlots, requireSlot, requireServices, requireService, claimIn, claimOut, claimType;