Java tutorial
//package com.java2s; /******************************************************************************* * Copyright (c) 2013 Actuate Corporation. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Actuate Corporation - initial API and implementation *******************************************************************************/ public class Main { public static String getRelativeUri(String parent, String child) { if (!isAbsolute(child)) { return child; } if (!isAbsolute(parent)) { throw new IllegalArgumentException("Parent uri must be absolute when child uri is absolute."); } parent = parent.substring(1); child = child.substring(1); if (child.startsWith(parent)) { return child.substring(parent.length()); } else { String[] parentPaths = parent.split("/"); String[] currentPaths = child.split("/"); int max = Math.max(parentPaths.length, currentPaths.length); int sameCount = 0; for (int i = 0; i < max; i++) { if (parentPaths[i].equals(currentPaths[i])) { sameCount++; } else { break; } } int upLevel = parentPaths.length - sameCount; StringBuffer buffer = new StringBuffer(); for (int i = 0; i < upLevel - 1; i++) { buffer.append("../"); } for (int i = sameCount; i < currentPaths.length; i++) { buffer.append(currentPaths[i]); if (i != currentPaths.length - 1) buffer.append('/'); } return buffer.toString(); } } public static boolean isAbsolute(String uri) { if (uri == null) { return false; } return uri.startsWith("/"); } }