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