View Javadoc
1   package org.apache.maven.api.services.xml;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.InputStream;
23  import java.io.Reader;
24  import java.net.URL;
25  import java.nio.file.Path;
26  
27  import org.apache.maven.api.annotations.Experimental;
28  import org.apache.maven.api.annotations.Immutable;
29  import org.apache.maven.api.annotations.Nonnull;
30  import org.apache.maven.api.annotations.NotThreadSafe;
31  
32  /**
33   * An XML reader request.
34   *
35   * @since 4.0
36   */
37  @Experimental
38  @Immutable
39  public interface XmlReaderRequest
40  {
41  
42      Path getPath();
43  
44      URL getURL();
45  
46      InputStream getInputStream();
47  
48      Reader getReader();
49  
50      Transformer getTransformer();
51  
52      boolean isStrict();
53  
54      String getModelId();
55  
56      String getLocation();
57  
58      boolean isAddDefaultEntities();
59  
60      interface Transformer
61      {
62          /**
63           * Interpolate the value read from the xml document
64           *
65           * @param source    The source value
66           * @param fieldName A description of the field being interpolated. The implementation may use this to
67           *                  log stuff.
68           * @return The interpolated value.
69           */
70          String transform( String source, String fieldName );
71      }
72  
73      @Nonnull
74      static XmlReaderRequestBuilder builder()
75      {
76          return new XmlReaderRequestBuilder();
77      }
78  
79      @NotThreadSafe
80      class XmlReaderRequestBuilder
81      {
82          Path path;
83          URL url;
84          InputStream inputStream;
85          Reader reader;
86          Transformer transformer;
87          boolean strict;
88          String modelId;
89          String location;
90          boolean addDefaultEntities = true;
91  
92          public XmlReaderRequestBuilder path( Path path )
93          {
94              this.path = path;
95              return this;
96          }
97  
98          public XmlReaderRequestBuilder url( URL url )
99          {
100             this.url = url;
101             return this;
102         }
103 
104         public XmlReaderRequestBuilder inputStream( InputStream inputStream )
105         {
106             this.inputStream = inputStream;
107             return this;
108         }
109 
110         public XmlReaderRequestBuilder reader( Reader reader )
111         {
112             this.reader = reader;
113             return this;
114         }
115 
116         public XmlReaderRequestBuilder transformer( Transformer transformer )
117         {
118             this.transformer = transformer;
119             return this;
120         }
121 
122         public XmlReaderRequestBuilder strict( boolean strict )
123         {
124             this.strict = strict;
125             return this;
126         }
127 
128         public XmlReaderRequestBuilder modelId( String modelId )
129         {
130             this.modelId = modelId;
131             return this;
132         }
133 
134         public XmlReaderRequestBuilder location( String location )
135         {
136             this.location = location;
137             return this;
138         }
139 
140         public XmlReaderRequestBuilder addDefaultEntities( boolean addDefaultEntities )
141         {
142             this.addDefaultEntities = addDefaultEntities;
143             return this;
144         }
145 
146         public XmlReaderRequest build()
147         {
148             return new DefaultXmlReaderRequest( path, url, inputStream, reader, transformer, strict,
149                     modelId, location, addDefaultEntities );
150         }
151 
152         private static class DefaultXmlReaderRequest implements XmlReaderRequest
153         {
154             final Path path;
155             final URL url;
156             final InputStream inputStream;
157             final Reader reader;
158             final Transformer transformer;
159             final boolean strict;
160             final String modelId;
161             final String location;
162             final boolean addDefaultEntities;
163 
164             @SuppressWarnings( "checkstyle:ParameterNumber" )
165             DefaultXmlReaderRequest( Path path, URL url, InputStream inputStream, Reader reader,
166                                      Transformer transformer, boolean strict,
167                                      String modelId, String location,
168                                      boolean addDefaultEntities )
169             {
170                 this.path = path;
171                 this.url = url;
172                 this.inputStream = inputStream;
173                 this.reader = reader;
174                 this.transformer = transformer;
175                 this.strict = strict;
176                 this.modelId = modelId;
177                 this.location = location;
178                 this.addDefaultEntities = addDefaultEntities;
179             }
180 
181             @Override
182             public Path getPath()
183             {
184                 return path;
185             }
186 
187             @Override
188             public URL getURL()
189             {
190                 return null;
191             }
192 
193             @Override
194             public InputStream getInputStream()
195             {
196                 return inputStream;
197             }
198 
199             public Reader getReader()
200             {
201                 return reader;
202             }
203 
204             @Override
205             public Transformer getTransformer()
206             {
207                 return transformer;
208             }
209 
210             @Override
211             public boolean isStrict()
212             {
213                 return strict;
214             }
215 
216             @Override
217             public String getModelId()
218             {
219                 return modelId;
220             }
221 
222             @Override
223             public String getLocation()
224             {
225                 return location;
226             }
227 
228             @Override
229             public boolean isAddDefaultEntities()
230             {
231                 return addDefaultEntities;
232             }
233         }
234     }
235 
236 }