blob: 481067e1c6e60d05db0bbe2f506afbc6a15545ea [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.xml.jaxb.util;
import java.io.File;
import java.net.URI;
/**
* org.netbeans.spi.project.support.ant.PropertyUtils is not available as pub
* API yet. Use the code locally.
*
*/
public class FileSysUtil {
private static String slashify(String path) {
if (path.endsWith(File.separator)) {
return path;
} else {
return path + File.separatorChar;
}
}
public static String relativizeFile(File basedir, File file) {
if (basedir.isFile()) {
throw new IllegalArgumentException("Cannot relative w.r.t. a data file " + basedir); // NOI18N
}
if (basedir.equals(file)) {
return "."; // NOI18N
}
StringBuffer b = new StringBuffer();
File base = basedir;
String filepath = file.getAbsolutePath();
while (!filepath.startsWith(slashify(base.getAbsolutePath()))) {
base = base.getParentFile();
if (base == null) {
return null;
}
if (base.equals(file)) {
b.append(".."); // NOI18N
return b.toString();
}
b.append("../"); // NOI18N
}
URI u = base.toURI().relativize(file.toURI());
assert !u.isAbsolute() : u + " from " + basedir + " and " + file + " with common root " + base;
b.append(u.getPath());
if (b.charAt(b.length() - 1) == '/') {
// file is an existing directory and file.toURI ends in /
// we do not want the trailing slash
b.setLength(b.length() - 1);
}
return b.toString();
}
public static boolean isAbsolutePath(File file){
return file.isAbsolute();
}
public static String Absolute2RelativePathStr(File base, File absPath){
String relPath = null;
if (isAbsolutePath(absPath)){
relPath = relativizeFile(base, absPath);
} else {
relPath = absPath.getPath();
}
return relPath;
}
public static File Relative2AbsolutePath(File base, String relPath){
File relPathFile = new File(relPath);
File absPath = null;
if (!isAbsolutePath(relPathFile)){
absPath = new File(base, relPath);
} else {
absPath = relPathFile;
}
return absPath;
}
}