From 6690bed08a376db82a2ee0ee1e5f24033809157d Mon Sep 17 00:00:00 2001 From: Bert Frees Date: Fri, 14 Oct 2022 13:19:37 +0200 Subject: [PATCH] Small refactoring to get rid of code --- bom/pom.xml | 2 +- .../webservice/impl/JobsResource.java | 4 +- .../pipeline/webservice/impl/Poster.java | 6 +-- .../webservice/xml/AliveXmlWriter.java | 3 +- .../webservice/xml/ClientXmlWriter.java | 2 +- .../webservice/xml/ClientsXmlWriter.java | 2 +- .../webservice/xml/DatatypesXmlWriter.java | 2 +- .../pipeline/webservice/xml/ErrorWriter.java | 2 +- .../webservice/xml/JobSizeXmlWriter.java | 6 --- .../pipeline/webservice/xml/JobXmlWriter.java | 2 +- .../webservice/xml/JobsSizeXmlWriter.java | 2 +- .../webservice/xml/JobsXmlWriter.java | 2 +- .../webservice/xml/QueueXmlWriter.java | 2 +- .../webservice/xml/ScriptXmlWriter.java | 2 +- .../webservice/xml/ScriptsXmlWriter.java | 2 +- .../pipeline/webservice/xml/XmlUtils.java | 39 +------------------ 16 files changed, 18 insertions(+), 62 deletions(-) diff --git a/bom/pom.xml b/bom/pom.xml index d097a4e7..f27948ea 100644 --- a/bom/pom.xml +++ b/bom/pom.xml @@ -100,7 +100,7 @@ org.daisy.pipeline webservice - 2.6.0 + 2.6.1-SNAPSHOT org.daisy.pipeline diff --git a/webservice/src/main/java/org/daisy/pipeline/webservice/impl/JobsResource.java b/webservice/src/main/java/org/daisy/pipeline/webservice/impl/JobsResource.java index bd69d787..afd03f06 100644 --- a/webservice/src/main/java/org/daisy/pipeline/webservice/impl/JobsResource.java +++ b/webservice/src/main/java/org/daisy/pipeline/webservice/impl/JobsResource.java @@ -170,7 +170,7 @@ public Representation createResource(Representation representation) { } } if (logger.isDebugEnabled()) - logger.debug(XmlUtils.DOMToString(doc)); + logger.debug(XmlUtils.nodeToString(doc)); ValidationStatus status= Validator.validateJobRequest(doc, webservice()); @@ -195,7 +195,7 @@ public Representation createResource(Representation representation) { //store the config webservice().getStorage().getJobConfigurationStorage() - .add(job.get().getId(),XmlUtils.DOMToString(doc)); + .add(job.get().getId(), XmlUtils.nodeToString(doc)); JobXmlWriter writer = XmlWriterFactory.createXmlWriterForJob(job.get()); Document jobXml = writer.withAllMessages().withScriptDetails().getXmlDocument(); diff --git a/webservice/src/main/java/org/daisy/pipeline/webservice/impl/Poster.java b/webservice/src/main/java/org/daisy/pipeline/webservice/impl/Poster.java index 0023c7bf..b81c0acd 100644 --- a/webservice/src/main/java/org/daisy/pipeline/webservice/impl/Poster.java +++ b/webservice/src/main/java/org/daisy/pipeline/webservice/impl/Poster.java @@ -52,8 +52,6 @@ public static void postStatusUpdate(Job job, Status status,Callback callback) { } private static void postXml(Document doc, URI url, Client client) { - //System.out.println(XmlUtils.DOMToString(doc)); - URI requestUri = url; if (client != null) { requestUri = Authenticator.createUriWithCredentials(url.toString(), client); @@ -97,8 +95,8 @@ private static void postXml(Document doc, URI url, Client client) { // Send the request data. try { - logger.debug("Posting XML: "+XmlUtils.DOMToString(doc)); - output.writeBytes(XmlUtils.DOMToString(doc)); + logger.debug("Posting XML: "+XmlUtils.nodeToString(doc)); + output.writeBytes(XmlUtils.nodeToString(doc)); output.flush(); output.close(); } catch (IOException e) { diff --git a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/AliveXmlWriter.java b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/AliveXmlWriter.java index 4dfb48bd..916aff87 100644 --- a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/AliveXmlWriter.java +++ b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/AliveXmlWriter.java @@ -28,8 +28,7 @@ public Document getXmlDocument() { aliveElm.setAttribute("authentication", Properties.AUTHENTICATION.get()); aliveElm.setAttribute("version", getVersion()); if (!XmlValidator.validate(doc, XmlValidator.ALIVE_SCHEMA_URL)) { - logger.error("INVALID XML:\n" + XmlUtils.DOMToString(doc)); - logger.error(XmlUtils.DOMToString(doc)); + logger.error("INVALID XML:\n" + XmlUtils.nodeToString(doc)); } return doc; } diff --git a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/ClientXmlWriter.java b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/ClientXmlWriter.java index 783a9d75..14b02dd0 100644 --- a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/ClientXmlWriter.java +++ b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/ClientXmlWriter.java @@ -41,7 +41,7 @@ private static Document clientToXmlDocument(Client client) { // for debugging only if (!XmlValidator.validate(doc, XmlValidator.CLIENT_SCHEMA_URL)) { - logger.error("INVALID XML:\n" + XmlUtils.DOMToString(doc)); + logger.error("INVALID XML:\n" + XmlUtils.nodeToString(doc)); } return doc; } diff --git a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/ClientsXmlWriter.java b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/ClientsXmlWriter.java index d35eb5db..4fc5c6c7 100644 --- a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/ClientsXmlWriter.java +++ b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/ClientsXmlWriter.java @@ -39,7 +39,7 @@ private Document clientsToXmlDoc() { } // for debugging only if (!XmlValidator.validate(doc, XmlValidator.CLIENTS_SCHEMA_URL)) { - logger.error("INVALID XML:\n" + XmlUtils.DOMToString(doc)); + logger.error("INVALID XML:\n" + XmlUtils.nodeToString(doc)); } return doc; } diff --git a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/DatatypesXmlWriter.java b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/DatatypesXmlWriter.java index 090aee62..305fa852 100644 --- a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/DatatypesXmlWriter.java +++ b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/DatatypesXmlWriter.java @@ -42,7 +42,7 @@ private Document buildXml(){ // for debugging only if (!XmlValidator.validate(doc, XmlValidator.DATATYPES_SCHEMA_URL)) { - logger.error("INVALID XML:\n" + XmlUtils.DOMToString(doc)); + logger.error("INVALID XML:\n" + XmlUtils.nodeToString(doc)); } return doc; } diff --git a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/ErrorWriter.java b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/ErrorWriter.java index db69a170..a4cb621a 100644 --- a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/ErrorWriter.java +++ b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/ErrorWriter.java @@ -57,7 +57,7 @@ public Document getXmlDocument() { trace.setTextContent(os.toString()); } if (!XmlValidator.validate(doc, XmlValidator.ERROR_SCHEMA_URL)) { - logger.error("INVALID XML:\n" + XmlUtils.DOMToString(doc)); + logger.error("INVALID XML:\n" + XmlUtils.nodeToString(doc)); } return doc; } diff --git a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/JobSizeXmlWriter.java b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/JobSizeXmlWriter.java index 1ca93463..5c627d83 100644 --- a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/JobSizeXmlWriter.java +++ b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/JobSizeXmlWriter.java @@ -20,12 +20,6 @@ public Document getXmlDocument(){ Document doc = XmlUtils.createDom("jobSize"); Element jobSizeElm = doc.getDocumentElement(); addElementData(size, jobSizeElm); - - // for debugging only - //if (!XmlValidator.validate(doc, XmlValidator.JOBSIZE_SCHEMA_URL)) { - //logger.error("INVALID XML:\n" + XmlUtils.DOMToString(doc)); - //} - return doc; } diff --git a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/JobXmlWriter.java b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/JobXmlWriter.java index dce79b0f..7e80b0dc 100644 --- a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/JobXmlWriter.java +++ b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/JobXmlWriter.java @@ -126,7 +126,7 @@ private Document jobToXmlDocument() { // for debugging only if (!XmlValidator.validate(doc, XmlValidator.JOB_SCHEMA_URL)) { - logger.error("INVALID XML:\n" + XmlUtils.DOMToString(doc)); + logger.error("INVALID XML:\n" + XmlUtils.nodeToString(doc)); } return doc; diff --git a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/JobsSizeXmlWriter.java b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/JobsSizeXmlWriter.java index f019ba77..b677d163 100644 --- a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/JobsSizeXmlWriter.java +++ b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/JobsSizeXmlWriter.java @@ -37,7 +37,7 @@ private static Document sizesToXml(Iterable sizes) { } if (!XmlValidator.validate(doc, XmlValidator.SIZES_SCHEMA_URL)) { - logger.error("INVALID XML:\n" + XmlUtils.DOMToString(doc)); + logger.error("INVALID XML:\n" + XmlUtils.nodeToString(doc)); } return doc; diff --git a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/JobsXmlWriter.java b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/JobsXmlWriter.java index 74fd6dfd..7d9241f5 100644 --- a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/JobsXmlWriter.java +++ b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/JobsXmlWriter.java @@ -47,7 +47,7 @@ private Document jobsToXml(Iterable jobs) { // for debugging only if (!XmlValidator.validate(doc, XmlValidator.JOBS_SCHEMA_URL)) { - logger.error("INVALID XML:\n" + XmlUtils.DOMToString(doc)); + logger.error("INVALID XML:\n" + XmlUtils.nodeToString(doc)); } return doc; diff --git a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/QueueXmlWriter.java b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/QueueXmlWriter.java index cffb55fb..c746311e 100644 --- a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/QueueXmlWriter.java +++ b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/QueueXmlWriter.java @@ -32,7 +32,7 @@ public Document getXmlDocument(){ // for debugging only if (!XmlValidator.validate(doc, XmlValidator.QUEUE_SCHEMA_URL)) { - logger.error("INVALID XML:\n" + XmlUtils.DOMToString(doc)); + logger.error("INVALID XML:\n" + XmlUtils.nodeToString(doc)); } return doc; diff --git a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/ScriptXmlWriter.java b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/ScriptXmlWriter.java index 056bd806..798a23cc 100644 --- a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/ScriptXmlWriter.java +++ b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/ScriptXmlWriter.java @@ -57,7 +57,7 @@ private Document scriptToXmlDocument(XProcScript script) { // for debugging only if (!XmlValidator.validate(doc, XmlValidator.SCRIPT_SCHEMA_URL)) { - logger.error("INVALID XML:\n" + XmlUtils.DOMToString(doc)); + logger.error("INVALID XML:\n" + XmlUtils.nodeToString(doc)); } return doc; diff --git a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/ScriptsXmlWriter.java b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/ScriptsXmlWriter.java index 0da5e593..e253491a 100644 --- a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/ScriptsXmlWriter.java +++ b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/ScriptsXmlWriter.java @@ -39,7 +39,7 @@ private Document scriptsToXml(Iterable scripts) { // for debugging only if (!XmlValidator.validate(doc, XmlValidator.SCRIPTS_SCHEMA_URL)) { - logger.error("INVALID XML:\n" + XmlUtils.DOMToString(doc)); + logger.error("INVALID XML:\n" + XmlUtils.nodeToString(doc)); } return doc; diff --git a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/XmlUtils.java b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/XmlUtils.java index 6b2b7eb9..c01d8cc4 100644 --- a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/XmlUtils.java +++ b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/XmlUtils.java @@ -25,40 +25,6 @@ public class XmlUtils { public static final String NS_PIPELINE_DATA = "http://www.daisy.org/ns/pipeline/data"; private static final Logger logger = LoggerFactory.getLogger(XmlUtils.class); - /** - * DOM to string. - * - * @param doc - * the doc - * @return the string - */ - /* - * from: - * http://www.journaldev.com/71/utility-java-class-to-format-xml-document - * -to-xml-string-and-xml-to-document - */ - public static String DOMToString(Document doc) { - String xmlString = ""; - if (doc != null) { - try { - TransformerFactory transfac = TransformerFactory.newInstance(); - Transformer trans = transfac.newTransformer(); - trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); - trans.setOutputProperty(OutputKeys.INDENT, "yes"); - StringWriter sw = new StringWriter(); - StreamResult result = new StreamResult(sw); - DOMSource source = new DOMSource(doc); - trans.transform(source, result); - xmlString = sw.toString(); - } catch (Exception e) { - logger.warn("Error converting dom to string ",e); - - } - } - return xmlString; - } - - /** * Node to string. * @@ -67,9 +33,8 @@ public static String DOMToString(Document doc) { * @return the string */ public static String nodeToString(Node node) { - Document doc = node.getOwnerDocument(); - DOMImplementationLS domImplLS = (DOMImplementationLS) doc - .getImplementation(); + Document doc = node instanceof Document ? (Document)node : node.getOwnerDocument(); + DOMImplementationLS domImplLS = (DOMImplementationLS)doc.getImplementation(); LSSerializer serializer = domImplLS.createLSSerializer(); serializer.getDomConfig().setParameter("xml-declaration", false); String string = serializer.writeToString(node);