|
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; |
26 | | -import java.nio.file.Files; |
27 | | -import java.nio.file.Path; |
28 | | - |
29 | 21 | import org.apache.maven.api.annotations.Nonnull; |
30 | 22 | import org.apache.maven.api.di.Named; |
31 | 23 | import org.apache.maven.api.di.Singleton; |
|
35 | 27 | import org.apache.maven.api.services.xml.XmlReaderRequest; |
36 | 28 | import org.apache.maven.api.services.xml.XmlWriterException; |
37 | 29 | import org.apache.maven.api.services.xml.XmlWriterRequest; |
38 | | -import org.apache.maven.plugin.descriptor.io.PluginDescriptorStaxReader; |
39 | | -import org.apache.maven.plugin.descriptor.io.PluginDescriptorStaxWriter; |
40 | | - |
41 | | -import static org.apache.maven.impl.ImplUtils.nonNull; |
42 | | -import static org.apache.maven.impl.StaxLocation.getLocation; |
43 | | -import static org.apache.maven.impl.StaxLocation.getMessage; |
44 | 30 |
|
45 | 31 | @Named |
46 | 32 | @Singleton |
47 | 33 | public class DefaultPluginXmlFactory implements PluginXmlFactory { |
| 34 | + |
48 | 35 | @Override |
49 | 36 | 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 | | - } |
58 | | - 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()); |
72 | | - } |
73 | | - } |
74 | | - } catch (Exception e) { |
75 | | - throw new XmlReaderException("Unable to read plugin: " + getMessage(e), getLocation(e), e); |
76 | | - } |
| 37 | + return new ReadRequest(request).read(); |
77 | 38 | } |
78 | 39 |
|
79 | 40 | @Override |
80 | 41 | 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 | | - } |
89 | | - 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); |
97 | | - } |
98 | | - } |
99 | | - } catch (Exception e) { |
100 | | - throw new XmlWriterException("Unable to write plugin: " + getMessage(e), getLocation(e), e); |
101 | | - } |
| 42 | + new WriteRequest(request).write(); |
102 | 43 | } |
103 | 44 |
|
104 | 45 | /** |
|
0 commit comments