|
18 | 18 | */
|
19 | 19 | package org.apache.maven.impl;
|
20 | 20 |
|
21 |
| -import java.io.InputStream; |
22 |
| -import java.io.OutputStream; |
23 |
| -import java.io.Reader; |
24 |
| -import java.io.Writer; |
25 |
| -import java.net.URL; |
| 21 | +import javax.xml.stream.XMLStreamException; |
| 22 | + |
| 23 | +import java.io.IOException; |
26 | 24 | import java.nio.file.Files;
|
27 |
| -import java.nio.file.Path; |
28 | 25 |
|
29 | 26 | import org.apache.maven.api.annotations.Nonnull;
|
30 | 27 | import org.apache.maven.api.di.Named;
|
|
38 | 35 | import org.apache.maven.plugin.descriptor.io.PluginDescriptorStaxReader;
|
39 | 36 | import org.apache.maven.plugin.descriptor.io.PluginDescriptorStaxWriter;
|
40 | 37 |
|
41 |
| -import static org.apache.maven.impl.ImplUtils.nonNull; |
42 | 38 | import static org.apache.maven.impl.StaxLocation.getLocation;
|
43 | 39 | import static org.apache.maven.impl.StaxLocation.getMessage;
|
44 | 40 |
|
45 | 41 | @Named
|
46 | 42 | @Singleton
|
47 | 43 | public class DefaultPluginXmlFactory implements PluginXmlFactory {
|
| 44 | + |
48 | 45 | @Override
|
49 | 46 | public PluginDescriptor read(@Nonnull XmlReaderRequest request) throws XmlReaderException {
|
50 |
| - nonNull(request, "request"); |
51 |
| - Path path = request.getPath(); |
52 |
| - URL url = request.getURL(); |
53 |
| - Reader reader = request.getReader(); |
54 |
| - InputStream inputStream = request.getInputStream(); |
55 |
| - if (path == null && url == null && reader == null && inputStream == null) { |
56 |
| - throw new IllegalArgumentException("path, url, reader or inputStream must be non null"); |
57 |
| - } |
| 47 | + return read(request, setAddDefaultEntities(request, new PluginDescriptorStaxReader())); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void write(XmlWriterRequest<PluginDescriptor> request) throws XmlWriterException { |
| 52 | + write(request, new PluginDescriptorStaxWriter()); |
| 53 | + } |
| 54 | + |
| 55 | + private static PluginDescriptor read(XmlReaderRequest request, PluginDescriptorStaxReader reader) { |
| 56 | + request.assertReadable(); |
58 | 57 | try {
|
59 |
| - PluginDescriptorStaxReader xml = new PluginDescriptorStaxReader(); |
60 |
| - xml.setAddDefaultEntities(request.isAddDefaultEntities()); |
61 |
| - if (inputStream != null) { |
62 |
| - return xml.read(inputStream, request.isStrict()); |
63 |
| - } else if (reader != null) { |
64 |
| - return xml.read(reader, request.isStrict()); |
65 |
| - } else if (path != null) { |
66 |
| - try (InputStream is = Files.newInputStream(path)) { |
67 |
| - return xml.read(is, request.isStrict()); |
| 58 | + if (request.getInputStream() != null) { |
| 59 | + return reader.read(request.getInputStream(), request.isStrict()); |
| 60 | + } else if (request.getReader() != null) { |
| 61 | + return reader.read(request.getReader(), request.isStrict()); |
| 62 | + } else if (request.getPath() != null) { |
| 63 | + try (var is = Files.newInputStream(request.getPath())) { |
| 64 | + return reader.read(is, request.isStrict()); |
68 | 65 | }
|
69 | 66 | } else {
|
70 |
| - try (InputStream is = url.openStream()) { |
71 |
| - return xml.read(is, request.isStrict()); |
| 67 | + try (var is = request.getURL().openStream()) { |
| 68 | + return reader.read(is, request.isStrict()); |
72 | 69 | }
|
73 | 70 | }
|
74 |
| - } catch (Exception e) { |
| 71 | + } catch (IOException | XMLStreamException e) { |
75 | 72 | throw new XmlReaderException("Unable to read plugin: " + getMessage(e), getLocation(e), e);
|
76 | 73 | }
|
77 | 74 | }
|
78 | 75 |
|
79 |
| - @Override |
80 |
| - public void write(XmlWriterRequest<PluginDescriptor> request) throws XmlWriterException { |
81 |
| - nonNull(request, "request"); |
82 |
| - PluginDescriptor content = nonNull(request.getContent(), "content"); |
83 |
| - Path path = request.getPath(); |
84 |
| - OutputStream outputStream = request.getOutputStream(); |
85 |
| - Writer writer = request.getWriter(); |
86 |
| - if (writer == null && outputStream == null && path == null) { |
87 |
| - throw new IllegalArgumentException("writer, outputStream or path must be non null"); |
88 |
| - } |
| 76 | + private static PluginDescriptorStaxReader setAddDefaultEntities( |
| 77 | + XmlReaderRequest request, PluginDescriptorStaxReader reader) { |
| 78 | + reader.setAddDefaultEntities(request.isAddDefaultEntities()); |
| 79 | + return reader; |
| 80 | + } |
| 81 | + |
| 82 | + private static void write(XmlWriterRequest<PluginDescriptor> request, PluginDescriptorStaxWriter writer) { |
| 83 | + request.assertWritable(); |
89 | 84 | try {
|
90 |
| - if (writer != null) { |
91 |
| - new PluginDescriptorStaxWriter().write(writer, content); |
92 |
| - } else if (outputStream != null) { |
93 |
| - new PluginDescriptorStaxWriter().write(outputStream, content); |
| 85 | + if (request.getWriter() != null) { |
| 86 | + writer.write(request.getWriter(), request.getContent()); |
| 87 | + } else if (request.getOutputStream() != null) { |
| 88 | + writer.write(request.getOutputStream(), request.getContent()); |
94 | 89 | } else {
|
95 |
| - try (OutputStream os = Files.newOutputStream(path)) { |
96 |
| - new PluginDescriptorStaxWriter().write(os, content); |
| 90 | + try (var os = Files.newOutputStream(request.getPath())) { |
| 91 | + writer.write(os, request.getContent()); |
97 | 92 | }
|
98 | 93 | }
|
99 |
| - } catch (Exception e) { |
| 94 | + } catch (XmlWriterException | XMLStreamException | IOException e) { |
100 | 95 | throw new XmlWriterException("Unable to write plugin: " + getMessage(e), getLocation(e), e);
|
101 | 96 | }
|
102 | 97 | }
|
|
0 commit comments