Java examples for Network:URL
Convert to relative url based on base
/******************************************************************************* * Copyright (c) 2006, 2010 Soyatec (http://www.soyatec.com) and others. * * 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: * * Soyatec - initial API and implementation * *******************************************************************************/ //package com.java2s; import java.util.StringTokenizer; public class Main { public static final String FORWARD_SLASH = "/"; public static final String RELATIVE_PATH_SIG = "../"; /**//from w w w. j ava 2 s . c o m * Convert to relative url based on base */ public static String convertToRelative(String input, String base) { StringTokenizer inputTokenizer = new StringTokenizer(input, FORWARD_SLASH); StringTokenizer baseTokenizer = new StringTokenizer(base, FORWARD_SLASH); String token1 = "", token2 = ""; // while (true) { if (!inputTokenizer.hasMoreTokens() || !baseTokenizer.hasMoreTokens()) break; token1 = baseTokenizer.nextToken(); token2 = inputTokenizer.nextToken(); if (!token1.equals(token2)) break; } StringBuilder builder = new StringBuilder(); while (baseTokenizer.hasMoreTokens()) { baseTokenizer.nextToken(); builder.append(RELATIVE_PATH_SIG); } builder.append(token2); while (inputTokenizer.hasMoreTokens()) { builder.append(FORWARD_SLASH + inputTokenizer.nextToken()); } return builder.toString(); } }