From 13171b39abc0801d380ad439989bcab552376e91 Mon Sep 17 00:00:00 2001 From: Willem Date: Sun, 19 Jan 2025 18:57:19 +0100 Subject: [PATCH] Added closeable interface on content writers --- .../iomf5/ScopicManifest5ContentParser.java | 5 + .../io/sax3/AbstractContentWriterHandler.java | 37 ++--- .../org/x4o/o2o/io/sax3/ContentWriter.java | 4 +- .../o2o/io/sax3/ContentWriterTagWrapper.java | 24 ++-- .../sax3/xdbx/AbstractXDBXWriterHandler.java | 36 ++--- .../org/x4o/o2o/tdoc/ApiDocGenerator.java | 127 ++++++++---------- .../web/WarpCorePlasmaInspectorServlet.java | 35 ++--- 7 files changed, 118 insertions(+), 150 deletions(-) diff --git a/nx01-warp-manifestor/src/main/java/love/distributedrebirth/nx01/warp/manifestor/scopic/iomf5/ScopicManifest5ContentParser.java b/nx01-warp-manifestor/src/main/java/love/distributedrebirth/nx01/warp/manifestor/scopic/iomf5/ScopicManifest5ContentParser.java index 850c30c..9816773 100644 --- a/nx01-warp-manifestor/src/main/java/love/distributedrebirth/nx01/warp/manifestor/scopic/iomf5/ScopicManifest5ContentParser.java +++ b/nx01-warp-manifestor/src/main/java/love/distributedrebirth/nx01/warp/manifestor/scopic/iomf5/ScopicManifest5ContentParser.java @@ -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(); diff --git a/nx01-x4o-o2o/src/main/java/org/x4o/o2o/io/sax3/AbstractContentWriterHandler.java b/nx01-x4o-o2o/src/main/java/org/x4o/o2o/io/sax3/AbstractContentWriterHandler.java index 55e374c..398aef4 100644 --- a/nx01-x4o-o2o/src/main/java/org/x4o/o2o/io/sax3/AbstractContentWriterHandler.java +++ b/nx01-x4o-o2o/src/main/java/org/x4o/o2o/io/sax3/AbstractContentWriterHandler.java @@ -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 prefixMapping = null; private List 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(15); - printedMappings = new ArrayList(15); - elements = new Stack(); - propertyConfig = new PropertyConfig(DEFAULT_PROPERTY_CONFIG,PROPERTY_CONTEXT_PREFIX); + this.out = Objects.requireNonNull(out, "Can't write on null writer."); + this.prefixMapping = new HashMap(15); + this.printedMappings = new ArrayList(15); + this.elements = new Stack(); + 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() */ diff --git a/nx01-x4o-o2o/src/main/java/org/x4o/o2o/io/sax3/ContentWriter.java b/nx01-x4o-o2o/src/main/java/org/x4o/o2o/io/sax3/ContentWriter.java index feb21ef..6437e24 100644 --- a/nx01-x4o-o2o/src/main/java/org/x4o/o2o/io/sax3/ContentWriter.java +++ b/nx01-x4o-o2o/src/main/java/org/x4o/o2o/io/sax3/ContentWriter.java @@ -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. diff --git a/nx01-x4o-o2o/src/main/java/org/x4o/o2o/io/sax3/ContentWriterTagWrapper.java b/nx01-x4o-o2o/src/main/java/org/x4o/o2o/io/sax3/ContentWriterTagWrapper.java index 12c4d53..dfb2e2e 100644 --- a/nx01-x4o-o2o/src/main/java/org/x4o/o2o/io/sax3/ContentWriterTagWrapper.java +++ b/nx01-x4o-o2o/src/main/java/org/x4o/o2o/io/sax3/ContentWriterTagWrapper.java @@ -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_WRITER extends ContentWriter> implements ContentWriterTag { +public class ContentWriterTagWrapper,TAG_WRITER extends ContentWriter> implements ContentWriterTag, Closeable { private final Attributes EMPTY_ATTRIBUTES = new AttributesImpl(); private final TAG_WRITER contentWriter; @@ -46,18 +48,14 @@ public class ContentWriterTagWrapper,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() { diff --git a/nx01-x4o-o2o/src/main/java/org/x4o/o2o/io/sax3/xdbx/AbstractXDBXWriterHandler.java b/nx01-x4o-o2o/src/main/java/org/x4o/o2o/io/sax3/xdbx/AbstractXDBXWriterHandler.java index ee54535..54d0e8d 100644 --- a/nx01-x4o-o2o/src/main/java/org/x4o/o2o/io/sax3/xdbx/AbstractXDBXWriterHandler.java +++ b/nx01-x4o-o2o/src/main/java/org/x4o/o2o/io/sax3/xdbx/AbstractXDBXWriterHandler.java @@ -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 prefixMapping = null; private List printedMappings = null; private Stack 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(15); - printedMappings = new ArrayList(15); - elements = new Stack(); - 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(15); + this.printedMappings = new ArrayList(15); + this.elements = new Stack(); + 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; diff --git a/nx01-x4o-o2o/src/main/java/org/x4o/o2o/tdoc/ApiDocGenerator.java b/nx01-x4o-o2o/src/main/java/org/x4o/o2o/tdoc/ApiDocGenerator.java index 1de345e..dde3baa 100644 --- a/nx01-x4o-o2o/src/main/java/org/x4o/o2o/tdoc/ApiDocGenerator.java +++ b/nx01-x4o-o2o/src/main/java/org/x4o/o2o/tdoc/ApiDocGenerator.java @@ -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 bodyEvent = new ApiDocWriteEvent(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 bodyEvent = new ApiDocWriteEvent(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 writer = new ContentWriterTagWrapper(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 writer = new ContentWriterTagWrapper(new ContentWriterXml(out, response.getCharacterEncoding()))) { + printReport(writer, request); + out.flush(); + } } - private void printReport(ContentWriterInspector writer, HttpServletRequest request) { + private void printReport(ContentWriterTagWrapper writer, HttpServletRequest request) { WarpCoreReactor reactor = ZeroFungus.INSTANCE.getWarpCore(); List 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 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> clazzes) throws IOException { + private void printClaims(ContentWriterTagWrapper writer, Tag tag, List> 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> clazzes) throws IOException { + private void printRequireServices(ContentWriterTagWrapper writer, List> 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 slots) throws IOException { + private void printRequireSlots(ContentWriterTagWrapper writer, List 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 { - - public ContentWriterInspector(ContentWriter writer) { - super(writer); - } - } +// public class ContentWriterInspector extends ContentWriterTagWrapper { +// +// public ContentWriterInspector(ContentWriter writer) { +// super(writer); +// } +// } public enum Tag { reactor, slot, requireSlots, requireSlot, requireServices, requireService, claimIn, claimOut, claimType;