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