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