Skip to content

Commit

Permalink
fixed - impl new XMLUtil class
Browse files Browse the repository at this point in the history
Issue #212
  • Loading branch information
rsoika committed Mar 10, 2023
1 parent 38d55b0 commit 0921741
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.openbpmn.bpmn.exceptions.BPMNInvalidTypeException;
import org.openbpmn.bpmn.exceptions.BPMNMissingElementException;
import org.openbpmn.bpmn.exceptions.BPMNModelException;
import org.openbpmn.bpmn.util.BPMNXMLUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
Expand Down Expand Up @@ -1687,9 +1688,13 @@ private void writeXml(Document doc, OutputStream output) throws TransformerExcep
transformer.transform(source, dest);
String xmlString = w.toString();
// No indentation (whitespace) for elements with a CDATA section.
// See.
// See
// https://stackoverflow.com/questions/55853220/handling-change-in-newlines-by-xml-transformation-for-cdata-from-java-8-to-java/75568933
xmlString = xmlString.replaceAll(">\\s*(<\\!\\[CDATA\\[(.|\\n|\\r\\n)*?]\\]>)\\s*</", ">$1</");
// xmlString =
// xmlString.replaceAll(">\\s*+(<\\!\\[CDATA\\[(.|\\n|\\r\\n)*?]\\]>)\\s*</",
// ">$1</");
xmlString = BPMNXMLUtil.cleanCDATAWhiteSpace(xmlString);

// write output
try {
output.write(xmlString.getBytes(Charset.forName("UTF-8")));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/********************************************************************************
* Copyright (c) 2022 Imixs Software Solutions GmbH and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
package org.openbpmn.bpmn.util;

/**
* The BPMNXMLUtil provides helper methods for xml strings
*
* @author rsoika
*
*/
public class BPMNXMLUtil {

/**
* This method removes whitespace around CDATA Tags like:
*
* <imixs:value>
* <![CDATA[ some data ]]>
* </imixs:value>
*
* results in:
*
* <imixs:value><![CDATA[ some data ]]></imixs:value>
*
* We can not use regex (replaceAll) here
*
* xmlString =
* xmlString.replaceAll(">\\s*+(<\\!\\[CDATA\\[(.|\\n|\\r\\n)*?]\\]>)\\s*</",
* ">$1</");
*
*
* because of a known bug with large XML Strings. See
* https://stackoverflow.com/questions/7509905/java-lang-stackoverflowerror-while-using-a-regex-to-parse-big-strings
*
* For this reason we parse and optimize the xml manually here.
*
* @param xml
* @return
*/
public static String cleanCDATAWhiteSpace(String xml) {

boolean completed = false;
int lastPos = 0;
while (!completed) {
/**
* search for
* CDATA start '<![CDATA['
*/
int pos = xml.indexOf("<![CDATA[", lastPos);
if (pos == -1) {
// no more CDATA !
completed = true;
break;
}
// find upper '>'
int backwardTagPos = xml.lastIndexOf(">", pos);
// do we have whitespace?
if (pos > backwardTagPos - 1) {
xml = xml.substring(0, backwardTagPos + 1) + xml.substring(pos);
}
lastPos = pos + 1;
/**
* next search for
* CDATA end ']]>'
*/
pos = xml.indexOf("]]>", lastPos);
if (pos == -1) {
// wrong CDATA!
System.out.println("WRONG CDATA ELEMENT - unexpected end at: " + lastPos);
completed = true;
break;
}
pos = pos + 3;
// find upper '>'
int forwardTagPos = xml.indexOf("<", pos);
// do we have whitespace?
if (forwardTagPos > pos) {
xml = xml.substring(0, pos) + xml.substring(forwardTagPos);
}
lastPos = pos + 1;
}

return xml;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.openbpmn.output;

import org.junit.jupiter.api.Test;
import org.openbpmn.bpmn.util.BPMNXMLUtil;

/**
* Test Class to test clean up of cdata whitespace
*/
public class TestCleanCDATA {

@Test
public void testSimple() {

String xml = "<imixs:value> \n <![CDATA[ some data ]]>\n </imixs:value>";

System.out.println(xml);
String result = BPMNXMLUtil.cleanCDATAWhiteSpace(xml);

System.out.println(result);

}
}

0 comments on commit 0921741

Please sign in to comment.