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