Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/sun/xacml/cond/StringNormalizeFunction.java


1   
2   /*
3    * @(#)StringNormalizeFunction.java
4    *
5    * Copyright 2003-2004 Sun Microsystems, Inc. All Rights Reserved.
6    *
7    * Redistribution and use in source and binary forms, with or without
8    * modification, are permitted provided that the following conditions are met:
9    *
10   *   1. Redistribution of source code must retain the above copyright notice,
11   *      this list of conditions and the following disclaimer.
12   * 
13   *   2. Redistribution in binary form must reproduce the above copyright
14   *      notice, this list of conditions and the following disclaimer in the
15   *      documentation and/or other materials provided with the distribution.
16   *
17   * Neither the name of Sun Microsystems, Inc. or the names of contributors may
18   * be used to endorse or promote products derived from this software without
19   * specific prior written permission.
20   * 
21   * This software is provided "AS IS," without a warranty of any kind. ALL
22   * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
23   * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
24   * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN")
25   * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
26   * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
27   * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
28   * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
29   * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
30   * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
31   * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32   *
33   * You acknowledge that this software is not designed or intended for use in
34   * the design, construction, operation or maintenance of any nuclear facility.
35   */
36  
37  package com.sun.xacml.cond;
38  
39  import com.sun.xacml.EvaluationCtx;
40  
41  import com.sun.xacml.attr.AttributeValue;
42  import com.sun.xacml.attr.StringAttribute;
43  
44  import java.util.HashSet;
45  import java.util.List;
46  import java.util.Set;
47  
48  
49  /**
50   * A class that implements all the string conversion functions
51   * (string-normalize-space and string-normalize-to-lower-case).
52   * It takes string argument, normalizes that value, and returns
53   * the result. If the argument is indeterminate, an indeterminate
54   * result is returned.
55   *
56   * @since 1.0
57   * @author Steve Hanna
58   * @author Seth Proctor
59   */
60  public class StringNormalizeFunction extends FunctionBase
61  {
62  
63      /**
64       * Standard identifier for the string-normalize-space function.
65       */
66      public static final String NAME_STRING_NORMALIZE_SPACE =
67          FUNCTION_NS + "string-normalize-space";
68  
69      /**
70       * Standard identifier for the string-normalize-to-lower-case function.
71       */
72      public static final String NAME_STRING_NORMALIZE_TO_LOWER_CASE =
73          FUNCTION_NS + "string-normalize-to-lower-case";
74  
75      // private identifiers for the supported functions
76      private static final int ID_STRING_NORMALIZE_SPACE = 0;
77      private static final int ID_STRING_NORMALIZE_TO_LOWER_CASE = 1;
78  
79      /**
80       * Creates a new <code>StringNormalizeFunction</code> object.
81       *
82       * @param functionName the standard XACML name of the function to be
83       *                     handled by this object, including the full namespace
84       *
85       * @throws IllegalArgumentException if the function is unknown
86       */
87      public StringNormalizeFunction(String functionName) {
88          super(functionName, getId(functionName), StringAttribute.identifier,
89                false, 1, StringAttribute.identifier, false);
90      }
91  
92      /**
93       * Private helper that returns the internal identifier used for the
94       * given standard function.
95       */
96      private static int getId(String functionName) {
97          if (functionName.equals(NAME_STRING_NORMALIZE_SPACE))
98              return ID_STRING_NORMALIZE_SPACE;
99          else if (functionName.equals(NAME_STRING_NORMALIZE_TO_LOWER_CASE))
100             return ID_STRING_NORMALIZE_TO_LOWER_CASE;
101         else
102             throw new IllegalArgumentException("unknown normalize function " +
103                                                functionName);
104     }
105 
106     /**
107      * Returns a <code>Set</code> containing all the function identifiers
108      * supported by this class.
109      *
110      * @return a <code>Set</code> of <code>String</code>s
111      */
112     public static Set getSupportedIdentifiers() {
113         Set set = new HashSet();
114 
115         set.add(NAME_STRING_NORMALIZE_SPACE);
116         set.add(NAME_STRING_NORMALIZE_TO_LOWER_CASE);
117 
118         return set;
119     }
120 
121     /**
122      * Evaluate the function, using the specified parameters.
123      *
124      * @param inputs a <code>List</code> of <code>Evaluatable</code>
125      *               objects representing the arguments passed to the function
126      * @param context an <code>EvaluationCtx</code> so that the
127      *                <code>Evaluatable</code> objects can be evaluated
128      * @return an <code>EvaluationResult</code> representing the
129      *         function's result
130      */
131     public EvaluationResult evaluate(List inputs, EvaluationCtx context) {
132         // Evaluate the arguments
133         AttributeValue [] argValues = new AttributeValue[inputs.size()];
134         EvaluationResult result = evalArgs(inputs, context, argValues);
135         if (result != null)
136             return result;
137 
138         // Now that we have real values, perform the numeric conversion
139         // operation in the manner appropriate for this function.
140         switch (getFunctionId()) {
141         case ID_STRING_NORMALIZE_SPACE: {
142             String str = ((StringAttribute) argValues[0]).getValue();
143 
144             // Trim whitespace from start and end of string
145             int startIndex = 0;
146             int endIndex = str.length() - 1;
147             while ((startIndex <= endIndex) &&
148                    Character.isWhitespace(str.charAt(startIndex)))
149                 startIndex++;
150             while ((startIndex <= endIndex) &&
151                    Character.isWhitespace(str.charAt(endIndex)))
152                 endIndex--;
153             String strResult = str.substring(startIndex, endIndex+1);
154 
155             result = new EvaluationResult(new StringAttribute(strResult));
156             break;
157         }
158         case ID_STRING_NORMALIZE_TO_LOWER_CASE: {
159             String str = ((StringAttribute) argValues[0]).getValue();
160 
161             // Convert string to lower case
162             String strResult = str.toLowerCase();
163 
164             result = new EvaluationResult(new StringAttribute(strResult));
165             break;
166         }
167         }
168 
169         return result;
170     }
171 }