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.util.Objects;
26
27 import org.apache.maven.api.annotations.Nonnull;
28 import org.apache.maven.api.di.Named;
29 import org.apache.maven.api.di.Singleton;
30 import org.apache.maven.api.services.xml.ToolchainsXmlFactory;
31 import org.apache.maven.api.services.xml.XmlReaderException;
32 import org.apache.maven.api.services.xml.XmlReaderRequest;
33 import org.apache.maven.api.services.xml.XmlWriterException;
34 import org.apache.maven.api.services.xml.XmlWriterRequest;
35 import org.apache.maven.api.toolchain.InputSource;
36 import org.apache.maven.api.toolchain.PersistedToolchains;
37 import org.apache.maven.toolchain.v4.MavenToolchainsStaxReader;
38 import org.apache.maven.toolchain.v4.MavenToolchainsStaxWriter;
39
40 import static org.apache.maven.internal.impl.StaxLocation.getLocation;
41 import static org.apache.maven.internal.impl.StaxLocation.getMessage;
42 import static org.apache.maven.internal.impl.Utils.nonNull;
43
44 @Named
45 @Singleton
46 public class DefaultToolchainsXmlFactory implements ToolchainsXmlFactory {
47 @Override
48 public PersistedToolchains read(@Nonnull XmlReaderRequest request) throws XmlReaderException {
49 Objects.requireNonNull(request, "request");
50 Reader reader = request.getReader();
51 InputStream inputStream = request.getInputStream();
52 if (reader == null && inputStream == null) {
53 throw new IllegalArgumentException("reader or inputStream must be non null");
54 }
55 try {
56 InputSource source = null;
57 if (request.getModelId() != null || request.getLocation() != null) {
58 source = new InputSource(request.getLocation());
59 }
60 MavenToolchainsStaxReader xml = new MavenToolchainsStaxReader();
61 xml.setAddDefaultEntities(request.isAddDefaultEntities());
62 if (reader != null) {
63 return xml.read(reader, request.isStrict(), source);
64 } else {
65 return xml.read(inputStream, request.isStrict(), source);
66 }
67 } catch (Exception e) {
68 throw new XmlReaderException("Unable to read toolchains: " + getMessage(e), getLocation(e), e);
69 }
70 }
71
72 @Override
73 public void write(XmlWriterRequest<PersistedToolchains> request) throws XmlWriterException {
74 nonNull(request, "request");
75 PersistedToolchains content = Objects.requireNonNull(request.getContent(), "content");
76 OutputStream outputStream = request.getOutputStream();
77 Writer writer = request.getWriter();
78 if (writer == null && outputStream == null) {
79 throw new IllegalArgumentException("writer or outputStream must be non null");
80 }
81 try {
82 if (writer != null) {
83 new MavenToolchainsStaxWriter().write(writer, content);
84 } else {
85 new MavenToolchainsStaxWriter().write(outputStream, content);
86 }
87 } catch (Exception e) {
88 throw new XmlWriterException("Unable to write toolchains: " + getMessage(e), getLocation(e), e);
89 }
90 }
91 }