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