1 package org.apache.maven.plugins.help;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.io.StringReader;
25 import java.io.StringWriter;
26 import java.io.Writer;
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.LinkedHashSet;
30 import java.util.List;
31 import java.util.Properties;
32 import java.util.Set;
33
34 import org.apache.commons.lang3.time.DateFormatUtils;
35 import org.codehaus.plexus.util.IOUtil;
36 import org.codehaus.plexus.util.WriterFactory;
37 import org.codehaus.plexus.util.xml.XMLWriter;
38 import org.codehaus.plexus.util.xml.XmlWriterUtil;
39 import org.jdom.Document;
40 import org.jdom.JDOMException;
41 import org.jdom.input.SAXBuilder;
42 import org.jdom.output.Format;
43 import org.jdom.output.XMLOutputter;
44
45
46
47
48
49
50
51
52 public abstract class AbstractEffectiveMojo
53 extends AbstractHelpMojo
54 {
55
56
57
58
59
60
61
62
63
64 protected static void writeXmlFile( File output, String content )
65 throws IOException
66 {
67 if ( output == null )
68 {
69 return;
70 }
71
72 Writer out = null;
73 try
74 {
75 output.getParentFile().mkdirs();
76
77 out = WriterFactory.newXmlWriter( output );
78
79 out.write( content );
80
81 out.close();
82 out = null;
83 }
84 finally
85 {
86 IOUtil.close( out );
87 }
88 }
89
90
91
92
93
94
95 protected static void writeHeader( XMLWriter writer )
96 {
97 XmlWriterUtil.writeCommentLineBreak( writer );
98 XmlWriterUtil.writeComment( writer, " " );
99
100 String formattedDateTime = DateFormatUtils.ISO_8601_EXTENDED_DATETIME_TIME_ZONE_FORMAT
101 .format( System.currentTimeMillis() );
102 XmlWriterUtil.writeComment( writer, "Generated by Maven Help Plugin on " + formattedDateTime );
103 XmlWriterUtil.writeComment( writer, "See: http://maven.apache.org/plugins/maven-help-plugin/" );
104 XmlWriterUtil.writeComment( writer, " " );
105 XmlWriterUtil.writeCommentLineBreak( writer );
106 }
107
108
109
110
111
112
113
114 protected static void writeComment( XMLWriter writer, String comment )
115 {
116 XmlWriterUtil.writeCommentLineBreak( writer );
117 XmlWriterUtil.writeComment( writer, " " );
118 XmlWriterUtil.writeComment( writer, comment );
119 XmlWriterUtil.writeComment( writer, " " );
120 XmlWriterUtil.writeCommentLineBreak( writer );
121 }
122
123
124
125
126
127
128
129 protected static String prettyFormat( String effectiveModel, String encoding, boolean omitDeclaration )
130 {
131 SAXBuilder builder = new SAXBuilder();
132
133 try
134 {
135 Document effectiveDocument = builder.build( new StringReader( effectiveModel ) );
136
137 StringWriter w = new StringWriter();
138 Format format = Format.getPrettyFormat();
139 if ( encoding != null )
140 {
141
142 format.setEncoding( encoding );
143 }
144 format.setLineSeparator( System.lineSeparator() );
145 format.setOmitDeclaration( omitDeclaration );
146 XMLOutputter out = new XMLOutputter( format );
147 out.output( effectiveDocument, w );
148
149 return w.toString();
150 }
151 catch ( JDOMException e )
152 {
153 return effectiveModel;
154 }
155 catch ( IOException e )
156 {
157 return effectiveModel;
158 }
159 }
160
161
162
163
164 protected static class SortedProperties
165 extends Properties
166 {
167
168 static final long serialVersionUID = -8985316072702233744L;
169
170
171 @SuppressWarnings( { "rawtypes", "unchecked" } )
172 public Set<Object> keySet()
173 {
174 Set<Object> keynames = super.keySet();
175 List list = new ArrayList( keynames );
176 Collections.sort( list );
177
178 return new LinkedHashSet<Object>( list );
179 }
180 }
181 }