Skip to content

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ interface Transformer {
String transform(String source, String fieldName);
}

default void throwIfIncomplete() {
if (getInputStream() == null && getReader() == null && getPath() == null && getURL() == null) {
throw new IllegalArgumentException("writer, outputStream, or path must be non null");
}
}

@Nonnull
static XmlReaderRequestBuilder builder() {
return new XmlReaderRequestBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ static <T> XmlWriterRequestBuilder<T> builder() {
return new XmlWriterRequestBuilder<>();
}

default void throwIfIncomplete() {
if (getWriter() == null && getOutputStream() == null && getPath() == null) {
throw new IllegalArgumentException("writer, outputStream, or path must be non null");
}
}

class XmlWriterRequestBuilder<T> {
Path path;
OutputStream outputStream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ private Model doRead(XmlReaderRequest request) throws XmlReaderException {
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");
}
request.throwIfIncomplete();
try {
InputSource source = null;
if (request.getModelId() != null || request.getLocation() != null) {
Expand Down Expand Up @@ -120,9 +118,7 @@ public void write(XmlWriterRequest<Model> request) throws XmlWriterException {
OutputStream outputStream = request.getOutputStream();
Writer writer = request.getWriter();
Function<Object, String> inputLocationFormatter = request.getInputLocationFormatter();
if (writer == null && outputStream == null && path == null) {
throw new IllegalArgumentException("writer, outputStream or path must be non null");
}
request.throwIfIncomplete();
try {
MavenStaxWriter w = new MavenStaxWriter();
if (inputLocationFormatter != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

@Override
public PluginDescriptor read(@Nonnull XmlReaderRequest request) throws XmlReaderException {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public PluginDescriptor read(@Nonnull XmlReaderRequest request) throws XmlReaderException {
public PluginDescriptor request(@Nonnull XmlReaderRequest request) throws XmlReaderException {

might have generic request name propagate overload, as type seems enough to determine.

nonNull(request, "request");
Path path = request.getPath();
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");
}
return read(request, setAddDefaultEntities(request, new PluginDescriptorStaxReader()));
}

@Override
public void write(XmlWriterRequest<PluginDescriptor> request) throws XmlWriterException {
Copy link
Contributor Author

@Pankraz76 Pankraz76 May 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having a factory not creating anything, seems kind of goofy:

image

This seems more service, then factory layer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public void write(XmlWriterRequest<PluginDescriptor> request) throws XmlWriterException {
public void request(XmlWriterRequest<PluginDescriptor> request) throws XmlWriterException {

might have generic request name propagate overload, as type seems enough to determine.

write(request, new PluginDescriptorStaxWriter());
}

private static PluginDescriptor read(XmlReaderRequest request, PluginDescriptorStaxReader reader) {
request.throwIfIncomplete();
try {
PluginDescriptorStaxReader xml = new PluginDescriptorStaxReader();
xml.setAddDefaultEntities(request.isAddDefaultEntities());
if (inputStream != null) {
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);
}
}

@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");
}
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);
} 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)) {
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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

class DefaultPluginXmlFactoryReadWriteTest {
Expand Down Expand Up @@ -184,7 +185,6 @@ void staticToXmlGeneratesValidXml() {
.version("1.0.0")
.build());
assertThat(xml).contains("<name>" + NAME + "</name>");
assertThat(xml).contains("<name>" + NAME + "</name>");
assertThat(xml).contains("<groupId>org.example</groupId>");
assertThat(xml).contains("<artifactId>sample-plugin</artifactId>");
assertThat(xml).contains("<version>1.0.0</version>");
Expand Down Expand Up @@ -229,7 +229,7 @@ void writeWithNoTargetThrowsIllegalArgumentException() {
.build())
.build()))
.getMessage())
.isEqualTo("writer, outputStream or path must be non null");
.isEqualTo("writer, outputStream, or path must be non null");
}

@Test
Expand Down Expand Up @@ -268,7 +268,7 @@ void readFromUrlParsesPluginDescriptorCorrectly() throws Exception {
void testReadWithPath() throws Exception {
Path tempPath = Files.createTempFile("plugin", ".xml");
Files.writeString(tempPath, "<plugin/>");
XmlReaderRequest request = mock(XmlReaderRequest.class);
XmlReaderRequest request = spy(XmlReaderRequest.class);
when(request.getPath()).thenReturn(tempPath);
when(request.getInputStream()).thenReturn(null);
when(request.getReader()).thenReturn(null);
Expand All @@ -283,7 +283,7 @@ void testReadWithPath() throws Exception {
void testReadWithPathUrlDefault() throws Exception {
Path tempPath = Files.createTempFile("plugin", ".xml");
Files.writeString(tempPath, "<plugin/>");
XmlReaderRequest request = mock(XmlReaderRequest.class);
XmlReaderRequest request = spy(XmlReaderRequest.class);
when(request.getPath()).thenReturn(null);
when(request.getInputStream()).thenReturn(null);
when(request.getReader()).thenReturn(null);
Expand Down