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.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
30
31
32
33 public class Part
34 {
35
36
37
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
51
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
65
66
67
68 private Boolean nonl = Boolean.FALSE;
69
70
71
72
73
74
75 private String encoding;
76
77
78
79
80
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 }