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