1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.help;
20
21 import javax.xml.XMLConstants;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.io.StringReader;
26 import java.io.StringWriter;
27 import java.io.Writer;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.LinkedHashSet;
31 import java.util.List;
32 import java.util.Properties;
33 import java.util.Set;
34
35 import org.codehaus.plexus.util.WriterFactory;
36 import org.codehaus.plexus.util.xml.XMLWriter;
37 import org.codehaus.plexus.util.xml.XmlWriterUtil;
38 import org.jdom2.Document;
39 import org.jdom2.JDOMException;
40 import org.jdom2.input.SAXBuilder;
41 import org.jdom2.output.Format;
42 import org.jdom2.output.XMLOutputter;
43
44
45
46
47
48
49
50 public abstract class AbstractEffectiveMojo extends AbstractHelpMojo {
51
52
53
54
55
56
57
58
59 protected static void writeXmlFile(File output, String content) throws IOException {
60 if (output == null) {
61 return;
62 }
63
64 output.getParentFile().mkdirs();
65 try (Writer out = WriterFactory.newXmlWriter(output)) {
66 out.write(content);
67 }
68 }
69
70
71
72
73
74
75 protected static void writeHeader(XMLWriter writer) {
76 XmlWriterUtil.writeCommentLineBreak(writer);
77 XmlWriterUtil.writeComment(writer, " ");
78 XmlWriterUtil.writeComment(writer, "Generated by Maven Help Plugin");
79 XmlWriterUtil.writeComment(writer, "See: https://maven.apache.org/plugins/maven-help-plugin/");
80 XmlWriterUtil.writeComment(writer, " ");
81 XmlWriterUtil.writeCommentLineBreak(writer);
82 }
83
84
85
86
87
88
89
90 protected static void writeComment(XMLWriter writer, String comment) {
91 XmlWriterUtil.writeCommentLineBreak(writer);
92 XmlWriterUtil.writeComment(writer, " ");
93 XmlWriterUtil.writeComment(writer, comment);
94 XmlWriterUtil.writeComment(writer, " ");
95 XmlWriterUtil.writeCommentLineBreak(writer);
96 }
97
98
99
100
101
102
103
104 protected static String prettyFormat(String effectiveModel, String encoding, boolean omitDeclaration) {
105 SAXBuilder builder = new SAXBuilder();
106 builder.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "");
107 builder.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
108 try {
109 Document effectiveDocument = builder.build(new StringReader(effectiveModel));
110
111 StringWriter w = new StringWriter();
112 Format format = Format.getPrettyFormat();
113 if (encoding != null) {
114
115 format.setEncoding(encoding);
116 }
117 format.setLineSeparator(System.lineSeparator());
118 format.setOmitDeclaration(omitDeclaration);
119 XMLOutputter out = new XMLOutputter(format);
120 out.output(effectiveDocument, w);
121
122 return w.toString();
123 } catch (JDOMException | IOException e) {
124 return effectiveModel;
125 }
126 }
127
128
129
130
131 protected static class SortedProperties extends Properties {
132
133 static final long serialVersionUID = -8985316072702233744L;
134
135
136 @SuppressWarnings({"rawtypes", "unchecked"})
137 @Override
138 public Set<Object> keySet() {
139 Set<Object> keynames = super.keySet();
140 List list = new ArrayList(keynames);
141 Collections.sort(list);
142
143 return new LinkedHashSet<>(list);
144 }
145 }
146 }