1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package edu.iu.ul.maven.plugins.fileweaver;
17
18 import java.io.BufferedWriter;
19 import java.io.File;
20 import java.io.FileNotFoundException;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.OutputStreamWriter;
24 import java.io.Reader;
25 import java.io.Writer;
26 import java.nio.CharBuffer;
27 import java.nio.charset.Charset;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import org.apache.maven.plugin.MojoExecutionException;
32 import org.apache.maven.plugin.logging.Log;
33 import org.codehaus.plexus.util.InterpolationFilterReader;
34
35
36
37
38
39
40 public class Output
41 {
42 private static final int BUFFER_SIZE = 256;
43
44
45
46
47
48
49 private String name;
50
51
52
53
54
55 private File outputPath;
56
57
58
59
60
61 private Map<String, String> properties;
62
63
64
65
66
67
68 private List<Part> parts;
69
70
71
72
73
74
75 private String encoding;
76
77
78 private File finalPath = null;
79
80
81
82
83
84
85
86
87 Writer open(File defaultPath) throws FileNotFoundException, IOException
88 {
89 File pathTo;
90 if (null != outputPath)
91 pathTo = outputPath;
92 else
93 pathTo = defaultPath;
94 finalPath = new File(pathTo, name);
95
96
97 File parentPath = finalPath.getParentFile();
98 if (null != parentPath)
99 parentPath.mkdirs();
100
101 Charset charset;
102 if (null == encoding)
103 charset = Charset.defaultCharset();
104 else
105 charset = Charset.forName(encoding);
106
107 return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
108 finalPath), charset));
109 }
110
111
112
113
114
115
116
117
118 void build(File defaultPath, Map<Object, Object> executionProps, Log log)
119 throws MojoExecutionException
120 {
121 Map<Object, Object> fileProps = new HashMap<Object, Object>();
122 if (null != executionProps)
123 fileProps.putAll(executionProps);
124 if (null != properties)
125 fileProps.putAll(properties);
126
127 CharBuffer bupher = CharBuffer.allocate(BUFFER_SIZE);
128 Writer output = null;
129 try
130 {
131 output = open(defaultPath);
132
133 int nParts = 0;
134 for (Part part : parts)
135 {
136 Reader ir = new InterpolationFilterReader(part.getReader(),
137 fileProps);
138 bupher.clear();
139 while (ir.read(bupher) >= 0)
140 {
141 bupher.flip();
142 output.write(bupher.toString());
143 bupher.clear();
144 }
145 nParts++;
146 }
147 output.close();
148 log.info(String.format("Wove %s from %d parts.",
149 finalPath, nParts));
150 } catch (Exception e)
151 {
152 throw new MojoExecutionException("Could not write " + finalPath, e);
153 }
154 }
155
156 @Override
157 public String toString()
158 {
159 StringBuilder value = new StringBuilder(32);
160
161 value.append("File:\n");
162 value.append(" name: ").append(name).append('\n');
163 if (null != outputPath)
164 value.append(" outputPath").append(outputPath);
165 for (Part part : parts)
166 value.append(part.toString()).append('\n');
167
168 if (null != properties)
169 for (String key : properties.keySet())
170 {
171 value.append(" property: ").append(key).append(" = ")
172 .append(properties.get(key));
173 }
174
175 return value.toString();
176 }
177 }