|
43 | 43 | "java:S1119", |
44 | 44 | "java:S3740", |
45 | 45 | "java:S3776", |
46 | | - "java:S4276", |
47 | | - "java:S5843", |
48 | | - "java:S5852", |
49 | | - "java:S5998", |
50 | | - "java:S6019", |
51 | | - "java:S6035", |
52 | | - "java:S6395" |
| 46 | + "java:S4276" |
53 | 47 | }) |
54 | 48 | public final class Xml { |
55 | 49 | private Xml() {} |
@@ -84,10 +78,6 @@ private Xml() {} |
84 | 78 | private static final String DOCTYPE_TEXT = "!DOCTYPE"; |
85 | 79 | private static final String ROOT = "root"; |
86 | 80 | private static final String DOCTYPE_HEADER = "<" + DOCTYPE_TEXT + " "; |
87 | | - private static final java.util.regex.Pattern ATTRS = |
88 | | - java.util.regex.Pattern.compile( |
89 | | - "((?:(?!\\s|=).)*)\\s*?=\\s*?[\"']?((?:(?<=\")(?:(?<=\\\\)\"|[^\"])*|(?<=')" |
90 | | - + "(?:(?<=\\\\)'|[^'])*)|(?:(?!\"|')(?:(?!\\/>|>|\\s).)+))"); |
91 | 81 | private static final Map<String, String> XML_UNESCAPE = new HashMap<>(); |
92 | 82 | private static final org.w3c.dom.Document DOCUMENT = Document.createDocument(); |
93 | 83 |
|
@@ -1490,19 +1480,18 @@ private static Object addElement( |
1490 | 1480 | final FromType fromType) { |
1491 | 1481 | final Map<String, Object> attrMapLocal = new LinkedHashMap<>(); |
1492 | 1482 | if (currentNode.getAttributes().getLength() > 0) { |
1493 | | - final java.util.regex.Matcher matcher = |
1494 | | - ATTRS.matcher(getAttributes(sourceIndex[0], source)); |
1495 | | - while (matcher.find()) { |
1496 | | - if (matcher.group(1).startsWith("xmlns:")) { |
1497 | | - namespaces.add(matcher.group(1).substring(6)); |
| 1483 | + final Map<String, String> attributes = |
| 1484 | + parseAttributes(getAttributes(sourceIndex[0], source)); |
| 1485 | + for (Map.Entry<String, String> attribute : attributes.entrySet()) { |
| 1486 | + if (attribute.getKey().startsWith("xmlns:")) { |
| 1487 | + namespaces.add(attribute.getKey().substring(6)); |
1498 | 1488 | } |
1499 | 1489 | } |
1500 | | - matcher.reset(); |
1501 | | - while (matcher.find()) { |
| 1490 | + for (Map.Entry<String, String> attribute : attributes.entrySet()) { |
1502 | 1491 | addNodeValue( |
1503 | 1492 | attrMapLocal, |
1504 | | - '-' + matcher.group(1), |
1505 | | - matcher.group(2), |
| 1493 | + '-' + attribute.getKey(), |
| 1494 | + attribute.getValue(), |
1506 | 1495 | elementMapper, |
1507 | 1496 | nodeMapper, |
1508 | 1497 | uniqueIds, |
@@ -1531,6 +1520,36 @@ private static Object addElement( |
1531 | 1520 | fromType); |
1532 | 1521 | } |
1533 | 1522 |
|
| 1523 | + static Map<String, String> parseAttributes(final String source) { |
| 1524 | + final Map<String, String> result = new LinkedHashMap<>(); |
| 1525 | + final StringBuilder key = new StringBuilder(); |
| 1526 | + final StringBuilder value = new StringBuilder(); |
| 1527 | + boolean quoteFound = false; |
| 1528 | + boolean equalFound = false; |
| 1529 | + for (int index = 0; index < source.length(); index += 1) { |
| 1530 | + if (source.charAt(index) == '=') { |
| 1531 | + equalFound = !equalFound; |
| 1532 | + continue; |
| 1533 | + } |
| 1534 | + if (source.charAt(index) == '"') { |
| 1535 | + if (quoteFound && equalFound) { |
| 1536 | + result.put(key.toString(), value.toString()); |
| 1537 | + key.setLength(0); |
| 1538 | + value.setLength(0); |
| 1539 | + equalFound = false; |
| 1540 | + } |
| 1541 | + quoteFound = !quoteFound; |
| 1542 | + } else if (quoteFound || source.charAt(index) == ' ') { |
| 1543 | + if (quoteFound) { |
| 1544 | + value.append(source.charAt(index)); |
| 1545 | + } |
| 1546 | + } else { |
| 1547 | + key.append(source.charAt(index)); |
| 1548 | + } |
| 1549 | + } |
| 1550 | + return result; |
| 1551 | + } |
| 1552 | + |
1534 | 1553 | static String getAttributes(final int sourceIndex, final String source) { |
1535 | 1554 | boolean scanQuote = false; |
1536 | 1555 | for (int index = sourceIndex; index < source.length(); index += 1) { |
@@ -1745,9 +1764,9 @@ private static Map<String, String> getHeaderAttributes(final String xml) { |
1745 | 1764 | xml.substring( |
1746 | 1765 | XML_HEADER.length(), |
1747 | 1766 | Math.max(XML_HEADER.length(), xml.indexOf("?>", XML_HEADER.length()))); |
1748 | | - final java.util.regex.Matcher matcher = ATTRS.matcher(xmlLocal); |
1749 | | - while (matcher.find()) { |
1750 | | - result.put(matcher.group(1), matcher.group(2)); |
| 1767 | + final Map<String, String> attributes = parseAttributes(xmlLocal); |
| 1768 | + for (Map.Entry<String, String> attribute : attributes.entrySet()) { |
| 1769 | + result.put(attribute.getKey(), attribute.getValue()); |
1751 | 1770 | } |
1752 | 1771 | } |
1753 | 1772 | return result; |
|
0 commit comments