KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > toc > HrefUtil


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.internal.toc;
12
13 import org.eclipse.core.runtime.Path;
14
15 public class HrefUtil {
16     /**
17      * Creates /pluginid/directory from directory name
18      */

19     public static String JavaDoc normalizeDirectoryHref(String JavaDoc pluginID, String JavaDoc dir) {
20         // "" is treated as if extra directory was not provided
21
if (dir == null || dir.length() <= 0)
22             return null;
23         // "." means all the files in the plugin
24
if (".".equals(dir)) //$NON-NLS-1$
25
dir = ""; //$NON-NLS-1$
26
// remove not needed trailing separator
27
if (dir.length() > 0 && dir.lastIndexOf('/') == dir.length() - 1) {
28             dir = dir.substring(0, dir.length() - 1);
29         }
30         return normalizeHref(pluginID, dir);
31     }
32
33     /**
34      * Creates /pluginid/href from href relative to the current plugin
35      *
36      * @param pluginID
37      * id of a plugin to which href is relative
38      * @param href
39      * relative href ex: path[#anchorID] ex:
40      * ../pluginID/path[#anchorID]
41      * @return String representation of href, formatted as
42      * /pluginID/path[#anchorID]
43      */

44     public final static String JavaDoc normalizeHref(String JavaDoc pluginID, String JavaDoc href) {
45         if (href == null)
46             return null;
47         if (href.startsWith("http:") //$NON-NLS-1$
48
|| href.startsWith("https:") //$NON-NLS-1$
49
|| href.startsWith("file:") //$NON-NLS-1$
50
|| href.startsWith("jar:")) //$NON-NLS-1$
51
// external doc
52
return href;
53         href = normalizeDirectoryPath(href);
54         if (href.startsWith("/")) //$NON-NLS-1$
55
// already normalized
56
return href;
57         if (href.startsWith("../")) { //$NON-NLS-1$
58
return href.substring(2);
59         }
60         if (href.length() > 0) {
61             StringBuffer JavaDoc buf = new StringBuffer JavaDoc(2 + pluginID.length()
62                     + href.length());
63             buf.append('/').append(pluginID);
64             buf.append('/').append(href);
65             return buf.toString();
66         }
67         return "/" + pluginID; //$NON-NLS-1$
68
}
69     /**
70      * Parses href and obtains plugin id
71      *
72      * @param href
73      * String in format /string1[/string2]
74      * @return plugin ID, or null
75      */

76     public static String JavaDoc getPluginIDFromHref(String JavaDoc href) {
77         if (href == null || href.length() < 2 || href.charAt(0) != '/')
78             return null;
79         int secondSlashIx = href.indexOf("/", 1); //$NON-NLS-1$
80
if (secondSlashIx < 0) // href is /pluginID
81
return href.substring(1);
82         // href is /pluginID/path[#anchorID]
83
return href.substring(1, secondSlashIx);
84     }
85
86     /**
87      * Parses href and obtains resource path relative to the plugin
88      *
89      * @param href
90      * String in format /string1[/[string2]][#string3]
91      * @return relative resource path, or null
92      */

93     public static String JavaDoc getResourcePathFromHref(String JavaDoc href) {
94         if (href == null)
95             return null;
96         // drop anchor id
97
int anchorIx = href.lastIndexOf("#"); //$NON-NLS-1$
98
if (anchorIx >= 0) //anchor exists, drop it
99
href = href.substring(0, anchorIx);
100         if (href.length() < 2 || href.charAt(0) != '/')
101             return null;
102         int secondSlashIx = href.indexOf("/", 1); //$NON-NLS-1$
103
if (secondSlashIx < 0) // href is /pluginID
104
return null;
105         if (secondSlashIx + 1 < href.length()) // href is /pluginID/path
106
return href.substring(secondSlashIx + 1);
107         // href is /pluginID/
108
return ""; //$NON-NLS-1$
109
}
110     
111     /**
112      * Parses directory path and obtains simple form path
113      *
114      * @param href
115      * directory path in format a/../c/1 or a//b/c
116      * to /c/1 and a/b/c
117      * @return normalized directory path, or null
118      */

119     public static String JavaDoc normalizeDirectoryPath(String JavaDoc href) {
120         if (href != null) {
121             return new Path(href).toString();
122         }
123         return null;
124     }
125 }
126
Popular Tags