|
Android example source code file (ExtendedWikiHelper.java)
The ExtendedWikiHelper.java Android example source code/* * Copyright (C) 2009 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.example.android.wiktionary; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.net.Uri; import android.text.TextUtils; import android.webkit.WebView; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * Extended version of {@link SimpleWikiHelper}. This version adds methods to * pick a random word, and to format generic wiki-style text into HTML. */ public class ExtendedWikiHelper extends SimpleWikiHelper { /** * HTML style sheet to include with any {@link #formatWikiText(String)} HTML * results. It formats nicely for a mobile screen, and hides some content * boxes to keep things tidy. */ private static final String STYLE_SHEET = "<style>h2 {font-size:1.2em;font-weight:normal;} " + "a {color:#6688cc;} ol {padding-left:1.5em;} blockquote {margin-left:0em;} " + ".interProject, .noprint {display:none;} " + "li, blockquote {margin-top:0.5em;margin-bottom:0.5em;}</style>"; /** * Pattern of section titles we're interested in showing. This trims out * extra sections that can clutter things up on a mobile screen. */ private static final Pattern sValidSections = Pattern.compile("(verb|noun|adjective|pronoun|interjection)", Pattern.CASE_INSENSITIVE); /** * Pattern that can be used to split a returned wiki page into its various * sections. Doesn't treat children sections differently. */ private static final Pattern sSectionSplit = Pattern.compile("^=+(.+?)=+.+?(?=^=)", Pattern.MULTILINE | Pattern.DOTALL); /** * When picking random words in {@link #getRandomWord()}, we sometimes * encounter special articles or templates. This pattern ignores any words * like those, usually because they have ":" or other punctuation. */ private static final Pattern sInvalidWord = Pattern.compile("[^A-Za-z0-9 ]"); /** * {@link Uri} authority to use when creating internal links. */ public static final String WIKI_AUTHORITY = "wiktionary"; /** * {@link Uri} host to use when creating internal links. */ public static final String WIKI_LOOKUP_HOST = "lookup"; /** * Mime-type to use when showing parsed results in a {@link WebView}. */ public static final String MIME_TYPE = "text/html"; /** * Encoding to use when showing parsed results in a {@link WebView}. */ public static final String ENCODING = "utf-8"; /** * {@link Uri} to use when requesting a random page. */ private static final String WIKTIONARY_RANDOM = "http://en.wiktionary.org/w/api.php?action=query&list=random&format=json"; /** * Fake section to insert at the bottom of a wiki response before parsing. * This ensures that {@link #sSectionSplit} will always catch the last * section, as it uses section headers in its searching. */ private static final String STUB_SECTION = "\n=Stub section="; /** * Number of times to try finding a random word in {@link #getRandomWord()}. * These failures are usually when the found word fails the * {@link #sInvalidWord} test, or when a network error happens. */ private static final int RANDOM_TRIES = 3; /** * Internal class to hold a wiki formatting rule. It's mostly a wrapper to * simplify {@link Matcher#replaceAll(String)}. */ private static class FormatRule { private Pattern mPattern; private String mReplaceWith; /** * Create a wiki formatting rule. * * @param pattern Search string to be compiled into a {@link Pattern}. * @param replaceWith String to replace any found occurances with. This * string can also include back-references into the given * pattern. * @param flags Any flags to compile the {@link Pattern} with. */ public FormatRule(String pattern, String replaceWith, int flags) { mPattern = Pattern.compile(pattern, flags); mReplaceWith = replaceWith; } /** * Create a wiki formatting rule. * * @param pattern Search string to be compiled into a {@link Pattern}. * @param replaceWith String to replace any found occurances with. This * string can also include back-references into the given * pattern. */ public FormatRule(String pattern, String replaceWith) { this(pattern, replaceWith, 0); } /** * Apply this formatting rule to the given input string, and return the * resulting new string. */ public String apply(String input) { Matcher m = mPattern.matcher(input); return m.replaceAll(mReplaceWith); } } /** * List of internal formatting rules to apply when parsing wiki text. These * include indenting various bullets, apply italic and bold styles, and * adding internal linking. */ private static final List<FormatRule> sFormatRules = new ArrayList Other Android examples (source code examples)Here is a short list of links related to this Android ExtendedWikiHelper.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.