1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.geronimo.ews.ws4j2ee.toWs;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.apache.geronimo.ews.ws4j2ee.context.J2EEWebServiceContext;
22
23 import java.io.File;
24 import java.io.FileWriter;
25 import java.io.IOException;
26 import java.io.PrintWriter;
27
28 /***
29 * <p>This is a conveniance class to wite the Writers</p>
30 *
31 * @author Srinath Perera(hemapani@opensource.lk)
32 */
33
34 public abstract class AbstractWriter implements Writer {
35 /***
36 * <p>this parameter act as a mediator, It contains all the information
37 * this will be passed to the each writer. This will make sure
38 * even if the information that should passed around it will keep
39 * method signatures intact.</p>
40 */
41 protected static Log log =
42 LogFactory.getLog(AbstractWriter.class.getName());
43 protected J2EEWebServiceContext j2eewscontext;
44
45 protected PrintWriter out;
46 private String fileName;
47 private boolean verbose;
48
49 public AbstractWriter(J2EEWebServiceContext j2eewscontext, String filename)
50 throws GenerationFault {
51 this.j2eewscontext = j2eewscontext;
52 this.fileName = filename;
53 verbose = j2eewscontext.getMiscInfo().isVerbose();
54 }
55
56 protected abstract void writeCode() throws GenerationFault;
57
58 protected final void prepare() throws GenerationFault {
59 try {
60 File file = new File(this.fileName);
61 if (verbose) {
62 log.info("genarating ... " + file.getAbsolutePath());
63 }
64 if (!isOverWrite() && file.exists()) {
65 out = null;
66 if (verbose) {
67 log.info("the file already exists .. tool will not overwrite it ");
68 }
69 } else {
70 File parent = file.getParentFile();
71 if (parent != null)
72 parent.mkdirs();
73 file.createNewFile();
74 out = new PrintWriter(new FileWriter(file, false));
75 }
76 } catch (IOException e) {
77 log.error(e);
78 throw GenerationFault.createGenerationFault(e);
79 }
80 }
81
82 protected final void cleanUp() throws GenerationFault {
83 if (out != null)
84 out.close();
85 }
86
87 protected boolean isOverWrite() {
88 return true;
89 }
90
91 public final void write() throws GenerationFault {
92 try {
93 prepare();
94 writeCode();
95 } finally {
96 cleanUp();
97 }
98 }
99 }