use of com.gargoylesoftware.htmlunit.html.DomAttr in project htmlunit by HtmlUnit.
the class XmlUtils method lookupPrefix.
/**
* Search for the prefix associated with specified namespace URI.
* @param element the element to start searching from
* @param namespace the namespace prefix
* @return the prefix bound to the namespace URI; or null if there is no such namespace
*/
public static String lookupPrefix(final DomElement element, final String namespace) {
final Map<String, DomAttr> attributes = element.getAttributesMap();
for (final Map.Entry<String, DomAttr> entry : attributes.entrySet()) {
final String name = entry.getKey();
final DomAttr value = entry.getValue();
if (name.startsWith("xmlns:") && value.getValue().equals(namespace)) {
return name.substring(6);
}
}
for (final DomNode child : element.getChildren()) {
if (child instanceof DomElement) {
final String prefix = lookupPrefix((DomElement) child, namespace);
if (prefix != null) {
return prefix;
}
}
}
return null;
}
use of com.gargoylesoftware.htmlunit.html.DomAttr in project htmlunit by HtmlUnit.
the class SvgElementFactory method toMap.
private static Map<String, DomAttr> toMap(final SgmlPage page, final Attributes attributes) {
Map<String, DomAttr> attributeMap = null;
if (attributes != null) {
attributeMap = new LinkedHashMap<>(attributes.getLength());
for (int i = 0; i < attributes.getLength(); i++) {
final String qName = attributes.getQName(i);
// browsers consider only first attribute (ex: <div id='foo' id='something'>...</div>)
if (!attributeMap.containsKey(qName)) {
String namespaceURI = attributes.getURI(i);
if (namespaceURI != null && namespaceURI.isEmpty()) {
namespaceURI = null;
}
final DomAttr newAttr = new DomAttr(page, namespaceURI, qName, attributes.getValue(i), true);
attributeMap.put(qName, newAttr);
}
}
}
return attributeMap;
}
use of com.gargoylesoftware.htmlunit.html.DomAttr in project htmlunit by HtmlUnit.
the class HtmlUnitPrefixResolver method getNamespace.
private String getNamespace(final DomElement element, final String prefix) {
final Map<String, DomAttr> attributes = element.getAttributesMap();
final String xmlns = "xmlns:";
final int xmlnsLength = xmlns.length();
for (final Map.Entry<String, DomAttr> entry : attributes.entrySet()) {
final String name = entry.getKey();
if (name.startsWith(xmlns) && name.regionMatches(xmlnsLength, prefix, 0, prefix.length())) {
return entry.getValue().getValue();
}
}
for (final DomNode child : element.getChildren()) {
if (child instanceof DomElement) {
final String namespace = getNamespace((DomElement) child, prefix);
if (namespace != null) {
return namespace;
}
}
}
return null;
}
use of com.gargoylesoftware.htmlunit.html.DomAttr in project htmlunit by HtmlUnit.
the class DOMTokenList method updateAttribute.
private void updateAttribute(final String value) {
final DomElement domNode = (DomElement) getDomNodeOrDie();
DomAttr attr = (DomAttr) domNode.getAttributes().getNamedItem(attributeName_);
if (null == attr) {
attr = domNode.getPage().createAttribute(attributeName_);
}
attr.setValue(value);
domNode.setAttributeNode(attr);
}
Aggregations