1 20 package org.objectweb.modfact.jmi.generator; 21 22 import java.io.IOException ; 23 import java.io.OutputStream ; 24 import java.io.PrintWriter ; 25 26 30 public abstract class PrintGenerator implements Generator { 31 32 33 protected PrintWriter out; 34 35 36 protected int level; 37 38 39 protected String TABULATION = " "; 40 41 42 protected boolean newLine = true; 43 44 47 public PrintGenerator() { 48 level = 0; 49 } 50 51 55 public void setOutput(OutputStream stream) throws IOException { 56 out = new PrintWriter (stream); 57 } 58 59 62 public void outputln() 63 { 64 out.println(); 65 newLine = true; 66 } 67 68 72 public void outputln(String output) 73 { 74 output(output); 75 outputln(); 76 } 77 78 82 protected void output(String outputString) { 83 int index = outputString.indexOf("\n"); 84 if (index != -1 && index != outputString.length() - 1) { 85 java.util.StringTokenizer token = new java.util.StringTokenizer (outputString, "\n"); 87 while (token.hasMoreTokens()) { 88 String next = token.nextToken(); 89 if (!token.hasMoreTokens()) { 90 if (outputString.endsWith("\n")) 91 out.println(next); 92 else 93 out.print(next); 94 } else { 95 out.println(next); 96 } 97 } 98 } else { 99 if (outputString.trim().startsWith("}")) 100 level--; 101 if (newLine) { 102 for (int j = 0; j < level; j++) 103 out.print(TABULATION); 104 } 105 if (outputString.trim().startsWith("}")) { 106 out.print(outputString); 107 if (outputString.trim().endsWith("{")) 108 level++; 109 } else { 110 out.print(outputString); 111 for (int i = 0; i < outputString.length(); i++) { 112 String current = String.valueOf(outputString.charAt(i)); 113 if (current.equals("{")) 114 level++; 115 else if (current.equals("}")) 116 level--; 117 } 118 } 119 if (outputString.trim().endsWith("\n")) 120 newLine = true; 121 else 122 newLine = false; 123 } 124 } 125 126 129 public void flushFile() { 130 out.flush(); 131 } 132 133 137 public void annotationTemplate(String annotation_) { 138 if (annotation_.trim().length() > 0) { 139 String annotation = "/** \n" + replaceAll(annotation_.trim(), "\\\\n", "\n") + "\n/"; 140 annotation = replaceAll(annotation, "\\\\s", " "); 141 StringBuffer margin = new StringBuffer (); 142 for (int i = 0; i < level; i++) 143 margin.append(TABULATION); 144 annotation = replaceAll(annotation, "\n", "\n" + margin.toString() + " *"); 145 outputln(annotation); 146 } 147 } 148 149 150 157 public static String replaceAll(String original, String to_replace, String new_value) { 158 StringBuffer newString = new StringBuffer (); 159 int indexBegin = 0; 160 int index = original.indexOf(to_replace, indexBegin); 161 while (index != -1) { 162 newString.append(original.substring(indexBegin, index)); 163 newString.append(new_value); 164 indexBegin = index + to_replace.length(); 165 index = original.indexOf(to_replace, indexBegin); 166 } 167 newString.append(original.substring(indexBegin)); 168 return newString.toString(); 169 } 170 171 } 172 | Popular Tags |