@@ -241,11 +241,7 @@ public void setReferrerPolicy(final String referrerPolicy) {
241241 @ JsxGetter
242242 public String getSearch () {
243243 try {
244- final String query = getUrl ().getQuery ();
245- if (query == null ) {
246- return "" ;
247- }
248- return "?" + query ;
244+ return HTMLHyperlinkElementUtils .getSearch (getUrl ());
249245 }
250246 catch (final MalformedURLException e ) {
251247 return "" ;
@@ -261,20 +257,7 @@ public String getSearch() {
261257 */
262258 @ JsxSetter
263259 public void setSearch (final String search ) throws Exception {
264- final String query ;
265- if (search == null
266- || StringUtils .isEmptyString (search )
267- || StringUtils .equalsChar ('?' , search )) {
268- query = null ;
269- }
270- else if (search .charAt (0 ) == '?' ) {
271- query = search .substring (1 );
272- }
273- else {
274- query = search ;
275- }
276-
277- setUrl (UrlUtils .getUrlWithNewQuery (getUrl (), query ));
260+ setUrl (HTMLHyperlinkElementUtils .setSearch (getUrl (), search ));
278261 }
279262
280263 /**
@@ -285,11 +268,7 @@ else if (search.charAt(0) == '?') {
285268 @ JsxGetter
286269 public String getHash () {
287270 try {
288- final String hash = getUrl ().getRef ();
289- if (hash == null ) {
290- return "" ;
291- }
292- return "#" + hash ;
271+ return HTMLHyperlinkElementUtils .getHash (getUrl ());
293272 }
294273 catch (final MalformedURLException e ) {
295274 return "" ;
@@ -304,7 +283,7 @@ public String getHash() {
304283 */
305284 @ JsxSetter
306285 public void setHash (final String hash ) throws Exception {
307- setUrl (UrlUtils . getUrlWithNewRef (getUrl (), hash ));
286+ setUrl (HTMLHyperlinkElementUtils . setHash (getUrl (), hash ));
308287 }
309288
310289 /**
@@ -319,7 +298,7 @@ public String getHost() {
319298 final int port = url .getPort ();
320299 final String host = url .getHost ();
321300
322- if (port == -1 || isDefaultPort (url .getProtocol (), port )) {
301+ if (port == -1 || HTMLHyperlinkElementUtils . isDefaultPort (url .getProtocol (), port )) {
323302 return host ;
324303 }
325304 return host + ":" + port ;
@@ -337,19 +316,7 @@ public String getHost() {
337316 */
338317 @ JsxSetter
339318 public void setHost (final String host ) throws Exception {
340- final String hostname ;
341- final int port ;
342- final int index = host .indexOf (':' );
343- if (index != -1 ) {
344- hostname = host .substring (0 , index );
345- port = Integer .parseInt (host .substring (index + 1 ));
346- }
347- else {
348- hostname = host ;
349- port = -1 ;
350- }
351- final URL url = UrlUtils .getUrlWithNewHostAndPort (getUrl (), hostname , port );
352- setUrl (url );
319+ setUrl (HTMLHyperlinkElementUtils .setHost (getUrl (), host ));
353320 }
354321
355322 /**
@@ -360,7 +327,7 @@ public void setHost(final String host) throws Exception {
360327 @ JsxGetter
361328 public String getHostname () {
362329 try {
363- return UrlUtils . encodeAnchor (getUrl (). getHost ());
330+ return HTMLHyperlinkElementUtils . getHostname (getUrl ());
364331 }
365332 catch (final MalformedURLException e ) {
366333 return "" ;
@@ -426,7 +393,7 @@ public String getPathname() {
426393 */
427394 @ JsxSetter
428395 public void setPathname (final String pathname ) throws Exception {
429- setUrl (UrlUtils . getUrlWithNewPath (getUrl (), pathname ));
396+ setUrl (HTMLHyperlinkElementUtils . setPathname (getUrl (), pathname ));
430397 }
431398
432399 /**
@@ -439,7 +406,7 @@ public String getPort() {
439406 try {
440407 final URL url = getUrl ();
441408 final int port = url .getPort ();
442- if (port == -1 || isDefaultPort (url .getProtocol (), port )) {
409+ if (port == -1 || HTMLHyperlinkElementUtils . isDefaultPort (url .getProtocol (), port )) {
443410 return "" ;
444411 }
445412 return Integer .toString (port );
@@ -459,7 +426,7 @@ public String getPort() {
459426 public void setPort (final String port ) throws Exception {
460427 final URL url = getUrl ();
461428 final int newPort = Integer .parseInt (port );
462- if (isDefaultPort (url .getProtocol (), newPort )) {
429+ if (HTMLHyperlinkElementUtils . isDefaultPort (url .getProtocol (), newPort )) {
463430 setUrl (UrlUtils .getUrlWithNewPort (url , -1 ));
464431 }
465432 else {
@@ -503,25 +470,9 @@ public String getProtocol() {
503470 */
504471 @ JsxSetter
505472 public void setProtocol (final String protocol ) throws Exception {
506- if (protocol .isEmpty ()) {
507- return ;
508- }
509-
510- final String bareProtocol = StringUtils .substringBefore (protocol , ":" ).trim ();
511- if (!UrlUtils .isValidScheme (bareProtocol )) {
512- return ;
513- }
514- if (!UrlUtils .isSpecialScheme (bareProtocol )) {
515- return ;
516- }
517-
518- try {
519- URL url = UrlUtils .getUrlWithNewProtocol (getUrl (), bareProtocol );
520- url = UrlUtils .removeRedundantPort (url );
521- setUrl (url );
522- }
523- catch (final MalformedURLException ignored ) {
524- // ignore
473+ final URL result = HTMLHyperlinkElementUtils .setProtocol (getUrl (), protocol );
474+ if (result != null ) {
475+ setUrl (result );
525476 }
526477 }
527478
@@ -661,11 +612,7 @@ public String getOrigin() {
661612 @ JsxGetter
662613 public String getUsername () {
663614 try {
664- final String userInfo = getUrl ().getUserInfo ();
665- if (userInfo == null ) {
666- return "" ;
667- }
668- return org .apache .commons .lang3 .StringUtils .substringBefore (userInfo , ':' );
615+ return HTMLHyperlinkElementUtils .getUsername (getUrl ());
669616 }
670617 catch (final MalformedURLException e ) {
671618 return "" ;
@@ -700,11 +647,7 @@ public void setUsername(final String username) {
700647 @ JsxGetter
701648 public String getPassword () {
702649 try {
703- final String userName = getUrl ().getUserInfo ();
704- if (userName == null ) {
705- return "" ;
706- }
707- return StringUtils .substringAfter (userName , ":" );
650+ return HTMLHyperlinkElementUtils .getPassword (getUrl ());
708651 }
709652 catch (final MalformedURLException e ) {
710653 return "" ;
@@ -821,15 +764,4 @@ public DOMTokenList getRelList() {
821764 public void setRelList (final Object rel ) {
822765 setRel (JavaScriptEngine .toString (rel ));
823766 }
824-
825- /**
826- * Checks whether the given port is the default port for the protocol.
827- * @param protocol the protocol (e.g. {@code "http"}, {@code "https"})
828- * @param port the port number
829- * @return {@code true} if the port is the default for the protocol
830- */
831- private static boolean isDefaultPort (final String protocol , final int port ) {
832- return ("http" .equals (protocol ) && port == 80 )
833- || ("https" .equals (protocol ) && port == 443 );
834- }
835767}
0 commit comments