1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.api.services.xml;
20
21 import java.io.InputStream;
22 import java.io.OutputStream;
23 import java.io.Reader;
24 import java.io.StringReader;
25 import java.io.StringWriter;
26 import java.io.Writer;
27 import java.nio.file.Path;
28 import org.apache.maven.api.Service;
29 import org.apache.maven.api.annotations.Experimental;
30 import org.apache.maven.api.annotations.Nonnull;
31
32
33
34
35
36
37
38 @Experimental
39 public interface XmlFactory<T> extends Service {
40
41 @Nonnull
42 default T read(@Nonnull Path path) throws XmlReaderException {
43 return read(path, true);
44 }
45
46 @Nonnull
47 default T read(@Nonnull Path path, boolean strict) throws XmlReaderException {
48 return read(XmlReaderRequest.builder().path(path).strict(strict).build());
49 }
50
51 @Nonnull
52 default T read(@Nonnull InputStream input) throws XmlReaderException {
53 return read(input, true);
54 }
55
56 @Nonnull
57 default T read(@Nonnull InputStream input, boolean strict) throws XmlReaderException {
58 return read(XmlReaderRequest.builder().inputStream(input).strict(strict).build());
59 }
60
61 @Nonnull
62 default T read(@Nonnull Reader reader) throws XmlReaderException {
63 return read(reader, true);
64 }
65
66 @Nonnull
67 default T read(@Nonnull Reader reader, boolean strict) throws XmlReaderException {
68 return read(XmlReaderRequest.builder().reader(reader).strict(strict).build());
69 }
70
71 @Nonnull
72 T read(@Nonnull XmlReaderRequest request) throws XmlReaderException;
73
74 default void write(@Nonnull T content, @Nonnull Path path) throws XmlWriterException {
75 write(XmlWriterRequest.<T>builder().content(content).path(path).build());
76 }
77
78 default void write(@Nonnull T content, @Nonnull OutputStream outputStream) throws XmlWriterException {
79 write(XmlWriterRequest.<T>builder()
80 .content(content)
81 .outputStream(outputStream)
82 .build());
83 }
84
85 default void write(@Nonnull T content, @Nonnull Writer writer) throws XmlWriterException {
86 write(XmlWriterRequest.<T>builder().content(content).writer(writer).build());
87 }
88
89 void write(@Nonnull XmlWriterRequest<T> request) throws XmlWriterException;
90
91
92
93
94
95
96
97
98
99 default T fromXmlString(@Nonnull String xml) throws XmlReaderException {
100 return read(new StringReader(xml));
101 }
102
103
104
105
106
107
108
109
110
111 default String toXmlString(@Nonnull T content) throws XmlWriterException {
112 StringWriter sw = new StringWriter();
113 write(content, sw);
114 return sw.toString();
115 }
116 }