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.util;
20  
21  /**
22   * @author Jacob Hookom
23   * @version $Id: Path.java 1187700 2011-10-22 12:19:37Z bommel $
24   */
25  public final class Path
26  {
27  
28      public static final String normalize(String path)
29      {
30          if (path.length() == 0)
31              return path;
32          String n = path;
33          boolean abs = false;
34          while (n.indexOf('\\') >= 0)
35          {
36              n = n.replace('\\', '/');
37          }
38          if (n.charAt(0) != '/')
39          {
40              n = '/' + n;
41              abs = true;
42          }
43          int idx = 0;
44          while (true)
45          {
46              idx = n.indexOf("%20");
47              if (idx == -1)
48              {
49                  break;
50              }
51              n = n.substring(0, idx) + " " + n.substring(idx + 3);
52          }
53          while (true)
54          {
55              idx = n.indexOf("/./");
56              if (idx == -1)
57              {
58                  break;
59              }
60              n = n.substring(0, idx) + n.substring(idx + 2);
61          }
62          if (abs)
63          {
64              n = n.substring(1);
65          }
66          return n;
67      }
68  
69      public static final String relative(String ctx, String path)
70      {
71          if (path.length() == 0)
72          {
73              return context(ctx);
74          }
75          String c = context(normalize(ctx));
76          String p = normalize(path);
77          p = c + p;
78  
79          int idx = 0;
80          while (true)
81          {
82              idx = p.indexOf("/../");
83              if (idx == -1)
84              {
85                  break;
86              }
87              int s = p.lastIndexOf('/', idx - 3);
88              if (s == -1)
89              {
90                  break;
91              }
92              p = p.substring(0, s) + p.substring(idx + 3);
93          }
94          return p;
95      }
96  
97      public static final String context(String path)
98      {
99          int idx = path.lastIndexOf('/');
100         if (idx == -1)
101         {
102             return "/";
103         }
104         return path.substring(0, idx + 1);
105     }
106 
107 }