| /******************************************************************************* |
| * Copyright (c) 2004, 2008 Tasktop Technologies 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: |
| * Tasktop Technologies - initial API and implementation |
| *******************************************************************************/ |
| |
| package org.eclipse.mylyn.internal.provisional.tasks.core; |
| |
| /** |
| * @author Steffen Pingel |
| */ |
| public class TasksUtil { |
| |
| public static String decode(String text) { |
| boolean escaped = false; |
| StringBuffer sb = new StringBuffer(text.length()); |
| StringBuffer escapedText = new StringBuffer(4); |
| char[] chars = text.toCharArray(); |
| for (char c : chars) { |
| if (c >= '0' && c <= '9' || c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c == '.') { |
| if (escaped) { |
| escapedText.append(c); |
| } else { |
| sb.append(c); |
| } |
| } else if (c == '%') { |
| if (escaped) { |
| throw new IllegalArgumentException("Unexpected '%' sign in '" + text + "'"); //$NON-NLS-1$ //$NON-NLS-2$ |
| } |
| escaped = !escaped; |
| } else if (c == '_') { |
| if (!escaped) { |
| throw new IllegalArgumentException("Unexpected '_' sign in '" + text + "'"); //$NON-NLS-1$ //$NON-NLS-2$ |
| } |
| try { |
| sb.append((char) Integer.parseInt(escapedText.toString(), 16)); |
| escapedText.setLength(0); |
| } catch (NumberFormatException e) { |
| throw new IllegalArgumentException("Invalid escape code in '" + text + "'"); //$NON-NLS-1$ //$NON-NLS-2$ |
| } |
| escaped = !escaped; |
| } else { |
| throw new IllegalArgumentException("Unexpected character '" + c + "' in '" + text + "'"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
| } |
| } |
| return sb.toString(); |
| } |
| |
| public static String encode(String text) { |
| StringBuffer sb = new StringBuffer(text.length()); |
| char[] chars = text.toCharArray(); |
| for (char c : chars) { |
| if (c >= '0' && c <= '9' || c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c == '.') { |
| sb.append(c); |
| } else { |
| sb.append("%" + Integer.toHexString(c).toUpperCase() + "_"); //$NON-NLS-1$ //$NON-NLS-2$ |
| } |
| } |
| return sb.toString(); |
| } |
| |
| } |