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.File;
19  import java.io.FileInputStream;
20  import java.io.FileNotFoundException;
21  import java.io.InputStreamReader;
22  import java.io.Reader;
23  import java.io.StringReader;
24  import java.io.UnsupportedEncodingException;
25  import java.nio.charset.Charset;
26  import org.apache.maven.plugin.MojoExecutionException;
27  
28  /**
29   * Represent a portion of an output file:  literal text, or an input file.
30   * It is an error to specify both, or neither.
31   * @author mwood
32   */
33  public class Part
34  {
35      /**
36       * Text of a literal part.
37       * @parameter
38       */
39      private String text;
40  
41      public void setText(String text) throws MojoExecutionException
42      {
43          if (null != path)
44              throw new MojoExecutionException("Part cannot have text and path.");
45          else
46              this.text = text;
47      }
48  
49      /**
50       * Path to an external file part.
51       * @parameter
52       */
53      private File path;
54  
55      public void setPath(File path) throws MojoExecutionException
56      {
57          if (null != text)
58              throw new MojoExecutionException("Part cannot have text and path.");
59          else
60              this.path = path;
61      }
62  
63      /**
64       * Do not append a newline.
65       * @parameter default-value="false"
66       * @required
67       */
68      private Boolean nonl = Boolean.FALSE;
69  
70      /**
71       * @parameter default-value="${{project.build.encoding}"
72       * @readonly
73       * @required
74       */
75      private String encoding;
76  
77      /**
78       * Get a Reader on whatever kind of content we have.
79       * @return
80       * @throws FileNotFoundException 
81       */
82      Reader getReader()
83              throws FileNotFoundException, MojoExecutionException,
84              UnsupportedEncodingException
85      {
86          Charset charset;
87          if (null == encoding)
88              charset = Charset.defaultCharset();
89          else
90              charset = Charset.forName(encoding);
91  
92          if (null != text)
93              if (nonl)
94                  return new StringReader(text);
95              else
96                  return new StringReader(text + "\n");
97          else if (null != path)
98              return new InputStreamReader(new FileInputStream(path),charset);
99          else
100             throw new MojoExecutionException("Part has neither text nor path.");
101     }
102 
103     @Override
104     public String toString()
105     {
106         if (null != text)
107             return "literal part:  " + text;
108         else if (null != path)
109             return "file part:  " + path;
110         else
111             return "unknown part";
112     }
113 }