From 4335f420dfbc73db5121e93bc6d6b3539eafb95b Mon Sep 17 00:00:00 2001 From: "michele.artini" Date: Fri, 2 Dec 2022 14:14:50 +0100 Subject: [PATCH] xml indent --- .../is/importer/OldProfilesImporter.java | 11 +++-- .../is/resources/ResourcesRestController.java | 5 ++- .../java/eu/dnetlib/is/util/XmlIndenter.java | 42 +++++++++++++++++++ .../resources/templates/simpleResources.html | 6 +-- 4 files changed, 54 insertions(+), 10 deletions(-) create mode 100644 apps/dnet-is-application/src/main/java/eu/dnetlib/is/util/XmlIndenter.java diff --git a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/importer/OldProfilesImporter.java b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/importer/OldProfilesImporter.java index dc3a1c4b..79e883b6 100644 --- a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/importer/OldProfilesImporter.java +++ b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/importer/OldProfilesImporter.java @@ -16,6 +16,7 @@ import org.springframework.stereotype.Service; import eu.dnetlib.is.resource.model.SimpleResource; import eu.dnetlib.is.resource.repository.SimpleResourceRepository; import eu.dnetlib.is.util.InformationServiceException; +import eu.dnetlib.is.util.XmlIndenter; import eu.dnetlib.is.vocabulary.model.Synonym; import eu.dnetlib.is.vocabulary.model.Vocabulary; import eu.dnetlib.is.vocabulary.model.VocabularyTerm; @@ -55,23 +56,21 @@ public class OldProfilesImporter { res.setType("cleaning_rule"); res.setName(doc.valueOf("//CLEANER_NAME")); res.setDescription(doc.valueOf("//CLEANER_DESCRIPTION")); - resContent = doc.selectSingleNode("//CLEANER_RULES").asXML(); + resContent = XmlIndenter.indent(doc.selectSingleNode("//CLEANER_RULES")); break; case "TransformationRuleDSResourceType": res.setName(doc.valueOf("//SCRIPT/TITLE")); res.setDescription(""); if (doc.selectNodes("//*[local-name() = 'stylesheet']").size() > 0) { res.setType("transformation_rule_xslt"); - resContent = doc.selectSingleNode("//*[local-name() = 'stylesheet']").asXML(); + resContent = XmlIndenter.indent(doc.selectSingleNode("//*[local-name() = 'stylesheet']")); } else { final String code = doc.valueOf("//SCRIPT/CODE").trim(); try { final Document xsltDoc = reader.read(new StringReader(code)); res.setType("transformation_rule_xslt"); - resContent = xsltDoc.asXML(); + resContent = XmlIndenter.indent(xsltDoc); } catch (final DocumentException e) { - e.printStackTrace(); - res.setType("transformation_rule_legacy"); resContent = code; } @@ -81,7 +80,7 @@ public class OldProfilesImporter { res.setType("hadoop_job_configuration"); res.setName(doc.valueOf("//HADOOP_JOB/@name")); res.setDescription(doc.valueOf("//HADOOP_JOB/DESCRIPTION")); - resContent = doc.selectSingleNode("//HADOOP_JOB").asXML(); + resContent = XmlIndenter.indent(doc.selectSingleNode("//HADOOP_JOB")); break; case "DedupConfigurationDSResourceType": res.setType("dedup_configuration"); diff --git a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/resources/ResourcesRestController.java b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/resources/ResourcesRestController.java index e8e5547a..66e2cdbd 100644 --- a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/resources/ResourcesRestController.java +++ b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/resources/ResourcesRestController.java @@ -22,6 +22,7 @@ import org.springframework.web.bind.annotation.RestController; import eu.dnetlib.common.controller.AbstractDnetController; import eu.dnetlib.is.resource.model.SimpleResource; import eu.dnetlib.is.util.InformationServiceException; +import eu.dnetlib.is.util.XmlIndenter; @RestController @RequestMapping("/api/resources") @@ -58,11 +59,13 @@ public class ResourcesRestController extends AbstractDnetController { @GetMapping("/{id}/content") public void getContent(@PathVariable final String id, final HttpServletResponse res) throws InformationServiceException { final String ctype = service.getContentType(id); - final String content = service.getContent(id); res.setCharacterEncoding(StandardCharsets.UTF_8.name()); res.setContentType(ctype); + final String content = + ctype.equals(MediaType.APPLICATION_XML_VALUE) ? XmlIndenter.indent(service.getContent(id)) : service.getContent(id); + try { IOUtils.write(content, res.getOutputStream(), StandardCharsets.UTF_8.name()); } catch (final IOException e) { diff --git a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/util/XmlIndenter.java b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/util/XmlIndenter.java new file mode 100644 index 00000000..bf266c95 --- /dev/null +++ b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/util/XmlIndenter.java @@ -0,0 +1,42 @@ +package eu.dnetlib.is.util; + +import java.io.StringWriter; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.dom4j.Document; +import org.dom4j.DocumentHelper; +import org.dom4j.Node; +import org.dom4j.io.OutputFormat; +import org.dom4j.io.XMLWriter; + +import eu.dnetlib.is.resources.SimpleResourceService; + +public class XmlIndenter { + + private static final Log log = LogFactory.getLog(SimpleResourceService.class); + + public static String indent(final String xml) { + try { + final Document doc = DocumentHelper.parseText(xml); + return indent(doc); + } catch (final Exception e) { + log.warn("Error indenting xml"); + return xml; + } + } + + public static String indent(final Node node) { + try { + final StringWriter sw = new StringWriter(); + final XMLWriter writer = new XMLWriter(sw, new OutputFormat("\t", true)); + writer.write(node); + writer.close(); + return sw.toString(); + } catch (final Exception e) { + log.warn("Error indenting xml"); + return node.asXML(); + } + } + +} diff --git a/apps/dnet-is-application/src/main/resources/templates/simpleResources.html b/apps/dnet-is-application/src/main/resources/templates/simpleResources.html index d48394b0..8a2b874c 100644 --- a/apps/dnet-is-application/src/main/resources/templates/simpleResources.html +++ b/apps/dnet-is-application/src/main/resources/templates/simpleResources.html @@ -85,7 +85,7 @@