-
Notifications
You must be signed in to change notification settings - Fork 2.8k
extract common functionality of DefaultPluginXmlFactory #2326
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -18,13 +18,12 @@ | |||||
| */ | ||||||
| package org.apache.maven.impl; | ||||||
|
|
||||||
| import javax.xml.stream.XMLStreamException; | ||||||
|
|
||||||
| import java.io.IOException; | ||||||
| import java.io.InputStream; | ||||||
| import java.io.OutputStream; | ||||||
| import java.io.Reader; | ||||||
| import java.io.Writer; | ||||||
| import java.net.URL; | ||||||
| import java.nio.file.Files; | ||||||
| import java.nio.file.Path; | ||||||
|
|
||||||
| import org.apache.maven.api.annotations.Nonnull; | ||||||
| import org.apache.maven.api.di.Named; | ||||||
|
|
@@ -38,65 +37,63 @@ | |||||
| import org.apache.maven.plugin.descriptor.io.PluginDescriptorStaxReader; | ||||||
| import org.apache.maven.plugin.descriptor.io.PluginDescriptorStaxWriter; | ||||||
|
|
||||||
| import static org.apache.maven.impl.ImplUtils.nonNull; | ||||||
| import static org.apache.maven.impl.StaxLocation.getLocation; | ||||||
| import static org.apache.maven.impl.StaxLocation.getMessage; | ||||||
|
|
||||||
| @Named | ||||||
| @Singleton | ||||||
| public class DefaultPluginXmlFactory implements PluginXmlFactory { | ||||||
Pankraz76 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
|
||||||
| @Override | ||||||
| public PluginDescriptor read(@Nonnull XmlReaderRequest request) throws XmlReaderException { | ||||||
Pankraz76 marked this conversation as resolved.
Show resolved
Hide resolved
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
might have generic |
||||||
| nonNull(request, "request"); | ||||||
| Path path = request.getPath(); | ||||||
Pankraz76 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| URL url = request.getURL(); | ||||||
| Reader reader = request.getReader(); | ||||||
| InputStream inputStream = request.getInputStream(); | ||||||
| if (path == null && url == null && reader == null && inputStream == null) { | ||||||
| throw new IllegalArgumentException("path, url, reader or inputStream must be non null"); | ||||||
Pankraz76 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| } | ||||||
| return read(request, setAddDefaultEntities(request, new PluginDescriptorStaxReader())); | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public void write(XmlWriterRequest<PluginDescriptor> request) throws XmlWriterException { | ||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having a factory
This seems more service, then factory layer.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, but in that case, the PR is off-topic and maybe the point is to rename the interfaces.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
might have generic |
||||||
| write(request, new PluginDescriptorStaxWriter()); | ||||||
| } | ||||||
|
|
||||||
| private static PluginDescriptor read(XmlReaderRequest request, PluginDescriptorStaxReader reader) { | ||||||
| request.throwIfIncomplete(); | ||||||
| try { | ||||||
| PluginDescriptorStaxReader xml = new PluginDescriptorStaxReader(); | ||||||
Pankraz76 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| xml.setAddDefaultEntities(request.isAddDefaultEntities()); | ||||||
| if (inputStream != null) { | ||||||
Pankraz76 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| return xml.read(inputStream, request.isStrict()); | ||||||
| } else if (reader != null) { | ||||||
| return xml.read(reader, request.isStrict()); | ||||||
| } else if (path != null) { | ||||||
| try (InputStream is = Files.newInputStream(path)) { | ||||||
| return xml.read(is, request.isStrict()); | ||||||
| if (request.getInputStream() != null) { | ||||||
| return reader.read(request.getInputStream(), request.isStrict()); | ||||||
| } else if (request.getReader() != null) { | ||||||
| return reader.read(request.getReader(), request.isStrict()); | ||||||
| } else if (request.getPath() != null) { | ||||||
| try (InputStream is = Files.newInputStream(request.getPath())) { | ||||||
| return reader.read(is, request.isStrict()); | ||||||
| } | ||||||
| } else { | ||||||
| try (InputStream is = url.openStream()) { | ||||||
| return xml.read(is, request.isStrict()); | ||||||
| try (InputStream is = request.getURL().openStream()) { | ||||||
| return reader.read(is, request.isStrict()); | ||||||
| } | ||||||
| } | ||||||
| } catch (Exception e) { | ||||||
| } catch (IOException | XMLStreamException e) { | ||||||
| throw new XmlReaderException("Unable to read plugin: " + getMessage(e), getLocation(e), e); | ||||||
Pankraz76 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| } | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public void write(XmlWriterRequest<PluginDescriptor> request) throws XmlWriterException { | ||||||
| nonNull(request, "request"); | ||||||
| PluginDescriptor content = nonNull(request.getContent(), "content"); | ||||||
| Path path = request.getPath(); | ||||||
| OutputStream outputStream = request.getOutputStream(); | ||||||
| Writer writer = request.getWriter(); | ||||||
| if (writer == null && outputStream == null && path == null) { | ||||||
| throw new IllegalArgumentException("writer, outputStream or path must be non null"); | ||||||
| } | ||||||
Pankraz76 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| private static PluginDescriptorStaxReader setAddDefaultEntities( | ||||||
| XmlReaderRequest request, PluginDescriptorStaxReader reader) { | ||||||
| reader.setAddDefaultEntities(request.isAddDefaultEntities()); | ||||||
| return reader; | ||||||
| } | ||||||
|
|
||||||
| private static void write(XmlWriterRequest<PluginDescriptor> request, PluginDescriptorStaxWriter writer) { | ||||||
| request.throwIfIncomplete(); | ||||||
| try { | ||||||
| if (writer != null) { | ||||||
| new PluginDescriptorStaxWriter().write(writer, content); | ||||||
Pankraz76 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| } else if (outputStream != null) { | ||||||
| new PluginDescriptorStaxWriter().write(outputStream, content); | ||||||
| if (request.getWriter() != null) { | ||||||
| writer.write(request.getWriter(), request.getContent()); | ||||||
| } else if (request.getOutputStream() != null) { | ||||||
| writer.write(request.getOutputStream(), request.getContent()); | ||||||
| } else { | ||||||
| try (OutputStream os = Files.newOutputStream(path)) { | ||||||
Pankraz76 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| new PluginDescriptorStaxWriter().write(os, content); | ||||||
| try (OutputStream os = Files.newOutputStream(request.getPath())) { | ||||||
| writer.write(os, request.getContent()); | ||||||
| } | ||||||
| } | ||||||
| } catch (Exception e) { | ||||||
| } catch (XmlWriterException | XMLStreamException | IOException e) { | ||||||
| throw new XmlWriterException("Unable to write plugin: " + getMessage(e), getLocation(e), e); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||

Uh oh!
There was an error while loading. Please reload this page.