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.xmp.impl; 11 12 import java.io.ByteArrayOutputStream; 13 import java.io.OutputStream; 14 import java.io.UnsupportedEncodingException; 15 16 import com.adobe.xmp.XMPException; 17 import com.adobe.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 * @since 11.07.2006 25 */ 26 public class XMPSerializerHelper 27 { 28 /** 29 * Static method to serialize the metadata object. For each serialisation, a new XMPSerializer 30 * instance is created, either XMPSerializerRDF or XMPSerializerPlain so thats its possible to 31 * serialialize the same XMPMeta objects in two threads. 32 * 33 * @param xmp a metadata implementation object 34 * @param out the output stream to serialize to 35 * @param options serialization options, can be <code>null</code> for default. 36 * @throws XMPException 37 */ serialize(XMPMetaImpl xmp, OutputStream out, SerializeOptions options)38 public static void serialize(XMPMetaImpl xmp, OutputStream out, 39 SerializeOptions options) 40 throws XMPException 41 { 42 options = options != null ? options : new SerializeOptions(); 43 44 // sort the internal data model on demand 45 if (options.getSort()) 46 { 47 xmp.sort(); 48 } 49 new XMPSerializerRDF().serialize(xmp, out, options); 50 } 51 52 53 /** 54 * Serializes an <code>XMPMeta</code>-object as RDF into a string. 55 * <em>Note:</em> Encoding is forced to UTF-16 when serializing to a 56 * string to ensure the correctness of "exact packet size". 57 * 58 * @param xmp a metadata implementation object 59 * @param options Options to control the serialization (see 60 * {@link SerializeOptions}). 61 * @return Returns a string containing the serialized RDF. 62 * @throws XMPException on serializsation errors. 63 */ serializeToString(XMPMetaImpl xmp, SerializeOptions options)64 public static String serializeToString(XMPMetaImpl xmp, SerializeOptions options) 65 throws XMPException 66 { 67 // forces the encoding to be UTF-16 to get the correct string length 68 options = options != null ? options : new SerializeOptions(); 69 options.setEncodeUTF16BE(true); 70 71 ByteArrayOutputStream out = new ByteArrayOutputStream(2048); 72 serialize(xmp, out, options); 73 74 try 75 { 76 return out.toString(options.getEncoding()); 77 } 78 catch (UnsupportedEncodingException e) 79 { 80 // cannot happen as UTF-8/16LE/BE is required to be implemented in 81 // Java 82 return out.toString(); 83 } 84 } 85 86 87 /** 88 * Serializes an <code>XMPMeta</code>-object as RDF into a byte buffer. 89 * 90 * @param xmp a metadata implementation object 91 * @param options Options to control the serialization (see {@link SerializeOptions}). 92 * @return Returns a byte buffer containing the serialized RDF. 93 * @throws XMPException on serializsation errors. 94 */ serializeToBuffer(XMPMetaImpl xmp, SerializeOptions options)95 public static byte[] serializeToBuffer(XMPMetaImpl xmp, SerializeOptions options) 96 throws XMPException 97 { 98 ByteArrayOutputStream out = new ByteArrayOutputStream(2048); 99 serialize(xmp, out, options); 100 return out.toByteArray(); 101 } 102 }