| 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 | |
| 10 | package com.adobe.internal.xmp.impl; |
| 11 | |
| 12 | import java.io.ByteArrayOutputStream; |
| 13 | import java.io.OutputStream; |
| 14 | import java.io.UnsupportedEncodingException; |
| 15 | |
| 16 | import com.adobe.internal.xmp.XMPException; |
| 17 | import com.adobe.internal.xmp.options.SerializeOptions; |
| 18 | |
| 19 | |
| 20 | /** |
| 21 | * Serializes the <code>XMPMeta</code>-object to an <code>OutputStream</code> according to the |
| 22 | * <code>SerializeOptions</code>. |
| 23 | * |
| 24 | * @author Stefan Makswit |
| 25 | * @version $Revision$ |
| 26 | * @since 11.07.2006 |
| 27 | */ |
| 28 | public class XMPSerializerHelper |
| 29 | { |
| 30 | /** |
| 31 | * Static method to serialize the metadata object. For each serialisation, a new XMPSerializer |
| 32 | * instance is created, either XMPSerializerRDF or XMPSerializerPlain so thats its possible to |
| 33 | * serialialize the same XMPMeta objects in two threads. |
| 34 | * |
| 35 | * @param xmp a metadata implementation object |
| 36 | * @param out the output stream to serialize to |
| 37 | * @param options serialization options, can be <code>null</code> for default. |
| 38 | * @throws XMPException |
| 39 | */ |
| 40 | public static void serialize(XMPMetaImpl xmp, OutputStream out, |
| 41 | SerializeOptions options) |
| 42 | throws XMPException |
| 43 | { |
| 44 | options = options != null ? options : new SerializeOptions(); |
| 45 | |
| 46 | // sort the internal data model on demand |
| 47 | if (options.getSort()) |
| 48 | { |
| 49 | xmp.sort(); |
| 50 | } |
| 51 | |
| 52 | // <#AdobePrivate> This is not very aesthetic, but needed to strip the block |
| 53 | // The Plain XMP format is disabled |
| 54 | // if (options.getUsePlainXMP()) |
| 55 | // { |
| 56 | // new XMPSerializerPlain().serialize(xmp, out, options); |
| 57 | // return; |
| 58 | // } |
| 59 | // </#AdobePrivate> |
| 60 | new XMPSerializerRDF().serialize(xmp, out, options); |
| 61 | } |
| 62 | |
| 63 | |
| 64 | /** |
| 65 | * Serializes an <code>XMPMeta</code>-object as RDF into a string. |
| 66 | * <em>Note:</em> Encoding is forced to UTF-16 when serializing to a |
| 67 | * string to ensure the correctness of "exact packet size". |
| 68 | * |
| 69 | * @param xmp a metadata implementation object |
| 70 | * @param options Options to control the serialization (see |
| 71 | * {@link SerializeOptions}). |
| 72 | * @return Returns a string containing the serialized RDF. |
| 73 | * @throws XMPException on serializsation errors. |
| 74 | */ |
| 75 | public static String serializeToString(XMPMetaImpl xmp, SerializeOptions options) |
| 76 | throws XMPException |
| 77 | { |
| 78 | // forces the encoding to be UTF-16 to get the correct string length |
| 79 | options = options != null ? options : new SerializeOptions(); |
| 80 | // By default encoding is utf8 |
| 81 | // options should be set by the client. Commenting setting utf16 option |
| 82 | // so that users can get the string in whichever encoding they want (by setting the options bits) |
| 83 | // options.setEncodeUTF16BE(true); |
| 84 | |
| 85 | ByteArrayOutputStream out = new ByteArrayOutputStream(2048); |
| 86 | serialize(xmp, out, options); |
| 87 | |
| 88 | try |
| 89 | { |
| 90 | return out.toString(options.getEncoding()); |
| 91 | } |
| 92 | catch (UnsupportedEncodingException e) |
| 93 | { |
| 94 | // cannot happen as UTF-8/16LE/BE is required to be implemented in |
| 95 | // Java |
| 96 | return out.toString(); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | |
| 101 | /** |
| 102 | * Serializes an <code>XMPMeta</code>-object as RDF into a byte buffer. |
| 103 | * |
| 104 | * @param xmp a metadata implementation object |
| 105 | * @param options Options to control the serialization (see {@link SerializeOptions}). |
| 106 | * @return Returns a byte buffer containing the serialized RDF. |
| 107 | * @throws XMPException on serializsation errors. |
| 108 | */ |
| 109 | public static byte[] serializeToBuffer(XMPMetaImpl xmp, SerializeOptions options) |
| 110 | throws XMPException |
| 111 | { |
| 112 | ByteArrayOutputStream out = new ByteArrayOutputStream(2048); |
| 113 | serialize(xmp, out, options); |
| 114 | return out.toByteArray(); |
| 115 | } |
| 116 | } |