View Javadoc

1   /*
2    * $Id: HRef.java 2993 2011-11-24 19:51:48Z andrewinkler $
3    * ============================================================================
4    * Project gluehloch-homepage-core
5    * Copyright (c) 2004-2007 by Andre Winkler. All rights reserved.
6    * ============================================================================
7    *          GNU LESSER GENERAL PUBLIC LICENSE
8    *  TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
9    *
10   *  This library is free software; you can redistribute it and/or
11   *  modify it under the terms of the GNU Lesser General Public
12   *  License as published by the Free Software Foundation; either
13   *  version 2.1 of the License, or (at your option) any later version.
14   *
15   *  This library is distributed in the hope that it will be useful,
16   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18   *  Lesser General Public License for more details.
19   *
20   *  You should have received a copy of the GNU Lesser General Public
21   *  License along with this library; if not, write to the Free Software
22   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23   *
24   */
25  
26  package de.awtools.homegen.navigation;
27  
28  import org.apache.commons.lang.StringUtils;
29  
30  /**
31   * Eine Utility Klasse für HTML-Referenzen.
32   * 
33   * @version $LastChangedRevision: 2993 $ $LastChangedDate: 2011-11-24 20:51:48 +0100 (Thu, 24 Nov 2011) $
34   * @author by Andre Winkler, $LastChangedBy: andrewinkler $
35   *
36   * @since 1.1
37   */
38  public class HRef {
39  
40      /** Ein Leerstring. */
41      private static final String EMPTY = "";
42  
43      /** Ein ID-Generator. */
44      private static long counter = 0;
45  
46      /** Jede Instanz erhält eine eindeutige ID. */
47      private final long id = counter++;
48  
49      // -- id ------------------------------------------------------------------
50  
51      /**
52       * Liefert eine ID für dieses Objekt.
53       *
54       * @return Eine ID.
55       */
56      public final long getId() {
57          return id;
58      }
59  
60      // -- href ----------------------------------------------------------------
61  
62      /** Der HTML Link. */
63      private String href;
64  
65      /**
66       * Liefert eine HTML Referenz
67       *
68       * @return Eine HTML Referenz.
69       */
70      public final String getHref() {
71          // Die Zeichen './' und '/' dürfen jeweils nur 1x entfernt werden!
72          // Ansonsten werden nachfolgende '/' gelöscht.
73          if ((href != null) && (href.startsWith("./"))) {
74              href = StringUtils.replace(href, "./", EMPTY, 1);
75          } else if ((href != null) && (href.startsWith("/"))) {
76              href = StringUtils.replace(href, "/", EMPTY, 1);
77          }
78          return href;
79      }
80  
81      /**
82       * Setzt die HTML Referenz.
83       *
84       * @param value Eine HTML Referenz.
85       */
86      public final void setHref(final String value) {
87          href = value;
88      }
89  
90      /**
91       * Fragt nach, ob das Href Attribut gesetzt ist.
92       *
93       * @return Liefert <code>true</code> wenn blank; In allen anderen Fällen
94       *     liefert diese Methode <code>false</code>.
95       */
96      public final boolean isHrefBlank() {
97          return (StringUtils.isBlank(getHref()));
98      }
99  
100     // ------------------------------------------------------------------------
101 
102     /**
103      * Externer Link?
104      * 
105      * @return true, ein externer Link; false sonst.
106      */
107     public final boolean isExtern() {
108         if (StringUtils.isBlank(getHref())) {
109             return false;
110         } else {
111             return (getHref().startsWith("http://"));
112         }
113     }
114 
115 }