Skip to content

Commit

Permalink
Export XML fix JIRA 716 & 719
Browse files Browse the repository at this point in the history
  • Loading branch information
lernermh committed Jun 11, 2021
1 parent ff99a7b commit bb2916b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 96 deletions.
7 changes: 7 additions & 0 deletions software/cananolab-webapp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,13 @@
<version>1.1</version>
</dependency>

<!-- For JSON to XML transform -->
<dependency>
<groupId>com.github.javadev</groupId>
<artifactId>underscore</artifactId>
<version>1.67</version>
</dependency>

</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gov.nih.nci.cananolab.restful;


import com.github.underscore.lodash.U;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParser;
Expand Down Expand Up @@ -1249,112 +1250,23 @@ private String buildSampleJson( HttpServletRequest httpRequest, HttpServletRespo


/**
* Convert (formatted) JSON to XML
* Convert JSON to XML
*
* @param jsonText
* @return Formatted XML
*/
private String jsonToXml( String jsonText ) {
private String jsonToXml( String jsonText ) {
try {
jsonText = jsonText.replaceAll( "\\\\u00[0-1][1-9a-fA-F]", " " );
JSONObject jso = new JSONObject( cleanJson(jsonText) );
String xml = XML.toString(jso, "caNanoLabXml");
StreamSource source = new StreamSource(new StringReader(xml));
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty( OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(source, result);
return xmlFormat( writer.toString());
String xml = U.jsonToXml(jsonText);
// System.out.println("gov.nih.nci.cananolab.restful.SampleServices.jsonToXml: " + xml );
return xml;
}
catch (Exception e)
{
System.err.println( "Error converting JSON to XML: " + cleanJson(jsonText) );
// e.printStackTrace();
return null;
}
}


/**
* Some JSON elements do not translate cleanly to XML
*
* @param json
* @return modified JSON String
*
* @TODO Character reference "&#x2" is an invalid XML character.
*/
private String cleanJson( String json){
StringBuilder cleanData = new StringBuilder( );
try
{
BufferedReader bufReader = new BufferedReader(new StringReader(json));
String line;

while( (line=bufReader.readLine()) != null )
{
if(line.matches("^\\s*\\\"[^:]+\\s.*:.*\\[")){
String pattern = "^(.*):.*";
String temp = line.replaceAll(pattern, "$1");
pattern = "^(\\s*)(.*)";
String data = temp.replaceAll( pattern, "$2");
temp = temp.replaceAll( pattern, "$1" + data.replaceAll( " ", "_" ));
line = temp + ": [";
}
cleanData.append( line );
cleanData.append( "\n" );
}
}
catch( IOException e )
{
System.err.println( "Error converting JSON to XML");
e.printStackTrace();
return null;
}
return cleanData.toString();
}


/**
* Format (indent etc.) XML
*
* @param xml
* @return formatted xml
*/
private String xmlFormat(String xml) {
try {
// Turn xml string into a document
Document document = DocumentBuilderFactory.newInstance()
.newDocumentBuilder()
.parse(new InputSource(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8))));

// Remove whitespaces outside tags
document.normalize();
XPath xPath = XPathFactory.newInstance().newXPath();
NodeList nodeList = (NodeList) xPath.evaluate("//text()[normalize-space()='']",
document,
XPathConstants.NODESET);

for (int i = 0; i < nodeList.getLength(); ++i) {
Node node = nodeList.item(i);
node.getParentNode().removeChild(node);
}

// Setup transformer options
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformerFactory.setAttribute("indent-number", 4);
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");

// Return the formatted xml string
StringWriter stringWriter = new StringWriter();
transformer.transform(new DOMSource(document), new StreamResult(stringWriter));
return stringWriter.toString();
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
}

0 comments on commit bb2916b

Please sign in to comment.