1 16 package org.apache.axis.wsdl.toJava; 17 18 import org.apache.axis.utils.Messages; 19 import org.apache.axis.wsdl.gen.Generator; 20 import org.w3c.dom.Element ; 21 import org.w3c.dom.Node ; 22 23 import java.io.File ; 24 import java.io.FileWriter ; 25 import java.io.IOException ; 26 import java.io.PrintWriter ; 27 import java.util.StringTokenizer ; 28 29 81 public abstract class JavaWriter implements Generator { 82 83 84 protected final static int LINE_LENGTH = 65; 85 86 87 protected Emitter emitter; 88 89 90 protected String type; 91 92 98 protected JavaWriter(Emitter emitter, String type) { 99 this.emitter = emitter; 100 this.type = type; 101 } 103 108 public void generate() throws IOException { 109 110 String file = getFileName(); 111 112 if (isFileGenerated(file)) { 113 throw new DuplicateFileException( 114 Messages.getMessage("duplicateFile00", file), file); 115 } 116 117 registerFile(file); 118 119 if (emitter.isVerbose()) { 120 String msg = verboseMessage(file); 121 122 if (msg != null) { 123 System.out.println(msg); 124 } 125 } 126 127 PrintWriter pw = getPrintWriter(file); 128 129 writeFileHeader(pw); 130 writeFileBody(pw); 131 writeFileFooter(pw); 132 closePrintWriter(pw); 133 } 135 142 protected abstract String getFileName(); 143 144 152 protected boolean isFileGenerated(String file) { 153 return emitter.getGeneratedFileNames().contains(file); 154 } 156 163 protected void registerFile(String file) { 164 emitter.getGeneratedFileInfo().add(file, null, type); 165 } 167 174 protected String verboseMessage(String file) { 175 return Messages.getMessage("generating", file); 176 } 178 186 protected PrintWriter getPrintWriter(String filename) throws IOException { 187 188 File file = new File (filename); 189 File parent = new File (file.getParent()); 190 191 parent.mkdirs(); 192 193 return new PrintWriter (new FileWriter (file)); 194 } 196 204 protected void writeFileHeader(PrintWriter pw) 205 throws IOException { 206 } 208 215 protected abstract void writeFileBody(PrintWriter pw) throws IOException ; 216 217 224 protected void writeFileFooter(PrintWriter pw) 225 throws IOException { 226 } 228 233 protected void closePrintWriter(PrintWriter pw) { 234 pw.close(); 235 } 237 242 protected String getJavadocDescriptionPart(String documentation, boolean addTab) { 243 if (documentation == null) { 244 return ""; 245 } 246 247 String doc = documentation.trim(); 248 249 if (documentation.trim().length() == 0) { 250 return doc; 252 } 253 254 StringTokenizer st = new StringTokenizer (doc, "@"); 256 StringBuffer newComments; 257 if (st.hasMoreTokens()) { 258 String token = st.nextToken(); 259 boolean startLine = Character.isWhitespace(token.charAt(token.length() - 1)) 260 && (token.charAt(token.length() - 1) != '\n'); 261 newComments = new StringBuffer (token); 262 263 while (st.hasMoreTokens()) { 264 token = st.nextToken(); 265 if (startLine) { 267 newComments.append('\n'); 268 } 269 newComments.append('@'); 270 startLine = Character.isWhitespace(token.charAt(token.length() - 1)) 271 & (token.charAt(token.length() - 1) != '\n'); 272 273 newComments.append(token); 274 } 275 } else { 276 newComments = new StringBuffer (doc); 277 } 278 newComments.insert(0, addTab ? " * " : " * "); 279 280 int pos = newComments.toString().indexOf("*/"); 283 while (pos >= 0) { 284 newComments.insert(pos + 1, ' '); 285 pos = newComments.toString().indexOf("*/"); 286 } 287 288 int lineStart = 0; 290 int newlinePos = 0; 291 while (lineStart < newComments.length()) { 292 newlinePos = newComments.toString().indexOf("\n", lineStart); 293 if (newlinePos == -1) { 294 newlinePos = newComments.length(); 295 } 296 if ((newlinePos - lineStart) > LINE_LENGTH) { 297 lineStart += LINE_LENGTH; 299 while ((lineStart < newComments.length()) 300 && !Character.isWhitespace(newComments.charAt(lineStart))) { 301 lineStart++; 302 } 303 304 if (lineStart < newComments.length()) { 305 char next = newComments.charAt(lineStart); 307 if ((next == '\r') || (next == '\n')) { 309 newComments.insert(lineStart + 1, addTab ? " * " : " * "); 311 lineStart += addTab ? 8 : 4; 312 } else { 313 newComments.insert(lineStart, addTab ? "\n * " : "\n * "); 314 lineStart += addTab ? 8 : 4; 315 } 316 } 317 318 while ((lineStart < newComments.length()) 320 && (newComments.charAt(lineStart) == ' ')) { newComments.delete(lineStart, lineStart + 1); 322 } 323 } else { 324 if (++newlinePos < newComments.length()) { 325 newComments.insert(newlinePos, addTab ? " * " : " * "); 326 } 327 lineStart = newlinePos; 328 lineStart += addTab ? 7 : 3; 329 } 330 } 331 332 return newComments.toString(); 333 } 334 335 341 protected void writeComment(PrintWriter pw, Element element) { 342 writeComment(pw, element, true); 343 } 344 345 352 protected void writeComment(PrintWriter pw, Element element, boolean addTab) { 353 354 if (element == null) { 355 return; 356 } 357 358 Node child = element.getFirstChild(); 359 360 if (child == null) { 361 return; 362 } 363 364 String comment = child.getNodeValue(); 365 366 if (comment != null) { 367 int start = 0; 368 369 pw.println(); 371 pw.println(addTab ? " /**" : "/**"); 372 pw.println(getJavadocDescriptionPart(comment, addTab)); 373 pw.println(addTab ? " */" : " */"); 374 } 375 } } | Popular Tags |