1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.maven.io;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.io.OutputStream;
21
22 import javax.xml.transform.OutputKeys;
23 import javax.xml.transform.Transformer;
24 import javax.xml.transform.TransformerException;
25 import javax.xml.transform.TransformerFactory;
26 import javax.xml.transform.dom.DOMSource;
27 import javax.xml.transform.stream.StreamResult;
28
29 import org.apache.maven.plugin.MojoExecutionException;
30 import org.apache.maven.reporting.MavenReportException;
31 import org.w3c.dom.Document;
32
33
34
35
36
37
38
39 public final class MojoIoUtils
40 {
41
42
43
44
45
46
47
48
49
50
51
52
53
54 private MojoIoUtils()
55 {
56 }
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 public static File provideMojoDirectory(final String dirName)
77 throws MojoExecutionException
78 {
79 final File dir = new File(dirName);
80 provideMojoDirectory(dir);
81 return dir;
82 }
83
84
85
86
87
88
89
90 public static void provideDirectory(final File directory) throws IOException
91 {
92 if (!directory.exists())
93 {
94 final boolean created = directory.mkdirs();
95 if (!created && !directory.exists())
96 {
97 throw new IOException("Cannot create directory: "
98 + directory.getAbsolutePath());
99 }
100 }
101 }
102
103
104
105
106
107
108
109
110 public static void provideMojoDirectory(final File directory)
111 throws MojoExecutionException
112 {
113 if (!directory.exists())
114 {
115 final boolean created = directory.mkdirs();
116 if (!created && !directory.exists())
117 {
118 throw new MojoExecutionException("Cannot create directory: "
119 + directory.getAbsolutePath());
120 }
121 }
122 }
123
124
125
126
127
128
129
130
131 public static void provideReportDirectory(final File directory)
132 throws MavenReportException
133 {
134 if (!directory.exists())
135 {
136 final boolean created = directory.mkdirs();
137 if (!created && !directory.exists())
138 {
139 throw new MavenReportException("Cannot create directory: "
140 + directory.getAbsolutePath());
141 }
142 }
143 }
144
145
146
147
148
149
150
151
152
153
154
155
156
157 public static String normalizeFileName(final String input)
158 {
159 final int length = input.length();
160 final StringBuilder buffer = new StringBuilder(length);
161 for (int i = 0; i < length; i++)
162 {
163 final char c = input.charAt(i);
164 if (isValidUrlChar(c))
165 {
166 buffer.append(c);
167 }
168 else
169 {
170 buffer.append('_');
171 }
172 }
173
174 return buffer.toString();
175 }
176
177 private static boolean isValidUrlChar(final char c)
178 {
179 return isUrlLetter(c) || isDigit(c) || c == '_' || c == '-' || c == '.';
180 }
181
182 private static boolean isUrlLetter(final char c)
183 {
184 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
185 }
186
187 private static boolean isDigit(final char c)
188 {
189 return c >= '0' && c <= '9';
190 }
191
192
193
194
195
196
197
198
199 public static void serialize(final Document document, final OutputStream out)
200 throws TransformerException
201 {
202 serialize(document, out, false);
203 }
204
205
206
207
208
209
210
211
212 public static void serialize(final Document document, final OutputStream out,
213 final boolean prettyPrint) throws TransformerException
214 {
215 final TransformerFactory factory = TransformerFactory.newInstance();
216 final Transformer serializer = factory.newTransformer();
217 if (prettyPrint)
218 {
219 serializer.setOutputProperty(OutputKeys.INDENT, "yes");
220 serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount",
221 "2");
222 }
223
224 serializer.transform(new DOMSource(document), new StreamResult(out));
225 }
226
227
228
229 }