Added closeable interface on content writers
This commit is contained in:
parent
d77f9dfc57
commit
13171b39ab
|
@ -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();
|
||||
|
|
|
@ -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()
|
||||
*/
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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");
|
||||
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");
|
||||
}
|
||||
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;
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
contentWriter.close();
|
||||
}
|
||||
|
||||
public TAG_WRITER getContentWriterWrapped() {
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -124,8 +124,7 @@ public class ApiDocGenerator implements ApiDocContentPrinter {
|
|||
pathPrefix += "../";
|
||||
}
|
||||
File outputFile = createOutputPathFile(basePath,path.toArray(new String[]{}));
|
||||
ApiDocContentWriter writer = createContentWriter(outputFile);
|
||||
|
||||
try (ApiDocContentWriter writer = createContentWriter(outputFile)) {
|
||||
doc.getNodeData().clearGroupTypeLinks();
|
||||
doc.getNodeData().setNavSelected(concept.getId());
|
||||
configNodeData(pathPrefix,outputFile);
|
||||
|
@ -178,7 +177,7 @@ public class ApiDocGenerator implements ApiDocContentPrinter {
|
|||
writer.docPageClassEnd();
|
||||
docNavBar(writer,false,concept,node);
|
||||
writer.docHtmlEnd(doc.getDocCopyright(),doc.getDocStatsJS());
|
||||
writer.getContentWriterWrapped().closeWriterSafe();
|
||||
}
|
||||
|
||||
// Writer other files
|
||||
writeAllFrameNavNode(node);
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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()));
|
||||
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()));
|
||||
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;
|
||||
|
|
Loading…
Reference in a new issue