Skip to content

Commit

Permalink
Small refactoring to get rid of code
Browse files Browse the repository at this point in the history
  • Loading branch information
bertfrees committed Jan 20, 2023
1 parent c39f3be commit 6690bed
Show file tree
Hide file tree
Showing 16 changed files with 18 additions and 62 deletions.
2 changes: 1 addition & 1 deletion bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
<dependency>
<groupId>org.daisy.pipeline</groupId>
<artifactId>webservice</artifactId>
<version>2.6.0</version>
<version>2.6.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.daisy.pipeline</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private static Document sizesToXml(Iterable<JobSize> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private Document jobsToXml(Iterable<? extends Job> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private Document scriptsToXml(Iterable<XProcScript> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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);
Expand Down

0 comments on commit 6690bed

Please sign in to comment.