package org.wikipedia;

import android.os.Parcel;
import android.os.Parcelable;

import java.util.regex.*;

/**
 * Represents a particular Wikimedia project.
 */
public class Site implements Parcelable {
    private final String domain;

    public Site(String domain) {
        this.domain = normalizeDomainName(domain);
    }

    protected String normalizeDomainName(String domain) {
        return domain.replace(".m.", ".");
    }

    public Site(Parcel in) {
        domain = in.readString();
    }

    public String getDomain() {
        return domain;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(domain);
    }

    public static final Parcelable.Creator<Site> CREATOR
            = new Parcelable.Creator<Site>() {
        public Site createFromParcel(Parcel in) {
            return new Site(in);
        }

        public Site[] newArray(int size) {
            return new Site[size];
        }
    };

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof Site)) {
            return false;
        }

        return ((Site)o).getDomain().equals(domain);
    }

    /**
     * Regex to match internal relative wikilinks.
     *
     * Start with /wiki/, can contain a Namespace indicator and a fragment.
     * Capturing Group 1 is namespace (or null for no namespace). 2 is Page Title text. 3 is Fragment.
     */
    public static Pattern internalLinkMatchPattern = Pattern.compile("/wiki/(?:([^:]+):)?([^#]*)(?:#(.+))?");

    /**
     * Create a PageTitle object from an internal link string.
     *
     * @param internalLink Internal link target text (eg. /wiki/Target).
     *                     Should be URL decoded before passing in
     * @return A {@link PageTitle} object representing the internalLink passed in.
     */
    public PageTitle titleForInternalLink(String internalLink) {
        //TODO: Do better validation of internal links!
        //TODO: Handle fragments better!
        Matcher matches = internalLinkMatchPattern.matcher(internalLink);
        if (matches.matches()) {
            String namespace = matches.group(1);
            String pageText = matches.group(2);
            return new PageTitle(namespace, pageText, this);
        } else {
            throw new RuntimeException("Did not  match internalLinkPattern: " + internalLink);
        }

    }

    private String language;
    public String getLanguage() {
        if (language == null) {
            language = domain.split("\\.")[0];
        }
        return language;
    }

    public static Site forLang(String lang) {
        return new Site(lang + ".wikipedia.org");
    }

    /**
     * Returns if the site is supported
     * @param domain the site domain
     * @return boolean
     */
    public static boolean isSupportedSite(String domain) {
        return domain.matches("[a-z\\-]+\\.(m\\.)?wikipedia\\.org");
    }
}

@@ -74,16 +74,89 @@
 lable;%0A%0A
+import java.io.UnsupportedEncodingException;%0Aimport java.net.URLDecoder;%0A
 import j
@@ -2238,24 +2238,46 @@
 atches()) %7B%0A
+            try %7B%0A    
             
@@ -2315,118 +2315,385 @@
 p(1)
-;%0A            String pageText = matches.group(2);%0A            return new PageTitle(namespace, pageText, this);
+ != null ? URLDecoder.decode(matches.group(1), %22utf-8%22) : null;%0A                String pageText = URLDecoder.decode(matches.group(2), %22utf-8%22);%0A                return new PageTitle(namespace, pageText, this);%0A            %7D catch (UnsupportedEncodingException e) %7B%0A                // NOT HAPPENING! JESUS CHRIST JAVA!%0A                throw new RuntimeException(e);%0A            %7D
 %0A   

Handle URLEncoded internal page links properly