|
18 | 18 | */ |
19 | 19 | package org.apache.maven.impl; |
20 | 20 |
|
21 | | -import javax.xml.stream.XMLStreamException; |
22 | | - |
23 | | -import java.io.IOException; |
24 | 21 | import java.io.InputStream; |
25 | 22 | import java.io.OutputStream; |
26 | 23 | import java.io.Reader; |
|
48 | 45 | @Named |
49 | 46 | @Singleton |
50 | 47 | public class DefaultPluginXmlFactory implements PluginXmlFactory { |
51 | | - |
52 | 48 | @Override |
53 | 49 | public PluginDescriptor read(@Nonnull XmlReaderRequest request) throws XmlReaderException { |
54 | 50 | nonNull(request, "request"); |
55 | 51 | Path path = request.getPath(); |
56 | 52 | URL url = request.getURL(); |
57 | 53 | Reader reader = request.getReader(); |
58 | 54 | InputStream inputStream = request.getInputStream(); |
59 | | - if (inputStream == null && reader == null && path == null && url == null) { |
| 55 | + if (path == null && url == null && reader == null && inputStream == null) { |
60 | 56 | throw new IllegalArgumentException("path, url, reader or inputStream must be non null"); |
61 | 57 | } |
62 | | - return read(request.isAddDefaultEntities(), request.isStrict(), inputStream, reader, path, url); |
63 | | - } |
64 | | - |
65 | | - private static PluginDescriptor read( |
66 | | - boolean addDefaultEntities, boolean strict, InputStream inputStream, Reader reader, Path path, URL url) { |
67 | 58 | try { |
68 | 59 | PluginDescriptorStaxReader xml = new PluginDescriptorStaxReader(); |
69 | | - xml.setAddDefaultEntities(addDefaultEntities); |
| 60 | + xml.setAddDefaultEntities(request.isAddDefaultEntities()); |
70 | 61 | if (inputStream != null) { |
71 | | - return read(xml, inputStream, strict); |
| 62 | + return xml.read(inputStream, request.isStrict()); |
72 | 63 | } else if (reader != null) { |
73 | | - return xml.read(reader, strict); |
| 64 | + return xml.read(reader, request.isStrict()); |
74 | 65 | } else if (path != null) { |
75 | 66 | try (InputStream is = Files.newInputStream(path)) { |
76 | | - return read(xml, is, strict); |
| 67 | + return xml.read(is, request.isStrict()); |
| 68 | + } |
| 69 | + } else { |
| 70 | + try (InputStream is = url.openStream()) { |
| 71 | + return xml.read(is, request.isStrict()); |
77 | 72 | } |
78 | 73 | } |
79 | | - try (InputStream is = url.openStream()) { |
80 | | - return read(xml, is, strict); |
81 | | - } |
82 | | - } catch (IOException | XMLStreamException e) { |
| 74 | + } catch (Exception e) { |
83 | 75 | throw new XmlReaderException("Unable to read plugin: " + getMessage(e), getLocation(e), e); |
84 | 76 | } |
85 | 77 | } |
86 | 78 |
|
87 | | - private static PluginDescriptor read(PluginDescriptorStaxReader xml, InputStream is, boolean strict) |
88 | | - throws XMLStreamException { |
89 | | - return xml.read(is, strict); |
90 | | - } |
91 | | - |
92 | 79 | @Override |
93 | 80 | public void write(XmlWriterRequest<PluginDescriptor> request) throws XmlWriterException { |
94 | 81 | nonNull(request, "request"); |
| 82 | + PluginDescriptor content = nonNull(request.getContent(), "content"); |
95 | 83 | Path path = request.getPath(); |
96 | 84 | OutputStream outputStream = request.getOutputStream(); |
97 | 85 | Writer writer = request.getWriter(); |
98 | 86 | if (writer == null && outputStream == null && path == null) { |
99 | 87 | throw new IllegalArgumentException("writer, output stream, or path must be non null"); |
100 | 88 | } |
101 | | - write(writer, request.getContent(), outputStream, path); |
102 | | - } |
103 | | - |
104 | | - private static void write(Writer writer, PluginDescriptor content, OutputStream outputStream, Path path) { |
105 | 89 | try { |
106 | 90 | if (writer != null) { |
107 | 91 | new PluginDescriptorStaxWriter().write(writer, content); |
108 | 92 | } else if (outputStream != null) { |
109 | 93 | new PluginDescriptorStaxWriter().write(outputStream, content); |
110 | 94 | } else { |
111 | 95 | try (OutputStream os = Files.newOutputStream(path)) { |
112 | | - new PluginDescriptorStaxWriter().write(os, content); |
| 96 | + new PluginDescriptorStaxWriter().write(outputStream, content); |
113 | 97 | } |
114 | 98 | } |
115 | | - } catch (IOException | XMLStreamException e) { |
| 99 | + } catch (Exception e) { |
116 | 100 | throw new XmlWriterException("Unable to write plugin: " + getMessage(e), getLocation(e), e); |
117 | 101 | } |
118 | 102 | } |
|
0 commit comments