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.InputStream;
22  import java.io.Reader;
23  import java.net.URL;
24  import java.nio.file.Path;
25  
26  import org.apache.maven.api.annotations.Experimental;
27  import org.apache.maven.api.annotations.Immutable;
28  import org.apache.maven.api.annotations.Nonnull;
29  import org.apache.maven.api.annotations.NotThreadSafe;
30  import org.apache.maven.api.annotations.Nullable;
31  
32  /**
33   * An XML reader request.
34   *
35   * @since 4.0.0
36   */
37  @Experimental
38  @Immutable
39  public interface XmlReaderRequest {
40  
41      @Nullable
42      Path getPath();
43  
44      @Nullable
45      Path getRootDirectory();
46  
47      @Nullable
48      URL getURL();
49  
50      @Nullable
51      InputStream getInputStream();
52  
53      @Nullable
54      Reader getReader();
55  
56      @Nullable
57      Transformer getTransformer();
58  
59      boolean isStrict();
60  
61      @Nullable
62      String getModelId();
63  
64      @Nullable
65      String getLocation();
66  
67      boolean isAddDefaultEntities();
68  
69      interface Transformer {
70          /**
71           * Interpolate the value read from the xml document
72           *
73           * @param source    The source value
74           * @param fieldName A description of the field being interpolated. The implementation may use this to
75           *                  log stuff.
76           * @return the interpolated value
77           */
78          String transform(String source, String fieldName);
79      }
80  
81      @Nonnull
82      static XmlReaderRequestBuilder builder() {
83          return new XmlReaderRequestBuilder();
84      }
85  
86      @NotThreadSafe
87      class XmlReaderRequestBuilder {
88          Path path;
89          Path rootDirectory;
90          URL url;
91          InputStream inputStream;
92          Reader reader;
93          Transformer transformer;
94          boolean strict;
95          String modelId;
96          String location;
97          boolean addDefaultEntities = true;
98  
99          public XmlReaderRequestBuilder path(Path path) {
100             this.path = path;
101             return this;
102         }
103 
104         public XmlReaderRequestBuilder rootDirectory(Path rootDirectory) {
105             this.rootDirectory = rootDirectory;
106             return this;
107         }
108 
109         public XmlReaderRequestBuilder url(URL url) {
110             this.url = url;
111             return this;
112         }
113 
114         public XmlReaderRequestBuilder inputStream(InputStream inputStream) {
115             this.inputStream = inputStream;
116             return this;
117         }
118 
119         public XmlReaderRequestBuilder reader(Reader reader) {
120             this.reader = reader;
121             return this;
122         }
123 
124         public XmlReaderRequestBuilder transformer(Transformer transformer) {
125             this.transformer = transformer;
126             return this;
127         }
128 
129         public XmlReaderRequestBuilder strict(boolean strict) {
130             this.strict = strict;
131             return this;
132         }
133 
134         public XmlReaderRequestBuilder modelId(String modelId) {
135             this.modelId = modelId;
136             return this;
137         }
138 
139         public XmlReaderRequestBuilder location(String location) {
140             this.location = location;
141             return this;
142         }
143 
144         public XmlReaderRequestBuilder addDefaultEntities(boolean addDefaultEntities) {
145             this.addDefaultEntities = addDefaultEntities;
146             return this;
147         }
148 
149         public XmlReaderRequest build() {
150             return new DefaultXmlReaderRequest(
151                     path,
152                     rootDirectory,
153                     url,
154                     inputStream,
155                     reader,
156                     transformer,
157                     strict,
158                     modelId,
159                     location,
160                     addDefaultEntities);
161         }
162 
163         private static class DefaultXmlReaderRequest implements XmlReaderRequest {
164             final Path path;
165             final Path rootDirectory;
166             final URL url;
167             final InputStream inputStream;
168             final Reader reader;
169             final Transformer transformer;
170             final boolean strict;
171             final String modelId;
172             final String location;
173             final boolean addDefaultEntities;
174 
175             @SuppressWarnings("checkstyle:ParameterNumber")
176             DefaultXmlReaderRequest(
177                     Path path,
178                     Path rootDirectory,
179                     URL url,
180                     InputStream inputStream,
181                     Reader reader,
182                     Transformer transformer,
183                     boolean strict,
184                     String modelId,
185                     String location,
186                     boolean addDefaultEntities) {
187                 this.path = path;
188                 this.rootDirectory = rootDirectory;
189                 this.url = url;
190                 this.inputStream = inputStream;
191                 this.reader = reader;
192                 this.transformer = transformer;
193                 this.strict = strict;
194                 this.modelId = modelId;
195                 this.location = location;
196                 this.addDefaultEntities = addDefaultEntities;
197             }
198 
199             @Override
200             public Path getPath() {
201                 return path;
202             }
203 
204             @Override
205             public Path getRootDirectory() {
206                 return rootDirectory;
207             }
208 
209             @Override
210             public URL getURL() {
211                 return null;
212             }
213 
214             @Override
215             public InputStream getInputStream() {
216                 return inputStream;
217             }
218 
219             public Reader getReader() {
220                 return reader;
221             }
222 
223             @Override
224             public Transformer getTransformer() {
225                 return transformer;
226             }
227 
228             @Override
229             public boolean isStrict() {
230                 return strict;
231             }
232 
233             @Override
234             public String getModelId() {
235                 return modelId;
236             }
237 
238             @Override
239             public String getLocation() {
240                 return location;
241             }
242 
243             @Override
244             public boolean isAddDefaultEntities() {
245                 return addDefaultEntities;
246             }
247         }
248     }
249 }