View Javadoc

1   /*
2    * Copyright 2010-2013 smartics, Kronseder & Reiner GmbH
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  package de.smartics.xml.encoding;
17  
18  import java.io.Serializable;
19  
20  /**
21   * Provides the information detected from the given XML document.
22   */
23  public final class XmlDescriptor implements Serializable // NOPMD
24  {
25    // ********************************* Fields *********************************
26  
27    // --- constants ------------------------------------------------------------
28  
29    /**
30     * The class version identifier.
31     */
32    private static final long serialVersionUID = 1L;
33  
34    // --- members --------------------------------------------------------------
35  
36    /**
37     * The detected XML version. May be <code>null</code>.
38     *
39     * @serial
40     */
41    private final String version;
42  
43    /**
44     * The detected encoding. May be <code>null</code>.
45     *
46     * @serial
47     */
48    private final String encoding;
49  
50    /**
51     * The detected public ID version. May be <code>null</code>.
52     *
53     * @serial
54     */
55    private final String publicId;
56  
57    /**
58     * The detected system ID version. May be <code>null</code>.
59     *
60     * @serial
61     */
62    private final String systemId;
63  
64    // ****************************** Initializer *******************************
65  
66    // ****************************** Constructors ******************************
67  
68    private XmlDescriptor(final XmlDescriptor.Builder builder)
69    {
70      this.version = builder.version;
71      this.encoding = builder.encoding;
72      this.publicId = builder.publicId;
73      this.systemId = builder.systemId;
74    }
75  
76    // ****************************** Inner Classes *****************************
77  
78    /**
79     * The builder for {@link XMLDescriptor} instances.
80     */
81    public static final class Builder
82    {
83      // ******************************** Fields ********************************
84  
85      // --- constants ----------------------------------------------------------
86  
87      // --- members ------------------------------------------------------------
88  
89      /**
90       * The detected XML version.
91       */
92      private String version;
93  
94      /**
95       * The detected encoding.
96       */
97      private String encoding;
98  
99      /**
100      * The detected public ID version.
101      */
102     private String publicId;
103 
104     /**
105      * The detected system ID version.
106      */
107     private String systemId;
108 
109     // ***************************** Initializer ******************************
110 
111     // ***************************** Constructors *****************************
112 
113     // ***************************** Inner Classes ****************************
114 
115     // ******************************** Methods *******************************
116 
117     // --- init ---------------------------------------------------------------
118 
119     // --- get&set ------------------------------------------------------------
120 
121     /**
122      * Sets the detected XML version.
123      *
124      * @param version the detected XML version.
125      * @return a reference to this builder instance.
126      */
127     public Builder withVersion(final String version)
128     {
129       this.version = version;
130       return this;
131     }
132 
133     /**
134      * Sets the detected encoding.
135      *
136      * @param encoding the detected encoding.
137      * @return a reference to this builder instance.
138      */
139     public Builder withEncoding(final String encoding)
140     {
141       this.encoding = encoding;
142       return this;
143     }
144 
145     /**
146      * Sets the detected public ID version.
147      *
148      * @param publicId the detected public ID version.
149      * @return a reference to this builder instance.
150      */
151     public Builder withPublicId(final String publicId)
152     {
153       this.publicId = publicId;
154       return this;
155     }
156 
157     /**
158      * Sets the detected system ID version.
159      *
160      * @param systemId the detected system ID version.
161      * @return a reference to this builder instance.
162      */
163     public Builder withSystemId(final String systemId)
164     {
165       this.systemId = systemId;
166       return this;
167     }
168 
169     // --- business -----------------------------------------------------------
170 
171     /**
172      * Creates the instance upon the builder's data.
173      *
174      * @return the created instance.
175      */
176     public XmlDescriptor build()
177     {
178       return new XmlDescriptor(this);
179     }
180 
181     // --- object basics ------------------------------------------------------
182 
183   }
184 
185   // ********************************* Methods ********************************
186 
187   // --- init -----------------------------------------------------------------
188 
189   // --- get&set --------------------------------------------------------------
190 
191   /**
192    * Returns the detected XML version.
193    *
194    * @return the detected XML version. May be <code>null</code>.
195    */
196   public String getVersion()
197   {
198     return version;
199   }
200 
201   /**
202    * Returns the detected encoding.
203    *
204    * @return the detected encoding. May be <code>null</code>.
205    */
206   public String getEncoding()
207   {
208     return encoding;
209   }
210 
211   /**
212    * Returns the detected public ID version.
213    *
214    * @return the detected public ID version. May be <code>null</code>.
215    */
216   public String getPublicId()
217   {
218     return publicId;
219   }
220 
221   /**
222    * Returns the detected system ID version.
223    *
224    * @return the detected system ID version. May be <code>null</code>.
225    */
226   public String getSystemId()
227   {
228     return systemId;
229   }
230 
231   // --- business -------------------------------------------------------------
232 
233   /**
234    * Checks if at least one piece of information has been provided.
235    *
236    * @return <code>false</code> if no information has been found,
237    *         <code>true</code> if at least one piece of information has been
238    *         found.
239    */
240   public boolean isAnyInformationProvided()
241   {
242     return publicId != null || systemId != null || version != null
243            || encoding != null;
244   }
245 
246   private boolean providesIdAndContentInfo()
247   {
248     return (publicId != null || systemId != null) && providesContentInfo();
249   }
250 
251   private boolean providesContentInfo()
252   {
253     return version != null || encoding != null;
254   }
255 
256   // --- object basics --------------------------------------------------------
257 
258   /* CHECKSTYLE:OFF */
259   /**
260    * {@inheritDoc}
261    *
262    * @see java.lang.Object#toString()
263    */
264   @Override
265   public String toString() // NOPMD
266   {
267     final StringBuilder buffer = new StringBuilder(64);
268 
269     if (publicId != null)
270     {
271       buffer.append(publicId);
272 
273       if (systemId != null)
274       {
275         buffer.append('/');
276       }
277     }
278 
279     if (systemId != null)
280     {
281       buffer.append(systemId);
282     }
283 
284     if (providesIdAndContentInfo())
285     {
286       buffer.append(' ');
287     }
288 
289     if (providesContentInfo())
290     {
291       buffer.append("XML");
292 
293       if (version != null)
294       {
295         buffer.append(" version ").append(version);
296       }
297 
298       if (encoding != null)
299       {
300         buffer.append(" encoded with ").append(encoding);
301       }
302     }
303 
304     if (isAnyInformationProvided())
305     {
306       buffer.append('.');
307     }
308     else
309     {
310       buffer.append("No information provided.");
311     }
312 
313     return buffer.toString();
314   }
315   /* CHECKSTYLE:ON */
316 }