GenerateBasicMethods { public static String generatePreprocessorCode(String filename, String behCodeType) {{ Formatter f = new Formatter(filename, "Beh", behCodeType); f.open(); f.startClass("Preprocessor"); f.startMethod("public static String process(String input)" + " throws Exception"); f.writeTextLine("String output = input;"); f.skipLine(); f.writeTextLine("output = removeComments(output);"); f.writeTextLine("String nsPrefixes[] = getNamespacePrefixes(output);"); f.writeTextLine("output = removeNilAttribute(output, nsPrefixes);"); f.writeTextLine("output = removeNsPrefixes(output, nsPrefixes);"); f.writeTextLine("output = convertShorthandSyntax(output);"); f.skipLine(); f.writeTextLine("saveOutputInFile(output);"); f.skipLine(); f.writeTextLine("return output;"); f.endMethod(); f.skipLine(); f.startMethod("public static void saveOutputInFile(String output)" + " throws Exception"); f.writeTextLine("String filename = \"X2Jinput.pp\";"); f.writeTextLine("try {", '+'); f.writeTextLine("BufferedWriter file = new BufferedWriter(new FileWriter(filename));"); f.writeTextLine("file.write(output);"); f.writeTextLine("file.close();", '-'); f.writeTextLine("}"); f.writeTextLine("catch (IOException e) {", '+'); f.writeTextLine("throw new Exception(\"Could not save preprocessed content in file: '\" + filename + \"'\");", '-'); f.writeTextLine("}"); f.endMethod(); f.skipLine(); f.skipLine(); f.startVerbatimCode(); addPreprocessorMethods(f); f.endVerbatimCode(); f.endClass("Preprocessor"); f.close(); return f.getErrors(); }} public static void addPreprocessorMethods(Formatter f) {{ f.writeWithNoIndent( " private static String removeComments(String input) {\n" + " String output = input;\n" + " int startIndex = 0;\n" + " for (int startTagIndex = output.indexOf(\"\", startTagIndex+4);\n" + " if (endTagIndex > startTagIndex)\n" + " {\n" + " endTagIndex = skipWhitespace(output, endTagIndex+3);\n" + " output = output.substring(0, startTagIndex) +\n" + " output.substring(endTagIndex);\n" + " startIndex = startTagIndex;\n" + " }\n" + " }\n" + " return output;\n" + " }\n" + "\n" + " private static String[] getNamespacePrefixes(String input) {\n" + " Vector prefixVector = new Vector();\n" + " int startPatternIndex, endPatternIndex;\n" + "\n" + " String searchRegion = input;\n" + " // First, skip over the tag in the input\n" + " startPatternIndex = input.indexOf(\"= 0)\n" + " {\n" + " startPatternIndex =\n" + " input.toLowerCase().indexOf(\"xml\", startPatternIndex+2);\n" + " if (startPatternIndex >= 0)\n" + " {\n" + " endPatternIndex = input.indexOf(\"?>\", startPatternIndex+3);\n" + " if (endPatternIndex > startPatternIndex)\n" + " {\n" + " startPatternIndex = input.indexOf(\"<\", endPatternIndex+2);\n" + " if (startPatternIndex > endPatternIndex)\n" + " searchRegion =\n" + " input.substring(startPatternIndex, input.length());\n" + " }\n" + " }\n" + " }\n" + "\n" + " // Now locate the start of the root element\n" + " int regionStart = searchRegion.indexOf(\"<\");\n" + " int regionEnd = searchRegion.indexOf(\">\", regionStart);\n" + " searchRegion = searchRegion.substring(regionStart, regionEnd+1);\n" + "\n" + " // And then search for the namespace prefixes\n" + " int startIndex = 0;\n" + " for (startPatternIndex = searchRegion.indexOf(\"xmlns:\", startIndex);\n" + " startPatternIndex >= 0;\n" + " startPatternIndex = searchRegion.indexOf(\"xmlns:\", startIndex))\n" + " {\n" + " startPatternIndex = startPatternIndex+6;\n" + " endPatternIndex = startPatternIndex;\n" + " while ((endPatternIndex < searchRegion.length()) &&\n" + " ((searchRegion.charAt(endPatternIndex) >= 'A' &&\n" + " searchRegion.charAt(endPatternIndex) <= 'Z') ||\n" + " (searchRegion.charAt(endPatternIndex) >= 'a' &&\n" + " searchRegion.charAt(endPatternIndex) <= 'z') ||\n" + " (searchRegion.charAt(endPatternIndex) >= '0' &&\n" + " searchRegion.charAt(endPatternIndex) <= '9') ||\n" + " (searchRegion.charAt(endPatternIndex) == '_') ||\n" + " (searchRegion.charAt(endPatternIndex) == '-') ||\n" + " (searchRegion.charAt(endPatternIndex) == '.')))\n" + " endPatternIndex++;\n" + " prefixVector.add(\n" + " searchRegion.substring(startPatternIndex, endPatternIndex));\n" + " startIndex = endPatternIndex;\n" + " }\n" + "\n" + " String prefixes[];\n" + " prefixes = new String[prefixVector.size()];\n" + " prefixVector.copyInto(prefixes);\n" + " return prefixes;\n" + " }\n" + "\n" + " private static String removeNilAttribute(String input, String nsPrefixes[]) {\n" + " String output = input;\n" + " for (int pIndex = 0; pIndex < nsPrefixes.length; pIndex++)\n" + " {\n" + " String nilAttr = nsPrefixes[pIndex] + \":nil\";\n" + " int startIndex = 0;\n" + " for (int startAttrIndex = output.indexOf(nilAttr, startIndex);\n" + " startAttrIndex >= 0;\n" + " startAttrIndex = output.indexOf(nilAttr, startIndex))\n" + " {\n" + " int endAttrIndex = startAttrIndex + nilAttr.length();\n" + " // Skip over whitespace (if any)\n" + " endAttrIndex = skipWhitespace(output, endAttrIndex);\n" + " if ((endAttrIndex < output.length()) &&\n" + " (output.charAt(endAttrIndex) == '='))\n" + " {\n" + " endAttrIndex++;\n" + " // Skip over whitespace (if any) again\n" + " endAttrIndex = skipWhitespace(output, endAttrIndex);\n" + " if (endAttrIndex < output.length())\n" + " {\n" + " if (output.startsWith(\"\\\"true\\\"\", endAttrIndex))\n" + " endAttrIndex = endAttrIndex+6;\n" + " else if (output.startsWith(\"\\\"false\\\"\", endAttrIndex))\n" + " endAttrIndex = endAttrIndex+7;\n" + " else // should never come here\n" + " {\n" + " startIndex = startAttrIndex + nilAttr.length();\n" + " continue;\n" + " }\n" + " endAttrIndex = skipWhitespace(output, endAttrIndex);\n" + " output = output.substring(0, startAttrIndex) +\n" + " output.substring(endAttrIndex);\n" + " startIndex = startAttrIndex;\n" + " }\n" + " else\n" + " startIndex = startAttrIndex + nilAttr.length();\n" + " }\n" + " else\n" + " startIndex = startAttrIndex + nilAttr.length();\n" + " }\n" + " }\n" + " return output;\n" + " }\n" + "\n" + " private static String removeNsPrefixes(String input, String nsPrefixes[]) {\n" + " String output = input;\n" + " for (int pIndex = 0; pIndex < nsPrefixes.length; pIndex++)\n" + " {\n" + " String pattern = nsPrefixes[pIndex] + \":\";\n" + " int startIndex = 0;\n" + " for (int startPatternIndex = output.indexOf(pattern, startIndex);\n" + " startPatternIndex >= 0;\n" + " startPatternIndex = output.indexOf(pattern, startIndex))\n" + " {\n" + " int endPatternIndex = startPatternIndex + pattern.length();\n" + " output = output.substring(0, startPatternIndex) +\n" + " output.substring(endPatternIndex);\n" + " startIndex = startPatternIndex;\n" + " }\n" + " }\n" + " return output;\n" + " }\n" + "\n" + " private static String convertShorthandSyntax(String input) {\n" + " String output = input;\n" + " int startIndex = 0;\n" + " for (int elementEndIndex = output.indexOf(\"/>\", startIndex);\n" + " elementEndIndex >= 0;\n" + " elementEndIndex = output.indexOf(\"/>\", startIndex))\n" + " {\n" + " int elementStartIndex = output.lastIndexOf(\"<\", elementEndIndex);\n" + " if (elementStartIndex >= 0)\n" + " {\n" + " elementStartIndex++;\n" + " int endNameIndex = elementStartIndex;\n" + " while ((endNameIndex < output.length()) &&\n" + " ((output.charAt(endNameIndex) >= 'A' &&\n" + " output.charAt(endNameIndex) <= 'Z') ||\n" + " (output.charAt(endNameIndex) >= 'a' &&\n" + " output.charAt(endNameIndex) <= 'z') ||\n" + " (output.charAt(endNameIndex) >= '0' &&\n" + " output.charAt(endNameIndex) <= '9') ||\n" + " (output.charAt(endNameIndex) == '_') ||\n" + " (output.charAt(endNameIndex) == '-') ||\n" + " (output.charAt(endNameIndex) == '.')))\n" + " endNameIndex++;\n" + " if (endNameIndex > elementStartIndex)\n" + " {\n" + " String elementName =\n" + " output.substring(elementStartIndex, endNameIndex);\n" + " output = output.substring(0, elementEndIndex) +\n" + " \">\" +\n" + " output.substring(elementEndIndex+2);\n" + " }\n" + " }\n" + " startIndex = elementEndIndex+1;\n" + " }\n" + " return output;\n" + " }\n" + "\n" + "\n" + " private static int skipWhitespace(String input, int startIndex) {\n" + " int newIndex = startIndex;\n" + " while ((newIndex < input.length()) &&\n" + " ((input.charAt(newIndex) == ' ') ||\n" + " (input.charAt(newIndex) == '\\t') ||\n" + " (input.charAt(newIndex) == '\\r') ||\n" + " (input.charAt(newIndex) == '\\n')))\n" + " newIndex++;\n" + " return newIndex;\n" + " }\n" ); }} }