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.text.DateFormat;
28 import java.text.SimpleDateFormat;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.Date;
32 import java.util.Iterator;
33 import java.util.LinkedHashSet;
34 import java.util.List;
35 import java.util.Properties;
36 import java.util.Set;
37
38 import org.codehaus.plexus.util.IOUtil;
39 import org.codehaus.plexus.util.StringUtils;
40 import org.codehaus.plexus.util.WriterFactory;
41 import org.codehaus.plexus.util.xml.XMLWriter;
42 import org.codehaus.plexus.util.xml.XmlWriterUtil;
43 import org.jdom.Document;
44 import org.jdom.Element;
45 import org.jdom.JDOMException;
46 import org.jdom.Namespace;
47 import org.jdom.filter.ElementFilter;
48 import org.jdom.input.SAXBuilder;
49 import org.jdom.output.Format;
50 import org.jdom.output.XMLOutputter;
51
52
53
54
55
56
57
58
59 public abstract class AbstractEffectiveMojo
60 extends AbstractHelpMojo
61 {
62
63 private static final String POM_XSD_URL = "http://maven.apache.org/maven-v4_0_0.xsd";
64
65
66 private static final String SETTINGS_XSD_URL = "http://maven.apache.org/xsd/settings-1.0.0.xsd";
67
68
69
70
71
72
73
74
75
76
77 protected static void writeXmlFile( File output, String content, String encoding )
78 throws IOException
79 {
80 if ( output == null )
81 {
82 return;
83 }
84
85 if ( StringUtils.isEmpty( encoding ) )
86 {
87 writeFile( output, content );
88 return;
89 }
90
91 Writer out = null;
92 try
93 {
94 output.getParentFile().mkdirs();
95
96 out = WriterFactory.newXmlWriter( output );
97
98 out.write( content );
99
100 out.flush();
101 }
102 finally
103 {
104 IOUtil.close( out );
105 }
106 }
107
108
109
110
111
112
113 protected static void writeHeader( XMLWriter writer )
114 {
115 XmlWriterUtil.writeCommentLineBreak( writer );
116 XmlWriterUtil.writeComment( writer, " " );
117
118 DateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd'T'hh:mm:ss" );
119 XmlWriterUtil.writeComment( writer,
120 "Generated by Maven Help Plugin on "
121 + dateFormat.format( new Date( System.currentTimeMillis() ) ) );
122 XmlWriterUtil.writeComment( writer, "See: http://maven.apache.org/plugins/maven-help-plugin/" );
123 XmlWriterUtil.writeComment( writer, " " );
124 XmlWriterUtil.writeCommentLineBreak( writer );
125
126 XmlWriterUtil.writeLineBreak( writer );
127 }
128
129
130
131
132
133
134
135 protected static void writeComment( XMLWriter writer, String comment )
136 {
137 XmlWriterUtil.writeCommentLineBreak( writer );
138 XmlWriterUtil.writeComment( writer, " " );
139 XmlWriterUtil.writeComment( writer, comment );
140 XmlWriterUtil.writeComment( writer, " " );
141 XmlWriterUtil.writeCommentLineBreak( writer );
142
143 XmlWriterUtil.writeLineBreak( writer );
144 }
145
146
147
148
149
150
151
152
153
154
155
156 protected static String addMavenNamespace( String effectiveXml, boolean isPom )
157 {
158 SAXBuilder builder = new SAXBuilder();
159
160 try
161 {
162 Document document = builder.build( new StringReader( effectiveXml ) );
163 Element rootElement = document.getRootElement();
164
165
166 Namespace pomNamespace = Namespace.getNamespace( "", "http://maven.apache.org/POM/4.0.0" );
167 rootElement.setNamespace( pomNamespace );
168
169 Namespace xsiNamespace = Namespace.getNamespace( "xsi", "http://www.w3.org/2001/XMLSchema-instance" );
170 rootElement.addNamespaceDeclaration( xsiNamespace );
171 if ( rootElement.getAttribute( "schemaLocation", xsiNamespace ) == null )
172 {
173 rootElement.setAttribute( "schemaLocation", "http://maven.apache.org/POM/4.0.0 "
174 + ( isPom ? POM_XSD_URL : SETTINGS_XSD_URL ), xsiNamespace );
175 }
176
177 ElementFilter elementFilter = new ElementFilter( Namespace.getNamespace( "" ) );
178 for ( Iterator<?> i = rootElement.getDescendants( elementFilter ); i.hasNext(); )
179 {
180 Element e = (Element) i.next();
181 e.setNamespace( pomNamespace );
182 }
183
184 StringWriter w = new StringWriter();
185 Format format = Format.getPrettyFormat();
186 XMLOutputter out = new XMLOutputter( format );
187 out.output( document.getRootElement(), w );
188
189 return w.toString();
190 }
191 catch ( JDOMException e )
192 {
193 return effectiveXml;
194 }
195 catch ( IOException e )
196 {
197 return effectiveXml;
198 }
199 }
200
201
202
203
204 protected static class SortedProperties
205 extends Properties
206 {
207
208 static final long serialVersionUID = -8985316072702233744L;
209
210
211 @SuppressWarnings( { "rawtypes", "unchecked" } )
212 public Set<Object> keySet()
213 {
214 Set<Object> keynames = super.keySet();
215 List list = new ArrayList( keynames );
216 Collections.sort( list );
217
218 return new LinkedHashSet<Object>( list );
219 }
220 }
221 }