📄 keywordfinder.java
字号:
return attribArray.length; } /** * This method takes the raw data from a specially formatted file (wml.txt, * si.txt, or sl.txt) and then plucks out the special section of the raw data. * @param String data - the raw data from one of the specially formatted files. * the tags section of the raw data. * @return int containing the number of elements in the array. */ private int generateSpecialArray(String data) { int specialSize = 0; // look for the start of the attribute section int startPos = data.indexOf("&"); int endPos = data.length(); // Get the whole section with verbose information String tempData = data.substring(startPos, endPos - 3); // Count the lines StringTokenizer st = new StringTokenizer(tempData, "\n"); while (st.hasMoreTokens()) { specialSize++; st.nextToken(); } // Create the string array int count = 0; specialArray = new String[specialSize]; st = new StringTokenizer(tempData, "\n"); while (st.hasMoreTokens()) { specialArray[count] = st.nextToken(); specialArray[count] = specialArray[count].substring(0, specialArray[count].indexOf("=")); count++; } return specialArray.length; } /** Get the required file name based on the document type. * @param int docType - the type of document being created. * @return String - the file name to be used, filename.txt. */ private String getFileName(int docType) { switch (docType) { case 1: return "wml12.txt"; case 2: return "si.txt"; case 3: return "sl.txt"; } return ""; // in case of an error or an unsupported document type, such as text, graphics, etc. } /** * Compares words with those from the arrays and if it matches returns a special code. * The code is as follows: * 0 - word not found. * 1 - a tag was found (<wml>. * 2 - an attribute was found (type="unknown"). * 3 - a special tag was found (&123 or &nsbp). * @param String word - the word being typed and looked at. * @param int Type - the type of word searched for, (1) tag, (2) attribute, (3) special. * @return int - one of the numbers listed above. */ public int checkKeyword(String word, int type) { if (type == 1) {// look for the word in the tagArray for (int i = 0; i < tagCount; i++) if (word.equals(tagArray[i])) return 1; } if (type == 2) {// look for the word in the attribArray - found in tags for (int i = 0; i < attribCount; i++) if (word.equals(attribArray[i])) return 2; } if (type == 3) {// look for the word in the specialArray - found in text for (int i = 0; i < specialCount; i++) if (word.equals(specialArray[i])) return 3; } // Default return type return 0; } /** * Searches the entire document for keywords, used when updating the document * in part. * @param String doc - the whole document. * @param int offset - the strarting point of the insertion. * @return Vector - A string object, comma seperated, containing the start position, * end position, and the type of coloring required. */ public Vector scanDocument(String doc) { Vector v = new Vector(); v = scanTags(doc, v); v = scanSpecial(doc, v); v = scanComments(doc, v); return v; } /** * Searches for all the tags in the document, if it finds a tag it encodes it * accordingly. * @param String doc - the whole document string. * @param Vector v - the vector with position and color code information. * @return Vector - vector containing poition and color codes. */ private Vector scanTags(String doc, Vector v) { int pos = -1; while (pos < doc.length()) { pos = doc.indexOf("<", pos); if (pos > -1) { int start = pos; int end = doc.indexOf("<", pos + 1); if (end == -1) end = doc.length(); v = parseTag(start, end, doc, v); } else pos = doc.length(); pos++; } return v; } /** * Helper function for the scanTags function. Parses a tag into its respective colors. * @param int start - the start of the tag. * @param int end - the start of a new tag. * @param String doc - the whole document. * @param Vector v - the vector containing the formastting data. * @return Vector - the vector containing position and color coding data. */ private Vector parseTag(int start, int end, String doc, Vector v) { // Clean up the tag and see where it ends. String theTag = doc.substring(start, end); int checkClose = theTag.indexOf(">"); if (checkClose > -1) theTag = theTag.substring(0, checkClose + 1); if (theTag.indexOf("/>") > -1) theTag = theTag.substring(0, theTag.length() - 2); if (theTag.indexOf("?>") > -1) theTag = theTag.substring(0, theTag.length() - 2); if (theTag.indexOf(">") > -1) theTag = theTag.substring(0, theTag.length() - 1); // Now that it is cleaned up, identify the tag being referred too. StringTokenizer st = new StringTokenizer(theTag, " "); String tag = st.nextToken(); if (tag.indexOf("<?") == 0) //just a safety precaution tag = tag.substring(2); if (tag.indexOf("<!") == 0) //just a safety precaution tag = tag.substring(2); if (tag.indexOf("</") == 0) //just a safety precaution tag = tag.substring(2); if (tag.indexOf("<") == 0) //just a safety precaution tag = tag.substring(1); // Color the tag accordingly, if it turns out to be a tag. int keyword = checkKeyword(tag, 1); if (keyword == 1) { v.add(new Integer(theTag.indexOf(tag) + start)); v.add(new Integer(tag.length())); v.add(new Integer(1)); } StringTokenizer attribString = new StringTokenizer(" ", " "); // Now check the attributes (use style 2(attrib) and 5(string)) int pos = tag.length() + 1; while (st.hasMoreTokens()) { attribString = new StringTokenizer(st.nextToken() , "="); while (attribString.hasMoreTokens()) { String attrib = attribString.nextToken(); if (attrib.indexOf("\"") == -1) { //check it, its an attribute keyword = checkKeyword(attrib, 2); if (keyword == 2) { v.add(new Integer(theTag.indexOf(attrib, pos) + start)); v.add(new Integer(attrib.length())); v.add(new Integer(2)); } } else { // its a comment code accordingly v.add(new Integer(theTag.indexOf(attrib, pos) + start)); v.add(new Integer(attrib.length())); v.add(new Integer(5)); } pos = theTag.indexOf(attrib, pos); } } return v; } /** * Searches for all the special (&) tags in the document, if it finds a tag it encodes it * accordingly. * @param String doc - the whole document string. * @param Vector v - the vector with position and color code information. * @return Vector - vector containing poition and color codes. */ private Vector scanSpecial(String doc, Vector v) { int pos = -1; while (pos < doc.length()) { pos = doc.indexOf("&", pos); if (pos > -1) { int start = pos; int end = doc.indexOf(";", pos + 1); if (end == -1) end = doc.length(); else end++; String special = doc.substring(start, end); int keyword = checkKeyword(special, 3); if (keyword == 3) { v.add(new Integer(start)); v.add(new Integer(special.length())); v.add(new Integer(3)); } } else pos = doc.length(); pos++; } return v; } /** * Searches for all the comment tags in the document, if it finds a tag it encodes it * accordingly. * @param String doc - the whole document string. * @param Vector v - the vector with position and color code information. * @return Vector - vector containing poition and color codes. */ private Vector scanComments(String doc, Vector v) { int pos = -1; while (pos < doc.length()) { pos = doc.indexOf("<!--", pos); if (pos > -1) { int start = pos; int end = doc.indexOf("-->", pos + 1); if (end == -1) end = doc.length(); else end = end + 3; String comment = doc.substring(start, end); v.add(new Integer(start)); v.add(new Integer(comment.length())); v.add(new Integer(4)); } else pos = doc.length(); pos++; } return v; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -