⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 decoder.java

📁 WAP ide 代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package wapide;

import java.io.*;
import java.lang.*;
import java.awt.*;
import javax.swing.*;

/**
 * This class decodes a wml, si, or sl xml document's
 * tags into a text based wml file.
 *
 * Copyright (c) 2003
 * @author  Mark
 * @version 1.0
 *
 * For License and contact information see WAPIDE.java
 */
public class Decoder extends Object {

    private String FileName = "";
    private String ErrorMessage = "";
    private String ConversionFile = "";
    private boolean ContinueDecode = true;

    /** Creates new Decoder */
    public Decoder() {

    }

    /** Creates new Decoder */
    public Decoder(String fileName) {

        FileName = fileName;
        String file = ReadFile(FileName);
        String TextData = PrelimDecode(file);
        if (ContinueDecode) {
          TextData = TextData + ConvertFile(file);
          WriteFile(FileName.substring(0, FileName.length() - 1), TextData);
        }
        else {
          WriteFile(FileName.substring(0, FileName.length() - 1), "");
        }
    }

    /** Decodes a file and returns the text representation of the byte values of the file */
    public String Decode(String fileName) {

        FileName = fileName;
        String file = ReadFile(FileName);
        String TextData = PrelimDecode(file);
        if (ContinueDecode) {
          TextData = TextData + ConvertFile(file);
          return TextData;
        }
        return "";
    }

    /** This method converts the 1st two hex digits into the
    appropriate header for the wml, si, or sl document */
    private String PrelimDecode(String DataFile) {

        try {
          String fileType = DataFile.substring(3, 5);
          String HeaderFile = ReadaFile(wapide.IDEFrame.class.getResource("headers.txt").getFile());
          int pos = HeaderFile.indexOf(fileType);
          if (pos > -1) {
            pos = HeaderFile.indexOf("\n", pos);
            String Header = HeaderFile.substring(pos + 1, HeaderFile.indexOf("\">", pos + 1)+2);
            String cfname = getConversionFileName(Header);
            try {
              ConversionFile = ReadaFile(wapide.IDEFrame.class.getResource(cfname + ".txt").getFile());
              ContinueDecode = true;
            }
            catch (NullPointerException nullerr) {
              ContinueDecode = false;
              JOptionPane.showMessageDialog(null, "Could not find conversion file.\n" +
                                        "Decoding of file stopped...",
                                        "Decoder: Fatal Error",
                                        JOptionPane.ERROR_MESSAGE);
            }
            return Header + "\n\n";
          }
          else {
            ErrorMessage = ErrorMessage + "Invalid File Type\n";
            return "";
          }
        }
        catch (StringIndexOutOfBoundsException strerr) {
          ContinueDecode = false;
          return "";
        }
    }

    /** This method performs the actual file conversion from hex to text */
    private String ConvertFile(String DataFile) {

        // Get the len of the string table so as to determine the
        // start of the wml or si or sl tag
        int TableLen = HextoInt(DataFile.substring(9, 11));

        // Move the file pointer to the 1st tag position
        int pos = 12 + TableLen * 3;
        String HexValue = "";
        String TextStr = "";
        int type = 0;
        int Level = -1;
        Stack TagStack = new Stack();
        int counter = 0;
        boolean Attribs = false;
        String temp ="";

        do {
            HexValue = DataFile.substring(pos, pos + 2);
            // Process the hex value & determine if it is a tag, attrib, or text
            type = ProcessHexValue(HexValue, Attribs);

            switch (type) {
                case 0:
                    temp = "";
                    if (HexValue.compareTo("03") == 0) { // inline string
                        temp = DataFile.substring(pos + 3, DataFile.indexOf(" 00", pos));
                        pos = DataFile.indexOf(" 00", pos) + 4;
                    }
                    if (HexValue.compareTo("83") == 0) { // string table
                        pos = pos + 3;
                        temp = GetStringFromTable(HextoInt(DataFile.substring(pos, pos + 2)), DataFile);
                        pos = pos + 3;
                    }
                    if (Attribs)
                        TextStr = TextStr + ProcessString(temp, Attribs);
                    else {
                        TextStr = TextStr + ProcessString(temp, Attribs);
                    }
                    break;
                case 1: // no attributes or content
                    Level++;
                    TextStr = TextStr + ProcessTag(HexValue) + "/>\n" + InsertTabs(Level);
                    Level--;
                    pos = pos + 3;
                    break;
                case 2: // Content only
                    Level++;
                    TextStr = TextStr + "\n" + InsertTabs(Level) + ProcessTag(HexValue) + ">\n";
                    Level++;
                    TextStr =  TextStr + InsertTabs(Level);
                    Level--;
                    TagStack.Push("\n" + InsertTabs(Level) + "</" + ProcessTag(HexValue).substring(1) + ">\n");
                    pos = pos + 3;

                    break;
                case 3: // Attributes Only
                    Level++;
                    TextStr = TextStr + ProcessTag(HexValue) + " ";
                    TagStack.Push("/>\n" + InsertTabs(Level));
                    Attribs = true;
                    pos = pos + 3;
                    break;
                case 4: // Content & attributes
                    Level++;
                    TextStr = TextStr + "\n" + InsertTabs(Level) + ProcessTag(HexValue) + " ";
                    TagStack.Push("\n" + InsertTabs(Level) + "</" + ProcessTag(HexValue).substring(1) + ">");
                    Level++;
                    TagStack.Push(">\n" + InsertTabs(Level));
                    //Level--;
                    Attribs = true;
                    pos = pos + 3;
                    break;
                case 5:
                    temp = "";
                    if (HexValue.compareTo("42") == 0) { // inline variable string
                        temp = DataFile.substring(pos + 3, DataFile.indexOf(" 00", pos));
                        pos = DataFile.indexOf(" 00", pos) + 4;
                    }
                    if (HexValue.compareTo("82") == 0) { // string table
                        pos = pos + 3;
                        temp = GetStringFromTable(HextoInt(DataFile.substring(pos, pos + 2)), DataFile);
                        pos = pos + 3;
                    }
                    if (Attribs)
                        TextStr = TextStr + "=(" + ProcessString(temp, false) + ")";
                    else
                        TextStr = TextStr + "(" + ProcessString(temp, false) + ")";
                    break;
                case 6:
                    Level--;
                    TextStr = TextStr + TagStack.Pop();
                    Attribs = false;
                    pos = pos + 3;
                    break;
                case 7: // Process the Attributes
                    TextStr = TextStr + ProcessAttribute(HexValue);
                    pos = pos + 3;
            }
            counter++;
        }
        //while (counter < 4);
        while (pos < DataFile.length());
        return TextStr;

    }

