Skip to content

Commit

Permalink
Added apache commons text dependency.
Browse files Browse the repository at this point in the history
Updated XMLUtils to use StringEscapeUtils to escape and unescape XML.
Updated XmlDecomposer to rely on the XML bytes and unscape/escape XML chars for the formalized extended attributes.
  • Loading branch information
harmen-xb committed Mar 6, 2024
1 parent a88daa3 commit 299f4e4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
6 changes: 6 additions & 0 deletions PowerDeComposer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<!-- Apache Commons Text for XML escape/unescaping. -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.11.0</version>
</dependency>
<!-- JAX-B for the config. -->
<dependency>
<groupId>jakarta.xml.bind</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,11 @@ private VTDNav formalizeExtendedAttributesText(VTDNav nv) throws Exception {
if (nv.getTokenType(extAttrsTextNodeIndex) == VTDNav.TOKEN_STARTING_TAG) {
// Get the extended attribute text.
// Replace LF with CRLF, since VTD-Nav removes the carriage returns in the file (and PowerDesigner always has CRLF).
String extendedAttributesText = nv.toString(nv.getText()).replace("\n", "\r\n");
int extendedAttributeTextIndex = nv.getText();
//String extendedAttributesText = nv.toString(extendedAttributeTextIndex).replace("\n", "\r\n");
String extendedAttributesText = new String(nv.getXML().getBytes(nv.getTokenOffset(extendedAttributeTextIndex), nv.getTokenLength(extendedAttributeTextIndex)));
// We unescape the XML characters here, so the length property in the extended attributes can be used (cause it doesn't account for escaped XML characters).
extendedAttributesText = XMLUtils.unescapeXMLChars(extendedAttributesText);
logger.fine(String.format("Found extended attributes text: %s", extendedAttributesText.replaceAll("\n", "[LF]\n").replaceAll("\r", "[CR]")));

// The extended attribute text needs to be parsed so we can create the new XML elements.
Expand All @@ -473,7 +477,8 @@ private VTDNav formalizeExtendedAttributesText(VTDNav nv) throws Exception {
if (extAttrsEnd > extendedAttributesText.length())
throw new Exception("Error while formalizing extended attributes text: The extended attribute length is longer then the contents of the string. This should never happen!");

String extExtAttrContent = extendedAttributesText.substring(extAttrStart, extAttrsEnd);
// Get the extended attribute contents and escape XML chars, so we can make valid XML.
String extExtAttrContent = XMLUtils.escapeXMLChars(extendedAttributesText.substring(extAttrStart, extAttrsEnd));

// If we are inside a extension section, so currentExtensionExtAttrEnd != -1. And we find a match where the the end index is after the end of extension section we have a problem.
// The end of an extension section should always be equal or after any child sections.
Expand Down Expand Up @@ -785,7 +790,7 @@ private Path parseAndWriteDocumentParts(VTDNav nv, Charset fileCharset, TargetFi
// Loop through the include attributes to add the min the include tag.
for (String includeAttributeName : includeAttributesWithValues.keySet()) {
// Insert the include sub element in the include tag.
includeElementStringBuffer.append(String.format(" %s=\"%s\"", includeAttributeName, XMLUtils.excapeXMLChars(includeAttributesWithValues.get(includeAttributeName))));
includeElementStringBuffer.append(String.format(" %s=\"%s\"", includeAttributeName, XMLUtils.escapeXMLChars(includeAttributesWithValues.get(includeAttributeName))));
}
includeElementStringBuffer.append(" />");

Expand Down
15 changes: 13 additions & 2 deletions PowerDeComposer/src/main/java/com/xbreeze/xml/utils/XMLUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.text.StringEscapeUtils;

import com.ximpleware.AutoPilot;
import com.ximpleware.ModifyException;
import com.ximpleware.NavException;
Expand All @@ -49,8 +51,17 @@ public class XMLUtils {
* @param input The text to escape.
* @return The escaped input.
*/
public static String excapeXMLChars(String input) {
return input.replaceAll("\\<", "&lt;").replaceAll("\\>", "&gt;");
public static String escapeXMLChars(String input) {
return StringEscapeUtils.escapeXml10(input);
}

/**
* Unescape XML characters.
* @param input The xml input with escaped XML.
* @return The xml with unescaped chars.
*/
public static String unescapeXMLChars(String input) {
return StringEscapeUtils.unescapeXml(input);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ Feature: Decompose Extended Attributes
</RootElement>
"""

@Debug
Scenario: Formalize extended attributes with XML chars
Given the config file:
"""
Expand Down

0 comments on commit 299f4e4

Please sign in to comment.