📄 encoder.java
字号:
package wapide;import java.lang.*;import java.io.*;import java.util.*;import java.awt.*;import javax.swing.*;/** * This class converts a wml, si, or sl xml document's * tags and text into binary wml to increase the efficiency * of sending WAP documents over a narrow bandwidth. * * Copyright (c) 2003 * @author mark * @version 1.0 * * For License and contact information see WAPIDE.java */public class Encoder extends Object { /** * The path and the filename of the DataFile. */ private String FileName = null; /** * The conversion file picked by the encoder. */ private String ConversionFile = null; /** * Error messages collected during conversion process. */ private String ErrorMessage = ""; /** * Global file data. */ private String fData = ""; /** Basic Encoder constructor, just constructs the Encoder object */ public Encoder() { fData = ""; } /** Creates new Encoder Class and proceeds to encode the file. Takes in the FileName */ public Encoder(String fileName) { String DocType; FileName = fileName; // First determine what type of document this is DocType = DetermineFileType("wml"); if (DocType == "unknown") { DocType = DetermineFileType("si"); } if (DocType == "unknown") { DocType = DetermineFileType("sl"); } if (DocType == "unknown") { ErrorMessage = ErrorMessage + "NOT A VALID WAP FILE, plase specify a valid WAP file name.\n"; } // Convert the file into its Hex equivelant String HexOutput = PrelimConvertToHex(DocType); HexOutput = FinalizeHexStream(HexOutput, true);/* // For testing purposes only !!!!! System.out.println(); System.out.println("----- Hex Output -----"); System.out.println(); //Print the output for testing int x = 0; int loop = 0; do { loop++; if (loop == 10){ System.out.println(HexOutput.substring(x, x + 3)); loop = 0; } else System.out.print(HexOutput.substring(x, x + 3)); x = x + 3; } while (x < HexOutput.length() - 3); System.out.print(HexOutput.substring(HexOutput.length() - 2));*/ // Determine File Name String fName = fileName.substring(0, fileName.indexOf(".") + 1) + DocType + "c"; WriteFile(fName, HexOutput); } /** Encoder constructor with String Table Option Takes in the FileName, and the option to turn on/off the creation of a string table. */ public Encoder(String fileName, boolean option) { String DocType; FileName = fileName; // First determine what type of document this is DocType = DetermineFileType("wml"); if (DocType == "unknown") { DocType = DetermineFileType("si"); } if (DocType == "unknown") { DocType = DetermineFileType("sl"); } if (DocType == "unknown") { ErrorMessage = ErrorMessage + "NOT A VALID WAP FILE, plase specify a valid WAP file name.\n"; } // Convert the file into its Hex equivelant String HexOutput = PrelimConvertToHex(DocType); HexOutput = FinalizeHexStream(HexOutput, option); // Determine File Name String fName = fileName.substring(0, fileName.indexOf(".") + 1) + DocType + "c"; WriteFile(fName, HexOutput); } /** * Encodes text into byte code for display in an editor. * @param String type - the type of file: wml, si, or sl. * @param String data - the data to encode. * @return String txt - encoded text. */ public String Encode(String type, String data) { String DocType = type; //wml, si, or sl FileName = ""; fData = data; // Convert the file into its Hex equivelant String HexOutput = PrelimConvertToHex(DocType); HexOutput = FinalizeHexStream(HexOutput, true); return HexOutput; } /** * Encodes text into byte code for display in an editor. * @param String type - the type of file: wml, si, or sl. * @param boolean option - if true then a string table is used when encoding. * @param String data - the data to encode. * @return String txt - encoded text. */ public String Encode(String type, boolean option, String data) { String DocType = type; //wml, si, or sl FileName = ""; fData = data; // Convert the file into its Hex equivelant String HexOutput = PrelimConvertToHex(DocType); HexOutput = FinalizeHexStream(HexOutput, option); return HexOutput; } /** * Encodes data and then presents the byte-code as it would appear in a * hex viewer. * @param String type - the type of file: wml, si, or sl. * @param String data - the data to encode. * @return String txt - the hex-editor like text. */ public String GetHexOutput(String type, boolean option, String data) { String encodedStr = Encode(type, option, data); Vector byteArray = new Vector(); StringTokenizer st = new StringTokenizer(encodedStr, " "); int totalBytes = st.countTokens(); while (st.hasMoreTokens()) byteArray.add(st.nextToken()); // Create hex string vector Vector hexTable = new Vector(); int count = 1; String HexString = ""; for (int i = 0; i < byteArray.size(); i++) { if ((count < 8) || (count > 8)) HexString = HexString + (String) byteArray.get(i) + " "; if (count == 8) HexString = HexString + (String) byteArray.get(i) + " - "; if (count == 16) { hexTable.add(HexString); HexString = ""; count = 0; } count++; } if (count > 0) { int counter = 1; HexString = ""; for (int i = byteArray.size() - count + 1; i < byteArray.size(); i++) { if ((counter < 8) || (counter > 8)) HexString = HexString + (String) byteArray.get(i) + " "; if (counter == 8) HexString = HexString + (String) byteArray.get(i) + " - "; counter++; } for (int i = counter; i < 16; i++) HexString = HexString + " "; HexString = HexString + " "; hexTable.add(HexString); } // Create ascii vector Vector asciiTable = new Vector(); String asciiText = ""; count = 0; for (int i = 0; i < byteArray.size(); i++) { int value = HextoInt((String) byteArray.get(i)); String charString = new Character((char) value).toString(); asciiText = asciiText + charString; if (count == 15) { asciiTable.add(asciiText); count = 0; asciiText = ""; } count++; } if (count > 0) { asciiText = " "; // a space to fillin a missing space on the last line for (int i = byteArray.size() - count + 1; i < byteArray.size(); i++) { int value = HextoInt((String) byteArray.get(i)); String charString = new Character((char) value).toString(); asciiText = asciiText + charString; } asciiTable.add(asciiText); } // Create the hex line number vector Vector lineNumbers = new Vector(); String fillin = "00000000"; for (int i = 0; i < hexTable.size(); i++) { int value = i * 10; String hexValue = new String(new Integer(value).toString()); hexValue = fillin.substring(0, fillin.length() - hexValue.length()) + hexValue; lineNumbers.add(hexValue); } // put them all together to get a hex editor like display String FinalOutput = ""; for (int i = 0; i < hexTable.size(); i++) FinalOutput = FinalOutput + (String) lineNumbers.get(i) + " " + (String) hexTable.get(i) + " " + (String) asciiTable.get(i) + "\n"; return FinalOutput; } /** Opens and searches a file for the <wml>, <si>, or <sl> tag returns "wml", "si", or "sl". The defualt is "unknown" The reason for this round about way is that the file might have an extension other than wml, si, or sl*/ private String DetermineFileType(String fileType) { String str = null; str = ReadFile(FileName); int pos = str.indexOf("<" + fileType); if (pos > 0) { return fileType; } else { return "unknown"; } } /** Performs the actual conversion of the tags and their attributes into their two digit hex codes. Also converts text into its respective ASCII text code */ private String PrelimConvertToHex(String docType) { String DataFile = ""; String HexOutput = null; int FilePointer, FilePointerTmp; boolean HasAttributes = false; if (fData.equals("")) DataFile = ReadFile(FileName); else DataFile = fData; // if there are tabs they should be replaced by spaces StringTokenizer st = new StringTokenizer(DataFile, "\t"); String data = ""; while (st.hasMoreTokens()) { String tmp = st.nextToken(); data = data + tmp + " "; } DataFile = data; // if there are newlines they should be removed st = new StringTokenizer(DataFile, "\n"); data = ""; while (st.hasMoreTokens()) { String tmp = st.nextToken(); data = data + tmp; } DataFile = data; // remove white space b/w tags st = new StringTokenizer(DataFile, "<", true); data = ""; while (st.hasMoreTokens()) { String tmp = st.nextToken(); data = data + tmp.trim(); } DataFile = data; //lastly, remove all comments int pos = -1; do { pos = DataFile.indexOf("<!--"); if (pos > -1) { int end = DataFile.indexOf("-->"); if (end < 0) end = DataFile.length() - 3; String str1 = DataFile.substring(0, pos); String str2 = DataFile.substring(end + 3); DataFile = str1 + str2; } } while (pos > -1); fData = DataFile; // assign cleaned up file to global file data // call routine here to determine what version file to use based on DataFile String cfname = getConversionFileName(DataFile);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -