Skip to content
Snippets Groups Projects
Commit f7c31a7c authored by Piotr Gawron's avatar Piotr Gawron
Browse files

method that allows to lowercase xml names created

parent 1fbb3760
No related branches found
No related tags found
1 merge request!717Resolve "process vcard data that can appear in model annotations"
......@@ -2,6 +2,8 @@ package lcsb.mapviewer.common;
import java.awt.Color;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
......@@ -14,13 +16,18 @@ import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.commons.text.StringEscapeUtils;
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
......@@ -31,6 +38,7 @@ import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import lcsb.mapviewer.common.exception.InvalidArgumentException;
import lcsb.mapviewer.common.exception.InvalidStateException;
import lcsb.mapviewer.common.exception.InvalidXmlSchemaException;
/**
......@@ -78,7 +86,8 @@ final public class XmlParser {
}
/**
* private constructor. This class has no state and contains only static utility methods.
* private constructor. This class has no state and contains only static utility
* methods.
*/
private XmlParser() {
}
......@@ -222,7 +231,8 @@ final public class XmlParser {
* @throws InvalidXmlSchemaException
* thrown when there is a problem with xml
*/
public static Document getXmlDocumentFromString(final String text, boolean validate) throws InvalidXmlSchemaException {
public static Document getXmlDocumentFromString(final String text, boolean validate)
throws InvalidXmlSchemaException {
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(text));
try {
......@@ -398,4 +408,29 @@ final public class XmlParser {
}
return result;
}
public static String lowercaseXmlNames(String inputXml) {
try {
TransformerFactory factory = TransformerFactory.newInstance();
// Use the factory to create a template containing the xsl file
Templates template = factory.newTemplates(new StreamSource(ClassLoader.getSystemResourceAsStream("to_lowercase.xsl")));
// Use the template to create a transformer
Transformer xformer = template.newTransformer();
ByteArrayOutputStream output = new ByteArrayOutputStream();
// Prepare the input and output files
Source source = new StreamSource(new ByteArrayInputStream(inputXml.getBytes()));
Result result = new StreamResult(output);
// Apply the xsl file to the source file and write the result
// to the output file
xformer.transform(source, result);
return output.toString("UTF-8");
} catch (Exception e ) {
throw new InvalidStateException("Problem with translating input xml", e);
}
}
}
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable
name="lcase">abcdefghijklmnopqrstuvwxyz</xsl:variable>
<xsl:variable
name="ucase">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{translate(local-name(),$ucase,$lcase)}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
......@@ -149,7 +149,7 @@ public class XmlParserTest {
}
boolean threadSucceded;
@Test
public void testConcurrencyParse() throws Exception {
try {
......@@ -182,7 +182,6 @@ public class XmlParserTest {
}
@Test
public void testInvalidNodeToString() throws Exception {
try {
......@@ -451,4 +450,19 @@ public class XmlParserTest {
throw e;
}
}
@Test
public void testLowercaseXmlNames() throws Exception {
try {
String inputXml = "<node><ASD>Test</ASD></node>";
String outputXml = XmlParser.lowercaseXmlNames(inputXml);
assertTrue(outputXml.indexOf("asd") >= 0);
assertTrue(outputXml.indexOf("Test") >= 0);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment