DSSXMLUtils { public static boolean isDuplicateIdsDetected(DSSDocument doc) { try { Document dom = DomUtils.buildDOM(doc); Element root = dom.getDocumentElement(); recursiveIdBrowse(root); XPathExpression xPathExpression = DomUtils.createXPathExpression(" NodeList nodeList = (NodeList) xPathExpression.evaluate(root, XPathConstants.NODESET); for (int i = 0; i < nodeList.getLength(); i++) { Attr attr = (Attr) nodeList.item(i); if (Utils.areStringsEqualIgnoreCase(XMLDSigAttribute.ID.getAttributeName(), attr.getName())) { XPathExpression xpathAllById = DomUtils.createXPathExpression(" NodeList nodeListById = (NodeList) xpathAllById.evaluate(root, XPathConstants.NODESET); if (nodeListById.getLength() != 1) { LOG.warn("Problem detected with Id '{}', nb occurences = {}", attr.getValue(), nodeListById.getLength()); return true; } } } } catch (XPathExpressionException e) { throw new DSSException("Unable to check if duplicate ids are present", e); } return false; } private  DSSXMLUtils(); static boolean registerTransform(final String transformURI); static boolean registerCanonicalizer(final String c14nAlgorithmURI); static Node indentAndReplace(final Document document, Node node); static Node indentAndExtend(final Document document, Node newNode, Node oldNode); static Document getDocWithIndentedSignatures(final Document documentDom, String signatureId, List<String> noIndentObjectIds); static Node getIndentedNode(final Node documentDom, final Node xmlNode); static Node alignChildrenIndents(Node parentNode); static byte[] serializeNode(final Node xmlNode); static boolean canCanonicalize(final String canonicalizationMethod); static byte[] canonicalize(final String canonicalizationMethod, final byte[] toCanonicalizeBytes); static byte[] canonicalizeSubtree(String canonicalizationMethod, final Node node); static String getCanonicalizationMethod(String canonicalizationMethod); static void recursiveIdBrowse(final Element element); static String getIDIdentifier(final Node node); static String getAttribute(final Node node, final String attributeName); static void setIDIdentifier(final Element childElement); static String validateAgainstXSD(XSDAbstractUtils xsdUtils, final Source source); static boolean isOid(String policyId); static String getOidCode(String oid); static boolean isDuplicateIdsDetected(DSSDocument doc); static byte[] getNodeBytes(Node node); static byte[] getReferenceOriginalContentBytes(Reference reference); static Digest getDigestAndValue(Element element); static boolean isSignedProperties(final Reference reference, final XAdESPaths xadesPaths); static boolean isCounterSignature(final Reference reference, final XAdESPaths xadesPaths); static boolean isKeyInfoReference(final Reference reference, final Element signature); static boolean isObjectReferenceType(String referenceType); static boolean isManifestReferenceType(String referenceType); static boolean isCounterSignatureReferenceType(String referenceType); static PublicKey getKeyInfoSigningCertificatePublicKey(final Element signatureElement); static final String DEFAULT_CANONICALIZATION_METHOD; }
@Test public void isDuplicateIdsDetected() { assertTrue(DSSXMLUtils.isDuplicateIdsDetected(new FileDocument("src/test/resources/sample-duplicate-ids.xml"))); assertFalse(DSSXMLUtils.isDuplicateIdsDetected(new FileDocument("src/test/resources/sample.xml"))); }