1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.internal.impl;
20
21 import javax.inject.Named;
22 import javax.inject.Singleton;
23
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.io.Reader;
27 import java.io.Writer;
28 import java.net.URL;
29 import java.nio.file.Files;
30 import java.nio.file.Path;
31
32 import org.apache.maven.api.annotations.Nonnull;
33 import org.apache.maven.api.plugin.descriptor.PluginDescriptor;
34 import org.apache.maven.api.services.xml.PluginXmlFactory;
35 import org.apache.maven.api.services.xml.XmlReaderException;
36 import org.apache.maven.api.services.xml.XmlReaderRequest;
37 import org.apache.maven.api.services.xml.XmlWriterException;
38 import org.apache.maven.api.services.xml.XmlWriterRequest;
39 import org.apache.maven.plugin.descriptor.io.PluginDescriptorStaxReader;
40 import org.apache.maven.plugin.descriptor.io.PluginDescriptorStaxWriter;
41
42 import static org.apache.maven.internal.impl.StaxLocation.getLocation;
43 import static org.apache.maven.internal.impl.StaxLocation.getMessage;
44 import static org.apache.maven.internal.impl.Utils.nonNull;
45
46 @Named
47 @Singleton
48 public class DefaultPluginXmlFactory implements PluginXmlFactory {
49 @Override
50 public PluginDescriptor read(@Nonnull XmlReaderRequest request) throws XmlReaderException {
51 nonNull(request, "request");
52 Path path = request.getPath();
53 URL url = request.getURL();
54 Reader reader = request.getReader();
55 InputStream inputStream = request.getInputStream();
56 if (path == null && url == null && reader == null && inputStream == null) {
57 throw new IllegalArgumentException("path, url, reader or inputStream must be non null");
58 }
59 try {
60 PluginDescriptorStaxReader xml = new PluginDescriptorStaxReader();
61 xml.setAddDefaultEntities(request.isAddDefaultEntities());
62 if (inputStream != null) {
63 return xml.read(inputStream, request.isStrict());
64 } else if (reader != null) {
65 return xml.read(reader, request.isStrict());
66 } else if (path != null) {
67 try (InputStream is = Files.newInputStream(path)) {
68 return xml.read(is, request.isStrict());
69 }
70 } else {
71 try (InputStream is = url.openStream()) {
72 return xml.read(is, request.isStrict());
73 }
74 }
75 } catch (Exception e) {
76 throw new XmlReaderException("Unable to read plugin: " + getMessage(e), getLocation(e), e);
77 }
78 }
79
80 @Override
81 public void write(XmlWriterRequest<PluginDescriptor> request) throws XmlWriterException {
82 nonNull(request, "request");
83 PluginDescriptor content = nonNull(request.getContent(), "content");
84 Path path = request.getPath();
85 OutputStream outputStream = request.getOutputStream();
86 Writer writer = request.getWriter();
87 if (writer == null && outputStream == null && path == null) {
88 throw new IllegalArgumentException("writer, outputStream or path must be non null");
89 }
90 try {
91 if (writer != null) {
92 new PluginDescriptorStaxWriter().write(writer, content);
93 } else if (outputStream != null) {
94 new PluginDescriptorStaxWriter().write(outputStream, content);
95 } else {
96 try (OutputStream os = Files.newOutputStream(path)) {
97 new PluginDescriptorStaxWriter().write(outputStream, content);
98 }
99 }
100 } catch (Exception e) {
101 throw new XmlWriterException("Unable to write plugin: " + getMessage(e), getLocation(e), e);
102 }
103 }
104
105
106
107
108
109
110
111
112
113 public static PluginDescriptor fromXml(@Nonnull String xml) throws XmlReaderException {
114 return new DefaultPluginXmlFactory().fromXmlString(xml);
115 }
116
117
118
119
120
121
122
123
124
125 public static String toXml(@Nonnull PluginDescriptor content) throws XmlWriterException {
126 return new DefaultPluginXmlFactory().toXmlString(content);
127 }
128 }