📄 keywordfinder.java
字号:
package wapide;import javax.swing.*;import java.text.*;import java.io.*;import java.util.*;import java.lang.*;/** * This class is a helper class for the IDE. It looks up keywords in the * file specified. The calling class specifies the file to look in, the section * to search in, and the keyword being looked for. This class then returns an * int expression, the number pertaining to what category the search was for. * A result of 0 means nothing was found. * Copyright (c) 2003 * @author Mark Busman * @version 1.0 * * For License and contact information see WAPIDE.java */public class KeywordFinder { /** * Stores the tags used for the document type. */ private String tagArray[]; /** * Stores the attributes allows for the docuemnt type. */ private String attribArray[]; /** * Stores any special symbols allowed by the document type such as &num, etc. */ private String specialArray[]; /** * Stores the number of tags found. */ private int tagCount; /** * Stores the number of attributes found. */ private int attribCount; /** * Stores the number of special elements found such as . */ private int specialCount; /** * The WML Version number foud, default is 1.0. */ private double WMLVersionNumber = 1.0; /** * Constructor - The default constructor used mainly for testing purposes. */ public KeywordFinder() { String fName = "/home/cougar/WAP/src/wap/wml.txt"; String rawData = readFile(fName); tagCount = generateTagArray(rawData); attribCount = generateAttribArray(rawData); specialCount = generateSpecialArray(rawData); } /** * Constructor - Used in the SyntaxDocument class to create an instance of this object. * @param int docType - the type of document (1) wml, (2) si, (3) sl, (4) wmls. */ public KeywordFinder(int docType) { try { if (docType > 0) { String fName = wapide.IDEFrame.class.getResource(getFileName(docType)).getFile(); if (!(fName.equals(""))) { String rawData = readFile(fName); if (docType == 1) WMLVersionNumber = 1.2; tagCount = generateTagArray(rawData); attribCount = generateAttribArray(rawData); specialCount = generateSpecialArray(rawData); } } } catch (StringIndexOutOfBoundsException strerr) {} catch (NullPointerException nullerr) {} } /** * Constructor - Used in the SyntaxDocument class to create an instance of this object. * @param String doctype - the type of document (1) wml, (2) si, (3) sl, (4) wmls. */ public KeywordFinder(String docType) { try { if (docType.length() > 0) { String fName = wapide.IDEFrame.class.getResource(docType + ".txt").getFile(); if (!(fName.equals(""))) { String rawData = readFile(fName); if (fName.toUpperCase().indexOf("WML") > -1) setWMLVersionNumber(rawData); tagCount = generateTagArray(rawData); attribCount = generateAttribArray(rawData); specialCount = generateSpecialArray(rawData); } } } catch (StringIndexOutOfBoundsException strerr) {} catch (NullPointerException nullerr) {} } public double getWMLVersion() { return WMLVersionNumber; } /** Main method used for testing purposes. */ //public static void main(String[] args) { //KeywordFinder keywordFinder1 = new KeywordFinder(); //} /** * Get the list of supported tags. * @return v - A vector with a list of tags that are supported. */ public Vector getTags() { Vector tags = new Vector(); for (int x = 0; x < tagArray.length; x++) tags.add(tagArray[x]); return tags; } /** * Get the list of supported attributes. * @return v - A vector with a list of attributes that are supported. */ public Vector getAttributes() { Vector attribs = new Vector(); for (int x = 0; x < attribArray.length; x++) attribs.add(attribArray[x]); return attribs; } /** * Get the list of supported special characters. * @return v - A vector with a list of characters that are supported. */ public Vector getSpecial() { Vector special = new Vector(); for (int x = 0; x < specialArray.length; x++) { special.add(specialArray[x]); } return special; } /** * Reads text files such as the conversion files and the header files. * @param fileName - the file name and path of the file to be read. * @return str - the data of the file stored as a string. */ private String readFile(String fileName) { FileInputStream fis = null; String str = null; try { fis = new FileInputStream (fileName); int size = fis.available (); byte[] bytes = new byte [size]; fis.read (bytes); str = new String (bytes); fis.close (); } catch (IOException e) { } return str; } /** * Used to set the WML version number in use as specified by the DOCTYPE * attribute. This ensures the proper file is loaded. * @param String rawData - the text to scan for the appropriate wml version being * used in the document. */ private void setWMLVersionNumber(String rawData) { String search = "// WAP Version Startups\n"; int pos = rawData.indexOf(search); if (pos > -1) { pos = pos + search.length(); int end = rawData.indexOf(" ", pos); if (end > -1) { String num = rawData.substring(pos, end).trim(); double d = new Double(num).doubleValue(); WMLVersionNumber = d; } else { WMLVersionNumber = 1.2; } } else { WMLVersionNumber = 1.0; } } /** * This method takes the raw data from a specially formatted file (wml.txt, * si.txt, or sl.txt) and then plucks out the tag section of the raw data. * @param String data - the raw data from one of the specially formatted files. * section of the raw data. * @return int containing the number of elements in the array. */ private int generateTagArray(String data) { int tagSize = 0; // look for the start of the tag section int startPos = data.indexOf("<"); int endPos = data.indexOf("////////////", startPos + 2) - 2; // Get the whole section with verbose information String tempData = data.substring(startPos, endPos); // Count the lines StringTokenizer st = new StringTokenizer(tempData, "\n"); while (st.hasMoreTokens()) { tagSize++; st.nextToken(); } // Create the string array int count = 0; tagSize = tagSize + 2; tagArray = new String[tagSize]; st = new StringTokenizer(tempData, "\n"); while (st.hasMoreTokens()) { tagArray[count] = st.nextToken(); tagArray[count] = tagArray[count].substring(1, tagArray[count].indexOf("=") - 1); count++; } tagArray[count] = "xml"; count++; tagArray[count] = "DOCTYPE"; return tagArray.length; } /** * This method takes the raw data from a specially formatted file (wml.txt, * si.txt, or sl.txt) and then plucks out the attributes section of the raw data. * @param String data - the raw data from one of the specially formatted files. * section of the raw data. * @return int containing the number of elements in the array. */ private int generateAttribArray(String data) { int attribSize = 0; // look for the start of the attribute section int startPos = data.indexOf("'"); int endPos = data.indexOf("////////////", startPos + 2) - 2; // Get the whole section with verbose information String tempData = data.substring(startPos, endPos); // Count the lines StringTokenizer st = new StringTokenizer(tempData, "\n"); while (st.hasMoreTokens()) { attribSize++; st.nextToken(); } // Create the string array int count = 0; attribSize = attribSize + 2; attribArray = new String[attribSize]; st = new StringTokenizer(tempData, "\n"); while (st.hasMoreTokens()) { attribArray[count] = st.nextToken(); attribArray[count] = attribArray[count].substring(1, attribArray[count].indexOf("'=")); if (attribArray[count].indexOf("=") > -1) // if there is an = sign then drop it attribArray[count] = attribArray[count].substring(0, attribArray[count].indexOf("=")); count++; } attribArray[count] = "PUBLIC"; count++; attribArray[count] = "version";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -