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
29 import org.apache.maven.api.Service;
30 import org.apache.maven.api.annotations.Experimental;
31 import org.apache.maven.api.annotations.Nonnull;
32
33
34
35
36
37
38
39 @Experimental
40 public interface XmlFactory<T> extends Service {
41
42 @Nonnull
43 default T read(@Nonnull Path path) throws XmlReaderException {
44 return read(path, true);
45 }
46
47 @Nonnull
48 default T read(@Nonnull Path path, boolean strict) throws XmlReaderException {
49 return read(XmlReaderRequest.builder().path(path).strict(strict).build());
50 }
51
52 @Nonnull
53 default T read(@Nonnull InputStream input) throws XmlReaderException {
54 return read(input, true);
55 }
56
57 @Nonnull
58 default T read(@Nonnull InputStream input, boolean strict) throws XmlReaderException {
59 return read(XmlReaderRequest.builder().inputStream(input).strict(strict).build());
60 }
61
62 @Nonnull
63 default T read(@Nonnull Reader reader) throws XmlReaderException {
64 return read(reader, true);
65 }
66
67 @Nonnull
68 default T read(@Nonnull Reader reader, boolean strict) throws XmlReaderException {
69 return read(XmlReaderRequest.builder().reader(reader).strict(strict).build());
70 }
71
72 @Nonnull
73 T read(@Nonnull XmlReaderRequest request) throws XmlReaderException;
74
75 default void write(@Nonnull T content, @Nonnull Path path) throws XmlWriterException {
76 write(XmlWriterRequest.<T>builder().content(content).path(path).build());
77 }
78
79 default void write(@Nonnull T content, @Nonnull OutputStream outputStream) throws XmlWriterException {
80 write(XmlWriterRequest.<T>builder()
81 .content(content)
82 .outputStream(outputStream)
83 .build());
84 }
85
86 default void write(@Nonnull T content, @Nonnull Writer writer) throws XmlWriterException {
87 write(XmlWriterRequest.<T>builder().content(content).writer(writer).build());
88 }
89
90 void write(@Nonnull XmlWriterRequest<T> request) throws XmlWriterException;
91
92
93
94
95
96
97
98
99
100 @Nonnull
101 default T fromXmlString(@Nonnull String xml) throws XmlReaderException {
102 return read(new StringReader(xml));
103 }
104
105
106
107
108
109
110
111
112
113 @Nonnull
114 default String toXmlString(@Nonnull T content) throws XmlWriterException {
115 StringWriter sw = new StringWriter();
116 write(content, sw);
117 return sw.toString();
118 }
119 }