View Javadoc

1   /**
2    * Licensed under the Artistic License; you may not use this file
3    * except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    *      http://displaytag.sourceforge.net/license.html
7    *
8    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
9    * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10   * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11   */
12  package org.displaytag.util;
13  
14  import java.io.Serializable;
15  
16  
17  /**
18   * Simple utility class for encoding parameter names.
19   * @author Fabrizio Giustina
20   * @version $Revision: 1081 $ ($Author: fgiust $)
21   */
22  public class ParamEncoder implements Serializable
23  {
24  
25      /**
26       * D1597A17A6.
27       */
28      private static final long serialVersionUID = 899149338534L;
29  
30      /**
31       * Unique identifier for a tag with the given id/name.
32       */
33      private String parameterIdentifier;
34  
35      /**
36       * Generates a new parameter encoder for the table with the given id.
37       * @param idAttribute value of "id" attribute
38       */
39      public ParamEncoder(String idAttribute)
40      {
41          // use name and id to get the unique identifier
42          String stringIdentifier = "x-" + idAttribute; //$NON-NLS-1$
43  
44          // get the array
45          char[] charArray = stringIdentifier.toCharArray();
46  
47          // calculate a simple checksum-like value
48          int checkSum = 17;
49  
50          for (int j = 0; j < charArray.length; j++)
51          {
52              checkSum = 3 * checkSum + charArray[j];
53          }
54  
55          // keep it positive
56          checkSum &= 0x7fffff;
57  
58          // this is the full identifier used for all the parameters
59          this.parameterIdentifier = "d-" + checkSum + "-"; //$NON-NLS-1$ //$NON-NLS-2$
60      }
61  
62      /**
63       * encode a parameter name prepending calculated <code>parameterIdentifier</code>.
64       * @param paramName parameter name
65       * @return encoded parameter name in the form <code>d-<em>XXXX</em>-<em>name</em></code>
66       */
67      public String encodeParameterName(String paramName)
68      {
69          return this.parameterIdentifier + paramName;
70      }
71  
72      /**
73       * Check if the given parameter has been encoded using paramEncoder. It actually check if the parameter name starts
74       * with the calculated <code>parameterIdentifier</code>. Null safe (a null string returns <code>false</code>).
75       * @param paramName parameter name
76       * @return <code>true</code> if the given parameter as been encoded using this param encoder
77       */
78      public boolean isParameterEncoded(String paramName)
79      {
80          return paramName != null && paramName.startsWith(this.parameterIdentifier);
81      }
82  
83  }