1 55 package org.jboss.axis.wsdl.toJava; 56 57 import org.jboss.axis.utils.Messages; 58 import org.jboss.axis.wsdl.gen.Generator; 59 import org.w3c.dom.Element ; 60 import org.w3c.dom.Node ; 61 62 import java.io.File ; 63 import java.io.FileWriter ; 64 import java.io.IOException ; 65 import java.io.PrintWriter ; 66 67 119 public abstract class JavaWriter implements Generator 120 { 121 protected Emitter emitter; 122 protected String type; 123 124 127 protected JavaWriter(Emitter emitter, String type) 128 { 129 this.emitter = emitter; 130 this.type = type; 131 } 133 136 public void generate() throws IOException 137 { 138 String file = getFileName(); 139 if (isFileGenerated(file)) 140 { 141 throw new DuplicateFileException(Messages.getMessage("duplicateFile00", file), file); 142 } 143 registerFile(file); 144 if (emitter.isVerbose()) 145 { 146 String msg = verboseMessage(file); 147 if (msg != null) 148 { 149 System.out.println(msg); 150 } 151 } 152 PrintWriter pw = getPrintWriter(file); 153 writeFileHeader(pw); 154 writeFileBody(pw); 155 writeFileFooter(pw); 156 closePrintWriter(pw); 157 } 159 164 protected abstract String getFileName(); 165 166 171 protected boolean isFileGenerated(String file) 172 { 173 return emitter.getGeneratedFileNames().contains(file); 174 } 176 181 protected void registerFile(String file) 182 { 183 emitter.getGeneratedFileInfo().add(file, null, type); 184 } 186 190 protected String verboseMessage(String file) 191 { 192 return Messages.getMessage("generating", file); 193 } 195 199 protected PrintWriter getPrintWriter(String filename) throws IOException 200 { 201 File file = new File (filename); 202 File parent = new File (file.getParent()); 203 parent.mkdirs(); 204 return new PrintWriter (new FileWriter (file)); 205 } 207 212 protected void writeFileHeader(PrintWriter pw) throws IOException 213 { 214 } 216 220 protected abstract void writeFileBody(PrintWriter pw) throws IOException ; 221 222 226 protected void writeFileFooter(PrintWriter pw) throws IOException 227 { 228 } 230 233 protected void closePrintWriter(PrintWriter pw) 234 { 235 pw.close(); 236 } 238 241 protected void writeComment(PrintWriter pw, Element element) 242 { 243 final int LINE_LENGTH = 65; 245 246 if (element == null) 247 { 248 return; 249 } 250 251 Node child = element.getFirstChild(); 252 if (child == null) 253 { 254 return; 255 } 256 257 String comment = child.getNodeValue(); 258 259 comment = comment.replace('\r', ' '); 261 comment = comment.replace('\n', ' '); 262 263 if (comment != null) 264 { 265 int start = 0; 266 267 pw.println(); 269 while (start < comment.length()) 271 { 272 int end = start + LINE_LENGTH; 273 if (end > comment.length()) 274 end = comment.length(); 275 while (end < comment.length() && 277 !Character.isWhitespace(comment.charAt(end))) 278 { 279 end++; 280 } 281 pw.println(" // " + comment.substring(start, end).trim()); 282 start = end + 1; 283 } 284 } 285 } 287 } | Popular Tags |