View Javadoc

1   package org.apache.maven.doxia.site.decoration.inheritance;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.net.MalformedURLException;
23  import java.net.URL;
24  
25  import org.codehaus.plexus.util.PathTool;
26  
27  /**
28   * Utilities that allow conversion of old and new pathes and URLs relative to each other.
29   *
30   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
31   * @author <a href="mailto:henning@apache.org">Henning P. Schmiedehausen</a>
32   * @deprecated this only operates on deprecated classes, it is not used anymore.
33   * @version $Id: PathUtils.java 1058083 2011-01-12 11:34:02Z ltheussl $
34   */
35  public abstract class PathUtils
36  {
37      /**
38       * Private constructor.
39       */
40      private PathUtils()
41      {
42          // do not instantiate
43      }
44  
45      /**
46       * <p>convertPath.</p>
47       *
48       * @param oldPath not null
49       * @param newPath not null
50       * @return a PathDescriptor converted by the new path
51       * @throws java.net.MalformedURLException if any
52       */
53      public static final PathDescriptor convertPath( final PathDescriptor oldPath, final PathDescriptor newPath )
54          throws MalformedURLException
55      {
56          String relative = getRelativePath( oldPath, newPath );
57  
58          if ( relative == null )
59          {
60              return oldPath;
61          }
62  
63          return new PathDescriptor( relative );
64      }
65  
66      /**
67       * <p>getRelativePath.</p>
68       *
69       * @param oldPathDescriptor not null
70       * @param newPathDescriptor not null
71       * @return a relative path depending if PathDescriptor is a file or a web url.
72       * @see PathTool#getRelativeFilePath(String, String)
73       * @see PathTool#getRelativeWebPath(String, String)
74       */
75      public static final String getRelativePath( final PathDescriptor oldPathDescriptor,
76                                                  final PathDescriptor newPathDescriptor )
77      {
78          // Cannot convert from URL to file.
79          if ( oldPathDescriptor.isFile() )
80          {
81              if ( !newPathDescriptor.isFile() )
82              {
83                  // We want to convert from a file to an URL. This is normally not possible...
84                  if ( oldPathDescriptor.isRelative() )
85                  {
86                      // unless the old path is a relative path. Then we might convert an existing
87                      // site into a new URL using resolvePaths()...
88                      return oldPathDescriptor.getPath();
89                  }
90  
91                  // The old path is not relative. Bail out.
92                  return null;
93              }
94              else
95              {
96                  // both are files, if either of them is relative, bail out
97                  // see DOXIASITETOOLS-29, MSITE-404, PLXUTILS-116
98                  if ( oldPathDescriptor.isRelative() || newPathDescriptor.isRelative() )
99                  {
100                     return null;
101                 }
102             }
103         }
104 
105         // Don't optimize to else. This might also be old.isFile && new.isFile ...
106         if ( !oldPathDescriptor.isFile() )
107         {
108             // URLs, determine if they share protocol and domain info
109             URL oldUrl = oldPathDescriptor.getPathUrl();
110             URL newUrl = newPathDescriptor.getPathUrl();
111 
112             if ( oldUrl == null || newUrl == null )
113             {
114                 // One of the sites has a strange URL. no relative path possible, bail out.
115                 return null;
116             }
117 
118             if ( ( newUrl.getProtocol().equalsIgnoreCase( oldUrl.getProtocol() ) )
119                             && ( newUrl.getHost().equalsIgnoreCase( oldUrl.getHost() ) )
120                             && ( newUrl.getPort() == oldUrl.getPort() ) )
121             {
122                 // Both paths point to the same site. So we can use relative paths.
123 
124                 String oldPath = oldPathDescriptor.getPath();
125                 String newPath = newPathDescriptor.getPath();
126 
127                 return PathTool.getRelativeWebPath( newPath, oldPath );
128             }
129 
130             // Different sites. No relative Path possible.
131             return null;
132         }
133 
134         // Both Descriptors point to an absolute path. We can build a relative path.
135         String oldPath = oldPathDescriptor.getPath();
136         String newPath = newPathDescriptor.getPath();
137 
138         if ( oldPath == null || newPath == null )
139         {
140             // One of the sites has a strange URL. no relative path possible, bail out.
141             return null;
142         }
143 
144         return PathTool.getRelativeFilePath( oldPath, newPath );
145     }
146 }