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      URL getURL();
46  
47      @Nullable
48      InputStream getInputStream();
49  
50      @Nullable
51      Reader getReader();
52  
53      @Nullable
54      Transformer getTransformer();
55  
56      boolean isStrict();
57  
58      @Nullable
59      String getModelId();
60  
61      @Nullable
62      String getLocation();
63  
64      boolean isAddDefaultEntities();
65  
66      interface Transformer {
67          /**
68           * Interpolate the value read from the xml document
69           *
70           * @param source    The source value
71           * @param fieldName A description of the field being interpolated. The implementation may use this to
72           *                  log stuff.
73           * @return the interpolated value
74           */
75          String transform(String source, String fieldName);
76      }
77  
78      @Nonnull
79      static XmlReaderRequestBuilder builder() {
80          return new XmlReaderRequestBuilder();
81      }
82  
83      @NotThreadSafe
84      class XmlReaderRequestBuilder {
85          Path path;
86          URL url;
87          InputStream inputStream;
88          Reader reader;
89          Transformer transformer;
90          boolean strict;
91          String modelId;
92          String location;
93          boolean addDefaultEntities = true;
94  
95          public XmlReaderRequestBuilder path(Path path) {
96              this.path = path;
97              return this;
98          }
99  
100         public XmlReaderRequestBuilder url(URL url) {
101             this.url = url;
102             return this;
103         }
104 
105         public XmlReaderRequestBuilder inputStream(InputStream inputStream) {
106             this.inputStream = inputStream;
107             return this;
108         }
109 
110         public XmlReaderRequestBuilder reader(Reader reader) {
111             this.reader = reader;
112             return this;
113         }
114 
115         public XmlReaderRequestBuilder transformer(Transformer transformer) {
116             this.transformer = transformer;
117             return this;
118         }
119 
120         public XmlReaderRequestBuilder strict(boolean strict) {
121             this.strict = strict;
122             return this;
123         }
124 
125         public XmlReaderRequestBuilder modelId(String modelId) {
126             this.modelId = modelId;
127             return this;
128         }
129 
130         public XmlReaderRequestBuilder location(String location) {
131             this.location = location;
132             return this;
133         }
134 
135         public XmlReaderRequestBuilder addDefaultEntities(boolean addDefaultEntities) {
136             this.addDefaultEntities = addDefaultEntities;
137             return this;
138         }
139 
140         public XmlReaderRequest build() {
141             return new DefaultXmlReaderRequest(
142                     path, url, inputStream, reader, transformer, strict, modelId, location, addDefaultEntities);
143         }
144 
145         private static class DefaultXmlReaderRequest implements XmlReaderRequest {
146             final Path path;
147             final URL url;
148             final InputStream inputStream;
149             final Reader reader;
150             final Transformer transformer;
151             final boolean strict;
152             final String modelId;
153             final String location;
154             final boolean addDefaultEntities;
155 
156             @SuppressWarnings("checkstyle:ParameterNumber")
157             DefaultXmlReaderRequest(
158                     Path path,
159                     URL url,
160                     InputStream inputStream,
161                     Reader reader,
162                     Transformer transformer,
163                     boolean strict,
164                     String modelId,
165                     String location,
166                     boolean addDefaultEntities) {
167                 this.path = path;
168                 this.url = url;
169                 this.inputStream = inputStream;
170                 this.reader = reader;
171                 this.transformer = transformer;
172                 this.strict = strict;
173                 this.modelId = modelId;
174                 this.location = location;
175                 this.addDefaultEntities = addDefaultEntities;
176             }
177 
178             @Override
179             public Path getPath() {
180                 return path;
181             }
182 
183             @Override
184             public URL getURL() {
185                 return null;
186             }
187 
188             @Override
189             public InputStream getInputStream() {
190                 return inputStream;
191             }
192 
193             public Reader getReader() {
194                 return reader;
195             }
196 
197             @Override
198             public Transformer getTransformer() {
199                 return transformer;
200             }
201 
202             @Override
203             public boolean isStrict() {
204                 return strict;
205             }
206 
207             @Override
208             public String getModelId() {
209                 return modelId;
210             }
211 
212             @Override
213             public String getLocation() {
214                 return location;
215             }
216 
217             @Override
218             public boolean isAddDefaultEntities() {
219                 return addDefaultEntities;
220             }
221         }
222     }
223 }