View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.sponsoredlinks.business;
35  
36  import org.apache.commons.lang.StringUtils;
37  
38  import java.util.regex.Matcher;
39  import java.util.regex.Pattern;
40  
41  
42  /**
43   *
44   * class SponsoredLink.
45   * Note: this class has a natural ordering that is inconsistent with equals.
46   * See {@link #compareTo(SponsoredLink) compareTo}
47   *
48   */
49  public class SponsoredLink implements java.lang.Comparable<SponsoredLink>
50  {
51      // Number of the capturing group according to the regex
52      /** Key for the href attribute in the link */
53      public static final int HREF = 1;
54  
55      /** Key for the title attribute in the link */
56      public static final int TITLE = 2;
57  
58      /** Key for the alt attribute in the link */
59      public static final int ALT = 3;
60  
61      /** Key for the content in the link */
62      public static final int CONTENT = 4;
63  
64      //Regex used to validate a link and capture data from it
65      //^<a\b(?:href="([^>]*?)"|title="([^>]*?)"|alt="([^>]*?)"|[^>]*?)*>(.*?)</a>$
66      private static final String SPONSORED_LINK_REGEX = "^<a\\b(?:" + //opening of <a> tag and start of a non-capturing group
67          "href=\"([^>]*?)\"|" + //href attribute with capturing group #1
68          "title=\"([^>]*?)\"|" + //title attribute with capturing group #2
69          "alt=\"([^>]*?)\"|" + //alt attribute with capturing group #3
70          "[^>]*?" + // any non-matching attribute
71          ")*>" + // end of the non-capturing group and closing of the <a> tag
72          "(.*?)" + // inner content with capturing group #4
73          "</a>$"; // closing tag
74  
75      //Pre-compiled representation of the regex.
76      private static final Pattern _pattern = Pattern.compile( SPONSORED_LINK_REGEX, Pattern.CASE_INSENSITIVE );
77      private int _nOrder;
78      private String _strLink;
79  
80      /**
81       *
82       * @return the order of the SponsoredLink in the set
83       */
84      public int getOrder(  )
85      {
86          return _nOrder;
87      }
88  
89      /**
90       * Sets the order of the SponsoredLink in the set
91       * @param order The order in the set
92       */
93      public void setOrder( int order )
94      {
95          _nOrder = order;
96      }
97  
98      /**
99       *
100      * @return the html link of this SponsoredLink
101      */
102     public String getLink(  )
103     {
104         return _strLink;
105     }
106 
107     /**
108      * Sets the html link of this SponsoredLink
109      * @param link The url
110      */
111     public void setLink( String link )
112     {
113         _strLink = link;
114     }
115 
116     /**
117      * Defines the ordering of SponsoredLink object based on their {@link #getOrder() order}
118      * @param otherLink the SponsoredLink to be compared
119      * @return -1 if otherLink is greater, 0 if equal 1 otherwise
120      */
121     public int compareTo( SponsoredLink otherLink )
122     {
123         int otherOrder = otherLink.getOrder(  );
124 
125         if ( otherOrder > _nOrder )
126         {
127             return -1;
128         }
129         else
130         {
131             if ( otherOrder == _nOrder )
132             {
133                 return 0;
134             }
135             else
136             {
137                 return 1;
138             }
139         }
140     }
141 
142     /**
143      * Check if the link is a valid html link (as returned by an InsertService).
144      * The link must at least contain non-empty href attribute and content.
145      * Has to match the pattern <a (attr="[^>]*")*>.*</a>
146      * @return true if the link is valid html
147      */
148     public boolean isValidLink(  )
149     {
150         if ( StringUtils.isBlank( _strLink ) )
151         {
152             return false;
153         }
154 
155         Matcher matcher = _pattern.matcher( _strLink );
156 
157         if ( !matcher.matches(  ) )
158         {
159             return false;
160         }
161 
162         String strHref = matcher.group( HREF );
163         String strContent = matcher.group( CONTENT );
164 
165         if ( StringUtils.isBlank( strHref ) || StringUtils.isBlank( strContent ) )
166         {
167             return false;
168         }
169 
170         return true;
171     }
172 
173     /**
174      * Parses the link to find the specified attribute or the content.
175      * The link has to be valid html.
176      * @param nAttr the key for the attribute. Has to be in the set
177      *                 {ALT, HREF, TITLE, CONTENT}
178      * @return the attribute/content if exists. Returns null if the link is null
179      *  and if the key or the link is not valid
180      */
181     public String getLinkAttribute( int nAttr )
182     {
183         if ( StringUtils.isBlank( _strLink ) ||
184                 ( ( nAttr != HREF ) && ( nAttr != ALT ) && ( nAttr != TITLE ) && ( nAttr != CONTENT ) ) )
185         {
186             return null;
187         }
188 
189         String strHrefAttr = null;
190         Matcher matcher = _pattern.matcher( _strLink );
191 
192         if ( matcher.matches(  ) )
193         {
194             strHrefAttr = matcher.group( nAttr );
195         }
196 
197         return strHrefAttr;
198     }
199 }