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