View Javadoc

1   /**
2    * Copyright (C) 2011-2012 Indiana University
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Attributes of a file to be assembled.
37   * 
38   * @author mwood
39   */
40  public class Output
41  {
42      private static final int BUFFER_SIZE = 256;
43  
44      /**
45       * Name of the output file.
46       * @parameter
47       * @required
48       */
49      private String name;
50  
51      /**
52       * Path to the file's directory.  Defaults to the plugin's output directory.
53       * @parameter
54       */
55      private File outputPath;
56  
57      /**
58       * Properties for interpolation in this file only.
59       * @parameter
60       */
61      private Map<String, String> properties;
62  
63      /**
64       * Sources from which to assemble the output.
65       * @parameter
66       * @required
67       */
68      private List<Part> parts;
69  
70      /**
71       * @parameter default-value="${project.build.sourceEncoding}"
72       * @required
73       * @readonly
74       */
75      private String encoding;
76  
77      /** output path after defaults are applied */
78      private File finalPath = null;
79  
80      /**
81       * Create the output file and return a Writer on it.
82       * @param defaultPath directory in which to create if this WovenFile does
83       * not specify.
84       * @return a Writer on the open file.
85       * @throws FileNotFoundException, IOException
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          // Ensure that the output path exists
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      * Assemble the file's parts and write out.
113      * @param defaultPath place to put the output if not specified here.
114      * @param executionProps properties to be merged with this file's own (may be null).
115      * @param log a logger, if needed.
116      * @throws MojoExecutionException on I/O errors.
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 }