|
| 1 | +import java.io.BufferedReader; |
| 2 | +import java.io.ByteArrayOutputStream; |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileInputStream; |
| 5 | +import java.io.FileNotFoundException; |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.InputStreamReader; |
| 8 | +import java.io.PrintStream; |
| 9 | +import java.util.regex.Matcher; |
| 10 | +import java.util.regex.Pattern; |
| 11 | + |
| 12 | +import org.apache.tools.ant.BuildException; |
| 13 | +import org.apache.tools.ant.Task; |
| 14 | + |
| 15 | + |
| 16 | +/** |
| 17 | + * Ant Task for copying the PImage and PGraphics methods into PApplet. |
| 18 | + */ |
| 19 | +public class PAppletMethods extends Task { |
| 20 | + |
| 21 | + private File baseDir; |
| 22 | + |
| 23 | + |
| 24 | + public PAppletMethods() { |
| 25 | + } |
| 26 | + |
| 27 | + |
| 28 | + public void setDir(String dir) { |
| 29 | + baseDir = new File(dir); |
| 30 | + } |
| 31 | + |
| 32 | + |
| 33 | + public void execute() throws BuildException { |
| 34 | + // Do a bunch of checks... |
| 35 | + if (baseDir == null) { |
| 36 | + throw new BuildException("dir parameter must be set!"); |
| 37 | + } |
| 38 | + |
| 39 | + System.out.println("using basedir " + baseDir); |
| 40 | + File graphicsFile = new File(baseDir, "PGraphics.java"); |
| 41 | + File appletFile = new File(baseDir, "PApplet.java"); |
| 42 | + File imageFile = new File(baseDir, "PImage.java"); |
| 43 | + |
| 44 | + if (!graphicsFile.exists() || !graphicsFile.canRead()) { |
| 45 | + throw new BuildException("PGraphics file not readable: " + |
| 46 | + graphicsFile.getAbsolutePath()); |
| 47 | + } |
| 48 | + |
| 49 | + if (!appletFile.exists() || |
| 50 | + !appletFile.canRead() || |
| 51 | + !appletFile.canWrite()) { |
| 52 | + throw new BuildException("PApplet file not read/writeable: " + |
| 53 | + appletFile.getAbsolutePath()); |
| 54 | + } |
| 55 | + |
| 56 | + if (!imageFile.exists() || !imageFile.canRead()) { |
| 57 | + throw new BuildException("PImage file not readable: " + |
| 58 | + imageFile.getAbsolutePath()); |
| 59 | + } |
| 60 | + |
| 61 | + // Looking good, let's do this! |
| 62 | + ByteArrayOutputStream outBytes = new ByteArrayOutputStream(); |
| 63 | + PrintStream out = new PrintStream(outBytes); |
| 64 | + StringBuffer content = new StringBuffer(); |
| 65 | + |
| 66 | + try{ |
| 67 | + BufferedReader applet = createReader(appletFile); |
| 68 | + String line; |
| 69 | + while ((line = applet.readLine()) != null) { |
| 70 | + out.println(line); |
| 71 | + content.append(line); |
| 72 | + content.append('\n'); |
| 73 | + |
| 74 | + if (line.indexOf("public functions for processing.core") >= 0) { |
| 75 | + break; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // read the rest of the file and append it to the |
| 80 | + while ((line = applet.readLine()) != null) { |
| 81 | + content.append(line); |
| 82 | + content.append('\n'); |
| 83 | + } |
| 84 | + |
| 85 | + applet.close(); |
| 86 | + process(out, graphicsFile); |
| 87 | + process(out, imageFile); |
| 88 | + |
| 89 | + out.println("}"); |
| 90 | + |
| 91 | + } catch (IOException e) { |
| 92 | + e.printStackTrace(); |
| 93 | + |
| 94 | + } catch(Exception ex) { |
| 95 | + ex.printStackTrace(); |
| 96 | + } |
| 97 | + out.flush(); |
| 98 | + |
| 99 | + if (content.toString().equals(outBytes.toString())) { |
| 100 | + System.out.println("No changes to PApplet API."); |
| 101 | + } else { |
| 102 | + System.out.println("Updating PApplet with API changes " + |
| 103 | + "from PImage or PGraphics."); |
| 104 | + try { |
| 105 | + PrintStream temp = new PrintStream(appletFile); |
| 106 | + temp.print(outBytes.toString()); |
| 107 | + temp.flush(); |
| 108 | + temp.close(); |
| 109 | + } catch (FileNotFoundException e) { |
| 110 | + e.printStackTrace(); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + |
| 116 | + private void process(PrintStream out, File input) throws IOException { |
| 117 | + BufferedReader in = createReader(input); |
| 118 | + int comments = 0; |
| 119 | + String line = null; |
| 120 | + StringBuffer commentBuffer = new StringBuffer(); |
| 121 | + |
| 122 | + while ((line = in.readLine()) != null) { |
| 123 | + String decl = ""; |
| 124 | + |
| 125 | + // Keep track of comments |
| 126 | + //if (line.matches(Pattern.quote("/*"))) { |
| 127 | + if (line.indexOf("/*") != -1) { |
| 128 | + comments++; |
| 129 | + } |
| 130 | + |
| 131 | + //if (line.matches(Pattern.quote("*/"))) { |
| 132 | + if (line.indexOf("*/") != -1) { |
| 133 | + commentBuffer.append(line); |
| 134 | + commentBuffer.append('\n'); |
| 135 | + //System.out.println("comment is: " + commentBuffer.toString()); |
| 136 | + comments--; |
| 137 | + // otherwise gotSomething will be false, and nuke the comment |
| 138 | + continue; |
| 139 | + } |
| 140 | + |
| 141 | + // Ignore everything inside comments |
| 142 | + if (comments > 0) { |
| 143 | + commentBuffer.append(line); |
| 144 | + commentBuffer.append('\n'); |
| 145 | + continue; |
| 146 | + } |
| 147 | + |
| 148 | + boolean gotSomething = false; |
| 149 | + boolean gotStatic = false; |
| 150 | + |
| 151 | + Matcher result; |
| 152 | + |
| 153 | + if ((result = Pattern.compile("^\\s*public ([\\w\\[\\]]+) [a-zA-z_]+\\(.*$").matcher(line)).matches()) { |
| 154 | + gotSomething = true; |
| 155 | + |
| 156 | + } else if ((result = Pattern.compile("^\\s*abstract public ([\\w\\[\\]]+) [a-zA-z_]+\\(.*$").matcher(line)).matches()) { |
| 157 | + gotSomething = true; |
| 158 | + |
| 159 | + } else if ((result = Pattern.compile("^\\s*public final ([\\w\\[\\]]+) [a-zA-z_]+\\(.*$").matcher(line)).matches()) { |
| 160 | + gotSomething = true; |
| 161 | + |
| 162 | + } else if ((result = Pattern.compile("^\\s*static public ([\\w\\[\\]]+) [a-zA-z_]+\\(.*$").matcher(line)).matches()) { |
| 163 | + gotSomething = true; |
| 164 | + gotStatic = true; |
| 165 | + } |
| 166 | + |
| 167 | + // if function is marked "// ignore" then, uh, ignore it. |
| 168 | + if (gotSomething && line.indexOf("// ignore") >= 0) { |
| 169 | + gotSomething = false; |
| 170 | + } |
| 171 | + |
| 172 | + String returns = ""; |
| 173 | + if (gotSomething) { |
| 174 | + if (result.group(1).equals("void")) { |
| 175 | + returns = ""; |
| 176 | + } else { |
| 177 | + returns = "return "; |
| 178 | + } |
| 179 | + |
| 180 | + // remove the abstract modifier |
| 181 | + line = line.replaceFirst(Pattern.quote("abstract"), " "); |
| 182 | + |
| 183 | + // replace semicolons with a start def |
| 184 | + line = line.replaceAll(Pattern.quote(";"), " {\n"); |
| 185 | + |
| 186 | + //out.println("\n\n" + line); |
| 187 | + out.println(); |
| 188 | + out.println(); |
| 189 | + // end has its own newline |
| 190 | + //out.print(commentBuffer.toString()); // TODO disabled for now XXXX |
| 191 | + out.print(commentBuffer.toString()); // duplicates all comments |
| 192 | + commentBuffer.setLength(0); |
| 193 | + out.println(line); |
| 194 | + |
| 195 | + decl += line; |
| 196 | + while(line.indexOf(')') == -1) { |
| 197 | + line = in.readLine(); |
| 198 | + decl += line; |
| 199 | + line = line.replaceAll("\\;\\s*$", " {\n"); |
| 200 | + out.println(line); |
| 201 | + } |
| 202 | + |
| 203 | + result = Pattern.compile(".*?\\s(\\S+)\\(.*?").matcher(decl); |
| 204 | + // try to match. don't remove this or things will stop working! |
| 205 | + result.matches(); |
| 206 | + String declName = result.group(1); |
| 207 | + String gline = ""; |
| 208 | + String rline = ""; |
| 209 | + if (gotStatic) { |
| 210 | + gline = " " + returns + "PGraphics." + declName + "("; |
| 211 | + } else { |
| 212 | + rline = " if (recorder != null) recorder." + declName + "("; |
| 213 | + gline = " " + returns + "g." + declName + "("; |
| 214 | + } |
| 215 | + |
| 216 | + decl = decl.replaceAll("\\s+", " "); // smush onto a single line |
| 217 | + decl = decl.replaceFirst("^.*\\(", ""); |
| 218 | + decl = decl.replaceFirst("\\).*$", ""); |
| 219 | + |
| 220 | + int prev = 0; |
| 221 | + String parts[] = decl.split("\\, "); |
| 222 | + |
| 223 | + for (String part : parts) { |
| 224 | + if (!part.trim().equals("")) { |
| 225 | + String blargh[] = part.split(" "); |
| 226 | + String theArg = blargh[1].replaceAll("[\\[\\]]", ""); |
| 227 | + |
| 228 | + if (prev != 0) { |
| 229 | + gline += ", "; |
| 230 | + rline += ", "; |
| 231 | + } |
| 232 | + |
| 233 | + gline += theArg; |
| 234 | + rline += theArg; |
| 235 | + prev = 1; |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | + gline += ");"; |
| 240 | + rline += ");"; |
| 241 | + |
| 242 | + if (!gotStatic && returns.equals("")) { |
| 243 | + out.println(rline); |
| 244 | + } |
| 245 | + out.println(gline); |
| 246 | + out.println(" }"); |
| 247 | + |
| 248 | + } else { |
| 249 | + commentBuffer.setLength(0); |
| 250 | + } |
| 251 | + } |
| 252 | + |
| 253 | + in.close(); |
| 254 | + } |
| 255 | + |
| 256 | + |
| 257 | + static BufferedReader createReader(File file) throws IOException { |
| 258 | + FileInputStream fis = new FileInputStream(file); |
| 259 | + return new BufferedReader(new InputStreamReader(fis, "UTF-8")); |
| 260 | + } |
| 261 | +} |
0 commit comments