001/*
002 * Format.java July 2006
003 *
004 * Copyright (C) 2006, Niall Gallagher <niallg@users.sf.net>
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
015 * implied. See the License for the specific language governing
016 * permissions and limitations under the License.
017 */
018
019package org.simpleframework.xml.stream;
020
021import static org.simpleframework.xml.stream.Verbosity.HIGH;
022
023/**
024 * The <code>Format</code> object is used to provide information on
025 * how a generated XML document should be structured. The information
026 * provided tells the formatter whether an XML prolog is required and
027 * the number of spaces that should be used for indenting. The prolog
028 * specified will be written directly before the XML document.
029 * <p>
030 * Should a <code>Format</code> be created with an indent of zero or
031 * less then no indentation is done, and the generated XML will be on
032 * the same line. The prolog can contain any legal XML heading, which
033 * can domain a DTD declaration and XML comments if required.
034 *
035 * @author Niall Gallagher
036 */
037public class Format {
038
039   /**
040    * This is used to determine the verbosity preference of XML.
041    */
042   private final Verbosity verbosity;
043
044   /**
045    * Represents the prolog that appears in the generated XML.
046    */
047   private final String prolog;
048
049   /**
050    * This is the style that is used internally by the format.
051    */
052   private final Style style;
053
054   /**
055    * Represents the indent size to use for the generated XML.
056    */
057   private final int indent;
058
059   /**
060    * Represents whether to use single quotes instead of double quotes for the generated XML.
061    */
062   private final boolean singleQuote;
063
064   /**
065    * Constructor for the <code>Format</code> object. This creates an
066    * object that is used to describe how the formatter should create
067    * the XML document. This constructor uses an indent size of three.
068    */
069   public Format() {
070      this(3);
071   }
072
073   /**
074    * Constructor for the <code>Format</code> object. This creates an
075    * object that is used to describe how the formatter should create
076    * the XML document. This constructor uses the specified indent
077    * size and a null prolog, which means no prolog is generated.
078    *
079    * @param indent this is the number of spaces used in the indent
080    */
081   public Format(int indent) {
082      this(indent, null, new IdentityStyle());
083   }
084
085   /**
086    * Constructor for the <code>Format</code> object. This creates an
087    * object that is used to describe how the formatter should create
088    * the XML document. This constructor uses the specified prolog
089    * that is to be inserted at the start of the XML document.
090    *
091    * @param prolog this is the prolog for the generated XML document
092    */
093   public Format(String prolog) {
094      this(3, prolog);
095   }
096
097   /**
098    * Constructor for the <code>Format</code> object. This creates an
099    * object that is used to describe how the formatter should create
100    * the XML document. This constructor uses the specified indent
101    * size and the text to use in the generated prolog.
102    *
103    * @param indent this is the number of spaces used in the indent
104    * @param prolog this is the prolog for the generated XML document
105    */
106   public Format(int indent, String prolog) {
107      this(indent, prolog, new IdentityStyle());
108   }
109
110   public Format(int indent, String prolog, boolean singleQuote) {
111      this(indent, prolog, new IdentityStyle(), singleQuote);
112   }
113
114   /**
115    * Constructor for the <code>Format</code> object. This creates an
116    * object that is used to describe how the formatter should create
117    * the XML document. This constructor uses the specified style
118    * to style the attributes and elements of the XML document.
119    *
120    * @param verbosity this indicates the verbosity of the format
121    */
122   public Format(Verbosity verbosity) {
123      this(3, verbosity);
124   }
125
126   /**
127    * Constructor for the <code>Format</code> object. This creates an
128    * object that is used to describe how the formatter should create
129    * the XML document. This constructor uses the specified style
130    * to style the attributes and elements of the XML document.
131    *
132    * @param indent this is the number of spaces used in the indent
133    * @param verbosity this indicates the verbosity of the format
134    */
135   public Format(int indent, Verbosity verbosity) {
136      this(indent, new IdentityStyle(), verbosity);
137   }
138
139   /**
140    * Constructor for the <code>Format</code> object. This creates an
141    * object that is used to describe how the formatter should create
142    * the XML document. This constructor uses the specified style
143    * to style the attributes and elements of the XML document.
144    *
145    * @param style this is the style to apply to the format object
146    */
147   public Format(Style style) {
148      this(3, style);
149   }
150
151   /**
152    * Constructor for the <code>Format</code> object. This creates an
153    * object that is used to describe how the formatter should create
154    * the XML document. This constructor uses the specified style
155    * to style the attributes and elements of the XML document.
156    *
157    * @param style this is the style to apply to the format object
158    * @param verbosity this indicates the verbosity of the format
159    */
160   public Format(Style style, Verbosity verbosity) {
161      this(3, style, verbosity);
162   }
163
164   /**
165    * Constructor for the <code>Format</code> object. This creates an
166    * object that is used to describe how the formatter should create
167    * the XML document. This constructor uses the specified indent
168    * size and the style provided to style the XML document.
169    *
170    * @param indent this is the number of spaces used in the indent
171    * @param style this is the style to apply to the format object
172    */
173   public Format(int indent, Style style) {
174      this(indent, null, style);
175   }
176
177   /**
178    * Constructor for the <code>Format</code> object. This creates an
179    * object that is used to describe how the formatter should create
180    * the XML document. This constructor uses the specified indent
181    * size and the style provided to style the XML document.
182    *
183    * @param indent this is the number of spaces used in the indent
184    * @param style this is the style to apply to the format object
185    * @param verbosity this indicates the verbosity of the format
186    */
187   public Format(int indent, Style style, Verbosity verbosity) {
188      this(indent, null, style, verbosity);
189   }
190
191   /**
192    * Constructor for the <code>Format</code> object. This creates an
193    * object that is used to describe how the formatter should create
194    * the XML document. This constructor uses the specified indent
195    * size and the text to use in the generated prolog.
196    *
197    * @param indent this is the number of spaces used in the indent
198    * @param prolog this is the prolog for the generated XML document
199    * @param style this is the style to apply to the format object
200    */
201   public Format(int indent, String prolog, Style style) {
202     this(indent, prolog, style, HIGH);
203   }
204
205   public Format(int indent, String prolog, Style style, boolean singleQuote) {
206     this(indent, prolog, style, HIGH, singleQuote);
207   }
208
209   public Format(int indent, String prolog, Style style, Verbosity verbosity) {
210       this(indent, prolog, style, verbosity, false);
211   }
212
213   /**
214    * Constructor for the <code>Format</code> object. This creates an
215    * object that is used to describe how the formatter should create
216    * the XML document. This constructor uses the specified indent
217    * size and the text to use in the generated prolog.
218    *
219    * @param indent this is the number of spaces used in the indent
220    * @param prolog this is the prolog for the generated XML document
221    * @param style this is the style to apply to the format object
222    * @param verbosity this indicates the verbosity of the format
223    * @param singleQuote this indicates the quote style of the format
224    */
225   public Format(int indent, String prolog, Style style, Verbosity verbosity, boolean singleQuote) {
226      this.verbosity = verbosity;
227      this.prolog = prolog;
228      this.indent = indent;
229      this.style = style;
230      this.singleQuote = singleQuote;
231   }
232
233   /**
234    * This method returns the size of the indent to use for the XML
235    * generated. The indent size represents the number of spaces that
236    * are used for the indent, and indent of zero means no indenting.
237    *
238    * @return returns the number of spaces to used for indenting
239    */
240   public int getIndent() {
241      return indent;
242   }
243
244   /**
245    * This method returns the prolog that is to be used at the start
246    * of the generated XML document. This allows a DTD or a version
247    * to be specified at the start of a document. If this returns
248    * null then no prolog is written to the start of the XML document.
249    *
250    * @return this returns the prolog for the start of the document
251    */
252   public String getProlog() {
253      return prolog;
254   }
255
256   /**
257    * This is used to acquire the <code>Style</code> for the format.
258    * If no style has been set a default style is used, which does
259    * not modify the attributes and elements that are used to build
260    * the resulting XML document.
261    *
262    * @return this returns the style used for this format object
263    */
264   public Style getStyle() {
265      return style;
266   }
267
268   /**
269    * This method is used to indicate the preference of verbosity
270    * for the resulting XML. This is typically used when default
271    * serialization is used. It ensures that the various types
272    * that are serialized are of either high or low verbosity.
273    *
274    * @return this returns the verbosity preference for the XML
275    */
276   public Verbosity getVerbosity() {
277      return verbosity;
278   }
279
280    public boolean isSingleQuote() {
281        return singleQuote;
282    }
283}