blob: ea0c2bc802f6b38d07556e44b3dc80763d9ee879 [file] [log] [blame]
amywuecebb042007-04-10 20:07:35 +00001/*******************************************************************************
2 * Copyright (c) 2005, 2007 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 *******************************************************************************/
vbaciuf1e11902007-02-02 20:57:51 +000011package org.eclipse.wst.xml.ui.internal.catalog;
12
13import org.eclipse.wst.common.uriresolver.internal.util.URIHelper;
14
15public class URIUtils {
16
17 private static final String PROTOCOL_PATTERN = ":";
18 private static final String FILE_PROTOCOL = "file:";
19 private static final String PLATFORM_RESOURCE_PROTOCOL = "platform:/resource/";
20 private static final String LOCAL_FILE_PROTOCOL_FORWARD_SLASH = "\\\\\\";
21 private static final String LOCAL_FILE_PROTOCOL_BACK_SLASH = "///";
22 private static final char PATH_SEPARATOR_FORWARD_SLASH = '/';
23 private static final char PATH_SEPARATOR_BACK_SLASH = '\\';
24
25 public static String convertURIToLocation(String uri) {
26 String location = uri;
27 if (uri != null) {
28 if (uri.startsWith(FILE_PROTOCOL)) {
29 location = org.eclipse.wst.common.uriresolver.internal.URI.createURI(uri).toFileString();
30 if (location != null && (location.startsWith(LOCAL_FILE_PROTOCOL_BACK_SLASH)
31 || location.startsWith(LOCAL_FILE_PROTOCOL_FORWARD_SLASH))) {
32 location = location.substring(LOCAL_FILE_PROTOCOL_BACK_SLASH.length());
33 }
34 } else if (uri.startsWith(PLATFORM_RESOURCE_PROTOCOL)) {
35 location = uri.substring(PLATFORM_RESOURCE_PROTOCOL.length());
36 }
37 }
38 return location;
39 }
40
41 public static String convertLocationToURI(String location) {
42 String uri = location;
43 if (!URIHelper.hasProtocol(location)) {
44 uri = URIHelper.isAbsolute(location)? org.eclipse.wst.common.uriresolver.internal.URI.createFileURI(location).toString()
45 : URIHelper.prependPlatformResourceProtocol(location);
46 }
47 if (uri.startsWith(FILE_PROTOCOL) && uri.indexOf(PROTOCOL_PATTERN, FILE_PROTOCOL.length()) != -1) {
48 uri = URIHelper.ensureFileURIProtocolFormat(uri);
49 }
50 uri = uri.replace(PATH_SEPARATOR_BACK_SLASH, PATH_SEPARATOR_FORWARD_SLASH);
51 return uri;
52 }
53
54}