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 javax.inject.Named;
22 import javax.inject.Singleton;
23
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.io.Reader;
27 import java.io.Writer;
28 import java.net.URL;
29 import java.nio.file.Files;
30 import java.nio.file.Path;
31
32 import org.apache.maven.api.annotations.Nonnull;
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.MavenXpp3ReaderEx;
41 import org.apache.maven.model.v4.MavenXpp3WriterEx;
42 import org.codehaus.plexus.util.ReaderFactory;
43
44 import static org.apache.maven.internal.impl.Utils.nonNull;
45
46 @Named
47 @Singleton
48 public class DefaultModelXmlFactory implements ModelXmlFactory {
49 @Override
50 public Model read(@Nonnull XmlReaderRequest request) throws XmlReaderException {
51 nonNull(request, "request can not be null");
52 Path path = request.getPath();
53 URL url = request.getURL();
54 Reader reader = request.getReader();
55 InputStream inputStream = request.getInputStream();
56 if (path == null && url == null && reader == null && inputStream == null) {
57 throw new IllegalArgumentException("path, url, reader or inputStream must be non null");
58 }
59 try {
60 InputSource source = null;
61 if (request.getModelId() != null || request.getLocation() != null) {
62 source = new InputSource(request.getModelId(), request.getLocation());
63 }
64 MavenXpp3ReaderEx xml = new MavenXpp3ReaderEx();
65 xml.setAddDefaultEntities(request.isAddDefaultEntities());
66 if (path != null) {
67 reader = ReaderFactory.newXmlReader(path.toFile());
68 } else if (url != null) {
69 reader = ReaderFactory.newXmlReader(url);
70 } else if (inputStream != null) {
71 reader = ReaderFactory.newXmlReader(inputStream);
72 }
73 return xml.read(reader, request.isStrict(), source);
74 } catch (Exception e) {
75 throw new XmlReaderException("Unable to read model", e);
76 }
77 }
78
79 @Override
80 public void write(XmlWriterRequest<Model> request) throws XmlWriterException {
81 nonNull(request, "request can not be null");
82 Model content = nonNull(request.getContent(), "content can not be null");
83 Path path = request.getPath();
84 OutputStream outputStream = request.getOutputStream();
85 Writer writer = request.getWriter();
86 if (writer == null && outputStream == null && path == null) {
87 throw new IllegalArgumentException("writer, outputStream or path must be non null");
88 }
89 try {
90 if (writer != null) {
91 new MavenXpp3WriterEx().write(writer, content);
92 } else if (outputStream != null) {
93 new MavenXpp3WriterEx().write(outputStream, content);
94 } else {
95 try (OutputStream os = Files.newOutputStream(path)) {
96 new MavenXpp3WriterEx().write(outputStream, content);
97 }
98 }
99 } catch (Exception e) {
100 throw new XmlWriterException("Unable to write model", e);
101 }
102 }
103
104
105
106
107
108
109
110
111
112 public static Model fromXml(@Nonnull String xml) throws XmlReaderException {
113 return new DefaultModelXmlFactory().fromXmlString(xml);
114 }
115
116
117
118
119
120
121
122
123
124 public static String toXml(@Nonnull Model content) throws XmlWriterException {
125 return new DefaultModelXmlFactory().toXmlString(content);
126 }
127 }