xml indent

This commit is contained in:
Michele Artini 2022-12-02 14:14:50 +01:00
parent 5e86ab8723
commit 4335f420df
4 changed files with 54 additions and 10 deletions

View File

@ -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");

View File

@ -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) {

View File

@ -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();
}
}
}

View File

@ -85,7 +85,7 @@
</div>
<div class="modal fade" tabindex="-1" id="newResourceModal">
<div class="modal-dialog modal-lg">
<div class="modal-dialog modal-xl">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">New resource: <span th:text="${type.id}"></span></h4>
@ -116,7 +116,7 @@
</div>
<div class="modal fade" tabindex="-1" id="editContentModal">
<div class="modal-dialog modal-lg">
<div class="modal-dialog modal-xl">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Edit content</h4>
@ -126,7 +126,7 @@
<div class="modal-body">
<div class="form-group">
<label th:text="${type.contentType}"></label>
<textarea class="form-control small" ng-model="tmpContent" rows="25"></textarea>
<textarea class="form-control" ng-model="tmpContent" rows="25"></textarea>
</div>
</div>
<div class="modal-footer">