use of com.gargoylesoftware.htmlunit.html.DomAttr in project htmlunit by HtmlUnit.
the class XMLSerializer method toXml.
private void toXml(final int indent, final DomNode node, final StringBuilder builder) {
final String nodeName = node.getNodeName();
builder.append('<').append(nodeName);
final String optionalPrefix = "";
final String namespaceURI = node.getNamespaceURI();
final String prefix = node.getPrefix();
if (namespaceURI != null && prefix != null) {
boolean sameNamespace = false;
for (DomNode parentNode = node.getParentNode(); parentNode instanceof DomElement; parentNode = parentNode.getParentNode()) {
if (namespaceURI.equals(parentNode.getNamespaceURI())) {
sameNamespace = true;
break;
}
}
if (node.getParentNode() == null || !sameNamespace) {
((DomElement) node).setAttribute("xmlns:" + prefix, namespaceURI);
}
}
final NamedNodeMap attributesMap = node.getAttributes();
for (int i = 0; i < attributesMap.getLength(); i++) {
final DomAttr attrib = (DomAttr) attributesMap.item(i);
builder.append(' ').append(attrib.getQualifiedName()).append("=\"").append(attrib.getValue()).append('"');
}
boolean startTagClosed = false;
for (final DomNode child : node.getChildren()) {
if (!startTagClosed) {
builder.append(optionalPrefix).append('>');
startTagClosed = true;
}
switch(child.getNodeType()) {
case Node.ELEMENT_NODE:
toXml(indent + 1, child, builder);
break;
case Node.TEXT_NODE:
String value = child.getNodeValue();
value = StringUtils.escapeXmlChars(value);
if (preserveWhiteSpace_) {
builder.append(value.replace("\n", "\r\n"));
} else if (org.apache.commons.lang3.StringUtils.isBlank(value)) {
builder.append("\r\n");
final DomNode sibling = child.getNextSibling();
if (sibling != null && sibling.getNodeType() == Node.ELEMENT_NODE) {
for (int i = 0; i < indent; i++) {
builder.append('\t');
}
}
} else {
builder.append(value.replace("\n", "\r\n"));
}
break;
case Node.CDATA_SECTION_NODE:
case Node.COMMENT_NODE:
if (!preserveWhiteSpace_ && builder.charAt(builder.length() - 1) == '\n') {
for (int i = 0; i < indent; i++) {
builder.append('\t');
}
}
builder.append(child.asXml());
break;
default:
}
}
if (startTagClosed) {
if (!preserveWhiteSpace_ && builder.charAt(builder.length() - 1) == '\n') {
for (int i = 0; i < indent - 1; i++) {
builder.append('\t');
}
}
builder.append("</").append(nodeName).append('>');
} else {
builder.append(optionalPrefix).append("/>");
}
}
use of com.gargoylesoftware.htmlunit.html.DomAttr in project htmlunit by HtmlUnit.
the class XMLDOMAttribute method detachFromParent.
/**
* Detaches this attribute from the parent HTML element after caching the attribute value.
*/
public void detachFromParent() {
final DomAttr domNode = getDomNodeOrDie();
final DomElement parent = (DomElement) domNode.getParentNode();
if (parent != null) {
domNode.setValue(parent.getAttribute(getName()));
}
domNode.remove();
}
use of com.gargoylesoftware.htmlunit.html.DomAttr in project htmlunit by HtmlUnit.
the class XMLDocument method makeScriptableFor.
/**
* {@inheritDoc}
*/
@Override
public HtmlUnitScriptable makeScriptableFor(final DomNode domNode) {
final HtmlUnitScriptable scriptable;
// TODO: cleanup, getScriptObject() should be used!!!
if (domNode instanceof DomElement && !(domNode instanceof HtmlElement)) {
if (domNode instanceof SvgElement) {
final Class<? extends HtmlUnitScriptable> javaScriptClass = ((JavaScriptEngine) getWindow().getWebWindow().getWebClient().getJavaScriptEngine()).getJavaScriptClass(domNode.getClass());
try {
scriptable = javaScriptClass.newInstance();
} catch (final Exception e) {
throw Context.throwAsScriptRuntimeEx(e);
}
} else {
scriptable = new Element();
}
} else if (domNode instanceof DomAttr) {
scriptable = new Attr();
} else {
return super.makeScriptableFor(domNode);
}
scriptable.setPrototype(getPrototype(scriptable.getClass()));
scriptable.setParentScope(getParentScope());
scriptable.setDomNode(domNode);
return scriptable;
}
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;
}
Aggregations