View Javadoc

1   /*
2    * $Id: PathTypeTools.java,v 1.2 2010/03/19 14:53:48 oeuillot Exp $
3    */
4   package org.rcfaces.core.internal.util;
5   
6   import javax.faces.context.FacesContext;
7   
8   import org.apache.commons.logging.Log;
9   import org.apache.commons.logging.LogFactory;
10  import org.rcfaces.core.internal.contentAccessor.IContentPath;
11  import org.rcfaces.core.internal.renderkit.AbstractProcessContext;
12  import org.rcfaces.core.internal.renderkit.IProcessContext;
13  
14  /**
15   * 
16   * @author Olivier Oeuillot (latest modification by $Author: oeuillot $)
17   * @version $Revision: 1.2 $ $Date: 2010/03/19 14:53:48 $
18   */
19  public class PathTypeTools {
20      private static final String REVISION = "$Revision: 1.2 $";
21  
22      private static final Log LOG = LogFactory.getLog(PathTypeTools.class);
23  
24      public static String convertAbsolutePathToContextType(
25              FacesContext facesContext, String url) {
26  
27          // On peut peut-etre retirer le contextPath !
28  
29          if (facesContext == null) {
30              facesContext = FacesContext.getCurrentInstance();
31          }
32  
33          String contextPath = facesContext.getExternalContext()
34                  .getRequestContextPath();
35  
36          if (url.startsWith(contextPath)) {
37              String resolvedURL = url.substring(contextPath.length());
38  
39              if (resolvedURL.startsWith("/") == false) {
40                  resolvedURL = "/" + url;
41              }
42  
43              if (LOG.isDebugEnabled()) {
44                  LOG.debug("Convert absolute path '" + url
45                          + "' to context path '" + resolvedURL + "'.");
46              }
47  
48              return resolvedURL;
49          }
50  
51          if (LOG.isDebugEnabled()) {
52              LOG.debug("CAN NOT convert absolute path '" + url
53                      + "' to context path.");
54          }
55  
56          return null;
57      }
58  
59      public static String convertContextPathToAbsoluteType(
60              FacesContext facesContext, String url) {
61  
62          if (facesContext == null) {
63              facesContext = FacesContext.getCurrentInstance();
64          }
65  
66          String contextPath = facesContext.getExternalContext()
67                  .getRequestContextPath();
68          if (contextPath.endsWith("/")) {
69              if (url.startsWith("/")) {
70  
71                  String converted = contextPath + url.substring(1);
72  
73                  if (LOG.isDebugEnabled()) {
74                      LOG.debug("Convert context path '" + url
75                              + "' to absolute path '" + converted + "'.");
76                  }
77  
78                  return converted;
79              }
80  
81              String converted = contextPath + url;
82  
83              if (LOG.isDebugEnabled()) {
84                  LOG.debug("Convert context path '" + url
85                          + "' to absolute path '" + converted + "'.");
86              }
87  
88              return converted;
89          }
90  
91          if (url.startsWith("/")) {
92              String converted = contextPath + url;
93  
94              if (LOG.isDebugEnabled()) {
95                  LOG.debug("Convert context path '" + url
96                          + "' to absolute path '" + converted + "'.");
97              }
98  
99              return converted;
100 
101         }
102 
103         String converted = contextPath + "/" + url;
104 
105         if (LOG.isDebugEnabled()) {
106             LOG.debug("Convert context path '" + url + "' to absolute path '"
107                     + converted + "'.");
108         }
109 
110         return converted;
111     }
112 
113     public static String convertRelativePathToContextPath(
114             FacesContext facesContext, String url, IContentPath basePath) {
115 
116         IProcessContext processContext = AbstractProcessContext
117                 .getProcessContext(facesContext);
118 
119         String converted = processContext.getAbsolutePath(url, false);
120 
121         if (LOG.isDebugEnabled()) {
122             LOG.debug("Convert relative path '" + url + "' to context path '"
123                     + converted + "'.");
124         }
125 
126         return converted;
127 
128     }
129 
130 }