![JAR search and dependency download from the Maven repository](/logo.png)
com.gargoylesoftware.htmlunit.javascript.host.html.HTMLAnchorElement Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vaadin-client-compiler-deps Show documentation
Show all versions of vaadin-client-compiler-deps Show documentation
Vaadin is a web application framework for Rich Internet Applications (RIA).
Vaadin enables easy development and maintenance of fast and
secure rich web
applications with a stunning look and feel and a wide browser support.
It features a server-side architecture with the majority of the logic
running
on the server. Ajax technology is used at the browser-side to ensure a
rich
and interactive user experience.
/*
* Copyright (c) 2002-2011 Gargoyle Software Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gargoylesoftware.htmlunit.javascript.host.html;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.commons.lang.StringUtils;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.util.UrlUtils;
/**
* The JavaScript object that represents an anchor.
*
* @version $Revision: 6357 $
* @author Mike Bowler
* @author Alexei Goussev
* @author David D. Kilzer
* @author Marc Guillemot
* @author Chris Erskine
* @author Ahmed Ashour
* @author Sudhan Moghe
* @author Daniel Gredler
*/
public class HTMLAnchorElement extends HTMLElement {
/**
* Creates an instance.
*/
public HTMLAnchorElement() {
// Empty.
}
/**
* Sets the href property.
* @param href the href property value
*/
public void jsxSet_href(final String href) {
getDomNodeOrDie().setAttribute("href", href);
}
/**
* Returns the value of this link's href property.
* @return the value of this link's href property
*/
public String jsxGet_href() {
final HtmlAnchor anchor = (HtmlAnchor) getDomNodeOrDie();
final String hrefAttr = anchor.getHrefAttribute();
if (hrefAttr == DomElement.ATTRIBUTE_NOT_DEFINED) {
return "";
}
try {
return getUrl().toString();
}
catch (final MalformedURLException e) {
return hrefAttr;
}
}
/**
* Sets the name property.
* @param name name attribute value
*/
public void jsxSet_name(final String name) {
getDomNodeOrDie().setAttribute("name", name);
}
/**
* Returns the value of the name property of this link.
* @return the name property
*/
public String jsxGet_name() {
return getDomNodeOrDie().getAttribute("name");
}
/**
* Sets the target property of this link.
* @param target target attribute value
*/
public void jsxSet_target(final String target) {
getDomNodeOrDie().setAttribute("target", target);
}
/**
* Returns the value of the target property of this link.
* @return the href property
*/
public String jsxGet_target() {
return getDomNodeOrDie().getAttribute("target");
}
/**
* Returns this link's current URL.
* @return this link's current URL
* @throws MalformedURLException if an error occurs
*/
private URL getUrl() throws MalformedURLException {
final HtmlAnchor anchor = (HtmlAnchor) getDomNodeOrDie();
return ((HtmlPage) anchor.getPage()).getFullyQualifiedUrl(anchor.getHrefAttribute());
}
/**
* Sets the href attribute of this link to the specified URL.
* @param url the new value of the href attribute
*/
private void setUrl(final URL url) {
getDomNodeOrDie().setAttribute("href", url.toString());
}
/**
* Sets the rel property.
* @param rel rel attribute value
*/
public void jsxSet_rel(final String rel) {
getDomNodeOrDie().setAttribute("rel", rel);
}
/**
* Returns the value of the rel property.
* @return the rel property
* @throws Exception if an error occurs
*/
public String jsxGet_rel() throws Exception {
return ((HtmlAnchor) getDomNodeOrDie()).getRelAttribute();
}
/**
* Sets the rev property.
* @param rel rev attribute value
*/
public void jsxSet_rev(final String rel) {
getDomNodeOrDie().setAttribute("rev", rel);
}
/**
* Returns the value of the rev property.
* @return the rev property
* @throws Exception if an error occurs
*/
public String jsxGet_rev() throws Exception {
return ((HtmlAnchor) getDomNodeOrDie()).getRevAttribute();
}
/**
* Returns the search portion of the link's URL (the portion starting with
* '?' and up to but not including any '#').
* @return the search portion of the link's URL
* @throws Exception if an error occurs
* @see MSDN Documentation
*/
public String jsxGet_search() throws Exception {
final String query = getUrl().getQuery();
if (query == null) {
return "";
}
return "?" + query;
}
/**
* Sets the search portion of the link's URL (the portion starting with '?'
* and up to but not including any '#')..
* @param search the new search portion of the link's URL
* @throws Exception if an error occurs
* @see MSDN Documentation
*/
public void jsxSet_search(final String search) throws Exception {
final String query;
if (search == null || "?".equals(search) || "".equals(search)) {
query = null;
}
else if (search.charAt(0) == '?') {
query = search.substring(1);
}
else {
query = search;
}
setUrl(UrlUtils.getUrlWithNewQuery(getUrl(), query));
}
/**
* Returns the hash portion of the link's URL (the portion following the '#', including the '#').
* @return the hash portion of the link's URL
* @throws Exception if an error occurs
* @see MSDN Documentation
*/
public String jsxGet_hash() throws Exception {
final String hash = getUrl().getRef();
if (hash == null) {
return "";
}
return "#" + hash;
}
/**
* Sets the hash portion of the link's URL (the portion following the '#').
* @param hash the new hash portion of the link's URL
* @throws Exception if an error occurs
* @see MSDN Documentation
*/
public void jsxSet_hash(final String hash) throws Exception {
setUrl(UrlUtils.getUrlWithNewRef(getUrl(), hash));
}
/**
* Returns the host portion of the link's URL (the '[hostname]:[port]' portion).
* @return the host portion of the link's URL
* @throws Exception if an error occurs
* @see MSDN Documentation
*/
public String jsxGet_host() throws Exception {
final URL url = getUrl();
final int port = url.getPort();
final String host = url.getHost();
if (port == -1) {
return host;
}
return host + ":" + port;
}
/**
* Sets the host portion of the link's URL (the '[hostname]:[port]' portion).
* @param host the new host portion of the link's URL
* @throws Exception if an error occurs
* @see MSDN Documentation
*/
public void jsxSet_host(final String host) throws Exception {
final String hostname;
final int port;
final int index = host.indexOf(':');
if (index != -1) {
hostname = host.substring(0, index);
port = Integer.parseInt(host.substring(index + 1));
}
else {
hostname = host;
port = -1;
}
final URL url1 = UrlUtils.getUrlWithNewHost(getUrl(), hostname);
final URL url2 = UrlUtils.getUrlWithNewPort(url1, port);
setUrl(url2);
}
/**
* Returns the hostname portion of the link's URL.
* @return the hostname portion of the link's URL
* @throws Exception if an error occurs
* @see MSDN Documentation
*/
public String jsxGet_hostname() throws Exception {
return getUrl().getHost();
}
/**
* Sets the hostname portion of the link's URL.
* @param hostname the new hostname portion of the link's URL
* @throws Exception if an error occurs
* @see MSDN Documentation
*/
public void jsxSet_hostname(final String hostname) throws Exception {
setUrl(UrlUtils.getUrlWithNewHost(getUrl(), hostname));
}
/**
* Returns the pathname portion of the link's URL.
* @return the pathname portion of the link's URL
* @throws Exception if an error occurs
* @see MSDN Documentation
*/
public String jsxGet_pathname() throws Exception {
return getUrl().getPath();
}
/**
* Sets the pathname portion of the link's URL.
* @param pathname the new pathname portion of the link's URL
* @throws Exception if an error occurs
* @see MSDN Documentation
*/
public void jsxSet_pathname(final String pathname) throws Exception {
setUrl(UrlUtils.getUrlWithNewPath(getUrl(), pathname));
}
/**
* Returns the port portion of the link's URL.
* @return the port portion of the link's URL
* @throws Exception if an error occurs
* @see MSDN Documentation
*/
public String jsxGet_port() throws Exception {
final int port = getUrl().getPort();
if (port == -1) {
return "";
}
return Integer.toString(port);
}
/**
* Sets the port portion of the link's URL.
* @param port the new port portion of the link's URL
* @throws Exception if an error occurs
* @see MSDN Documentation
*/
public void jsxSet_port(final String port) throws Exception {
setUrl(UrlUtils.getUrlWithNewPort(getUrl(), Integer.parseInt(port)));
}
/**
* Returns the protocol portion of the link's URL, including the trailing ':'.
* @return the protocol portion of the link's URL, including the trailing ':'
* @throws Exception if an error occurs
* @see MSDN Documentation
*/
public String jsxGet_protocol() throws Exception {
return getUrl().getProtocol() + ":";
}
/**
* Sets the protocol portion of the link's URL.
* @param protocol the new protocol portion of the link's URL
* @throws Exception if an error occurs
* @see MSDN Documentation
*/
public void jsxSet_protocol(final String protocol) throws Exception {
final String bareProtocol = StringUtils.substringBefore(protocol, ":");
setUrl(UrlUtils.getUrlWithNewProtocol(getUrl(), bareProtocol));
}
/**
* Calls for instance for implicit conversion to string.
* @see com.gargoylesoftware.htmlunit.javascript.SimpleScriptable#getDefaultValue(java.lang.Class)
* @param hint the type hint
* @return the default value
*/
@Override
public Object getDefaultValue(final Class< ? > hint) {
final HtmlElement element = getDomNodeOrNull();
if (element == null) {
return super.getDefaultValue(null);
}
return getDefaultValue(element);
}
static String getDefaultValue(final HtmlElement element) {
String href = element.getAttribute("href");
if (DomElement.ATTRIBUTE_NOT_DEFINED == href) {
return ""; // for example for named anchors
}
href = href.trim();
final int indexAnchor = href.indexOf('#');
final String beforeAnchor;
final String anchorPart;
if (indexAnchor == -1) {
beforeAnchor = href;
anchorPart = "";
}
else {
beforeAnchor = href.substring(0, indexAnchor);
anchorPart = href.substring(indexAnchor);
}
try {
final String response =
((HtmlPage) element.getPage()).getFullyQualifiedUrl(beforeAnchor).toExternalForm() + anchorPart;
return response;
}
catch (final MalformedURLException e) {
return href;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy