📄 main.java
字号:
/* -*- Mode: java; tab-width: 4; indent-tabs-mode: 1; c-basic-offset: 4 -*- * * The contents of this file are subject to the Netscape Public * License Version 1.1 (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.mozilla.org/NPL/ * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * The Original Code is Rhino code, released * May 6, 1999. * * The Initial Developer of the Original Code is Netscape * Communications Corporation. Portions created by Netscape are * Copyright (C) 1997-1999 Netscape Communications Corporation. All * Rights Reserved. * * Contributor(s): * Igor Bukanov * * Alternatively, the contents of this file may be used under the * terms of the GNU Public License (the "GPL"), in which case the * provisions of the GPL are applicable instead of those above. * If you wish to allow use of your version of this file only * under the terms of the GPL and not to allow others to use your * version of this file under the NPL, indicate your decision by * deleting the provisions above and replace them with the notice * and other provisions required by the GPL. If you do not delete * the provisions above, a recipient may use your version of this * file under either the NPL or the GPL. */package org.mozilla.javascript.tools.idswitch;import java.io.*;import java.util.*;import java.text.SimpleDateFormat;import org.mozilla.javascript.EvaluatorException;import org.mozilla.javascript.tools.ToolErrorReporter;public class Main { private static final String PROGRAM_NAME = "Main"; private static final String SWITCH_TAG_STR = "string_id_map"; private static final String GENERATED_TAG_STR = "generated"; private static final String STRING_TAG_STR = "string"; private static final int NORMAL_LINE = 0, SWITCH_TAG = 1, GENERATED_TAG = 2, STRING_TAG = 3; private final Vector all_pairs = new Vector(); private ToolErrorReporter R; private CodePrinter P; private FileBody body; private String source_file; private int tag_definition_end; private int tag_value_start; private int tag_value_end; private static boolean is_value_type(int id) { if (id == STRING_TAG) { return true; } return false; } private static String tag_name(int id) { switch (id) { case SWITCH_TAG: return SWITCH_TAG_STR; case -SWITCH_TAG: return "/" + SWITCH_TAG_STR; case GENERATED_TAG: return GENERATED_TAG_STR; case -GENERATED_TAG: return "/" + GENERATED_TAG_STR; } return ""; } void process_file(String file_path) throws IOException { source_file = file_path; body = new FileBody(); InputStream is; if (file_path.equals("-")) { is = System.in; } else { is = new FileInputStream(file_path); } try { Reader r = new InputStreamReader(is, "ASCII"); body.readData(r); } finally { is.close(); } process_file(); if (body.wasModified()) { OutputStream os; if (file_path.equals("-")) { os = System.out; } else { os = new FileOutputStream(file_path); } try { Writer w = new OutputStreamWriter(os); body.writeData(w); w.flush(); } finally { os.close(); } } } private void process_file() throws IOException { int cur_state = 0; char[] buffer = body.getBuffer(); int generated_begin = -1, generated_end = -1; int time_stamp_begin = -1, time_stamp_end = -1; body.startLineLoop(); L:while (body.nextLine()) { int begin = body.getLineBegin(); int end = body.getLineEnd(); int tag_id = extract_line_tag_id(buffer, begin, end); boolean bad_tag = false; switch (cur_state) { case 0: if (tag_id == SWITCH_TAG) { cur_state = SWITCH_TAG; all_pairs.removeAllElements(); generated_begin = -1; } else if (tag_id == -SWITCH_TAG) { bad_tag = true; } break; case SWITCH_TAG: if (tag_id == 0) { look_for_id_definitions(buffer, begin, end, false); } else if (tag_id == STRING_TAG) { look_for_id_definitions(buffer, begin, end, true); } else if (tag_id == GENERATED_TAG) { if (generated_begin >= 0) { bad_tag = true; } else { cur_state = GENERATED_TAG; time_stamp_begin = tag_definition_end; time_stamp_end = end; } } else if (tag_id == -SWITCH_TAG) { cur_state = 0; if (generated_begin >= 0 && !all_pairs.isEmpty()) { generate_java_code(); String code = P.toString(); boolean different = body.setReplacement (generated_begin, generated_end, code); if (different) { String stamp = get_time_stamp(); body.setReplacement (time_stamp_begin, time_stamp_end, stamp); } } break; } else { bad_tag = true; } break; case GENERATED_TAG: if (tag_id == 0) { if (generated_begin < 0) { generated_begin = begin; } } else if (tag_id == -GENERATED_TAG) { if (generated_begin < 0) { generated_begin = begin; } cur_state = SWITCH_TAG; generated_end = begin; } else { bad_tag = true; } break; } if (bad_tag) { String text = ToolErrorReporter.getMessage( "msg.idswitch.bad_tag_order", tag_name(tag_id)); throw R.runtimeError (text, source_file, body.getLineNumber(), null, 0); } } if (cur_state != 0) { String text = ToolErrorReporter.getMessage( "msg.idswitch.file_end_in_switch", tag_name(cur_state)); throw R.runtimeError (text, source_file, body.getLineNumber(), null, 0); } } private String get_time_stamp() { SimpleDateFormat f = new SimpleDateFormat (" 'Last update:' yyyy-MM-dd HH:mm:ss z"); String dateString = f.format(new Date()); return f.format(new Date()); } private void generate_java_code() { P.clear(); IdValuePair[] pairs = new IdValuePair[all_pairs.size()]; all_pairs.copyInto(pairs); SwitchGenerator g = new SwitchGenerator(); g.char_tail_test_threshold = 2; g.setReporter(R); g.setCodePrinter(P); g.generateSwitch(pairs, "0"); } private int extract_line_tag_id(char[] array, int cursor, int end) { int id = 0; cursor = skip_white_space(array, cursor, end); int after_leading_white_space = cursor; cursor = look_for_slash_slash(array, cursor, end); if (cursor != end) { boolean at_line_start = (after_leading_white_space + 2 == cursor); cursor = skip_white_space(array, cursor, end); if (cursor != end && array[cursor] == '#') { ++cursor; boolean end_tag = false; if (cursor != end && array[cursor] == '/') { ++cursor; end_tag = true; } int tag_start = cursor; for (; cursor != end; ++cursor) { int c = array[cursor]; if (c == '#' || c == '=' ||is_white_space(c)) { break; } } if (cursor != end) { int tag_end = cursor; cursor = skip_white_space(array, cursor, end); if (cursor != end) { int c = array[cursor]; if (c == '=' || c == '#') { id = get_tag_id (array, tag_start, tag_end, at_line_start); if (id != 0) { String bad = null; if (c == '#') { if (end_tag) { id = -id; if (is_value_type(id)) { bad = "msg.idswitch.no_end_usage"; } } tag_definition_end = cursor + 1; } else { if (end_tag) { bad = "msg.idswitch.no_end_with_value"; } else if (!is_value_type(id)) { bad = "msg.idswitch.no_value_allowed"; } id = extract_tag_value (array, cursor + 1, end, id); } if (bad != null) { String s = ToolErrorReporter.getMessage( bad, tag_name(id)); throw R.runtimeError (s, source_file, body.getLineNumber(), null, 0); } } } } } } } return id; }// Return position after first of // or end if not found private int look_for_slash_slash(char[] array, int cursor, int end) { while (cursor + 2 <= end) { int c = array[cursor++]; if (c == '/') { c = array[cursor++];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -