Android Open Source - apps-android-commons Template Remove Modifier






From Project

Back to project page apps-android-commons.

License

The source code is released under:

Apache License

If you think the Android project apps-android-commons listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.wikimedia.commons.modifications;
/*w  w  w .  j av a 2  s  .com*/
import org.json.JSONException;
import org.json.JSONObject;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TemplateRemoveModifier extends PageModifier {

    public static final String MODIFIER_NAME = "TemplateRemoverModifier";

    public static final String PARAM_TEMPLATE_NAME = "template";

    public static final Pattern PATTERN_TEMPLATE_OPEN = Pattern.compile("\\{\\{");
    public static final Pattern PATTERN_TEMPLATE_CLOSE = Pattern.compile("\\}\\}");


    public TemplateRemoveModifier(String templateName) {
        super(MODIFIER_NAME);
        try {
            params.putOpt(PARAM_TEMPLATE_NAME, templateName);
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
    }

    public TemplateRemoveModifier(JSONObject data) {
        super(MODIFIER_NAME);
        this.params = data;
    }

    @Override
    public String doModification(String pageName, String pageContents) {
        String templateRawName = params.optString(PARAM_TEMPLATE_NAME);
        // Wikitext title normalizing rules. Spaces and _ equivalent
        // They also 'condense' - any number of them reduce to just one (just like HTML)
        String templateNormalized = templateRawName.trim().replaceAll("(\\s|_)+", "(\\s|_)+");

        // Not supporting {{ inside <nowiki> and HTML comments yet
        // (Thanks to marktraceur for reminding me of the HTML comments exception)
        Pattern templateStartPattern = Pattern.compile("\\{\\{" + templateNormalized, Pattern.CASE_INSENSITIVE);
        Matcher matcher = templateStartPattern.matcher(pageContents);

        while(matcher.find()) {
            int braceCount = 1;
            int startIndex = matcher.start();
            int curIndex = matcher.end();
            Matcher openMatch = PATTERN_TEMPLATE_OPEN.matcher(pageContents);
            Matcher closeMatch = PATTERN_TEMPLATE_CLOSE.matcher(pageContents);

            while(curIndex < pageContents.length()) {
                boolean openFound = openMatch.find(curIndex);
                boolean closeFound = closeMatch.find(curIndex);

                if(openFound && (!closeFound || openMatch.start() < closeMatch.start())) {
                    braceCount++;
                    curIndex = openMatch.end();
                } else if (closeFound) {
                    braceCount--;
                    curIndex = closeMatch.end();
                } else if (braceCount > 0) {
                    // The template never closes, so...remove nothing
                    curIndex = startIndex;
                    break;
                }

                if (braceCount == 0) {
                    // The braces have all been closed!
                    break;
                }
            }

            // Strip trailing whitespace
            while(curIndex < pageContents.length()) {
                if(pageContents.charAt(curIndex) == ' ' || pageContents.charAt(curIndex) == '\n') {
                    curIndex++;
                } else {
                    break;
                }
            }

            // I am so going to hell for this, sigh
            pageContents = pageContents.substring(0, startIndex) + pageContents.substring(curIndex);
            matcher = templateStartPattern.matcher(pageContents);
        }

        return pageContents;

    }

    @Override
    public String getEditSumary() {
        return "Removed template " + params.optString(PARAM_TEMPLATE_NAME) + ".";
    }
}




Java Source Code List

com.gst_sdk.GStreamer.java
com.gst_sdk.GStreamer.java
org.wikimedia.commons.AboutActivity.java
org.wikimedia.commons.CommonsApplication.java
org.wikimedia.commons.EventLog.java
org.wikimedia.commons.HandlerService.java
org.wikimedia.commons.LicenseList.java
org.wikimedia.commons.License.java
org.wikimedia.commons.MediaDataExtractor.java
org.wikimedia.commons.MediaWikiImageView.java
org.wikimedia.commons.Media.java
org.wikimedia.commons.Prefs.java
org.wikimedia.commons.SettingsActivity.java
org.wikimedia.commons.Utils.java
org.wikimedia.commons.WelcomeActivity.java
org.wikimedia.commons.api.MWApi.java
org.wikimedia.commons.auth.AuthenticatedActivity.java
org.wikimedia.commons.auth.LoginActivity.java
org.wikimedia.commons.auth.WikiAccountAuthenticatorService.java
org.wikimedia.commons.auth.WikiAccountAuthenticator.java
org.wikimedia.commons.campaigns.CampaignActivity.java
org.wikimedia.commons.campaigns.CampaignContribution.java
org.wikimedia.commons.campaigns.Campaign.java
org.wikimedia.commons.campaigns.CampaignsContentProvider.java
org.wikimedia.commons.campaigns.CampaignsListAdapter.java
org.wikimedia.commons.campaigns.CampaignsSyncAdapter.java
org.wikimedia.commons.campaigns.CampaignsSyncService.java
org.wikimedia.commons.category.CategorizationFragment.java
org.wikimedia.commons.category.CategoryContentProvider.java
org.wikimedia.commons.category.Category.java
org.wikimedia.commons.contributions.ContributionController.java
org.wikimedia.commons.contributions.ContributionViewHolder.java
org.wikimedia.commons.contributions.Contribution.java
org.wikimedia.commons.contributions.ContributionsActivity.java
org.wikimedia.commons.contributions.ContributionsContentProvider.java
org.wikimedia.commons.contributions.ContributionsListAdapter.java
org.wikimedia.commons.contributions.ContributionsListFragment.java
org.wikimedia.commons.contributions.ContributionsSyncAdapter.java
org.wikimedia.commons.contributions.ContributionsSyncService.java
org.wikimedia.commons.contributions.MediaListAdapter.java
org.wikimedia.commons.data.DBOpenHelper.java
org.wikimedia.commons.media.CategoryImagesLoader.java
org.wikimedia.commons.media.MediaDetailFragment.java
org.wikimedia.commons.media.MediaDetailPagerFragment.java
org.wikimedia.commons.media.MediaDetailSpacer.java
org.wikimedia.commons.modifications.CategoryModifier.java
org.wikimedia.commons.modifications.ModificationsContentProvider.java
org.wikimedia.commons.modifications.ModificationsSyncAdapter.java
org.wikimedia.commons.modifications.ModificationsSyncService.java
org.wikimedia.commons.modifications.ModifierSequence.java
org.wikimedia.commons.modifications.PageModifier.java
org.wikimedia.commons.modifications.TemplateRemoveModifier.java
org.wikimedia.commons.upload.MultipleShareActivity.java
org.wikimedia.commons.upload.MultipleUploadListFragment.java
org.wikimedia.commons.upload.ShareActivity.java
org.wikimedia.commons.upload.SingleUploadFragment.java
org.wikimedia.commons.upload.UploadController.java
org.wikimedia.commons.upload.UploadService.java