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 */ 34 public abstract class PathUtils 35 { 36 /** 37 * Private constructor. 38 */ 39 private PathUtils() 40 { 41 // do not instantiate 42 } 43 44 /** 45 * <p>convertPath.</p> 46 * 47 * @param oldPath not null 48 * @param newPath not null 49 * @return a PathDescriptor converted by the new path 50 * @throws java.net.MalformedURLException if any 51 */ 52 public static final PathDescriptor convertPath( final PathDescriptor oldPath, final PathDescriptor newPath ) 53 throws MalformedURLException 54 { 55 String relative = getRelativePath( oldPath, newPath ); 56 57 if ( relative == null ) 58 { 59 return oldPath; 60 } 61 62 return new PathDescriptor( relative ); 63 } 64 65 /** 66 * <p>getRelativePath.</p> 67 * 68 * @param oldPathDescriptor not null 69 * @param newPathDescriptor not null 70 * @return a relative path depending if PathDescriptor is a file or a web url. 71 * @see PathTool#getRelativeFilePath(String, String) 72 * @see PathTool#getRelativeWebPath(String, String) 73 */ 74 public static final String getRelativePath( final PathDescriptor oldPathDescriptor, 75 final PathDescriptor newPathDescriptor ) 76 { 77 // Cannot convert from URL to file. 78 if ( oldPathDescriptor.isFile() ) 79 { 80 if ( !newPathDescriptor.isFile() ) 81 { 82 // We want to convert from a file to an URL. This is normally not possible... 83 if ( oldPathDescriptor.isRelative() ) 84 { 85 // unless the old path is a relative path. Then we might convert an existing 86 // site into a new URL using resolvePaths()... 87 return oldPathDescriptor.getPath(); 88 } 89 90 // The old path is not relative. Bail out. 91 return null; 92 } 93 else 94 { 95 // both are files, if either of them is relative, bail out 96 // see DOXIASITETOOLS-29, MSITE-404, PLXUTILS-116 97 if ( oldPathDescriptor.isRelative() || newPathDescriptor.isRelative() ) 98 { 99 return null; 100 } 101 } 102 } 103 104 // Don't optimize to else. This might also be old.isFile && new.isFile ... 105 if ( !oldPathDescriptor.isFile() ) 106 { 107 // URLs, determine if they share protocol and domain info 108 URL oldUrl = oldPathDescriptor.getPathUrl(); 109 URL newUrl = newPathDescriptor.getPathUrl(); 110 111 if ( oldUrl == null || newUrl == null ) 112 { 113 // One of the sites has a strange URL. no relative path possible, bail out. 114 return null; 115 } 116 117 if ( ( newUrl.getProtocol().equalsIgnoreCase( oldUrl.getProtocol() ) ) 118 && ( newUrl.getHost().equalsIgnoreCase( oldUrl.getHost() ) ) 119 && ( newUrl.getPort() == oldUrl.getPort() ) ) 120 { 121 // Both paths point to the same site. So we can use relative paths. 122 123 String oldPath = oldPathDescriptor.getPath(); 124 String newPath = newPathDescriptor.getPath(); 125 126 return PathTool.getRelativeWebPath( newPath, oldPath ); 127 } 128 129 // Different sites. No relative Path possible. 130 return null; 131 } 132 133 // Both Descriptors point to an absolute path. We can build a relative path. 134 String oldPath = oldPathDescriptor.getPath(); 135 String newPath = newPathDescriptor.getPath(); 136 137 if ( oldPath == null || newPath == null ) 138 { 139 // One of the sites has a strange URL. no relative path possible, bail out. 140 return null; 141 } 142 143 return PathTool.getRelativeFilePath( oldPath, newPath ); 144 } 145 }