下面列出了org.w3c.dom.Element#setIdAttributeNS ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
*
* @see nu.validator.htmlparser.impl.TreeBuilder#createElement(java.lang.String,
* java.lang.String, nu.validator.htmlparser.impl.HtmlAttributes)
*/
@Override protected Element createElement(String ns, String name,
HtmlAttributes attributes) throws SAXException {
try {
Element rv = document.createElementNS(ns, name);
for (int i = 0; i < attributes.getLength(); i++) {
rv.setAttributeNS(attributes.getURI(i),
attributes.getLocalName(i), attributes.getValue(i));
if (attributes.getType(i) == "ID") {
rv.setIdAttributeNS(null,
attributes.getLocalName(i), true);
}
}
return rv;
} catch (DOMException e) {
fatal(e);
throw new RuntimeException("Unreachable");
}
}
/** {@inheritDoc} */
protected void marshallAttributes(XMLObject samlObject, Element domElement) throws MarshallingException {
Assertion assertion = (Assertion) samlObject;
if (assertion.getVersion() != null) {
domElement.setAttributeNS(null, Assertion.VERSION_ATTRIB_NAME, assertion.getVersion().toString());
}
if (assertion.getIssueInstant() != null) {
String issueInstantStr = Configuration.getSAMLDateFormatter().print(assertion.getIssueInstant());
domElement.setAttributeNS(null, Assertion.ISSUE_INSTANT_ATTRIB_NAME, issueInstantStr);
}
if (assertion.getID() != null) {
domElement.setAttributeNS(null, Assertion.ID_ATTRIB_NAME, assertion.getID());
domElement.setIdAttributeNS(null, Assertion.ID_ATTRIB_NAME, true);
}
}
private XMLSignature prepareEnvelopingSignature(Document doc,
String id,
String referenceId,
String sigAlgo,
String digestAlgo) throws Exception {
Element docEl = doc.getDocumentElement();
Document newDoc = DOMUtils.createDocument();
doc.removeChild(docEl);
newDoc.adoptNode(docEl);
Element object = newDoc.createElementNS(Constants.SignatureSpecNS, "ds:Object");
object.appendChild(docEl);
docEl.setAttributeNS(null, "Id", id);
docEl.setIdAttributeNS(null, "Id", true);
XMLSignature sig = new XMLSignature(newDoc, "", sigAlgo);
newDoc.appendChild(sig.getElement());
sig.getElement().appendChild(object);
Transforms transforms = new Transforms(newDoc);
transforms.addTransform(Transforms.TRANSFORM_C14N_EXCL_OMIT_COMMENTS);
sig.addDocument(referenceId, transforms, digestAlgo);
return sig;
}
/** {@inheritDoc} */
protected void marshallAttributes(XMLObject xmlObject, Element domElement) throws MarshallingException {
EncryptedType et = (EncryptedType) xmlObject;
if (et.getID() != null) {
domElement.setAttributeNS(null, EncryptedType.ID_ATTRIB_NAME, et.getID());
domElement.setIdAttributeNS(null, EncryptedType.ID_ATTRIB_NAME, true);
}
if (et.getType() != null) {
domElement.setAttributeNS(null, EncryptedType.TYPE_ATTRIB_NAME, et.getType());
}
if (et.getMimeType() != null) {
domElement.setAttributeNS(null, EncryptedType.MIMETYPE_ATTRIB_NAME, et.getMimeType());
}
if (et.getEncoding() != null) {
domElement.setAttributeNS(null, EncryptedType.ENCODING_ATTRIB_NAME, et.getEncoding());
}
}
/** {@inheritDoc} */
protected void marshallAttributes(XMLObject samlElement, Element domElement) throws MarshallingException {
AffiliationDescriptor descriptor = (AffiliationDescriptor) samlElement;
// Set affiliationOwnerID
if (descriptor.getOwnerID() != null) {
domElement.setAttributeNS(null, AffiliationDescriptor.OWNER_ID_ATTRIB_NAME, descriptor.getOwnerID());
}
// Set ID
if (descriptor.getID() != null) {
domElement.setAttributeNS(null, AffiliationDescriptor.ID_ATTRIB_NAME, descriptor.getID());
domElement.setIdAttributeNS(null, AffiliationDescriptor.ID_ATTRIB_NAME, true);
}
// Set the validUntil attribute
if (descriptor.getValidUntil() != null) {
log.debug("Writting validUntil attribute to AffiliationDescriptor DOM element");
String validUntilStr = Configuration.getSAMLDateFormatter().print(descriptor.getValidUntil());
domElement.setAttributeNS(null, TimeBoundSAMLObject.VALID_UNTIL_ATTRIB_NAME, validUntilStr);
}
// Set the cacheDuration attribute
if (descriptor.getCacheDuration() != null) {
log.debug("Writting cacheDuration attribute to AffiliationDescriptor DOM element");
String cacheDuration = XMLHelper.longToDuration(descriptor.getCacheDuration());
domElement.setAttributeNS(null, CacheableSAMLObject.CACHE_DURATION_ATTRIB_NAME, cacheDuration);
}
Attr attribute;
for (Entry<QName, String> entry : descriptor.getUnknownAttributes().entrySet()) {
attribute = XMLHelper.constructAttribute(domElement.getOwnerDocument(), entry.getKey());
attribute.setValue(entry.getValue());
domElement.setAttributeNodeNS(attribute);
if (Configuration.isIDAttribute(entry.getKey())
|| descriptor.getUnknownAttributes().isIDAttribute(entry.getKey())) {
attribute.getOwnerElement().setIdAttributeNode(attribute, true);
}
}
}
/** {@inheritDoc} */
protected void marshallAttributes(XMLObject xmlObject, Element domElement) throws MarshallingException {
KeyInfoType keyInfo = (KeyInfoType) xmlObject;
if (keyInfo.getID() != null) {
domElement.setAttributeNS(null, KeyInfoType.ID_ATTRIB_NAME, keyInfo.getID());
domElement.setIdAttributeNS(null, KeyInfoType.ID_ATTRIB_NAME, true);
}
}
/** {@inheritDoc} */
protected void marshallAttributes(XMLObject xmlObject, Element domElement) throws MarshallingException {
DEREncodedKeyValue der = (DEREncodedKeyValue) xmlObject;
if (der.getID() != null) {
domElement.setAttributeNS(null, DEREncodedKeyValue.ID_ATTRIB_NAME, der.getID());
domElement.setIdAttributeNS(null, DEREncodedKeyValue.ID_ATTRIB_NAME, true);
}
}
/** {@inheritDoc} */
public XMLObject unmarshall(Element domElement) throws UnmarshallingException {
// After regular unmarshalling, check the minor version and set ID-ness if not SAML 1.0
ResponseAbstractType response = (ResponseAbstractType) super.unmarshall(domElement);
if (response.getMinorVersion() != 0 && !DatatypeHelper.isEmpty(response.getID())) {
domElement.setIdAttributeNS(null, ResponseAbstractType.ID_ATTRIB_NAME, true);
}
return response;
}
/** {@inheritDoc} */
public XMLObject unmarshall(Element domElement) throws UnmarshallingException {
// After regular unmarshalling, check the minor version and set ID-ness if not SAML 1.0
Assertion assertion = (Assertion) super.unmarshall(domElement);
if (assertion.getMinorVersion() != 0 && !DatatypeHelper.isEmpty(assertion.getID())) {
domElement.setIdAttributeNS(null, Assertion.ID_ATTRIB_NAME, true);
}
return assertion;
}
/** {@inheritDoc} */
public XMLObject unmarshall(Element domElement) throws UnmarshallingException {
// After regular unmarshalling, check the minor version and set ID-ness if not SAML 1.0
RequestAbstractType request = (RequestAbstractType) super.unmarshall(domElement);
if (request.getMinorVersion() != 0 && !DatatypeHelper.isEmpty(request.getID())) {
domElement.setIdAttributeNS(null, RequestAbstractType.ID_ATTRIB_NAME, true);
}
return request;
}
public void startElement(String namespace, String localName, String qName,
Attributes attrs)
{
appendTextNode();
if (needToSetDocumentInfo) {
setDocumentInfo();
needToSetDocumentInfo = false;
}
final Element tmp = (Element)_document.createElementNS(namespace, qName);
// Add namespace declarations first
if (_namespaceDecls != null) {
final int nDecls = _namespaceDecls.size();
for (int i = 0; i < nDecls; i++) {
final String prefix = (String) _namespaceDecls.elementAt(i++);
if (prefix == null || prefix.equals(EMPTYSTRING)) {
tmp.setAttributeNS(XMLNS_URI, XMLNS_PREFIX,
(String) _namespaceDecls.elementAt(i));
}
else {
tmp.setAttributeNS(XMLNS_URI, XMLNS_STRING + prefix,
(String) _namespaceDecls.elementAt(i));
}
}
_namespaceDecls.clear();
}
// Add attributes to element
/* final int nattrs = attrs.getLength();
for (int i = 0; i < nattrs; i++) {
if (attrs.getLocalName(i) == null) {
tmp.setAttribute(attrs.getQName(i), attrs.getValue(i));
}
else {
tmp.setAttributeNS(attrs.getURI(i), attrs.getQName(i),
attrs.getValue(i));
}
} */
// Add attributes to element
final int nattrs = attrs.getLength();
for (int i = 0; i < nattrs; i++) {
// checking if Namespace processing is being done
String attQName = attrs.getQName(i);
String attURI = attrs.getURI(i);
if (attrs.getLocalName(i).equals("")) {
tmp.setAttribute(attQName, attrs.getValue(i));
if (attrs.getType(i).equals("ID")) {
tmp.setIdAttribute(attQName, true);
}
} else {
tmp.setAttributeNS(attURI, attQName, attrs.getValue(i));
if (attrs.getType(i).equals("ID")) {
tmp.setIdAttributeNS(attURI, attrs.getLocalName(i), true);
}
}
}
// Append this new node onto current stack node
Node last = (Node)_nodeStk.peek();
// If the SAX2DOM is created with a non-null next sibling node,
// insert the result nodes before the next sibling under the root.
if (last == _root && _nextSibling != null)
last.insertBefore(tmp, _nextSibling);
else
last.appendChild(tmp);
// Push this node onto stack
_nodeStk.push(tmp);
_lastSibling = null;
}
public void startElement(String namespace, String localName, String qName,
Attributes attrs)
{
appendTextNode();
if (needToSetDocumentInfo) {
setDocumentInfo();
needToSetDocumentInfo = false;
}
final Element tmp = (Element)_document.createElementNS(namespace, qName);
// Add namespace declarations first
if (_namespaceDecls != null) {
final int nDecls = _namespaceDecls.size();
for (int i = 0; i < nDecls; i++) {
final String prefix = (String) _namespaceDecls.elementAt(i++);
if (prefix == null || prefix.equals(EMPTYSTRING)) {
tmp.setAttributeNS(XMLNS_URI, XMLNS_PREFIX,
(String) _namespaceDecls.elementAt(i));
}
else {
tmp.setAttributeNS(XMLNS_URI, XMLNS_STRING + prefix,
(String) _namespaceDecls.elementAt(i));
}
}
_namespaceDecls.clear();
}
// Add attributes to element
/* final int nattrs = attrs.getLength();
for (int i = 0; i < nattrs; i++) {
if (attrs.getLocalName(i) == null) {
tmp.setAttribute(attrs.getQName(i), attrs.getValue(i));
}
else {
tmp.setAttributeNS(attrs.getURI(i), attrs.getQName(i),
attrs.getValue(i));
}
} */
// Add attributes to element
final int nattrs = attrs.getLength();
for (int i = 0; i < nattrs; i++) {
// checking if Namespace processing is being done
String attQName = attrs.getQName(i);
String attURI = attrs.getURI(i);
if (attrs.getLocalName(i).equals("")) {
tmp.setAttribute(attQName, attrs.getValue(i));
if (attrs.getType(i).equals("ID")) {
tmp.setIdAttribute(attQName, true);
}
} else {
tmp.setAttributeNS(attURI, attQName, attrs.getValue(i));
if (attrs.getType(i).equals("ID")) {
tmp.setIdAttributeNS(attURI, attrs.getLocalName(i), true);
}
}
}
// Append this new node onto current stack node
Node last = (Node)_nodeStk.peek();
// If the SAX2DOM is created with a non-null next sibling node,
// insert the result nodes before the next sibling under the root.
if (last == _root && _nextSibling != null)
last.insertBefore(tmp, _nextSibling);
else
last.appendChild(tmp);
// Push this node onto stack
_nodeStk.push(tmp);
_lastSibling = null;
}
public void startElement(String namespace, String localName, String qName,
Attributes attrs)
{
appendTextNode();
if (needToSetDocumentInfo) {
setDocumentInfo();
needToSetDocumentInfo = false;
}
final Element tmp = (Element)_document.createElementNS(namespace, qName);
// Add namespace declarations first
if (_namespaceDecls != null) {
final int nDecls = _namespaceDecls.size();
for (int i = 0; i < nDecls; i++) {
final String prefix = (String) _namespaceDecls.elementAt(i++);
if (prefix == null || prefix.equals(EMPTYSTRING)) {
tmp.setAttributeNS(XMLNS_URI, XMLNS_PREFIX,
(String) _namespaceDecls.elementAt(i));
}
else {
tmp.setAttributeNS(XMLNS_URI, XMLNS_STRING + prefix,
(String) _namespaceDecls.elementAt(i));
}
}
_namespaceDecls.clear();
}
// Add attributes to element
/* final int nattrs = attrs.getLength();
for (int i = 0; i < nattrs; i++) {
if (attrs.getLocalName(i) == null) {
tmp.setAttribute(attrs.getQName(i), attrs.getValue(i));
}
else {
tmp.setAttributeNS(attrs.getURI(i), attrs.getQName(i),
attrs.getValue(i));
}
} */
// Add attributes to element
final int nattrs = attrs.getLength();
for (int i = 0; i < nattrs; i++) {
// checking if Namespace processing is being done
String attQName = attrs.getQName(i);
String attURI = attrs.getURI(i);
if (attrs.getLocalName(i).equals("")) {
tmp.setAttribute(attQName, attrs.getValue(i));
if (attrs.getType(i).equals("ID")) {
tmp.setIdAttribute(attQName, true);
}
} else {
tmp.setAttributeNS(attURI, attQName, attrs.getValue(i));
if (attrs.getType(i).equals("ID")) {
tmp.setIdAttributeNS(attURI, attrs.getLocalName(i), true);
}
}
}
// Append this new node onto current stack node
Node last = (Node)_nodeStk.peek();
// If the SAX2DOM is created with a non-null next sibling node,
// insert the result nodes before the next sibling under the root.
if (last == _root && _nextSibling != null)
last.insertBefore(tmp, _nextSibling);
else
last.appendChild(tmp);
// Push this node onto stack
_nodeStk.push(tmp);
_lastSibling = null;
}
public void startElement(String namespace, String localName, String qName,
Attributes attrs)
{
appendTextNode();
if (needToSetDocumentInfo) {
setDocumentInfo();
needToSetDocumentInfo = false;
}
final Element tmp = (Element)_document.createElementNS(namespace, qName);
// Add namespace declarations first
if (_namespaceDecls != null) {
final int nDecls = _namespaceDecls.size();
for (int i = 0; i < nDecls; i++) {
final String prefix = (String) _namespaceDecls.elementAt(i++);
if (prefix == null || prefix.equals(EMPTYSTRING)) {
tmp.setAttributeNS(XMLNS_URI, XMLNS_PREFIX,
(String) _namespaceDecls.elementAt(i));
}
else {
tmp.setAttributeNS(XMLNS_URI, XMLNS_STRING + prefix,
(String) _namespaceDecls.elementAt(i));
}
}
_namespaceDecls.clear();
}
// Add attributes to element
/* final int nattrs = attrs.getLength();
for (int i = 0; i < nattrs; i++) {
if (attrs.getLocalName(i) == null) {
tmp.setAttribute(attrs.getQName(i), attrs.getValue(i));
}
else {
tmp.setAttributeNS(attrs.getURI(i), attrs.getQName(i),
attrs.getValue(i));
}
} */
// Add attributes to element
final int nattrs = attrs.getLength();
for (int i = 0; i < nattrs; i++) {
// checking if Namespace processing is being done
String attQName = attrs.getQName(i);
String attURI = attrs.getURI(i);
if (attrs.getLocalName(i).equals("")) {
tmp.setAttribute(attQName, attrs.getValue(i));
if (attrs.getType(i).equals("ID")) {
tmp.setIdAttribute(attQName, true);
}
} else {
tmp.setAttributeNS(attURI, attQName, attrs.getValue(i));
if (attrs.getType(i).equals("ID")) {
tmp.setIdAttributeNS(attURI, attrs.getLocalName(i), true);
}
}
}
// Append this new node onto current stack node
Node last = (Node)_nodeStk.peek();
// If the SAX2DOM is created with a non-null next sibling node,
// insert the result nodes before the next sibling under the root.
if (last == _root && _nextSibling != null)
last.insertBefore(tmp, _nextSibling);
else
last.appendChild(tmp);
// Push this node onto stack
_nodeStk.push(tmp);
_lastSibling = null;
}
/**
* Sets the "Id" attribute of an element and sets it as the element's XML ID.
* @param e the element where the ID should be set
* @param id the id
*/
public static void setIdAsXmlId(Element e, String id)
{
e.setAttributeNS(null, Constants._ATT_ID, id);
e.setIdAttributeNS(null, Constants._ATT_ID, true);
}
/**
* Sets an element's attribute (using DOM level 2) with the
* specified value and namespace prefix AND registers the ID value with
* the specified element. This is for resolving same-document
* ID references.
*
* @param elem the element to set the attribute on
* @param name the name of the attribute
* @param value the attribute value. If null, no attribute is set.
*/
public static void setAttributeID(Element elem, String name, String value) {
if (value == null) {
return;
}
elem.setAttributeNS(null, name, value);
elem.setIdAttributeNS(null, name, true);
}
/**
* Sets an element's attribute (using DOM level 2) with the
* specified value and namespace prefix AND registers the ID value with
* the specified element. This is for resolving same-document
* ID references.
*
* @param elem the element to set the attribute on
* @param name the name of the attribute
* @param value the attribute value. If null, no attribute is set.
*/
public static void setAttributeID(Element elem, String name, String value) {
if (value == null) {
return;
}
elem.setAttributeNS(null, name, value);
elem.setIdAttributeNS(null, name, true);
}
/**
* Sets an element's attribute (using DOM level 2) with the
* specified value and namespace prefix AND registers the ID value with
* the specified element. This is for resolving same-document
* ID references.
*
* @param elem the element to set the attribute on
* @param name the name of the attribute
* @param value the attribute value. If null, no attribute is set.
*/
public static void setAttributeID(Element elem, String name, String value) {
if (value == null) {
return;
}
elem.setAttributeNS(null, name, value);
elem.setIdAttributeNS(null, name, true);
}
/**
* Sets an element's attribute (using DOM level 2) with the
* specified value and namespace prefix AND registers the ID value with
* the specified element. This is for resolving same-document
* ID references.
*
* @param elem the element to set the attribute on
* @param name the name of the attribute
* @param value the attribute value. If null, no attribute is set.
*/
public static void setAttributeID(Element elem, String name, String value) {
if (value == null) {
return;
}
elem.setAttributeNS(null, name, value);
elem.setIdAttributeNS(null, name, true);
}
/**
* Sets an element's attribute (using DOM level 2) with the
* specified value and namespace prefix AND registers the ID value with
* the specified element. This is for resolving same-document
* ID references.
*
* @param elem the element to set the attribute on
* @param name the name of the attribute
* @param value the attribute value. If null, no attribute is set.
*/
public static void setAttributeID(Element elem, String name, String value) {
if (value == null) {
return;
}
elem.setAttributeNS(null, name, value);
elem.setIdAttributeNS(null, name, true);
}