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