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