private static Map<String, DomAttr> createAttributesCopyWithClonedAttribute(final HtmlElement elt, final String attrName) { final Map<String, DomAttr> newMap = new HashMap<>(elt.getAttributesMap()); // clone the specified element, if possible final DomAttr attr = newMap.get(attrName); if (null == attr) { return newMap; } final DomAttr clonedAttr = new DomAttr(attr.getPage(), attr.getNamespaceURI(), attr.getQualifiedName(), attr.getValue(), attr.getSpecified()); newMap.put(attrName, clonedAttr); return newMap; }
/** * {@inheritDoc} */ @Override public String getNodeName() { return getName(); }
/** * {@inheritDoc} */ @Override public String toString() { return getClass().getSimpleName() + "[name=" + getNodeName() + " value=" + getNodeValue() + "]"; }
/** * {@inheritDoc} */ @Override public String getCanonicalXPath() { return getParentNode().getCanonicalXPath() + "/@" + getName(); }
/** * Sets the value of the attribute specified by namespace and qualified name. * * @param namespaceURI the URI that identifies an XML namespace * @param qualifiedName the qualified name (prefix:local) of the attribute * @param attributeValue the value of the attribute * @param notifyAttributeChangeListeners to notify the associated {@link HtmlAttributeChangeListener}s * @param notifyMutationObservers to notify {@code MutationObserver}s or not */ protected void setAttributeNS(final String namespaceURI, final String qualifiedName, final String attributeValue, final boolean notifyAttributeChangeListeners, final boolean notifyMutationObservers) { final String value = attributeValue; final DomAttr newAttr = new DomAttr(getPage(), namespaceURI, qualifiedName, value, true); newAttr.setParentNode(this); attributes_.put(qualifiedName, newAttr); if (namespaceURI != null) { namespaces_.put(namespaceURI, newAttr.getPrefix()); } }
/** * Creates an instance of a DOM element that can have a namespace. * * @param namespaceURI the URI that identifies an XML namespace * @param qualifiedName the qualified name of the element type to instantiate * @param page the page that contains this element * @param attributes a map ready initialized with the attributes for this element, or * {@code null}. The map will be stored as is, not copied. */ public DomElement(final String namespaceURI, final String qualifiedName, final SgmlPage page, final Map<String, DomAttr> attributes) { super(namespaceURI, qualifiedName, page); if (attributes != null && !attributes.isEmpty()) { attributes_ = new NamedAttrNodeMapImpl(this, isAttributeCaseSensitive(), attributes); for (final DomAttr entry : attributes_.values()) { entry.setParentNode(this); final String attrNamespaceURI = entry.getNamespaceURI(); if (attrNamespaceURI != null) { namespaces_.put(attrNamespaceURI, entry.getPrefix()); } } } }
for (int i = 0; i < attributesMap.getLength(); i++) { final DomAttr attrib = (DomAttr) attributesMap.item(i); buffer.append(' ').append(attrib.getQualifiedName()).append('=') .append('"').append(attrib.getValue()).append('"');
/** * {@inheritDoc} Doesn't print the attribute if it is <tt>value="Submit Query"</tt>. */ @Override protected void printOpeningTagContentAsXml(final PrintWriter printWriter) { printWriter.print(getTagName()); for (final DomAttr attribute : getAttributesMap().values()) { if (!attribute.getNodeName().equals("value") || !attribute.getValue().equals(DEFAULT_VALUE)) { printWriter.print(" "); final String name = attribute.getNodeName(); printWriter.print(name); printWriter.print("=\""); printWriter.print(StringEscapeUtils.escapeXml(attribute.getNodeValue())); printWriter.print("\""); } } }
/** * 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(); }
/** * {@inheritDoc} */ @Override public String getNodeValue() { return getValue(); }
protected Map<String, DomAttr> getAttributesFor(final HtmlImage image) throws IOException { final Map<String, DomAttr> map = createAttributesCopyWithClonedAttribute(image, "src"); final DomAttr srcAttr = map.get("src"); if (srcAttr != null && StringUtils.isNotBlank(srcAttr.getValue())) { final WebResponse response = image.getWebResponse(true); final File file = createFile(srcAttr.getValue(), "." + getSuffix(response)); try (InputStream inputStream = response.getContentAsStream()) { FileUtils.copyInputStreamToFile(inputStream, file); } final String valueOnFileSystem = outputDir_.getName() + FILE_SEPARATOR + file.getName(); srcAttr.setValue(valueOnFileSystem); // this is the clone attribute node, not the original one of the page } return map; }
/** * Gets the attributes of the element in the form of a {@link org.xml.sax.Attributes}. * @param element the element to read the attributes from * @return the attributes */ protected AttributesImpl readAttributes(final HtmlElement element) { final AttributesImpl attributes = new AttributesImpl(); for (final DomAttr entry : element.getAttributesMap().values()) { final String name = entry.getName(); final String value = entry.getValue(); attributes.addAttribute(null, name, name, null, value); } return attributes; }
/** * {@inheritDoc} Doesn't print the attribute if it is <tt>value="Submit Query"</tt>. */ @Override protected void printOpeningTagContentAsXml(final PrintWriter printWriter) { printWriter.print(getTagName()); for (final DomAttr attribute : getAttributesMap().values()) { final String name = attribute.getNodeName(); final String value = attribute.getValue(); if (!"value".equals(name) || !DEFAULT_VALUE.equals(value)) { printWriter.print(" "); printWriter.print(name); printWriter.print("=\""); printWriter.print(StringUtils.escapeXmlAttributeValue(value)); printWriter.print("\""); } } }
/** * {@inheritDoc} */ @Override public String getTextContent() { return getNodeValue(); }
/** * Adds an attribute to the specified attribute map. This is just used by the element factories. * @param page the page which contains the attribute being created * @param attributeMap the attribute map where the attribute will be added * @param namespaceURI the URI that identifies an XML namespace * @param qualifiedName the qualified name of the attribute * @param value the value of the attribute * @return the new attribute which was added to the specified attribute map */ static DomAttr addAttributeToMap(final SgmlPage page, final Map<String, DomAttr> attributeMap, final String namespaceURI, final String qualifiedName, final String value) { final DomAttr newAttr = new DomAttr(page, namespaceURI, qualifiedName, value); attributeMap.put(qualifiedName, newAttr); return newAttr; }
/** * Creates a new HTML attribute with the specified name. * * @param attributeName the name of the attribute to create * @return an attribute with the specified name */ public Attr jsxFunction_createAttribute(final String attributeName) { final DomAttr attr = new DomAttr(getPage(), null, attributeName, null); return (Attr) attr.getScriptObject(); }
/** * {@inheritDoc} */ @Override public boolean isId() { return "id".equals(getNodeName()); }
/** * {@inheritDoc} */ @Override public String getName() { return getQualifiedName(); }
/** * {@inheritDoc} */ @Override public DomElement getOwnerElement() { return (DomElement) getParentNode(); }
/** * Retrieves an attribute node by name. * @param name the name of the attribute to retrieve * @return the XMLAttr node with the specified name or {@code null} if there is no such attribute */ @JsxFunction public Object getAttributeNode(final String name) { final Map<String, DomAttr> attributes = getDomNodeOrDie().getAttributesMap(); for (final DomAttr attr : attributes.values()) { if (attr.getName().equals(name)) { return attr.getScriptableObject(); } } return null; }