001: /*
002: * Copyright 2006-2007 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt.
007: *
008: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
009: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
010: * the license for the specific language governing your rights and limitations.
011: *
012: * Additional Contributor(s): Martin Schmid gridvision engineering GmbH
013: */
014: package org.pentaho.reportdesigner.crm.report.util;
015:
016: import org.jetbrains.annotations.NonNls;
017: import org.jetbrains.annotations.NotNull;
018: import org.jetbrains.annotations.Nullable;
019: import org.pentaho.reportdesigner.lib.common.xml.XMLContext;
020:
021: import java.io.File;
022: import java.net.MalformedURLException;
023: import java.net.URISyntaxException;
024: import java.net.URL;
025: import java.util.logging.Level;
026: import java.util.logging.Logger;
027:
028: /**
029: * User: Martin
030: * Date: 28.09.2007
031: * Time: 07:56:10
032: */
033: public class FileRelativator {
034: @NonNls
035: @NotNull
036: private static final Logger LOG = Logger
037: .getLogger(FileRelativator.class.getName());
038:
039: private FileRelativator() {
040: }
041:
042: @NotNull
043: private static String getRelativePathFromURL(@Nullable
044: File contextFile, @NotNull
045: URL url) {
046: try {
047: if (contextFile == null) {
048: return url.toURI().toASCIIString();
049: }
050:
051: String s1 = contextFile.toURI().toASCIIString();
052: String s2 = url.toURI().toASCIIString();
053:
054: if (s2.startsWith(s1)) {
055: return s2.substring(s1.length());
056: }
057: } catch (URISyntaxException e) {
058: if (LOG.isLoggable(Level.FINE))
059: LOG.log(Level.FINE, "FileRelativator.getRelativeFile ",
060: e);
061: }
062:
063: return url.toString();
064: }
065:
066: @NotNull
067: private static String getRelativePathFromFile(@Nullable
068: File contextFile, @NotNull
069: File file) {
070: String fp = file.getAbsolutePath();
071: if (contextFile != null) {
072: String cp = contextFile.getAbsolutePath();
073: if (!cp.endsWith(File.separator)) {
074: cp = cp + File.separator;
075: }
076:
077: if (fp.startsWith(cp)) {
078: return fp.substring(cp.length());
079: }
080: }
081: return fp;
082: }
083:
084: @NotNull
085: private static String getRelativePathFromFile(@Nullable
086: XMLContext xmlContext, @NotNull
087: File file) {
088: File contextFile = XMLContextKeys.CONTEXT_PATH
089: .getObject(xmlContext);
090: return getRelativePathFromFile(contextFile, file);
091: }
092:
093: @NotNull
094: public static String getRelativePathFromFile(@Nullable
095: XMLContext xmlContext, @NotNull
096: String file) {
097: if (file.trim().length() == 0) {
098: return "";
099: }
100: return getRelativePathFromFile(xmlContext, new File(file));
101: }
102:
103: @NotNull
104: public static String getRelativePathFromFile(@Nullable
105: File contextFile, @NotNull
106: String fileString) {
107: if (fileString.trim().length() == 0) {
108: return fileString;
109: }
110: return getRelativePathFromFile(contextFile,
111: new File(fileString));
112: }
113:
114: @NotNull
115: public static String getRelativePathFromURL(@Nullable
116: XMLContext xmlContext, @NotNull
117: String urlString) {
118: try {
119: File contextFile = XMLContextKeys.CONTEXT_PATH
120: .getObject(xmlContext);
121: URL url = new URL(urlString);
122: return getRelativePathFromURL(contextFile, url);
123: } catch (MalformedURLException e) {
124: if (LOG.isLoggable(Level.WARNING))
125: LOG.log(Level.WARNING,
126: "FileRelativator.getRelativePathFromFile ", e);
127: return urlString;
128: }
129: }
130:
131: @NotNull
132: public static URL getAbsoluteURL(@Nullable
133: XMLContext xmlContext, @NotNull
134: String spec) throws MalformedURLException {
135: File contextFile = XMLContextKeys.CONTEXT_PATH
136: .getObject(xmlContext);
137: if (contextFile == null) {
138: return new URL(spec);
139: } else {
140: return new URL(contextFile.toURI().toURL(), spec);
141: }
142: }
143:
144: @NotNull
145: public static String getAbsoluteFile(@Nullable
146: XMLContext xmlContext, @NotNull
147: String relativePath) {
148: if (relativePath.trim().length() == 0) {
149: return "";
150: }
151: File rf = new File(relativePath);
152: if (rf.isAbsolute()) {
153: return rf.getAbsolutePath();
154: } else {
155: File contextFile = XMLContextKeys.CONTEXT_PATH
156: .getObject(xmlContext);
157: return new File(contextFile, relativePath)
158: .getAbsolutePath();
159: }
160: }
161: }
|