Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import java.util.StringTokenizer;

public class Main {
    public static String unEscapeTemplatedURIComponent(String uri) {
        try {
            uri = URLDecoder.decode(uri, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        uri = restoreAll(uri, ":", "?");
        uri = restoreAll(uri, ":", "/");
        return uri;
    }

    public static String restoreAll(String url, String holder, String restore) {
        return restoreWithEndnote(url, holder, restore, "(", ".");
    }

    public static String restoreWithEndnote(String text, String holderString, String replacement, String noteTag,
            String noteSplit) {
        int start = text.lastIndexOf(noteTag);
        String note = text.substring(start);
        text = text.substring(0, start);
        if (note.length() == noteTag.length())
            return text;
        StringBuilder sb = new StringBuilder(text);
        StringTokenizer token = new StringTokenizer(note.substring(1), noteSplit);
        int[] index = new int[token.countTokens()];
        for (int i = index.length - 1; i >= 0; i--) {
            index[i] = Integer.parseInt(token.nextToken());
        }

        int h_length = holderString.length();
        for (int i = 0; i < index.length; i++) {
            sb.replace(index[i], index[i] + h_length, replacement);
        }
        return sb.toString();
    }
}