Converted warp core debug output to true XML

This commit is contained in:
Willem Cazander 2024-12-19 18:08:12 +01:00
parent 1990f9a19e
commit 597d69b0a7

View file

@ -29,8 +29,15 @@ package love.distributedrebirth.nx01.zerofungus.server.web;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.List;
import org.x4o.xml.io.XMLConstants;
import org.x4o.xml.io.sax.ext.ContentWriterTagWrapper;
import org.x4o.xml.io.sax.ext.ContentWriterXml;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@ -53,57 +60,74 @@ public class WarpCorePlasmaInspectorServlet extends HttpServlet {
WarpCoreReactor reactor = ZeroFungus.INSTANCE.getWarpCore();
List<WarpReactPlasma> slots = reactor.listChilds(null);
out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
out.println("<reactor>\n");
ContentWriterInspector writer = new ContentWriterInspector(out, response.getCharacterEncoding());
try {
writer.startDocument();
AttributesImpl atts = new AttributesImpl();
atts.addAttribute ("", "仙上主天", "", "", "𑀳𑁂𑀮𑀺𑀉𑁄𑀤𑁄𑀭𑁂𑀡𑀪𑀸𑀕");
writer.printTagStart(Tag.reactor, atts);
for (WarpReactPlasma slot : slots) {
WarpCorePlasmaIntermixChamber contract = reactor.getSlotContract(slot);
out.println("\t<slot path=\"" + slot.getSlotPath() + "\">\n");
printClaims(out, "in", contract.getSlotTypesIn());
printClaims(out, "out", contract.getSlotTypesOut());
printRequireServices(out, contract.getRequireServices());
printRequireSlots(out, contract.getRequireSlots());
out.println("</slot>\n");
atts = new AttributesImpl();
atts.addAttribute ("", "path", "", "", slot.getSlotPath());
if ("true".equalsIgnoreCase(request.getParameter("___willem"))) {
atts.addAttribute ("", "ᒡᒢᑊᒻᒻᓫᔿ", "", "", "𑀳𑁂𑀮𑀺𑀉𑁄𑀤𑁄𑀭𑁂𑀡𑀪𑀸𑀕");
}
writer.printTagStart(Tag.slot, atts);
printClaims(writer, Tag.claimIn, contract.getSlotTypesIn());
printClaims(writer, Tag.claimOut, contract.getSlotTypesOut());
printRequireServices(writer, contract.getRequireServices());
printRequireSlots(writer, contract.getRequireSlots());
writer.printTagEnd(Tag.slot);
}
writer.printTagEnd(Tag.reactor);
writer.endDocument();
} catch (SAXException e) {
throw new IllegalStateException(e);
}
out.println("\t</reactor>\n");
out.flush();
}
private void printClaims(PrintWriter out, String tag, List<Class<?>> clazzes) {
private void printClaims(ContentWriterInspector writer, Tag tag, List<Class<?>> clazzes) throws SAXException {
if (clazzes.isEmpty()) {
return;
}
out.println("\t\t<claim-"+tag+">\n");
writer.printTagStart(tag);
for (Class<?> cls : clazzes) {
out.print("\t\t\t<claim-type>");
out.print(cls);
out.println("</claim-type>\n");
writer.printTagCharacters(Tag.claimType, cls.toString());
}
out.println("\t\t</claim-"+tag+">\n");
writer.printTagEnd(tag);
}
private void printRequireServices(PrintWriter out, List<Class<?>> clazzes) {
private void printRequireServices(ContentWriterInspector writer, List<Class<?>> clazzes) throws SAXException {
if (clazzes.isEmpty()) {
return;
}
out.println("\t\t<require-services>\n");
writer.printTagStart(Tag.requireServices);
for (Class<?> cls : clazzes) {
out.print("\t\t\t<require-service>");
out.print(cls);
out.println("</require-service>\n");
writer.printTagCharacters(Tag.requireService, cls.toString());
}
out.println("\t\t</require-services>\n");
writer.printTagEnd(Tag.requireServices);
}
private void printRequireSlots(PrintWriter out, List<WarpReactPlasma> slots) {
private void printRequireSlots(ContentWriterInspector writer, List<WarpReactPlasma> slots) throws SAXException {
if (slots.isEmpty()) {
return;
}
out.println("\t\t<require-slots>\n");
writer.printTagStart(Tag.requireSlots);
for (WarpReactPlasma slot : slots) {
out.print("\t\t\t<require-slot>");
out.print(slot.getSlotPath());
out.println("</require-slot>\n");
writer.printTagCharacters(Tag.requireSlot, slot.getSlotPath());
}
out.println("\t\t</require-slots>\n");
writer.printTagEnd(Tag.requireSlots);
}
public class ContentWriterInspector extends ContentWriterTagWrapper<Tag,ContentWriterXml> {
public ContentWriterInspector(Writer out,String encoding) {
super(new ContentWriterXml(out, encoding),XMLConstants.XML_SCHEMA_NS_URI, XMLConstants.NULL_NS_URI);
}
}
public enum Tag {
reactor,slot,requireSlots,requireSlot,requireServices,requireService,claimIn,claimOut,claimType;
}
}