private static void sign(XMLSignatureFactory fac, ExternalSignature externalSignature, XmlLocator locator,
DOMSignedInfo si, XMLObject xo, KeyInfo ki, String signatureId) throws DocumentException {
Document doc = locator.getDocument();
DOMSignContext domSignContext = new DOMSignContext(EmptyKey.getInstance(), doc.getDocumentElement());
List objects = null;
if (xo != null)
objects = Collections.singletonList(xo);
DOMXMLSignature signature = (DOMXMLSignature)fac.newXMLSignature(si, ki, objects, signatureId, null);
ByteArrayOutputStream byteRange = new ByteArrayOutputStream();
try {
signature.marshal(domSignContext.getParent(), domSignContext.getNextSibling(),
DOMUtils.getSignaturePrefix(domSignContext), domSignContext);
Element signElement = findElement(doc.getDocumentElement().getChildNodes(), SecurityConstants.Signature);
if (signatureId != null)
signElement.setAttributeNS(SecurityConstants.XMLNS_URI, SecurityConstants.XMLNS_XADES, SecurityConstants.XADES_132_URI);