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.view.facelets;
20  
21  import java.io.Writer;
22  import java.util.Comparator;
23  
24  /**
25   *
26   * @author lu4242
27   */
28  public class FaceletsVDLUtils
29  {
30          /**
31       * As specified in JSF 2.2 section 11.4.2.1, note it is different from the
32       * allowed format in xml url-pattern type.
33       * 
34       * @param path
35       * @param pattern
36       * @return A
37       */
38      public static boolean matchPattern(String path, String pattern)
39      {
40          // Normalize the argument strings
41          if ((path == null) || (path.length() == 0))
42          {
43              path = "*";
44          }
45          if ((pattern == null) || (pattern.length() == 0))
46          {
47              pattern = "*";
48          }
49  
50          // Check for exact match
51          if (path.equals(pattern))
52          {
53              return (true);
54          }
55  
56          // Check for path prefix matching
57          if (pattern.startsWith("/") && pattern.endsWith("/*"))
58          {
59              pattern = pattern.substring(0, pattern.length() - 2);
60              if (pattern.length() == 0)
61              {
62                  return (true);  // "/*" is the same as "/"
63              }
64              if (path.endsWith("/"))
65              {
66                  path = path.substring(0, path.length() - 1);
67              }
68              while (true)
69              {
70                  if (pattern.equals(path))
71                  {
72                      return (true);
73                  }
74                  int slash = path.lastIndexOf('/');
75                  if (slash <= 0)
76                  {
77                      break;
78                  }
79                  path = path.substring(0, slash);
80              }
81              return (false);
82          }
83  
84          // Check for universal mapping
85          if (pattern.equals("*"))
86          {
87              return (true);
88          }
89  
90          return (false);
91      }
92      
93      public static final class KeyComparator implements Comparator<String>
94      {
95          public int compare(String s1, String s2)
96          {
97              return -s1.compareTo(s2);
98          }
99      }
100     
101     public static class NullWriter extends Writer
102     {
103 
104         static final NullWriter INSTANCE = new NullWriter();
105 
106         public void write(char[] buffer)
107         {
108         }
109 
110         public void write(char[] buffer, int off, int len)
111         {
112         }
113 
114         public void write(String str)
115         {
116         }
117 
118         public void write(int c)
119         {
120         }
121 
122         public void write(String str, int off, int len)
123         {
124         }
125 
126         public void close()
127         {
128         }
129 
130         public void flush()
131         {
132         }
133     }
134 }