    /** Check on the Hex Value to determine if it is a tag, an attribute,
    a string, a variable, or just a closing tag */
    private int ProcessHexValue(String HexValue, boolean ProcessAttrib) {

        // check to see if it is the start of string or variable
        if (HexValue.compareTo("03") == 0 || HexValue.compareTo("83") == 0)
            return 0; //its a string
        if (HexValue.compareTo("82") == 0 || HexValue.compareTo("42") == 0)
            return 5; //its a variable
        if (HexValue.compareTo("01") == 0)
            return 6; //its an end tag;
        if (ProcessAttrib)
            return 7;
        else {  // If its not, then its a tag
            String TagType = HexValue.substring(1, 2);
            String TagClass = HexValue.substring(0, 1);
            String SearchTag="";
            int Class = HextoInt("0" + TagClass);

            if (Class == 0 || Class == 1 || Class == 2 || Class == 3)
                return 1;
            if (Class == 4 || Class == 5 || Class == 6 || Class == 7)
                return 2;
            if (Class == 8 || Class == 9 || Class == 10 || Class == 11)
                return 3;
            if (Class == 12 || Class == 13 || Class == 14 || Class == 15)
                return 4;
        }

        return 8;// unknown
    }

    /** Converts the hex value of this tag into its string representation */
    private String ProcessTag(String HexValue) {

        String TagType = HexValue.substring(1, 2);
        String TagClass = HexValue.substring(0, 1);
        String SearchTag="";
        int Class = HextoInt("0" + TagClass);

        if (Class == 3 || Class == 7 || Class == 11 || Class == 15)
           SearchTag = "3" + TagType;
        if (Class == 2 || Class == 6 || Class == 10 || Class == 14)
           SearchTag = "2" + TagType;
        if (Class == 1 || Class == 5 || Class == 9 || Class == 13)
           SearchTag = "1" + TagType;
        if (Class == 0 || Class == 4 || Class == 8 || Class == 12)
           SearchTag = "0" + TagType;

        // Start of the tag section
        int StartTagSec = ConversionFile.indexOf("<");
        // End of the tag section
        int EndTagSec = ConversionFile.indexOf("/", StartTagSec) - 3;

        // look for the tag in the tag section of the conversion file
        String TagSection = ConversionFile.substring(StartTagSec, EndTagSec);
        int HexTag = TagSection.indexOf(SearchTag) - 2; //takes out the = sign & >
        int StartofTextTag = SearchBackwards(HexTag, "\n", TagSection);
        String theTag = TagSection.substring(StartofTextTag, HexTag);
        return theTag;
    }

    /** Converts the hex value of this attribute into its string representation */
    private String ProcessAttribute(String HexValue) {

        // Start of the attribute section
        int StartAttribSec = ConversionFile.indexOf("'");
        // End of the attribute section
        int EndAttribSec = ConversionFile.indexOf("/////", StartAttribSec) - 3;
        String AttribSection = ConversionFile.substring(StartAttribSec, EndAttribSec);
        int HexTag = AttribSection.indexOf(HexValue) - 1; //takes out the = sign
        int StartofTextAttrib = SearchBackwards(HexTag, "\n", AttribSection);
        String theAttrib = "";
        try {
          theAttrib = AttribSection.substring(StartofTextAttrib, HexTag);
          return " " + theAttrib.substring(1, theAttrib.length() -1);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -