net.yatomiya.nicherry.services.bbs.model.URLToken.java Source code

Java tutorial

Introduction

Here is the source code for net.yatomiya.nicherry.services.bbs.model.URLToken.java

Source

/*******************************************************************************
 * Copyright (c) 2014,2015 Hideki Yatomi
 * 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
 ******************************************************************************/
package net.yatomiya.nicherry.services.bbs.model;

import com.squareup.okhttp.*;
import net.yatomiya.e4.util.*;

public class URLToken extends Token {
    private HttpUrl url;
    private String ext;

    public URLToken(AnalyzedMessage message, int offset, int length, String text, String urlString) {
        super(message, offset, length, text);

        url = HttpUrl.parse(urlString);
        if (url == null) {
            url = HttpUrl.parse("http://localhost/");
        }
        ext = StringUtils.getExtension(HttpUtils.getPathName(url));
    }

    public HttpUrl getURL() {
        return url;
    }

    public String getExtension() {
        return ext;
    }

    public static URLToken create(AnalyzedMessage message, int offset, int length, String text, String urlString) {
        HttpUrl url = HttpUrl.parse(urlString);
        if (url == null) {
            url = HttpUrl.parse("http://localhost/");
        }
        String ext = StringUtils.getExtension(HttpUtils.getPathName(url));

        URLToken token = null;

        if (ImageURLToken.isLoadableImageExtension(ext)) {
            token = new ImageURLToken(message, offset, length, text, urlString);
        } else {
            try {
                token = new ThreadURLToken(message, offset, length, text, urlString);
            } catch (IllegalArgumentException e) {
            }
        }

        if (token == null) {
            token = new URLToken(message, offset, length, text, urlString);
        }
        return token;
    }

}