View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.trinidad.webapp;
20  
21  import java.util.ArrayList;
22  import java.util.HashSet;
23  import java.util.List;
24  import java.util.Set;
25  
26  
27  final class TagUtils
28  {
29    private TagUtils() {}
30  
31  
32    /**
33     * Parses a whitespace separated series of name tokens.
34     * @param o the full string
35     * @return an array of each constituent value, or null
36     *  if there are no tokens (that is, the string is empty or
37     *  all whitespace)
38     * @todo Move to utility function somewhere (ADF Share?)
39     */
40    @SuppressWarnings("oracle.jdeveloper.java.null-array-return")
41    static final String[] parseNameTokens(Object o)
42    {
43      List<String> list = parseNameTokensAsList(o);
44  
45      if (list == null)
46        return null;
47  
48      return list.toArray(new String[list.size()]);
49    }
50  
51    @SuppressWarnings("oracle.jdeveloper.java.null-collection-return")
52    static final List<String> parseNameTokensAsList(Object o)
53    {
54      if (o == null)
55        return null;
56  
57      String stringValue = o.toString();
58      ArrayList<String> list = new ArrayList<String>(5);
59  
60      int     length = stringValue.length();
61      boolean inSpace = true;
62      int     start = 0;
63      for (int i = 0; i < length; i++)
64      {
65        char ch = stringValue.charAt(i);
66  
67        // We're in whitespace;  if we've just departed
68        // a run of non-whitespace, append a string.
69        // Now, why do we use the supposedly deprecated "Character.isSpace()"
70        // function instead of "isWhitespace"?  We're following XML rules
71        // here for the meaning of whitespace, which specifically
72        // EXCLUDES general Unicode spaces.
73        if (Character.isWhitespace(ch))
74        {
75          if (!inSpace)
76          {
77            list.add(stringValue.substring(start, i));
78            inSpace = true;
79          }
80        }
81        // We're out of whitespace;  if we've just departed
82        // a run of whitespace, start keeping track of this string
83        else
84        {
85          if (inSpace)
86          {
87            start = i;
88            inSpace = false;
89          }
90        }
91      }
92  
93      if (!inSpace)
94        list.add(stringValue.substring(start));
95  
96      if (list.isEmpty())
97        return null;
98  
99      return list;
100   }
101 
102   @SuppressWarnings("oracle.jdeveloper.java.null-collection-return")
103   static final Set<String> parseNameTokensAsSet (Object o)
104   {
105     List<String> list = parseNameTokensAsList(o);
106 
107     if (list == null)
108       return null;
109     else
110       return new HashSet<String>(list);
111   }
112 }