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