📄 encoder.java
字号:
try { ConversionFile = ReadFile(wapide.IDEFrame.class.getResource(cfname + ".txt").getFile()); } catch (NullPointerException nullerr) { JOptionPane.showMessageDialog(null, "Could not find conversion file.\n" + "Encoding of file stopped...", "Encoder: Fatal Error", JOptionPane.ERROR_MESSAGE); } // Type is known but not what version of WAP the file is using //String DataFile = null; //DataFile = ReadFile(FileName); int posStart = DataFile.indexOf("//DTD " + docType + " "); int posEnd = DataFile.indexOf("//EN", posStart); String Version = DataFile.substring(posEnd - 3, posEnd); // Write the proper WML beginning sequence posStart = ConversionFile.indexOf(Version); posEnd = ConversionFile.indexOf("\n", posStart); HexOutput = ConversionFile.substring(posStart + 4, posEnd); // now call convert to hex in order to convert the rest of the file HexOutput = ConvertToHex(HexOutput, docType); return HexOutput; } /** Having converted the main tag this method then proceeds to convert the remainder of the file into its Hexadecimal equivalent. This method converts both tags & their respective attributes and the data contained by these tags*/ private String ConvertToHex(String PrelimHexOutput, String docType) { String DataFile = fData; int check; String tmp = ""; int tmpPos = 0; int filePos = 0; String HexOutput = PrelimHexOutput; int pos = DataFile.indexOf("<wml"); if (pos > -1) DataFile = DataFile.substring(pos); else { JOptionPane.showMessageDialog(null, "Could not find start of wml content.\n" + "Encoding of file stopped...", "Encoder: Fatal Error", JOptionPane.ERROR_MESSAGE); return HexOutput; // major error } int LastTag = DataFile.indexOf("</" + docType); do { // everything within <wml>...</wml> tag is wml content // find the position of the next tag and convert everything // between that tag into hex values, since it is data tmpPos = DataFile.indexOf("<", filePos) + 1; tmp = DataFile.substring(filePos, tmpPos - 1); boolean hasData = true; if (tmp.indexOf("<") == 0) tmp = DataFile.substring(filePos + 1, tmpPos - 1); // Check and see if there is any text between the tags filePos = tmpPos; tmpPos = 0; check = 1; try { //tmp = tmp.substring(0, tmp.length() - 1); if (tmp.length() > 0) { HexOutput = HexOutput + ConvertData(tmp.trim()); } } catch (StringIndexOutOfBoundsException e) { ErrorMessage = ErrorMessage + e.getMessage() + "\n"; } // Having converted the data, try and get to the next tag and // process it tmpPos = DataFile.indexOf(">", filePos); tmp = DataFile.substring(filePos - 1, tmpPos + 1); int newTagPos = DataFile.indexOf("<", tmpPos + 1); int endNewTagPos = DataFile.indexOf(">", newTagPos); if (endNewTagPos > -1) { String checkTag = DataFile.substring(newTagPos + 1, endNewTagPos); //get the current tag int endingpos = tmp.indexOf("/>"); if (endingpos < 0) { endingpos = tmp.indexOf(">"); } if (endingpos < 0) { //error } String tagName = tmp.substring(1, endingpos).trim(); int spacer = tagName.indexOf(" "); if (spacer > -1) tagName = tagName.substring(0, spacer); //System.out.println("current:" + tagName); System.out.println(checkTag); if (("/" + tagName).equals(checkTag)) { System.out.println("match"); if (newTagPos - (tmpPos + 1) == 0) { hasData = false; System.out.println("has data"); } } } HexOutput = HexOutput + ProcessElement(tmp, hasData); //hasData = false; filePos = tmpPos + 1; } while (filePos < LastTag); return HexOutput; } /** A helper method to determine what file to call */ private String getConversionFileName(String data) { String fname = ""; //locate !DOCTYPE int startPos = data.indexOf("!DOCTYPE"); if (startPos > -1) { int endPos = data.indexOf(">", startPos); if (endPos > 0) { String type = data.substring(startPos, endPos); String loc = "http://www.wapforum.org/DTD/"; startPos = type.indexOf(loc); if (startPos > -1) { // need to change this to look for xml 1st and then dtd endPos = type.indexOf(".xml", startPos + loc.length()); if (endPos > -1) fname = type.substring(startPos + loc.length(), endPos); else { endPos = type.indexOf(".dtd", startPos + loc.length()); if (endPos > -1) fname = type.substring(startPos + loc.length(), endPos); } } } } return fname; } /** A helper method to help convert the elements and their attributes */ private String ProcessElement(String tagData, boolean content) { String HexOutput = ""; // if ending tag, just add 01 if (tagData.indexOf("</") > -1) return " 01"; // otherwise get the tag int endingpos = tagData.indexOf("/>"); if (endingpos < 0) { endingpos = tagData.indexOf(">"); } if (endingpos < 0) { return ""; } String tagName = tagData.substring(1, endingpos).trim(); int spacer = tagName.indexOf(" "); if (spacer > -1) tagName = tagName.substring(0, spacer); int CheckforAttribs = tagData.indexOf(" "); if (CheckforAttribs > -1) { // it has attributes int hasContent = tagData.indexOf("/>"); if ((hasContent == -1) && (content)) // Tag w/ Content & Attributes HexOutput = HexOutput + ConvertElement(tagName.trim(), 4); else // Atleast it has attributes HexOutput = HexOutput + ConvertElement(tagName.trim(), 3); String Attrib; int tmp; boolean NoMoreAttribs = false; do { // Extract the remaining Attributes // look for the end of the attribute tmp = tagData.indexOf("\" ", CheckforAttribs + 1); if (tmp == -1) {// then there is only 1 attribute NoMoreAttribs = true; tmp = tagData.indexOf(">"); } else // there is more than 1 attribute tmp++; Attrib = tagData.substring(CheckforAttribs + 1, tmp); HexOutput = HexOutput + ConvertAttrib(Attrib); CheckforAttribs = tmp; } while (NoMoreAttribs == false); HexOutput = HexOutput + " 01"; } else { // does it atleast have content??? int hasContent = tagData.indexOf("/"); if ((hasContent == -1) && (content)) { // Only Content HexOutput = HexOutput + ConvertElement(tagName.trim(), 2); } else { // No Content or attributes HexOutput = HexOutput + ConvertElement(tagName.trim(), 1); } } return HexOutput; } /** Helper method concerned only with converting wml tags */ private String ConvertElement(String ElementName, int value) { // Find the Element in the conversion file int pos = ConversionFile.indexOf("<" + ElementName + ">"); pos = ConversionFile.indexOf("=", pos); pos++; String HexValue = ConversionFile.substring(pos, pos + 2); value--; int decValue = HextoInt(HexValue); decValue = decValue + (value * 64); HexValue = Integer.toHexString(decValue); HexValue = HexValue.toUpperCase(); return " " + HexValue; } /** Helper method concerned only with converting individual attributes passed to it by the ProcessElement method. */ private String ConvertAttrib(String Attrib) { // First get the attribute w/o its data int EndofAttrib = Attrib.indexOf("="); String theAttrib = ""; try { theAttrib = Attrib.substring(0, EndofAttrib); } catch (IndexOutOfBoundsException att) { theAttrib = Attrib.substring(0, EndofAttrib + 1); } theAttrib = theAttrib.trim(); // then get data int StartofData = Attrib.indexOf("\"", EndofAttrib + 1) + 1; int EndofData = Attrib.indexOf("\"", StartofData + 1); String data = ""; try { data = Attrib.substring(StartofData, EndofData); } catch (StringIndexOutOfBoundsException strerr) {} //once you have data you get new attrib by pasting them together Attrib = theAttrib + "=\"" + data + "\""; // Having done that, locate it, w/ data, in the conversion file int start = ConversionFile.indexOf("'" + Attrib); if (start > -1) { int end = ConversionFile.indexOf("'", start + 1); theAttrib = ConversionFile.substring(end+2, end + 4); } else { // if it was not found, then locate it without data start = ConversionFile.indexOf("'" + theAttrib); int end = ConversionFile.indexOf("'", start + 1); theAttrib = ConversionFile.substring(end + 2, end + 4); if (data.trim().length() < 1) data = null; if (data == null) { theAttrib = theAttrib + " 03 00"; } else { theAttrib = theAttrib + ConvertData(data.trim()); } } return " " + theAttrib; } /** Helper method concerned with converting the text data found within the tag pairs. */ private String ConvertData(String Data) { Data = Data.trim(); int lenofData = Data.length(); int pos = 0; String HexOutput = ""; String temp = ""; String Sequence = ""; int tmpPos = 0; HexOutput = HexOutput + " 03"; if (lenofData == 0) return HexOutput + " 00"; try { do { temp = Data.substring(pos, pos + 1); if (temp.compareTo("$") == 0) { // escape sequence // check if the next character matches an escape sequence Sequence = Data.substring(pos + 1, pos + 2); if (Sequence.compareTo("(") == 0) { // A variable follows HexOutput = HexOutput + " 00 42"; tmpPos = Data.indexOf(")", pos); pos = pos + 2; // Puts pointer past the opening bracket do { // Convert the variable name into hex HexOutput = HexOutput + " " + StringtoHex(Data.substring(pos, pos + 1)); pos++; } while (pos < tmpPos); HexOutput = HexOutput + " 00 03"; pos++; } if (Sequence.compareTo("$") == 0) { // A $ is used HexOutput = HexOutput + " " + "24"; pos = pos + 2; } } else if (temp.compareTo("&") == 0) {// special characters String tmpString = ""; tmpPos = Data.indexOf(";", pos + 1); // find out where it ends tmpString = Data.substring(pos, tmpPos + 1); HexOutput = HexOutput + " " + ConvertSpecialChar(tmpString); pos = tmpPos + 1; } else { HexOutput = HexOutput + " " + StringtoHex(temp); pos++; } } while (pos < lenofData); } catch (StringIndexOutOfBoundsException strhalt) {} return HexOutput + " 00"; } /** Helper method for converting special characters into their respective Hex equivalents. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -