1// =================================================================================================
2// ADOBE SYSTEMS INCORPORATED
3// Copyright 2006 Adobe Systems Incorporated
4// All Rights Reserved
5//
6// NOTICE:  Adobe permits you to use, modify, and distribute this file in accordance with the terms
7// of the Adobe license agreement accompanying it.
8// =================================================================================================
9
10package com.adobe.xmp.options;
11
12import com.adobe.xmp.XMPException;
13import com.adobe.xmp.XMPMeta;
14import com.adobe.xmp.XMPMetaFactory;
15
16
17/**
18 * Options for {@link XMPMetaFactory#serializeToBuffer(XMPMeta, SerializeOptions)}.
19 *
20 * @since 24.01.2006
21 */
22public final class SerializeOptions extends Options
23{
24	/** Omit the XML packet wrapper. */
25	public static final int OMIT_PACKET_WRAPPER = 0x0010;
26	/** Mark packet as read-only. Default is a writeable packet. */
27	public static final int READONLY_PACKET = 0x0020;
28	/** Use a compact form of RDF. */
29	public static final int USE_COMPACT_FORMAT = 0x0040;
30	/**
31	 * Include a padding allowance for a thumbnail image. If no <tt>xmp:Thumbnails</tt> property
32	 * is present, the typical space for a JPEG thumbnail is used.
33	 */
34	public static final int INCLUDE_THUMBNAIL_PAD = 0x0100;
35	/**
36	 * The padding parameter provides the overall packet length. The actual amount of padding is
37	 * computed. An exception is thrown if the packet exceeds this length with no padding.
38	 */
39	public static final int EXACT_PACKET_LENGTH = 0x0200;
40	/** Sort the struct properties and qualifier before serializing */
41	public static final int SORT = 0x1000;
42
43	// ---------------------------------------------------------------------------------------------
44	// encoding bit constants
45
46	/** Bit indicating little endian encoding, unset is big endian */
47	private static final int LITTLEENDIAN_BIT = 0x0001;
48	/** Bit indication UTF16 encoding. */
49	private static final int UTF16_BIT = 0x0002;
50	/** UTF8 encoding; this is the default */
51	public static final int ENCODE_UTF8 = 0;
52	/** UTF16BE encoding */
53	public static final int ENCODE_UTF16BE = UTF16_BIT;
54	/** UTF16LE encoding */
55	public static final int ENCODE_UTF16LE = UTF16_BIT | LITTLEENDIAN_BIT;
56	/** */
57	private static final int ENCODING_MASK = UTF16_BIT | LITTLEENDIAN_BIT;
58
59	/**
60	 * The amount of padding to be added if a writeable XML packet is created. If zero is passed
61	 * (the default) an appropriate amount of padding is computed.
62	 */
63	private int padding = 2048;
64	/**
65	 * The string to be used as a line terminator. If empty it defaults to; linefeed, U+000A, the
66	 * standard XML newline.
67	 */
68	private String newline = "\n";
69	/**
70	 * The string to be used for each level of indentation in the serialized
71	 * RDF. If empty it defaults to two ASCII spaces, U+0020.
72	 */
73	private String indent = "  ";
74	/**
75	 * The number of levels of indentation to be used for the outermost XML element in the
76	 * serialized RDF. This is convenient when embedding the RDF in other text, defaults to 0.
77	 */
78	private int baseIndent = 0;
79	/** Omits the Toolkit version attribute, not published, only used for Unit tests. */
80	private boolean omitVersionAttribute = false;
81
82
83	/**
84	 * Default constructor.
85	 */
86	public SerializeOptions()
87	{
88		// reveal default constructor
89	}
90
91
92	/**
93	 * Constructor using inital options
94	 * @param options the inital options
95	 * @throws XMPException Thrown if options are not consistant.
96	 */
97	public SerializeOptions(int options) throws XMPException
98	{
99		super(options);
100	}
101
102
103	/**
104	 * @return Returns the option.
105	 */
106	public boolean getOmitPacketWrapper()
107	{
108		return getOption(OMIT_PACKET_WRAPPER);
109	}
110
111
112	/**
113	 * @param value the value to set
114	 * @return Returns the instance to call more set-methods.
115	 */
116	public SerializeOptions setOmitPacketWrapper(boolean value)
117	{
118		setOption(OMIT_PACKET_WRAPPER, value);
119		return this;
120	}
121
122
123	/**
124	 * @return Returns the option.
125	 */
126	public boolean getReadOnlyPacket()
127	{
128		return getOption(READONLY_PACKET);
129	}
130
131
132	/**
133	 * @param value the value to set
134	 * @return Returns the instance to call more set-methods.
135	 */
136	public SerializeOptions setReadOnlyPacket(boolean value)
137	{
138		setOption(READONLY_PACKET, value);
139		return this;
140	}
141
142
143	/**
144	 * @return Returns the option.
145	 */
146	public boolean getUseCompactFormat()
147	{
148		return getOption(USE_COMPACT_FORMAT);
149	}
150
151
152	/**
153	 * @param value the value to set
154	 * @return Returns the instance to call more set-methods.
155	 */
156	public SerializeOptions setUseCompactFormat(boolean value)
157	{
158		setOption(USE_COMPACT_FORMAT, value);
159		return this;
160	}
161
162	/**
163	 * @return Returns the option.
164	 */
165	public boolean getIncludeThumbnailPad()
166	{
167		return getOption(INCLUDE_THUMBNAIL_PAD);
168	}
169
170
171	/**
172	 * @param value the value to set
173	 * @return Returns the instance to call more set-methods.
174	 */
175	public SerializeOptions setIncludeThumbnailPad(boolean value)
176	{
177		setOption(INCLUDE_THUMBNAIL_PAD, value);
178		return this;
179	}
180
181
182	/**
183	 * @return Returns the option.
184	 */
185	public boolean getExactPacketLength()
186	{
187		return getOption(EXACT_PACKET_LENGTH);
188	}
189
190
191	/**
192	 * @param value the value to set
193	 * @return Returns the instance to call more set-methods.
194	 */
195	public SerializeOptions setExactPacketLength(boolean value)
196	{
197		setOption(EXACT_PACKET_LENGTH, value);
198		return this;
199	}
200
201
202	/**
203	 * @return Returns the option.
204	 */
205	public boolean getSort()
206	{
207		return getOption(SORT);
208	}
209
210
211	/**
212	 * @param value the value to set
213	 * @return Returns the instance to call more set-methods.
214	 */
215	public SerializeOptions setSort(boolean value)
216	{
217		setOption(SORT, value);
218		return this;
219	}
220
221
222	/**
223	 * @return Returns the option.
224	 */
225	public boolean getEncodeUTF16BE()
226	{
227		return (getOptions() & ENCODING_MASK) == ENCODE_UTF16BE;
228	}
229
230
231	/**
232	 * @param value the value to set
233	 * @return Returns the instance to call more set-methods.
234	 */
235	public SerializeOptions setEncodeUTF16BE(boolean value)
236	{
237		// clear unicode bits
238		setOption(UTF16_BIT | LITTLEENDIAN_BIT, false);
239		setOption(ENCODE_UTF16BE, value);
240		return this;
241	}
242
243
244	/**
245	 * @return Returns the option.
246	 */
247	public boolean getEncodeUTF16LE()
248	{
249		return (getOptions() & ENCODING_MASK) == ENCODE_UTF16LE;
250	}
251
252
253	/**
254	 * @param value the value to set
255	 * @return Returns the instance to call more set-methods.
256	 */
257	public SerializeOptions setEncodeUTF16LE(boolean value)
258	{
259		// clear unicode bits
260		setOption(UTF16_BIT | LITTLEENDIAN_BIT, false);
261		setOption(ENCODE_UTF16LE, value);
262		return this;
263	}
264
265
266	/**
267	 * @return Returns the baseIndent.
268	 */
269	public int getBaseIndent()
270	{
271		return baseIndent;
272	}
273
274
275	/**
276	 * @param baseIndent
277	 *            The baseIndent to set.
278	 * @return Returns the instance to call more set-methods.
279	 */
280	public SerializeOptions setBaseIndent(int baseIndent)
281	{
282		this.baseIndent = baseIndent;
283		return this;
284	}
285
286
287	/**
288	 * @return Returns the indent.
289	 */
290	public String getIndent()
291	{
292		return indent;
293	}
294
295
296	/**
297	 * @param indent
298	 *            The indent to set.
299	 * @return Returns the instance to call more set-methods.
300	 */
301	public SerializeOptions setIndent(String indent)
302	{
303		this.indent = indent;
304		return this;
305	}
306
307
308	/**
309	 * @return Returns the newline.
310	 */
311	public String getNewline()
312	{
313		return newline;
314	}
315
316
317	/**
318	 * @param newline
319	 *            The newline to set.
320	 * @return Returns the instance to call more set-methods.
321	 */
322	public SerializeOptions setNewline(String newline)
323	{
324		this.newline = newline;
325		return this;
326	}
327
328
329	/**
330	 * @return Returns the padding.
331	 */
332	public int getPadding()
333	{
334		return padding;
335	}
336
337
338	/**
339	 * @param padding
340	 *            The padding to set.
341	 * @return Returns the instance to call more set-methods.
342	 */
343	public SerializeOptions setPadding(int padding)
344	{
345		this.padding = padding;
346		return this;
347	}
348
349
350	/**
351	 * @return Returns whether the Toolkit version attribute shall be omitted.
352	 * <em>Note:</em> This options can only be set by unit tests.
353	 */
354	public boolean getOmitVersionAttribute()
355	{
356		return omitVersionAttribute;
357	}
358
359
360	/**
361	 * @return Returns the encoding as Java encoding String.
362	 */
363	public String getEncoding()
364	{
365		if (getEncodeUTF16BE())
366		{
367			return "UTF-16BE";
368		}
369		else if (getEncodeUTF16LE())
370		{
371			return "UTF-16LE";
372		}
373		else
374		{
375			return "UTF-8";
376		}
377	}
378
379
380	/**
381	 *
382	 * @return Returns clone of this SerializeOptions-object with the same options set.
383	 * @throws CloneNotSupportedException Cannot happen in this place.
384	 */
385	public Object clone() throws CloneNotSupportedException
386	{
387		SerializeOptions clone;
388		try
389		{
390			clone = new SerializeOptions(getOptions());
391			clone.setBaseIndent(baseIndent);
392			clone.setIndent(indent);
393			clone.setNewline(newline);
394			clone.setPadding(padding);
395			return clone;
396		}
397		catch (XMPException e)
398		{
399			// This cannot happen, the options are already checked in "this" object.
400			return null;
401		}
402	}
403
404
405	/**
406	 * @see Options#defineOptionName(int)
407	 */
408	protected String defineOptionName(int option)
409	{
410		switch (option)
411		{
412			case OMIT_PACKET_WRAPPER : 		return "OMIT_PACKET_WRAPPER";
413			case READONLY_PACKET :			return "READONLY_PACKET";
414			case USE_COMPACT_FORMAT :		return "USE_COMPACT_FORMAT";
415			case INCLUDE_THUMBNAIL_PAD :	return "INCLUDE_THUMBNAIL_PAD";
416			case EXACT_PACKET_LENGTH :		return "EXACT_PACKET_LENGTH";
417			case SORT :				return "NORMALIZED";
418			default: 						return null;
419		}
420	}
421
422
423	/**
424	 * @see Options#getValidOptions()
425	 */
426	protected int getValidOptions()
427	{
428		return
429		OMIT_PACKET_WRAPPER |
430		READONLY_PACKET |
431		USE_COMPACT_FORMAT |
432		INCLUDE_THUMBNAIL_PAD |
433		EXACT_PACKET_LENGTH |
434		SORT;
435	}
436}