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.util.url;
35  
36  import org.apache.commons.lang.StringUtils;
37  
38  // Java Util
39  import java.util.ArrayList;
40  import java.util.List;
41  
42  
43  /**
44   * This class provides utility methods for the generation of Url String
45   */
46  public class UrlItem
47  {
48      ////////////////////////////////////////////////////////////////////////////
49      // Constants
50      private static final String ANCHOR_DELIMITER = "#";
51  
52      /** the root of the url. */
53      private String _strRoot;
54  
55      /** the list of parameters. */
56      private List<UrlParameterItem> _listParameters = new ArrayList<UrlParameterItem>(  );
57      private String _strAnchor;
58  
59      /**
60       * Constructs an url with no parameters.
61       * @param strRoot The url's root.
62       */
63      public UrlItem( String strRoot )
64      {
65          _strRoot = strRoot;
66      }
67  
68      /**
69       * Add a Parameter to the url.
70       * @param strName The name of the parameter.
71       * @param strValue The value of the parameter.
72       */
73      public void addParameter( String strName, String strValue )
74      {
75          _listParameters.add( new UrlParameterItem( strName, strValue ) );
76      }
77  
78      /**
79       * Add a Parameter to the url.
80       * @param strName The name of the parameter.
81       * @param nValue The value of the parameter.
82       */
83      public void addParameter( String strName, int nValue )
84      {
85          _listParameters.add( new UrlParameterItem( strName, nValue ) );
86      }
87  
88      /**
89       * Return the url string.
90       * @return String The url string.
91       */
92      public String getUrl(  )
93      {
94          StringBuilder urlCode = new StringBuilder( _strRoot );
95  
96          boolean bFirst = ( _strRoot.indexOf( '?' ) == -1 );
97  
98          for ( UrlParameterItem parameter : _listParameters )
99          {
100             // Add a ? or & to the root url if it does already contains one
101             urlCode.append( parameter.getCode( bFirst ) );
102             bFirst = false;
103         }
104 
105         if ( ( getAnchor(  ) != null ) && !getAnchor(  ).equals( StringUtils.EMPTY ) )
106         {
107             urlCode.append( ANCHOR_DELIMITER );
108             urlCode.append( getAnchor(  ) );
109         }
110 
111         return urlCode.toString(  );
112     }
113 
114     /**
115      * Return the url string.
116      * @return String The url string with entity code.
117      */
118     public String getUrlWithEntity(  )
119     {
120         StringBuilder urlCode = new StringBuilder( _strRoot );
121 
122         boolean bFirst = ( _strRoot.indexOf( '?' ) == -1 );
123 
124         for ( UrlParameterItem parameter : _listParameters )
125         {
126             // Add a ? or & to the root url if it does already contains one
127             urlCode.append( parameter.getCodeEntity( bFirst ) );
128             bFirst = false;
129         }
130 
131         if ( StringUtils.isNotEmpty( getAnchor(  ) ) )
132         {
133             urlCode.append( ANCHOR_DELIMITER + getAnchor(  ) );
134         }
135 
136         return urlCode.toString(  );
137     }
138 
139     /**
140      * Get the anchor
141      * @return the _srtAnchor
142      */
143     public String getAnchor(  )
144     {
145         return _strAnchor;
146     }
147 
148     /**
149      * Set the anchor
150      * @param strAnchorName the _srtAnchor to set
151      */
152     public void setAnchor( String strAnchorName )
153     {
154         _strAnchor = strAnchorName;
155     }
156 
157     /**
158      * {@inheritDoc}
159      */
160     @Override
161     public String toString(  )
162     {
163         return getUrl(  );
164     }
165 }