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.codehaus.plexus.util.WriterFactory;
35 import org.codehaus.plexus.util.xml.XMLWriter;
36 import org.codehaus.plexus.util.xml.XmlWriterUtil;
37 import org.jdom2.Document;
38 import org.jdom2.JDOMException;
39 import org.jdom2.input.SAXBuilder;
40 import org.jdom2.output.Format;
41 import org.jdom2.output.XMLOutputter;
42
43 import javax.xml.XMLConstants;
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 output.getParentFile().mkdirs();
71 try ( Writer out = WriterFactory.newXmlWriter( output ) )
72 {
73 out.write( content );
74 }
75 }
76
77
78
79
80
81
82 protected static void writeHeader( XMLWriter writer )
83 {
84 XmlWriterUtil.writeCommentLineBreak( writer );
85 XmlWriterUtil.writeComment( writer, " " );
86 XmlWriterUtil.writeComment( writer, "Generated by Maven Help Plugin" );
87 XmlWriterUtil.writeComment( writer, "See: https://maven.apache.org/plugins/maven-help-plugin/" );
88 XmlWriterUtil.writeComment( writer, " " );
89 XmlWriterUtil.writeCommentLineBreak( writer );
90 }
91
92
93
94
95
96
97
98 protected static void writeComment( XMLWriter writer, String comment )
99 {
100 XmlWriterUtil.writeCommentLineBreak( writer );
101 XmlWriterUtil.writeComment( writer, " " );
102 XmlWriterUtil.writeComment( writer, comment );
103 XmlWriterUtil.writeComment( writer, " " );
104 XmlWriterUtil.writeCommentLineBreak( writer );
105 }
106
107
108
109
110
111
112
113 protected static String prettyFormat( String effectiveModel, String encoding, boolean omitDeclaration )
114 {
115 SAXBuilder builder = new SAXBuilder();
116 builder.setProperty( XMLConstants.ACCESS_EXTERNAL_DTD, "" );
117 builder.setProperty( XMLConstants.ACCESS_EXTERNAL_SCHEMA, "" );
118 try
119 {
120 Document effectiveDocument = builder.build( new StringReader( effectiveModel ) );
121
122 StringWriter w = new StringWriter();
123 Format format = Format.getPrettyFormat();
124 if ( encoding != null )
125 {
126
127 format.setEncoding( encoding );
128 }
129 format.setLineSeparator( System.lineSeparator() );
130 format.setOmitDeclaration( omitDeclaration );
131 XMLOutputter out = new XMLOutputter( format );
132 out.output( effectiveDocument, w );
133
134 return w.toString();
135 }
136 catch ( JDOMException | IOException e )
137 {
138 return effectiveModel;
139 }
140 }
141
142
143
144
145 protected static class SortedProperties
146 extends Properties
147 {
148
149 static final long serialVersionUID = -8985316072702233744L;
150
151
152 @SuppressWarnings( { "rawtypes", "unchecked" } )
153 @Override
154 public Set<Object> keySet()
155 {
156 Set<Object> keynames = super.keySet();
157 List list = new ArrayList( keynames );
158 Collections.sort( list );
159
160 return new LinkedHashSet<>( list );
161 }
162 }
163 }