0

GitHub から JavaScript ファイルをダウンロードし、それを解析して文字列に変換し、MediaWiki サイトにプッシュする Java ベースのプログラムを作成しています。問題は、JS ファイルにかなり複雑な正規表現が含まれており、Java がそれをエスケープ文字に解析している (たとえば[^\S\r\n]、 に解析されている/\s) ことです。これらのエスケープ文字を有効にせずに文字列を保存する方法はありますか?

編集: コード:

package org.wikipedia.afchbuddy;

import java.io.IOException;
import static java.lang.System.*;
import java.nio.file.*;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.channels.*;
import java.net.URL;
import java.io.FileOutputStream;
import javax.security.auth.login.LoginException;

/**
 * This is a bot script designed to allow sysops to push releases for AFCH
 * on-wiki.
 *
 * @author Nathan2055
 */
public class AFCHBuddy {

    private static String username;
    private static String password;
    private static String location;
    private static String buffer;
    private static String error = "An error occured trying to access and write to Wikipedia. If you report this error, please provide the following debug info:";
    private static String basescript = "https://raw.github.com/WPAFC/afch/develop/src/";
    private static String basescript_stable = "https://raw.github.com/WPAFC/afch/master/src/";
    private static String mediawikilocation = "MediaWiki:Gadget-afchelper.js";

    /**
     * The main bot script.
     *
     * @param args This program accepts three command line arguments, 0 is the
     * push location (stable, beta, or testwiki), 1 is your username, and 2 is
     * your password.
     */
    public static void main(String[] args) {
        if (args.length != 3) {
            err.println("Incorrect number of arguments. Please see the README for more information.");
            System.exit(1);
        }

        location = args[0];
        username = args[1];
        password = args[2];

        switch (location) {
            case "testwiki":
                build("testwiki");
                break;
            case "beta":
                build("beta");
                break;
            case "stable":
                build("stable");
                break;
            default:
                err.println("Error: Unknown build location.");
                System.exit(1);
        }
    }

    /**
     * This method does the actual work of performing the edits.
     *
     * @param destarg The destination wiki.
     */
    private static void build(String destarg) {
        Wiki wiki;
        try {
            if ("testwiki".equals(destarg)) {
                wiki = new Wiki("test.wikipedia.org");
            } else {
                wiki = new Wiki();
            }

            wiki.login(username, password);

            downloadFile(basescript + "MediaWiki:Gadget-afchelper.js", "afch.js");
            buffer = readFile("afch.js");
            wiki.edit(mediawikilocation, buffer, "Update afch.js using AFCHBuddy");

            downloadFile(basescript + "MediaWiki:Gadget-afchelper.css", "afch.css");
            buffer = readFile("afch.css");
            wiki.edit("MediaWiki:Gadget-afchelper.css", buffer, "Update afch.css using AFCHBuddy");

            downloadFile(basescript + "core.js", "core.js");
            buffer = readFile("core.js");
            wiki.edit(mediawikilocation + "/core.js", buffer, "Update core.js using AFCHBuddy");

            downloadFile(basescript + "redirects.js", "redirects.js");
            buffer = readFile("redirects.js");
            wiki.edit(mediawikilocation + "/redirects.js", buffer, "Update redirects.js using AFCHBuddy");

            downloadFile(basescript + "submissions.js", "submissions.js");
            buffer = readFile("submissions.js");
            wiki.edit(mediawikilocation + "/submissions.js", buffer, "Update submissions.js using AFCHBuddy");

            downloadFile(basescript + "ffu.js", "ffu.js");
            buffer = readFile("ffu.js");
            wiki.edit(mediawikilocation + "/ffu.js", buffer, "Update ffu.js using AFCHBuddy");

            wiki.logout();

            err.println("Files syncronized successfully.");
            System.exit(0);
        } catch (IOException | LoginException e) {
            err.println(error);
            e.printStackTrace(err);
            System.exit(1);
        }
    }

    /**
     * Converts a file to a String.
     *
     * @param path This accepts the path of the file to be processed in String
     * form.
     * @return The output String.
     * @throws IOException The exception if something goes wrong while reading
     * the file.
     */
    private static String readFile(String path) throws IOException {
        byte[] encoded = Files.readAllBytes(Paths.get(path));
        return Charset.defaultCharset().decode(ByteBuffer.wrap(encoded)).toString();
    }

    /**
     * This downloads the input URL and saves it to dest.
     *
     * @param URL The file's location on the internet.
     * @param dest The file's soon to be location on your computer.
     * @throws IOException The exception if something goes wrong while
     * processing.
     */
    private static void downloadFile(String URL, String dest) throws IOException {
        URL website = new URL(URL);
        ReadableByteChannel rbc = Channels.newChannel(website.openStream());
        FileOutputStream fos = new FileOutputStream(dest);
        fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    }
}
4

0 に答える 0