1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.internal.impl;
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 import java.util.function.Function;
29
30 import org.apache.maven.api.annotations.Nonnull;
31 import org.apache.maven.api.di.Named;
32 import org.apache.maven.api.di.Singleton;
33 import org.apache.maven.api.model.InputSource;
34 import org.apache.maven.api.model.Model;
35 import org.apache.maven.api.services.xml.ModelXmlFactory;
36 import org.apache.maven.api.services.xml.XmlReaderException;
37 import org.apache.maven.api.services.xml.XmlReaderRequest;
38 import org.apache.maven.api.services.xml.XmlWriterException;
39 import org.apache.maven.api.services.xml.XmlWriterRequest;
40 import org.apache.maven.model.v4.MavenStaxReader;
41 import org.apache.maven.model.v4.MavenStaxWriter;
42
43 import static org.apache.maven.internal.impl.StaxLocation.getLocation;
44 import static org.apache.maven.internal.impl.StaxLocation.getMessage;
45 import static org.apache.maven.internal.impl.Utils.nonNull;
46
47 @Named
48 @Singleton
49 public class DefaultModelXmlFactory implements ModelXmlFactory {
50 @Override
51 public Model read(@Nonnull XmlReaderRequest request) throws XmlReaderException {
52 nonNull(request, "request");
53 Path path = request.getPath();
54 URL url = request.getURL();
55 Reader reader = request.getReader();
56 InputStream inputStream = request.getInputStream();
57 if (path == null && url == null && reader == null && inputStream == null) {
58 throw new IllegalArgumentException("path, url, reader or inputStream must be non null");
59 }
60 try {
61 InputSource source = null;
62 if (request.getModelId() != null || request.getLocation() != null) {
63 source = new InputSource(
64 request.getModelId(), path != null ? path.toUri().toString() : null);
65 }
66 MavenStaxReader xml = new MavenStaxReader();
67 xml.setAddDefaultEntities(request.isAddDefaultEntities());
68 if (inputStream != null) {
69 return xml.read(inputStream, request.isStrict(), source);
70 } else if (reader != null) {
71 return xml.read(reader, request.isStrict(), source);
72 } else if (path != null) {
73 try (InputStream is = Files.newInputStream(path)) {
74 return xml.read(is, request.isStrict(), source);
75 }
76 } else {
77 try (InputStream is = url.openStream()) {
78 return xml.read(is, request.isStrict(), source);
79 }
80 }
81 } catch (Exception e) {
82 throw new XmlReaderException("Unable to read model: " + getMessage(e), getLocation(e), e);
83 }
84 }
85
86 @Override
87 public void write(XmlWriterRequest<Model> request) throws XmlWriterException {
88 nonNull(request, "request");
89 Model content = nonNull(request.getContent(), "content");
90 Path path = request.getPath();
91 OutputStream outputStream = request.getOutputStream();
92 Writer writer = request.getWriter();
93 Function<Object, String> inputLocationFormatter = request.getInputLocationFormatter();
94 if (writer == null && outputStream == null && path == null) {
95 throw new IllegalArgumentException("writer, outputStream or path must be non null");
96 }
97 try {
98 MavenStaxWriter w = new MavenStaxWriter();
99 if (inputLocationFormatter != null) {
100 w.setStringFormatter((Function) inputLocationFormatter);
101 }
102 if (writer != null) {
103 w.write(writer, content);
104 } else if (outputStream != null) {
105 w.write(outputStream, content);
106 } else {
107 try (OutputStream os = Files.newOutputStream(path)) {
108 w.write(outputStream, content);
109 }
110 }
111 } catch (Exception e) {
112 throw new XmlWriterException("Unable to write model: " + getMessage(e), getLocation(e), e);
113 }
114 }
115
116
117
118
119
120
121
122
123
124 public static Model fromXml(@Nonnull String xml) throws XmlReaderException {
125 return new DefaultModelXmlFactory().fromXmlString(xml);
126 }
127
128
129
130
131
132
133
134
135
136 public static String toXml(@Nonnull Model content) throws XmlWriterException {
137 return new DefaultModelXmlFactory().toXmlString(content);
138 }
139 }