java02elements.attr_Java Attr.getOwnerElement方法代码示例

import org.w3c.dom.Attr; //导入方法依赖的package包/类

/**

* This method is a tree-search to help prevent against wrapping attacks. It checks that no

* two Elements have ID Attributes that match the "value" argument, if this is the case then

* "false" is returned. Note that a return value of "true" does not necessarily mean that

* a matching Element has been found, just that no wrapping attack has been detected.

*/

public static boolean protectAgainstWrappingAttack(Node startNode, String value) {

Node startParent = startNode.getParentNode();

Node processedNode = null;

Element foundElement = null;

String id = value.trim();

if (!id.isEmpty() && id.charAt(0) == '#') {

id = id.substring(1);

}

while (startNode != null) {

if (startNode.getNodeType() == Node.ELEMENT_NODE) {

Element se = (Element) startNode;

NamedNodeMap attributes = se.getAttributes();

if (attributes != null) {

for (int i = 0; i < attributes.getLength(); i++) {

Attr attr = (Attr)attributes.item(i);

if (attr.isId() && id.equals(attr.getValue())) {

if (foundElement == null) {

// Continue searching to find duplicates

foundElement = attr.getOwnerElement();

} else {

log.log(java.util.logging.Level.FINE, "Multiple elements with the same 'Id' attribute value!");

return false;

}

}

}

}

}

processedNode = startNode;

startNode = startNode.getFirstChild();

// no child, this node is done.

if (startNode == null) {

// close node processing, get sibling

startNode = processedNode.getNextSibling();

}

// no more siblings, get parent, all children

// of parent are processed.

while (startNode == null) {

processedNode = processedNode.getParentNode();

if (processedNode == startParent) {

return true;

}

// close parent node processing (processed node now)

startNode = processedNode.getNextSibling();

}

}

return true;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值