1
2
3
4
5 package org.apache.maven.model.v4;
6
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.io.OutputStream;
10 import java.io.Reader;
11 import java.io.Writer;
12 import java.text.DateFormat;
13 import java.util.ArrayList;
14 import java.util.Date;
15 import java.util.HashSet;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Objects;
19 import java.util.Properties;
20 import java.util.Set;
21 import java.util.function.Function;
22 import javax.xml.stream.XMLOutputFactory;
23 import javax.xml.stream.XMLStreamException;
24 import javax.xml.stream.XMLStreamWriter;
25 import org.apache.maven.api.annotations.Generated;
26 import org.apache.maven.api.model.InputLocation;
27 import org.apache.maven.api.model.InputLocationTracker;
28 import org.apache.maven.api.xml.XmlNode;
29 import org.apache.maven.internal.xml.XmlNodeBuilder;
30 import org.apache.maven.api.model.Model;
31 import org.apache.maven.api.model.ModelBase;
32 import org.apache.maven.api.model.PluginContainer;
33 import org.apache.maven.api.model.PluginConfiguration;
34 import org.apache.maven.api.model.BuildBase;
35 import org.apache.maven.api.model.Build;
36 import org.apache.maven.api.model.CiManagement;
37 import org.apache.maven.api.model.Notifier;
38 import org.apache.maven.api.model.Contributor;
39 import org.apache.maven.api.model.Dependency;
40 import org.apache.maven.api.model.Developer;
41 import org.apache.maven.api.model.Exclusion;
42 import org.apache.maven.api.model.IssueManagement;
43 import org.apache.maven.api.model.DistributionManagement;
44 import org.apache.maven.api.model.License;
45 import org.apache.maven.api.model.MailingList;
46 import org.apache.maven.api.model.Organization;
47 import org.apache.maven.api.model.PatternSet;
48 import org.apache.maven.api.model.Parent;
49 import org.apache.maven.api.model.Scm;
50 import org.apache.maven.api.model.FileSet;
51 import org.apache.maven.api.model.Resource;
52 import org.apache.maven.api.model.RepositoryBase;
53 import org.apache.maven.api.model.Repository;
54 import org.apache.maven.api.model.DeploymentRepository;
55 import org.apache.maven.api.model.RepositoryPolicy;
56 import org.apache.maven.api.model.Site;
57 import org.apache.maven.api.model.ConfigurationContainer;
58 import org.apache.maven.api.model.Plugin;
59 import org.apache.maven.api.model.PluginExecution;
60 import org.apache.maven.api.model.DependencyManagement;
61 import org.apache.maven.api.model.PluginManagement;
62 import org.apache.maven.api.model.Reporting;
63 import org.apache.maven.api.model.Profile;
64 import org.apache.maven.api.model.Activation;
65 import org.apache.maven.api.model.ActivationProperty;
66 import org.apache.maven.api.model.ActivationOS;
67 import org.apache.maven.api.model.ActivationFile;
68 import org.apache.maven.api.model.ReportPlugin;
69 import org.apache.maven.api.model.ReportSet;
70 import org.apache.maven.api.model.Prerequisites;
71 import org.apache.maven.api.model.Relocation;
72 import org.apache.maven.api.model.Extension;
73 import org.apache.maven.api.model.InputSource;
74 import org.codehaus.stax2.util.StreamWriterDelegate;
75
76 import static javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI;
77
78 @Generated
79 public class MavenStaxWriter {
80
81
82
83
84
85
86
87
88 private static final String NAMESPACE = "http://maven.apache.org/POM/4.0.0";
89
90
91
92
93 private static final String SCHEMA_LOCATION = "https://maven.apache.org/xsd/maven-4.0.0.xsd";
94
95
96
97
98 private String namespace = NAMESPACE;
99
100
101
102
103 private String schemaLocation = SCHEMA_LOCATION;
104
105
106
107
108 private String fileComment = null;
109
110 private boolean addLocationInformation = true;
111
112
113
114
115 protected Function<InputLocation, String> stringFormatter;
116
117
118
119
120
121
122
123
124
125
126 public void setNamespace(String namespace) {
127 this.namespace = Objects.requireNonNull(namespace);
128 }
129
130
131
132
133
134
135 public void setSchemaLocation(String schemaLocation) {
136 this.schemaLocation = Objects.requireNonNull(schemaLocation);
137 }
138
139
140
141
142
143
144 public void setFileComment(String fileComment) {
145 this.fileComment = fileComment;
146 }
147
148
149
150
151 public void setAddLocationInformation(boolean addLocationInformation) {
152 this.addLocationInformation = addLocationInformation;
153 }
154
155
156
157
158
159
160 public void setStringFormatter(Function<InputLocation, String> stringFormatter) {
161 this.stringFormatter = stringFormatter;
162 }
163
164
165
166
167
168
169
170
171 public void write(Writer writer, Model model) throws IOException, XMLStreamException {
172 XMLOutputFactory factory = new com.ctc.wstx.stax.WstxOutputFactory();
173 factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, false);
174 factory.setProperty(com.ctc.wstx.api.WstxOutputProperties.P_USE_DOUBLE_QUOTES_IN_XML_DECL, true);
175 factory.setProperty(com.ctc.wstx.api.WstxOutputProperties.P_ADD_SPACE_AFTER_EMPTY_ELEM, true);
176 XMLStreamWriter serializer = new IndentingXMLStreamWriter(factory.createXMLStreamWriter(writer));
177 serializer.writeStartDocument(model.getModelEncoding(), null);
178 writeModel("project", model, serializer);
179 serializer.writeEndDocument();
180 }
181
182
183
184
185
186
187
188
189 public void write(OutputStream stream, Model model) throws IOException, XMLStreamException {
190 XMLOutputFactory factory = new com.ctc.wstx.stax.WstxOutputFactory();
191 factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, false);
192 factory.setProperty(com.ctc.wstx.api.WstxOutputProperties.P_USE_DOUBLE_QUOTES_IN_XML_DECL, true);
193 factory.setProperty(com.ctc.wstx.api.WstxOutputProperties.P_ADD_SPACE_AFTER_EMPTY_ELEM, true);
194 XMLStreamWriter serializer = new IndentingXMLStreamWriter(factory.createXMLStreamWriter(stream, model.getModelEncoding()));
195 serializer.writeStartDocument(model.getModelEncoding(), null);
196 writeModel("project", model, serializer);
197 serializer.writeEndDocument();
198 }
199
200 private void writeModel(String tagName, Model model, XMLStreamWriter serializer)
201 throws IOException, XMLStreamException {
202 if (model != null) {
203 if (this.fileComment != null) {
204 serializer.writeCharacters("\n");
205 serializer.writeComment(this.fileComment);
206 serializer.writeCharacters("\n");
207 }
208 serializer.writeStartElement("", tagName, namespace);
209 serializer.writeNamespace("", namespace);
210 serializer.writeNamespace("xsi", W3C_XML_SCHEMA_INSTANCE_NS_URI);
211 serializer.writeAttribute(W3C_XML_SCHEMA_INSTANCE_NS_URI, "schemaLocation", namespace + " " + schemaLocation);
212 writeAttr("child.project.url.inherit.append.path", model.getChildProjectUrlInheritAppendPath(), serializer);
213 writeAttr("root", model.isRoot() ? "true" : null, serializer);
214 writeAttr("preserve.model.version", model.isPreserveModelVersion() ? "true" : null, serializer);
215 writeTag("modelVersion", null, model.getModelVersion(), serializer, model);
216 writeParent("parent", model.getParent(), serializer);
217 writeTag("groupId", null, model.getGroupId(), serializer, model);
218 writeTag("artifactId", null, model.getArtifactId(), serializer, model);
219 writeTag("version", null, model.getVersion(), serializer, model);
220 writeTag("packaging", "jar", model.getPackaging(), serializer, model);
221 writeTag("name", null, model.getName(), serializer, model);
222 writeTag("description", null, model.getDescription(), serializer, model);
223 writeTag("url", null, model.getUrl(), serializer, model);
224 writeTag("inceptionYear", null, model.getInceptionYear(), serializer, model);
225 writeOrganization("organization", model.getOrganization(), serializer);
226 writeList("licenses", false, model.getLicenses(), serializer, model,
227 t -> writeLicense("license", t, serializer));
228 writeList("developers", false, model.getDevelopers(), serializer, model,
229 t -> writeDeveloper("developer", t, serializer));
230 writeList("contributors", false, model.getContributors(), serializer, model,
231 t -> writeContributor("contributor", t, serializer));
232 writeList("mailingLists", false, model.getMailingLists(), serializer, model,
233 t -> writeMailingList("mailingList", t, serializer));
234 writePrerequisites("prerequisites", model.getPrerequisites(), serializer);
235 writeList("modules", model.getModules(), serializer, model,
236 t -> writeTag("module", null, t, serializer, null));
237 writeList("subprojects", model.getSubprojects(), serializer, model,
238 t -> writeTag("subproject", null, t, serializer, null));
239 writeScm("scm", model.getScm(), serializer);
240 writeIssueManagement("issueManagement", model.getIssueManagement(), serializer);
241 writeCiManagement("ciManagement", model.getCiManagement(), serializer);
242 writeDistributionManagement("distributionManagement", model.getDistributionManagement(), serializer);
243 writeProperties("properties", model.getProperties(), serializer, model);
244 writeDependencyManagement("dependencyManagement", model.getDependencyManagement(), serializer);
245 writeList("dependencies", false, model.getDependencies(), serializer, model,
246 t -> writeDependency("dependency", t, serializer));
247 writeList("repositories", false, model.getRepositories(), serializer, model,
248 t -> writeRepository("repository", t, serializer));
249 writeList("pluginRepositories", false, model.getPluginRepositories(), serializer, model,
250 t -> writeRepository("pluginRepository", t, serializer));
251 writeBuild("build", model.getBuild(), serializer);
252 writeReporting("reporting", model.getReporting(), serializer);
253 writeList("profiles", false, model.getProfiles(), serializer, model,
254 t -> writeProfile("profile", t, serializer));
255 serializer.writeEndElement();
256 }
257 }
258
259 private void writeModelBase(String tagName, ModelBase modelBase, XMLStreamWriter serializer)
260 throws IOException, XMLStreamException {
261 if (modelBase != null) {
262 serializer.writeStartElement(namespace, tagName);
263 writeList("modules", modelBase.getModules(), serializer, modelBase,
264 t -> writeTag("module", null, t, serializer, null));
265 writeList("subprojects", modelBase.getSubprojects(), serializer, modelBase,
266 t -> writeTag("subproject", null, t, serializer, null));
267 writeDistributionManagement("distributionManagement", modelBase.getDistributionManagement(), serializer);
268 writeProperties("properties", modelBase.getProperties(), serializer, modelBase);
269 writeDependencyManagement("dependencyManagement", modelBase.getDependencyManagement(), serializer);
270 writeList("dependencies", false, modelBase.getDependencies(), serializer, modelBase,
271 t -> writeDependency("dependency", t, serializer));
272 writeList("repositories", false, modelBase.getRepositories(), serializer, modelBase,
273 t -> writeRepository("repository", t, serializer));
274 writeList("pluginRepositories", false, modelBase.getPluginRepositories(), serializer, modelBase,
275 t -> writeRepository("pluginRepository", t, serializer));
276 writeReporting("reporting", modelBase.getReporting(), serializer);
277 serializer.writeEndElement();
278 }
279 }
280
281 private void writePluginContainer(String tagName, PluginContainer pluginContainer, XMLStreamWriter serializer)
282 throws IOException, XMLStreamException {
283 if (pluginContainer != null) {
284 serializer.writeStartElement(namespace, tagName);
285 writeList("plugins", false, pluginContainer.getPlugins(), serializer, pluginContainer,
286 t -> writePlugin("plugin", t, serializer));
287 serializer.writeEndElement();
288 }
289 }
290
291 private void writePluginConfiguration(String tagName, PluginConfiguration pluginConfiguration, XMLStreamWriter serializer)
292 throws IOException, XMLStreamException {
293 if (pluginConfiguration != null) {
294 serializer.writeStartElement(namespace, tagName);
295 writePluginManagement("pluginManagement", pluginConfiguration.getPluginManagement(), serializer);
296 writeList("plugins", false, pluginConfiguration.getPlugins(), serializer, pluginConfiguration,
297 t -> writePlugin("plugin", t, serializer));
298 serializer.writeEndElement();
299 }
300 }
301
302 private void writeBuildBase(String tagName, BuildBase buildBase, XMLStreamWriter serializer)
303 throws IOException, XMLStreamException {
304 if (buildBase != null) {
305 serializer.writeStartElement(namespace, tagName);
306 writeTag("defaultGoal", null, buildBase.getDefaultGoal(), serializer, buildBase);
307 writeList("resources", false, buildBase.getResources(), serializer, buildBase,
308 t -> writeResource("resource", t, serializer));
309 writeList("testResources", false, buildBase.getTestResources(), serializer, buildBase,
310 t -> writeResource("testResource", t, serializer));
311 writeTag("directory", null, buildBase.getDirectory(), serializer, buildBase);
312 writeTag("finalName", null, buildBase.getFinalName(), serializer, buildBase);
313 writeList("filters", buildBase.getFilters(), serializer, buildBase,
314 t -> writeTag("filter", null, t, serializer, null));
315 writePluginManagement("pluginManagement", buildBase.getPluginManagement(), serializer);
316 writeList("plugins", false, buildBase.getPlugins(), serializer, buildBase,
317 t -> writePlugin("plugin", t, serializer));
318 serializer.writeEndElement();
319 }
320 }
321
322 private void writeBuild(String tagName, Build build, XMLStreamWriter serializer)
323 throws IOException, XMLStreamException {
324 if (build != null) {
325 serializer.writeStartElement(namespace, tagName);
326 writeTag("sourceDirectory", null, build.getSourceDirectory(), serializer, build);
327 writeTag("scriptSourceDirectory", null, build.getScriptSourceDirectory(), serializer, build);
328 writeTag("testSourceDirectory", null, build.getTestSourceDirectory(), serializer, build);
329 writeTag("outputDirectory", null, build.getOutputDirectory(), serializer, build);
330 writeTag("testOutputDirectory", null, build.getTestOutputDirectory(), serializer, build);
331 writeList("extensions", false, build.getExtensions(), serializer, build,
332 t -> writeExtension("extension", t, serializer));
333 writeTag("defaultGoal", null, build.getDefaultGoal(), serializer, build);
334 writeList("resources", false, build.getResources(), serializer, build,
335 t -> writeResource("resource", t, serializer));
336 writeList("testResources", false, build.getTestResources(), serializer, build,
337 t -> writeResource("testResource", t, serializer));
338 writeTag("directory", null, build.getDirectory(), serializer, build);
339 writeTag("finalName", null, build.getFinalName(), serializer, build);
340 writeList("filters", build.getFilters(), serializer, build,
341 t -> writeTag("filter", null, t, serializer, null));
342 writePluginManagement("pluginManagement", build.getPluginManagement(), serializer);
343 writeList("plugins", false, build.getPlugins(), serializer, build,
344 t -> writePlugin("plugin", t, serializer));
345 serializer.writeEndElement();
346 }
347 }
348
349 private void writeCiManagement(String tagName, CiManagement ciManagement, XMLStreamWriter serializer)
350 throws IOException, XMLStreamException {
351 if (ciManagement != null) {
352 serializer.writeStartElement(namespace, tagName);
353 writeTag("system", null, ciManagement.getSystem(), serializer, ciManagement);
354 writeTag("url", null, ciManagement.getUrl(), serializer, ciManagement);
355 writeList("notifiers", false, ciManagement.getNotifiers(), serializer, ciManagement,
356 t -> writeNotifier("notifier", t, serializer));
357 serializer.writeEndElement();
358 }
359 }
360
361 private void writeNotifier(String tagName, Notifier notifier, XMLStreamWriter serializer)
362 throws IOException, XMLStreamException {
363 if (notifier != null) {
364 serializer.writeStartElement(namespace, tagName);
365 writeTag("type", "mail", notifier.getType(), serializer, notifier);
366 writeTag("sendOnError", "true", notifier.isSendOnError() ? null : "false", serializer, notifier);
367 writeTag("sendOnFailure", "true", notifier.isSendOnFailure() ? null : "false", serializer, notifier);
368 writeTag("sendOnSuccess", "true", notifier.isSendOnSuccess() ? null : "false", serializer, notifier);
369 writeTag("sendOnWarning", "true", notifier.isSendOnWarning() ? null : "false", serializer, notifier);
370 writeTag("address", null, notifier.getAddress(), serializer, notifier);
371 writeProperties("configuration", notifier.getConfiguration(), serializer, notifier);
372 serializer.writeEndElement();
373 }
374 }
375
376 private void writeContributor(String tagName, Contributor contributor, XMLStreamWriter serializer)
377 throws IOException, XMLStreamException {
378 if (contributor != null) {
379 serializer.writeStartElement(namespace, tagName);
380 writeTag("name", null, contributor.getName(), serializer, contributor);
381 writeTag("email", null, contributor.getEmail(), serializer, contributor);
382 writeTag("url", null, contributor.getUrl(), serializer, contributor);
383 writeTag("organization", null, contributor.getOrganization(), serializer, contributor);
384 writeTag("organizationUrl", null, contributor.getOrganizationUrl(), serializer, contributor);
385 writeList("roles", contributor.getRoles(), serializer, contributor,
386 t -> writeTag("role", null, t, serializer, null));
387 writeTag("timezone", null, contributor.getTimezone(), serializer, contributor);
388 writeProperties("properties", contributor.getProperties(), serializer, contributor);
389 serializer.writeEndElement();
390 }
391 }
392
393 private void writeDependency(String tagName, Dependency dependency, XMLStreamWriter serializer)
394 throws IOException, XMLStreamException {
395 if (dependency != null) {
396 serializer.writeStartElement(namespace, tagName);
397 writeTag("groupId", null, dependency.getGroupId(), serializer, dependency);
398 writeTag("artifactId", null, dependency.getArtifactId(), serializer, dependency);
399 writeTag("version", null, dependency.getVersion(), serializer, dependency);
400 writeTag("type", "jar", dependency.getType(), serializer, dependency);
401 writeTag("classifier", null, dependency.getClassifier(), serializer, dependency);
402 writeTag("scope", null, dependency.getScope(), serializer, dependency);
403 writeTag("systemPath", null, dependency.getSystemPath(), serializer, dependency);
404 writeList("exclusions", false, dependency.getExclusions(), serializer, dependency,
405 t -> writeExclusion("exclusion", t, serializer));
406 writeTag("optional", null, dependency.getOptional(), serializer, dependency);
407 serializer.writeEndElement();
408 }
409 }
410
411 private void writeDeveloper(String tagName, Developer developer, XMLStreamWriter serializer)
412 throws IOException, XMLStreamException {
413 if (developer != null) {
414 serializer.writeStartElement(namespace, tagName);
415 writeTag("id", null, developer.getId(), serializer, developer);
416 writeTag("name", null, developer.getName(), serializer, developer);
417 writeTag("email", null, developer.getEmail(), serializer, developer);
418 writeTag("url", null, developer.getUrl(), serializer, developer);
419 writeTag("organization", null, developer.getOrganization(), serializer, developer);
420 writeTag("organizationUrl", null, developer.getOrganizationUrl(), serializer, developer);
421 writeList("roles", developer.getRoles(), serializer, developer,
422 t -> writeTag("role", null, t, serializer, null));
423 writeTag("timezone", null, developer.getTimezone(), serializer, developer);
424 writeProperties("properties", developer.getProperties(), serializer, developer);
425 serializer.writeEndElement();
426 }
427 }
428
429 private void writeExclusion(String tagName, Exclusion exclusion, XMLStreamWriter serializer)
430 throws IOException, XMLStreamException {
431 if (exclusion != null) {
432 serializer.writeStartElement(namespace, tagName);
433 writeTag("groupId", null, exclusion.getGroupId(), serializer, exclusion);
434 writeTag("artifactId", null, exclusion.getArtifactId(), serializer, exclusion);
435 serializer.writeEndElement();
436 }
437 }
438
439 private void writeIssueManagement(String tagName, IssueManagement issueManagement, XMLStreamWriter serializer)
440 throws IOException, XMLStreamException {
441 if (issueManagement != null) {
442 serializer.writeStartElement(namespace, tagName);
443 writeTag("system", null, issueManagement.getSystem(), serializer, issueManagement);
444 writeTag("url", null, issueManagement.getUrl(), serializer, issueManagement);
445 serializer.writeEndElement();
446 }
447 }
448
449 private void writeDistributionManagement(String tagName, DistributionManagement distributionManagement, XMLStreamWriter serializer)
450 throws IOException, XMLStreamException {
451 if (distributionManagement != null) {
452 serializer.writeStartElement(namespace, tagName);
453 writeDeploymentRepository("repository", distributionManagement.getRepository(), serializer);
454 writeDeploymentRepository("snapshotRepository", distributionManagement.getSnapshotRepository(), serializer);
455 writeSite("site", distributionManagement.getSite(), serializer);
456 writeTag("downloadUrl", null, distributionManagement.getDownloadUrl(), serializer, distributionManagement);
457 writeRelocation("relocation", distributionManagement.getRelocation(), serializer);
458 writeTag("status", null, distributionManagement.getStatus(), serializer, distributionManagement);
459 serializer.writeEndElement();
460 }
461 }
462
463 private void writeLicense(String tagName, License license, XMLStreamWriter serializer)
464 throws IOException, XMLStreamException {
465 if (license != null) {
466 serializer.writeStartElement(namespace, tagName);
467 writeTag("name", null, license.getName(), serializer, license);
468 writeTag("url", null, license.getUrl(), serializer, license);
469 writeTag("distribution", null, license.getDistribution(), serializer, license);
470 writeTag("comments", null, license.getComments(), serializer, license);
471 serializer.writeEndElement();
472 }
473 }
474
475 private void writeMailingList(String tagName, MailingList mailingList, XMLStreamWriter serializer)
476 throws IOException, XMLStreamException {
477 if (mailingList != null) {
478 serializer.writeStartElement(namespace, tagName);
479 writeTag("name", null, mailingList.getName(), serializer, mailingList);
480 writeTag("subscribe", null, mailingList.getSubscribe(), serializer, mailingList);
481 writeTag("unsubscribe", null, mailingList.getUnsubscribe(), serializer, mailingList);
482 writeTag("post", null, mailingList.getPost(), serializer, mailingList);
483 writeTag("archive", null, mailingList.getArchive(), serializer, mailingList);
484 writeList("otherArchives", mailingList.getOtherArchives(), serializer, mailingList,
485 t -> writeTag("otherArchive", null, t, serializer, null));
486 serializer.writeEndElement();
487 }
488 }
489
490 private void writeOrganization(String tagName, Organization organization, XMLStreamWriter serializer)
491 throws IOException, XMLStreamException {
492 if (organization != null) {
493 serializer.writeStartElement(namespace, tagName);
494 writeTag("name", null, organization.getName(), serializer, organization);
495 writeTag("url", null, organization.getUrl(), serializer, organization);
496 serializer.writeEndElement();
497 }
498 }
499
500 private void writePatternSet(String tagName, PatternSet patternSet, XMLStreamWriter serializer)
501 throws IOException, XMLStreamException {
502 if (patternSet != null) {
503 serializer.writeStartElement(namespace, tagName);
504 writeList("includes", patternSet.getIncludes(), serializer, patternSet,
505 t -> writeTag("include", null, t, serializer, null));
506 writeList("excludes", patternSet.getExcludes(), serializer, patternSet,
507 t -> writeTag("exclude", null, t, serializer, null));
508 serializer.writeEndElement();
509 }
510 }
511
512 private void writeParent(String tagName, Parent parent, XMLStreamWriter serializer)
513 throws IOException, XMLStreamException {
514 if (parent != null) {
515 serializer.writeStartElement(namespace, tagName);
516 writeTag("groupId", null, parent.getGroupId(), serializer, parent);
517 writeTag("artifactId", null, parent.getArtifactId(), serializer, parent);
518 writeTag("version", null, parent.getVersion(), serializer, parent);
519 writeTag("relativePath", "..", parent.getRelativePath(), serializer, parent);
520 serializer.writeEndElement();
521 }
522 }
523
524 private void writeScm(String tagName, Scm scm, XMLStreamWriter serializer)
525 throws IOException, XMLStreamException {
526 if (scm != null) {
527 serializer.writeStartElement(namespace, tagName);
528 writeAttr("child.scm.connection.inherit.append.path", scm.getChildScmConnectionInheritAppendPath(), serializer);
529 writeAttr("child.scm.developerConnection.inherit.append.path", scm.getChildScmDeveloperConnectionInheritAppendPath(), serializer);
530 writeAttr("child.scm.url.inherit.append.path", scm.getChildScmUrlInheritAppendPath(), serializer);
531 writeTag("connection", null, scm.getConnection(), serializer, scm);
532 writeTag("developerConnection", null, scm.getDeveloperConnection(), serializer, scm);
533 writeTag("tag", "HEAD", scm.getTag(), serializer, scm);
534 writeTag("url", null, scm.getUrl(), serializer, scm);
535 serializer.writeEndElement();
536 }
537 }
538
539 private void writeFileSet(String tagName, FileSet fileSet, XMLStreamWriter serializer)
540 throws IOException, XMLStreamException {
541 if (fileSet != null) {
542 serializer.writeStartElement(namespace, tagName);
543 writeTag("directory", null, fileSet.getDirectory(), serializer, fileSet);
544 writeList("includes", fileSet.getIncludes(), serializer, fileSet,
545 t -> writeTag("include", null, t, serializer, null));
546 writeList("excludes", fileSet.getExcludes(), serializer, fileSet,
547 t -> writeTag("exclude", null, t, serializer, null));
548 serializer.writeEndElement();
549 }
550 }
551
552 private void writeResource(String tagName, Resource resource, XMLStreamWriter serializer)
553 throws IOException, XMLStreamException {
554 if (resource != null) {
555 serializer.writeStartElement(namespace, tagName);
556 writeTag("targetPath", null, resource.getTargetPath(), serializer, resource);
557 writeTag("filtering", null, resource.getFiltering(), serializer, resource);
558 writeTag("directory", null, resource.getDirectory(), serializer, resource);
559 writeList("includes", resource.getIncludes(), serializer, resource,
560 t -> writeTag("include", null, t, serializer, null));
561 writeList("excludes", resource.getExcludes(), serializer, resource,
562 t -> writeTag("exclude", null, t, serializer, null));
563 serializer.writeEndElement();
564 }
565 }
566
567 private void writeRepositoryBase(String tagName, RepositoryBase repositoryBase, XMLStreamWriter serializer)
568 throws IOException, XMLStreamException {
569 if (repositoryBase != null) {
570 serializer.writeStartElement(namespace, tagName);
571 writeTag("id", null, repositoryBase.getId(), serializer, repositoryBase);
572 writeTag("name", null, repositoryBase.getName(), serializer, repositoryBase);
573 writeTag("url", null, repositoryBase.getUrl(), serializer, repositoryBase);
574 writeTag("layout", "default", repositoryBase.getLayout(), serializer, repositoryBase);
575 serializer.writeEndElement();
576 }
577 }
578
579 private void writeRepository(String tagName, Repository repository, XMLStreamWriter serializer)
580 throws IOException, XMLStreamException {
581 if (repository != null) {
582 serializer.writeStartElement(namespace, tagName);
583 writeRepositoryPolicy("releases", repository.getReleases(), serializer);
584 writeRepositoryPolicy("snapshots", repository.getSnapshots(), serializer);
585 writeTag("id", null, repository.getId(), serializer, repository);
586 writeTag("name", null, repository.getName(), serializer, repository);
587 writeTag("url", null, repository.getUrl(), serializer, repository);
588 writeTag("layout", "default", repository.getLayout(), serializer, repository);
589 serializer.writeEndElement();
590 }
591 }
592
593 private void writeDeploymentRepository(String tagName, DeploymentRepository deploymentRepository, XMLStreamWriter serializer)
594 throws IOException, XMLStreamException {
595 if (deploymentRepository != null) {
596 serializer.writeStartElement(namespace, tagName);
597 writeTag("uniqueVersion", "true", deploymentRepository.isUniqueVersion() ? null : "false", serializer, deploymentRepository);
598 writeRepositoryPolicy("releases", deploymentRepository.getReleases(), serializer);
599 writeRepositoryPolicy("snapshots", deploymentRepository.getSnapshots(), serializer);
600 writeTag("id", null, deploymentRepository.getId(), serializer, deploymentRepository);
601 writeTag("name", null, deploymentRepository.getName(), serializer, deploymentRepository);
602 writeTag("url", null, deploymentRepository.getUrl(), serializer, deploymentRepository);
603 writeTag("layout", "default", deploymentRepository.getLayout(), serializer, deploymentRepository);
604 serializer.writeEndElement();
605 }
606 }
607
608 private void writeRepositoryPolicy(String tagName, RepositoryPolicy repositoryPolicy, XMLStreamWriter serializer)
609 throws IOException, XMLStreamException {
610 if (repositoryPolicy != null) {
611 serializer.writeStartElement(namespace, tagName);
612 writeTag("enabled", null, repositoryPolicy.getEnabled(), serializer, repositoryPolicy);
613 writeTag("updatePolicy", null, repositoryPolicy.getUpdatePolicy(), serializer, repositoryPolicy);
614 writeTag("checksumPolicy", null, repositoryPolicy.getChecksumPolicy(), serializer, repositoryPolicy);
615 serializer.writeEndElement();
616 }
617 }
618
619 private void writeSite(String tagName, Site site, XMLStreamWriter serializer)
620 throws IOException, XMLStreamException {
621 if (site != null) {
622 serializer.writeStartElement(namespace, tagName);
623 writeAttr("child.site.url.inherit.append.path", site.getChildSiteUrlInheritAppendPath(), serializer);
624 writeTag("id", null, site.getId(), serializer, site);
625 writeTag("name", null, site.getName(), serializer, site);
626 writeTag("url", null, site.getUrl(), serializer, site);
627 serializer.writeEndElement();
628 }
629 }
630
631 private void writeConfigurationContainer(String tagName, ConfigurationContainer configurationContainer, XMLStreamWriter serializer)
632 throws IOException, XMLStreamException {
633 if (configurationContainer != null) {
634 serializer.writeStartElement(namespace, tagName);
635 writeTag("inherited", null, configurationContainer.getInherited(), serializer, configurationContainer);
636 writeDom(configurationContainer.getConfiguration(), serializer);
637 serializer.writeEndElement();
638 }
639 }
640
641 private void writePlugin(String tagName, Plugin plugin, XMLStreamWriter serializer)
642 throws IOException, XMLStreamException {
643 if (plugin != null) {
644 serializer.writeStartElement(namespace, tagName);
645 writeTag("groupId", "org.apache.maven.plugins", plugin.getGroupId(), serializer, plugin);
646 writeTag("artifactId", null, plugin.getArtifactId(), serializer, plugin);
647 writeTag("version", null, plugin.getVersion(), serializer, plugin);
648 writeTag("extensions", null, plugin.getExtensions(), serializer, plugin);
649 writeList("executions", false, plugin.getExecutions(), serializer, plugin,
650 t -> writePluginExecution("execution", t, serializer));
651 writeList("dependencies", false, plugin.getDependencies(), serializer, plugin,
652 t -> writeDependency("dependency", t, serializer));
653 writeTag("inherited", null, plugin.getInherited(), serializer, plugin);
654 writeDom(plugin.getConfiguration(), serializer);
655 serializer.writeEndElement();
656 }
657 }
658
659 private void writePluginExecution(String tagName, PluginExecution pluginExecution, XMLStreamWriter serializer)
660 throws IOException, XMLStreamException {
661 if (pluginExecution != null) {
662 serializer.writeStartElement(namespace, tagName);
663 writeTag("id", "default", pluginExecution.getId(), serializer, pluginExecution);
664 writeTag("phase", null, pluginExecution.getPhase(), serializer, pluginExecution);
665 writeTag("priority", "0", Integer.toString(pluginExecution.getPriority()), serializer, pluginExecution);
666 writeList("goals", pluginExecution.getGoals(), serializer, pluginExecution,
667 t -> writeTag("goal", null, t, serializer, null));
668 writeTag("inherited", null, pluginExecution.getInherited(), serializer, pluginExecution);
669 writeDom(pluginExecution.getConfiguration(), serializer);
670 serializer.writeEndElement();
671 }
672 }
673
674 private void writeDependencyManagement(String tagName, DependencyManagement dependencyManagement, XMLStreamWriter serializer)
675 throws IOException, XMLStreamException {
676 if (dependencyManagement != null) {
677 serializer.writeStartElement(namespace, tagName);
678 writeList("dependencies", false, dependencyManagement.getDependencies(), serializer, dependencyManagement,
679 t -> writeDependency("dependency", t, serializer));
680 serializer.writeEndElement();
681 }
682 }
683
684 private void writePluginManagement(String tagName, PluginManagement pluginManagement, XMLStreamWriter serializer)
685 throws IOException, XMLStreamException {
686 if (pluginManagement != null) {
687 serializer.writeStartElement(namespace, tagName);
688 writeList("plugins", false, pluginManagement.getPlugins(), serializer, pluginManagement,
689 t -> writePlugin("plugin", t, serializer));
690 serializer.writeEndElement();
691 }
692 }
693
694 private void writeReporting(String tagName, Reporting reporting, XMLStreamWriter serializer)
695 throws IOException, XMLStreamException {
696 if (reporting != null) {
697 serializer.writeStartElement(namespace, tagName);
698 writeTag("excludeDefaults", null, reporting.getExcludeDefaults(), serializer, reporting);
699 writeTag("outputDirectory", null, reporting.getOutputDirectory(), serializer, reporting);
700 writeList("plugins", false, reporting.getPlugins(), serializer, reporting,
701 t -> writeReportPlugin("plugin", t, serializer));
702 serializer.writeEndElement();
703 }
704 }
705
706 private void writeProfile(String tagName, Profile profile, XMLStreamWriter serializer)
707 throws IOException, XMLStreamException {
708 if (profile != null) {
709 serializer.writeStartElement(namespace, tagName);
710 writeTag("id", "default", profile.getId(), serializer, profile);
711 writeActivation("activation", profile.getActivation(), serializer);
712 writeBuildBase("build", profile.getBuild(), serializer);
713 writeList("modules", profile.getModules(), serializer, profile,
714 t -> writeTag("module", null, t, serializer, null));
715 writeList("subprojects", profile.getSubprojects(), serializer, profile,
716 t -> writeTag("subproject", null, t, serializer, null));
717 writeDistributionManagement("distributionManagement", profile.getDistributionManagement(), serializer);
718 writeProperties("properties", profile.getProperties(), serializer, profile);
719 writeDependencyManagement("dependencyManagement", profile.getDependencyManagement(), serializer);
720 writeList("dependencies", false, profile.getDependencies(), serializer, profile,
721 t -> writeDependency("dependency", t, serializer));
722 writeList("repositories", false, profile.getRepositories(), serializer, profile,
723 t -> writeRepository("repository", t, serializer));
724 writeList("pluginRepositories", false, profile.getPluginRepositories(), serializer, profile,
725 t -> writeRepository("pluginRepository", t, serializer));
726 writeReporting("reporting", profile.getReporting(), serializer);
727 serializer.writeEndElement();
728 }
729 }
730
731 private void writeActivation(String tagName, Activation activation, XMLStreamWriter serializer)
732 throws IOException, XMLStreamException {
733 if (activation != null) {
734 serializer.writeStartElement(namespace, tagName);
735 writeTag("activeByDefault", "false", activation.isActiveByDefault() ? "true" : null, serializer, activation);
736 writeTag("jdk", null, activation.getJdk(), serializer, activation);
737 writeActivationOS("os", activation.getOs(), serializer);
738 writeActivationProperty("property", activation.getProperty(), serializer);
739 writeActivationFile("file", activation.getFile(), serializer);
740 writeTag("packaging", null, activation.getPackaging(), serializer, activation);
741 serializer.writeEndElement();
742 }
743 }
744
745 private void writeActivationProperty(String tagName, ActivationProperty activationProperty, XMLStreamWriter serializer)
746 throws IOException, XMLStreamException {
747 if (activationProperty != null) {
748 serializer.writeStartElement(namespace, tagName);
749 writeTag("name", null, activationProperty.getName(), serializer, activationProperty);
750 writeTag("value", null, activationProperty.getValue(), serializer, activationProperty);
751 serializer.writeEndElement();
752 }
753 }
754
755 private void writeActivationOS(String tagName, ActivationOS activationOS, XMLStreamWriter serializer)
756 throws IOException, XMLStreamException {
757 if (activationOS != null) {
758 serializer.writeStartElement(namespace, tagName);
759 writeTag("name", null, activationOS.getName(), serializer, activationOS);
760 writeTag("family", null, activationOS.getFamily(), serializer, activationOS);
761 writeTag("arch", null, activationOS.getArch(), serializer, activationOS);
762 writeTag("version", null, activationOS.getVersion(), serializer, activationOS);
763 serializer.writeEndElement();
764 }
765 }
766
767 private void writeActivationFile(String tagName, ActivationFile activationFile, XMLStreamWriter serializer)
768 throws IOException, XMLStreamException {
769 if (activationFile != null) {
770 serializer.writeStartElement(namespace, tagName);
771 writeTag("missing", null, activationFile.getMissing(), serializer, activationFile);
772 writeTag("exists", null, activationFile.getExists(), serializer, activationFile);
773 serializer.writeEndElement();
774 }
775 }
776
777 private void writeReportPlugin(String tagName, ReportPlugin reportPlugin, XMLStreamWriter serializer)
778 throws IOException, XMLStreamException {
779 if (reportPlugin != null) {
780 serializer.writeStartElement(namespace, tagName);
781 writeTag("groupId", "org.apache.maven.plugins", reportPlugin.getGroupId(), serializer, reportPlugin);
782 writeTag("artifactId", null, reportPlugin.getArtifactId(), serializer, reportPlugin);
783 writeTag("version", null, reportPlugin.getVersion(), serializer, reportPlugin);
784 writeList("reportSets", false, reportPlugin.getReportSets(), serializer, reportPlugin,
785 t -> writeReportSet("reportSet", t, serializer));
786 writeTag("inherited", null, reportPlugin.getInherited(), serializer, reportPlugin);
787 writeDom(reportPlugin.getConfiguration(), serializer);
788 serializer.writeEndElement();
789 }
790 }
791
792 private void writeReportSet(String tagName, ReportSet reportSet, XMLStreamWriter serializer)
793 throws IOException, XMLStreamException {
794 if (reportSet != null) {
795 serializer.writeStartElement(namespace, tagName);
796 writeTag("id", "default", reportSet.getId(), serializer, reportSet);
797 writeList("reports", reportSet.getReports(), serializer, reportSet,
798 t -> writeTag("report", null, t, serializer, null));
799 writeTag("inherited", null, reportSet.getInherited(), serializer, reportSet);
800 writeDom(reportSet.getConfiguration(), serializer);
801 serializer.writeEndElement();
802 }
803 }
804
805 private void writePrerequisites(String tagName, Prerequisites prerequisites, XMLStreamWriter serializer)
806 throws IOException, XMLStreamException {
807 if (prerequisites != null) {
808 serializer.writeStartElement(namespace, tagName);
809 writeTag("maven", "2.0", prerequisites.getMaven(), serializer, prerequisites);
810 serializer.writeEndElement();
811 }
812 }
813
814 private void writeRelocation(String tagName, Relocation relocation, XMLStreamWriter serializer)
815 throws IOException, XMLStreamException {
816 if (relocation != null) {
817 serializer.writeStartElement(namespace, tagName);
818 writeTag("groupId", null, relocation.getGroupId(), serializer, relocation);
819 writeTag("artifactId", null, relocation.getArtifactId(), serializer, relocation);
820 writeTag("version", null, relocation.getVersion(), serializer, relocation);
821 writeTag("message", null, relocation.getMessage(), serializer, relocation);
822 serializer.writeEndElement();
823 }
824 }
825
826 private void writeExtension(String tagName, Extension extension, XMLStreamWriter serializer)
827 throws IOException, XMLStreamException {
828 if (extension != null) {
829 serializer.writeStartElement(namespace, tagName);
830 writeTag("groupId", null, extension.getGroupId(), serializer, extension);
831 writeTag("artifactId", null, extension.getArtifactId(), serializer, extension);
832 writeTag("version", null, extension.getVersion(), serializer, extension);
833 writeDom(extension.getConfiguration(), serializer);
834 serializer.writeEndElement();
835 }
836 }
837
838 @FunctionalInterface
839 private interface ElementWriter<T> {
840 public void write(T t) throws IOException, XMLStreamException;
841 }
842
843 private <T> void writeList(String tagName, List<T> list, XMLStreamWriter serializer, InputLocationTracker locationTracker, ElementWriter<T> writer) throws IOException, XMLStreamException {
844 writeList(tagName, false, list, serializer, locationTracker, writer);
845 }
846
847 private <T> void writeList(String tagName, boolean flat, List<T> list, XMLStreamWriter serializer, InputLocationTracker locationTracker, ElementWriter<T> writer) throws IOException, XMLStreamException {
848 if (list != null && !list.isEmpty()) {
849 if (!flat) {
850 serializer.writeStartElement(namespace, tagName);
851 }
852 int index = 0;
853 InputLocation location = locationTracker != null ? locationTracker.getLocation(tagName) : null;
854 for (T t : list) {
855 writer.write(t);
856 writeLocationTracking(location, Integer.valueOf(index++), serializer);
857 }
858 if (!flat) {
859 serializer.writeEndElement();
860 }
861 }
862 }
863
864 private <T> void writeProperties(String tagName, Map<String, String> props, XMLStreamWriter serializer, InputLocationTracker locationTracker) throws IOException, XMLStreamException {
865 if (props != null && !props.isEmpty()) {
866 serializer.writeStartElement(namespace, tagName);
867 InputLocation location = locationTracker != null ? locationTracker.getLocation(tagName) : null;
868 for (Map.Entry<String, String> entry : props.entrySet()) {
869 String key = entry.getKey();
870 writeTag(key, null, entry.getValue(), serializer, null);
871 writeLocationTracking(location, key, serializer);
872 }
873 serializer.writeEndElement();
874 }
875 }
876
877 private void writeDom(XmlNode dom, XMLStreamWriter serializer) throws IOException, XMLStreamException {
878 if (dom != null) {
879 serializer.writeStartElement(namespace, dom.getName());
880 for (Map.Entry<String, String> attr : dom.getAttributes().entrySet()) {
881 if (attr.getKey().startsWith("xml:")) {
882 serializer.writeAttribute("http://www.w3.org/XML/1998/namespace",
883 attr.getKey().substring(4), attr.getValue());
884 } else {
885 serializer.writeAttribute(attr.getKey(), attr.getValue());
886 }
887 }
888 for (XmlNode child : dom.getChildren()) {
889 writeDom(child, serializer);
890 }
891 String value = dom.getValue();
892 if (value != null) {
893 serializer.writeCharacters(value);
894 }
895 serializer.writeEndElement();
896 if (addLocationInformation && dom.getInputLocation() instanceof InputLocation && dom.getChildren().isEmpty()) {
897 serializer.writeComment(toString((InputLocation) dom.getInputLocation()));
898 }
899 }
900 }
901
902 private void writeTag(String tagName, String defaultValue, String value, XMLStreamWriter serializer, InputLocationTracker locationTracker) throws IOException, XMLStreamException {
903 if (value != null && !Objects.equals(defaultValue, value)) {
904 serializer.writeStartElement(namespace, tagName);
905 serializer.writeCharacters(value);
906 serializer.writeEndElement();
907 writeLocationTracking(locationTracker, tagName, serializer);
908 }
909 }
910
911 private void writeAttr(String attrName, String value, XMLStreamWriter serializer) throws IOException, XMLStreamException {
912 if (value != null) {
913 serializer.writeAttribute(attrName, value);
914 }
915 }
916
917
918
919
920
921
922
923
924
925 protected void writeLocationTracking(InputLocationTracker locationTracker, Object key, XMLStreamWriter serializer) throws IOException, XMLStreamException {
926 if (addLocationInformation) {
927 InputLocation location = (locationTracker == null) ? null : locationTracker.getLocation(key);
928 if (location != null) {
929 serializer.writeComment(toString(location));
930 }
931 }
932 }
933
934
935
936
937
938
939
940 protected String toString(InputLocation location) {
941 if (stringFormatter != null) {
942 return stringFormatter.apply(location);
943 }
944 if (location.getSource() != null) {
945 return ' ' + location.getSource().toString() + ':' + location.getLineNumber() + ' ';
946 } else {
947 return " " + location.getLineNumber() + " ";
948 }
949 }
950
951 static class IndentingXMLStreamWriter extends StreamWriterDelegate {
952
953 int depth = 0;
954 boolean hasChildren = false;
955
956 public IndentingXMLStreamWriter(XMLStreamWriter parent) {
957 super(parent);
958 }
959
960 @Override
961 public void writeEmptyElement(String localName) throws XMLStreamException {
962 indent();
963 super.writeEmptyElement(localName);
964 hasChildren = true;
965 }
966
967 @Override
968 public void writeEmptyElement(String namespaceURI, String localName) throws XMLStreamException {
969 indent();
970 super.writeEmptyElement(namespaceURI, localName);
971 hasChildren = true;
972 }
973
974 @Override
975 public void writeEmptyElement(String prefix, String localName, String namespaceURI) throws XMLStreamException {
976 indent();
977 super.writeEmptyElement(prefix, localName, namespaceURI);
978 hasChildren = true;
979 }
980
981 @Override
982 public void writeStartElement(String localName) throws XMLStreamException {
983 indent();
984 super.writeStartElement(localName);
985 depth++;
986 hasChildren = false;
987 }
988
989 @Override
990 public void writeStartElement(String namespaceURI, String localName) throws XMLStreamException {
991 indent();
992 super.writeStartElement(namespaceURI, localName);
993 depth++;
994 hasChildren = false;
995 }
996
997 @Override
998 public void writeStartElement(String prefix, String localName, String namespaceURI) throws XMLStreamException {
999 indent();
1000 super.writeStartElement(prefix, localName, namespaceURI);
1001 depth++;
1002 hasChildren = false;
1003 }
1004
1005 @Override
1006 public void writeEndElement() throws XMLStreamException {
1007 depth--;
1008 if (hasChildren) {
1009 indent();
1010 }
1011 super.writeEndElement();
1012 hasChildren = true;
1013 }
1014
1015 private void indent() throws XMLStreamException {
1016 super.writeCharacters("\n");
1017 for (int i = 0; i < depth; i++) {
1018 super.writeCharacters(" ");
1019 }
1020 }
1021 }
1022 }