View Javadoc
1   /*
2    * Copyright 2022 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.bremersee.xml;
18  
19  import static java.util.Objects.isNull;
20  
21  import lombok.AccessLevel;
22  import lombok.EqualsAndHashCode;
23  import lombok.Getter;
24  import lombok.RequiredArgsConstructor;
25  import lombok.ToString;
26  
27  /**
28   * The jaxb context member.
29   *
30   * @author Christian Bremer
31   */
32  public interface JaxbContextMember {
33  
34    /**
35     * By class builder.
36     *
37     * @param clazz the clazz
38     * @return the clazz builder
39     */
40    static ClazzBuilder byClass(Class<?> clazz) {
41      return new ClazzBuilderImpl(clazz);
42    }
43  
44    /**
45     * By package builder.
46     *
47     * @param pakkage the pakkage
48     * @return the pakkage builder
49     */
50    static PakkageBuilder byPackage(Package pakkage) {
51      return new PakkageBuilderImpl(pakkage);
52    }
53  
54    /**
55     * Gets clazz.
56     *
57     * @return the clazz
58     */
59    Class<?> getClazz();
60  
61    /**
62     * Gets clazz element schema location.
63     *
64     * @return the clazz element schema location
65     */
66    default String getClazzElementSchemaLocation() {
67      return null;
68    }
69  
70    /**
71     * Gets clazz type schema location.
72     *
73     * @return the clazz type schema location
74     */
75    default String getClazzTypeSchemaLocation() {
76      return null;
77    }
78  
79    /**
80     * Gets pakkage.
81     *
82     * @return the pakkage
83     */
84    default Package getPakkage() {
85      return null;
86    }
87  
88    /**
89     * Gets pakkage schema location.
90     *
91     * @return the pakkage schema location
92     */
93    default String getPakkageSchemaLocation() {
94      return null;
95    }
96  
97    /**
98     * The pakkage builder.
99     */
100   interface PakkageBuilder {
101 
102     /**
103      * Schema location.
104      *
105      * @param schemaLocation the schema location
106      * @return the pakkage builder
107      */
108     PakkageBuilder schemaLocation(String schemaLocation);
109 
110     /**
111      * Build jaxb context member.
112      *
113      * @return the jaxb context member
114      */
115     JaxbContextMember build();
116   }
117 
118   /**
119    * The pakkage builder implementation.
120    */
121   @SuppressWarnings("SameNameButDifferent")
122   @EqualsAndHashCode
123   @ToString
124   @RequiredArgsConstructor(access = AccessLevel.PACKAGE)
125   class PakkageBuilderImpl implements PakkageBuilder {
126 
127     private final Package pakkage;
128 
129     private String schemaLocation;
130 
131     @Override
132     public PakkageBuilder schemaLocation(String schemaLocation) {
133       this.schemaLocation = schemaLocation;
134       return this;
135     }
136 
137     @Override
138     public JaxbContextMember build() {
139       if (isNull(this.pakkage)) {
140         throw new IllegalStateException("Package of jaxb context must be present.");
141       }
142       return new JaxbContextMemberImpl(this.pakkage, this.schemaLocation);
143     }
144   }
145 
146   /**
147    * The clazz builder.
148    */
149   interface ClazzBuilder {
150 
151     /**
152      * Clazz element schema location.
153      *
154      * @param clazzElementSchemaLocation the clazz element schema location
155      * @return the clazz builder
156      */
157     ClazzBuilder clazzElementSchemaLocation(String clazzElementSchemaLocation);
158 
159     /**
160      * Clazz type schema location.
161      *
162      * @param clazzTypeSchemaLocation the clazz type schema location
163      * @return the clazz builder
164      */
165     ClazzBuilder clazzTypeSchemaLocation(String clazzTypeSchemaLocation);
166 
167     /**
168      * Build jaxb context member.
169      *
170      * @return the jaxb context member
171      */
172     JaxbContextMember build();
173   }
174 
175   /**
176    * The clazz builder implementation.
177    */
178   @SuppressWarnings("SameNameButDifferent")
179   @EqualsAndHashCode
180   @ToString
181   @RequiredArgsConstructor(access = AccessLevel.PACKAGE)
182   class ClazzBuilderImpl implements ClazzBuilder {
183 
184     private final Class<?> clazz;
185 
186     private String clazzElementSchemaLocation;
187 
188     private String clazzTypeSchemaLocation;
189 
190     @Override
191     public ClazzBuilder clazzElementSchemaLocation(String clazzElementSchemaLocation) {
192       this.clazzElementSchemaLocation = clazzElementSchemaLocation;
193       return this;
194     }
195 
196     @Override
197     public ClazzBuilder clazzTypeSchemaLocation(String clazzTypeSchemaLocation) {
198       this.clazzTypeSchemaLocation = clazzTypeSchemaLocation;
199       return this;
200     }
201 
202     @Override
203     public JaxbContextMember build() {
204       if (isNull(this.clazz)) {
205         throw new IllegalStateException("Class of jaxb context must be present.");
206       }
207       return new JaxbContextMemberImpl(
208           this.clazz,
209           this.clazzElementSchemaLocation,
210           this.clazzTypeSchemaLocation);
211     }
212   }
213 
214   /**
215    * The jaxb context member implementation.
216    */
217   @SuppressWarnings("SameNameButDifferent")
218   @Getter
219   @EqualsAndHashCode
220   @ToString
221   class JaxbContextMemberImpl implements JaxbContextMember {
222 
223     private final Class<?> clazz;
224 
225     private final String clazzElementSchemaLocation;
226 
227     private final String clazzTypeSchemaLocation;
228 
229     private final Package pakkage;
230 
231     private final String pakkageSchemaLocation;
232 
233     /**
234      * Instantiates a new jaxb context member.
235      *
236      * @param clazz the clazz
237      * @param clazzElementSchemaLocation the clazz element schema location
238      * @param clazzTypeSchemaLocation the clazz type schema location
239      * @param pakkage the pakkage
240      * @param pakkageSchemaLocation the pakkage schema location
241      */
242     JaxbContextMemberImpl(
243         Class<?> clazz,
244         String clazzElementSchemaLocation,
245         String clazzTypeSchemaLocation,
246         Package pakkage,
247         String pakkageSchemaLocation) {
248       this.clazz = clazz;
249       this.clazzElementSchemaLocation = clazzElementSchemaLocation;
250       this.clazzTypeSchemaLocation = clazzTypeSchemaLocation;
251       this.pakkage = pakkage;
252       this.pakkageSchemaLocation = pakkageSchemaLocation;
253     }
254 
255     /**
256      * Instantiates a new jaxb context member.
257      *
258      * @param clazz the clazz
259      * @param clazzElementSchemaLocation the clazz element schema location
260      * @param clazzTypeSchemaLocation the clazz type schema location
261      */
262     JaxbContextMemberImpl(
263         Class<?> clazz,
264         String clazzElementSchemaLocation,
265         String clazzTypeSchemaLocation) {
266       this(clazz, clazzElementSchemaLocation, clazzTypeSchemaLocation, null, null);
267     }
268 
269     /**
270      * Instantiates a new jaxb context member.
271      *
272      * @param pakkage the pakkage
273      * @param schemaLocation the schema location
274      */
275     JaxbContextMemberImpl(Package pakkage, String schemaLocation) {
276       this(null, null, null, pakkage, schemaLocation);
277     }
278   }
279 
280 }