View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.api.services.xml;
20  
21  import java.io.OutputStream;
22  import java.io.Writer;
23  import java.nio.file.Path;
24  import java.util.function.Function;
25  
26  import org.apache.maven.api.annotations.Experimental;
27  import org.apache.maven.api.annotations.Nonnull;
28  import org.apache.maven.api.annotations.Nullable;
29  
30  /**
31   * An XML writer request.
32   *
33   * @since 4.0.0
34   * @param <T> the object type to read
35   */
36  @Experimental
37  public interface XmlWriterRequest<T> {
38  
39      @Nullable
40      Path getPath();
41  
42      @Nullable
43      OutputStream getOutputStream();
44  
45      @Nullable
46      Writer getWriter();
47  
48      @Nonnull
49      T getContent();
50  
51      @Nullable
52      Function<Object, String> getInputLocationFormatter();
53  
54      static <T> XmlWriterRequestBuilder<T> builder() {
55          return new XmlWriterRequestBuilder<>();
56      }
57  
58      class XmlWriterRequestBuilder<T> {
59          Path path;
60          OutputStream outputStream;
61          Writer writer;
62          T content;
63          Function<Object, String> inputLocationFormatter;
64  
65          public XmlWriterRequestBuilder<T> path(Path path) {
66              this.path = path;
67              return this;
68          }
69  
70          public XmlWriterRequestBuilder<T> outputStream(OutputStream outputStream) {
71              this.outputStream = outputStream;
72              return this;
73          }
74  
75          public XmlWriterRequestBuilder<T> writer(Writer writer) {
76              this.writer = writer;
77              return this;
78          }
79  
80          public XmlWriterRequestBuilder<T> content(T content) {
81              this.content = content;
82              return this;
83          }
84  
85          public XmlWriterRequestBuilder<T> inputLocationFormatter(Function<Object, String> inputLocationFormatter) {
86              this.inputLocationFormatter = inputLocationFormatter;
87              return this;
88          }
89  
90          public XmlWriterRequest<T> build() {
91              return new DefaultXmlWriterRequest<>(path, outputStream, writer, content, inputLocationFormatter);
92          }
93  
94          private static class DefaultXmlWriterRequest<T> implements XmlWriterRequest<T> {
95              final Path path;
96              final OutputStream outputStream;
97              final Writer writer;
98              final T content;
99              final Function<Object, String> inputLocationFormatter;
100 
101             DefaultXmlWriterRequest(
102                     Path path,
103                     OutputStream outputStream,
104                     Writer writer,
105                     T content,
106                     Function<Object, String> inputLocationFormatter) {
107                 this.path = path;
108                 this.outputStream = outputStream;
109                 this.writer = writer;
110                 this.content = content;
111                 this.inputLocationFormatter = inputLocationFormatter;
112             }
113 
114             @Override
115             public Path getPath() {
116                 return path;
117             }
118 
119             @Override
120             public OutputStream getOutputStream() {
121                 return outputStream;
122             }
123 
124             @Override
125             public Writer getWriter() {
126                 return writer;
127             }
128 
129             @Override
130             public T getContent() {
131                 return content;
132             }
133 
134             @Override
135             public Function<Object, String> getInputLocationFormatter() {
136                 return inputLocationFormatter;
137             }
138         }
139     }
140 }