Debian Codegraph @

src/com/adobe/internal/xmp/XMPMetaFactory.java

raw symbols SCIP
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;
11
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.io.OutputStream;
15 import java.net.URL;
16 import java.util.Enumeration;
17 import java.util.jar.Attributes;
18 import java.util.jar.Manifest;
19 import java.util.regex.Matcher;
20 import java.util.regex.Pattern;
21
22 import com.adobe.internal.xmp.impl.XMPMetaImpl;
23 import com.adobe.internal.xmp.impl.XMPMetaParser;
24 import com.adobe.internal.xmp.impl.XMPSchemaRegistryImpl;
25 import com.adobe.internal.xmp.impl.XMPSerializerHelper;
26 import com.adobe.internal.xmp.options.ParseOptions;
27 import com.adobe.internal.xmp.options.SerializeOptions;
28
29
30 /**
31 * Creates <code>XMPMeta</code>-instances from an <code>InputStream</code>
32 *
33 * @author Stefan Makswit
34 * @version $Revision$
35 * @since 30.01.2006
36 */
37 public final class XMPMetaFactory
38 {
39 /** The singleton instance of the <code>XMPSchemaRegistry</code>. */
40 private static XMPSchemaRegistry schema = new XMPSchemaRegistryImpl();
41 /** cache for version info */
42 private static XMPVersionInfo versionInfo = null;
43
44 /**
45 * Hides public constructor
46 */
47 private XMPMetaFactory()
48 {
49 // EMPTY
50 }
51
52
53 /**
54 * @return Returns the singleton instance of the <code>XMPSchemaRegistry</code>.
55 */
56 public static XMPSchemaRegistry getSchemaRegistry()
57 {
58 return schema;
59 }
60
61
62 /**
63 * @return Returns an empty <code>XMPMeta</code>-object.
64 */
65 public static XMPMeta create()
66 {
67 return new XMPMetaImpl();
68 }
69
70
71 /**
72 * Parsing with default options.
73 * @see XMPMetaFactory#parse(InputStream, ParseOptions)
74 *
75 * @param in an <code>InputStream</code>
76 * @return Returns the <code>XMPMeta</code>-object created from the input.
77 * @throws XMPException If the file is not well-formed XML or if the parsing fails.
78 */
79 public static XMPMeta parse(InputStream in) throws XMPException
80 {
81 return parse(in, null);
82 }
83
84
85 /**
86 * These functions support parsing serialized RDF into an XMP object, and serailizing an XMP
87 * object into RDF. The input for parsing may be any valid Unicode
88 * encoding. ISO Latin-1 is also recognized, but its use is strongly discouraged. Serialization
89 * is always as UTF-8.
90 * <p>
91 * <code>parseFromBuffer()</code> parses RDF from an <code>InputStream</code>. The encoding
92 * is recognized automatically.
93 *
94 * @param in an <code>InputStream</code>
95 * @param options Options controlling the parsing.<br>
96 * The available options are:
97 * <ul>
98 * <li> XMP_REQUIRE_XMPMETA - The &lt;x:xmpmeta&gt; XML element is required around
99 * <tt>&lt;rdf:RDF&gt;</tt>.
100 * <li> XMP_STRICT_ALIASING - Do not reconcile alias differences, throw an exception.
101 * </ul>
102 * <em>Note:</em>The XMP_STRICT_ALIASING option is not yet implemented.
103 * @return Returns the <code>XMPMeta</code>-object created from the input.
104 * @throws XMPException If the file is not well-formed XML or if the parsing fails.
105 */
106 public static XMPMeta parse(InputStream in, ParseOptions options)
107 throws XMPException
108 {
109 return XMPMetaParser.parse(in, options);
110 }
111
112
113 /**
114 * Parsing with default options.
115 * @see XMPMetaFactory#parse(InputStream)
116 *
117 * @param packet a String contain an XMP-file.
118 * @return Returns the <code>XMPMeta</code>-object created from the input.
119 * @throws XMPException If the file is not well-formed XML or if the parsing fails.
120 */
121 public static XMPMeta parseFromString(String packet) throws XMPException
122 {
123 return parseFromString(packet, null);
124 }
125
126
127 /**
128 * Creates an <code>XMPMeta</code>-object from a string.
129 * @see XMPMetaFactory#parseFromString(String, ParseOptions)
130 *
131 * @param packet a String contain an XMP-file.
132 * @param options Options controlling the parsing.
133 * @return Returns the <code>XMPMeta</code>-object created from the input.
134 * @throws XMPException If the file is not well-formed XML or if the parsing fails.
135 */
136 public static XMPMeta parseFromString(String packet, ParseOptions options)
137 throws XMPException
138 {
139 return XMPMetaParser.parse(packet, options);
140 }
141
142
143 /**
144 * Parsing with default options.
145 * @see XMPMetaFactory#parseFromBuffer(byte[], ParseOptions)
146 *
147 * @param buffer a String contain an XMP-file.
148 * @return Returns the <code>XMPMeta</code>-object created from the input.
149 * @throws XMPException If the file is not well-formed XML or if the parsing fails.
150 */
151 public static XMPMeta parseFromBuffer(byte[] buffer) throws XMPException
152 {
153 return parseFromBuffer(buffer, null);
154 }
155
156
157 /**
158 * Creates an <code>XMPMeta</code>-object from a byte-buffer.
159 * @see XMPMetaFactory#parse(InputStream, ParseOptions)
160 *
161 * @param buffer a String contain an XMP-file.
162 * @param options Options controlling the parsing.
163 * @return Returns the <code>XMPMeta</code>-object created from the input.
164 * @throws XMPException If the file is not well-formed XML or if the parsing fails.
165 */
166 public static XMPMeta parseFromBuffer(byte[] buffer,
167 ParseOptions options) throws XMPException
168 {
169 return XMPMetaParser.parse(buffer, options);
170 }
171
172
173 /**
174 * Serializes an <code>XMPMeta</code>-object as RDF into an <code>OutputStream</code>
175 * with default options.
176 *
177 * @param xmp a metadata object
178 * @param out an <code>OutputStream</code> to write the serialized RDF to.
179 * @throws XMPException on serializsation errors.
180 */
181 public static void serialize(XMPMeta xmp, OutputStream out) throws XMPException
182 {
183 serialize(xmp, out, null);
184 }
185
186
187 /**
188 * Serializes an <code>XMPMeta</code>-object as RDF into an <code>OutputStream</code>.
189 *
190 * @param xmp a metadata object
191 * @param options Options to control the serialization (see {@link SerializeOptions}).
192 * @param out an <code>OutputStream</code> to write the serialized RDF to.
193 * @throws XMPException on serializsation errors.
194 */
195 public static void serialize(XMPMeta xmp, OutputStream out, SerializeOptions options)
196 throws XMPException
197 {
198 assertImplementation(xmp);
199 XMPSerializerHelper.serialize((XMPMetaImpl) xmp, out, options);
200 }
201
202
203 /**
204 * Serializes an <code>XMPMeta</code>-object as RDF into a byte buffer.
205 *
206 * @param xmp a metadata object
207 * @param options Options to control the serialization (see {@link SerializeOptions}).
208 * @return Returns a byte buffer containing the serialized RDF.
209 * @throws XMPException on serializsation errors.
210 */
211 public static byte[] serializeToBuffer(XMPMeta xmp, SerializeOptions options)
212 throws XMPException
213 {
214 assertImplementation(xmp);
215 return XMPSerializerHelper.serializeToBuffer((XMPMetaImpl) xmp, options);
216 }
217
218
219 /**
220 * Serializes an <code>XMPMeta</code>-object as RDF into a string. <em>Note:</em> Encoding
221 * is ignored when serializing to a string.
222 *
223 * @param xmp a metadata object
224 * @param options Options to control the serialization (see {@link SerializeOptions}).
225 * @return Returns a string containing the serialized RDF.
226 * @throws XMPException on serializsation errors.
227 */
228 public static String serializeToString(XMPMeta xmp, SerializeOptions options)
229 throws XMPException
230 {
231 assertImplementation(xmp);
232 return XMPSerializerHelper.serializeToString((XMPMetaImpl) xmp, options);
233 }
234
235
236 /**
237 * @param xmp Asserts that xmp is compatible to <code>XMPMetaImpl</code>.s
238 */
239 private static void assertImplementation(XMPMeta xmp)
240 {
241 if (!(xmp instanceof XMPMetaImpl))
242 {
243 throw new UnsupportedOperationException("The serializing service works only" +
244 "with the XMPMeta implementation of this library");
245 }
246 }
247
248
249 /**
250 * Resets the schema registry to its original state (creates a new one).
251 * Be careful this might break all existing XMPMeta-objects and should be used
252 * only for testing purpurses.
253 */
254 public static void reset()
255 {
256 schema = new XMPSchemaRegistryImpl();
257 }
258
259
260 /**
261 * Obtain version information. The XMPVersionInfo singleton is created the first time
262 * its requested.
263 *
264 * @return Returns the version information.
265 */
266 public static synchronized XMPVersionInfo getVersionInfo()
267 {
268 if (versionInfo == null)
269 {
270 // Initialize with test version
271 String bundleVersion = "Test.SNAPSHOT";
272 int major= 5;
273 int minor = 0;
274 int micro = 0;
275
276 try
277 {
278 Enumeration<URL> resources = XMPMetaFactory.class
279 .getClassLoader().getResources("META-INF/MANIFEST.MF");
280
281 while (resources.hasMoreElements())
282 {
283 Manifest manifest = new Manifest(resources.nextElement()
284 .openStream());
285 Attributes attrs = manifest.getMainAttributes();
286 // find the correct manifest
287 if ("com.adobe.xmp.xmpcore".equals(attrs
288 .getValue("Bundle-SymbolicName")))
289 {
290 if (attrs.getValue("Bundle-Version") != null)
291 {
292 bundleVersion = attrs.getValue("Bundle-Version");
293 Pattern pattern = Pattern.compile("(\\d+)\\.(\\d+)\\.(\\d+).*");
294 Matcher matcher = pattern.matcher(bundleVersion);
295 if (matcher.find())
296 {
297 major = Integer.parseInt(matcher.group(1));
298 minor = Integer.parseInt(matcher.group(2));
299 micro = Integer.parseInt(matcher.group(3));
300 break;
301 }
302 }
303 }
304 }
305 }
306 catch (IOException E)
307 {
308 // EMPTY
309 }
310
311 // Adobe XMP Core 5.0.19.SNAPSHOT
312 final String message = "Adobe XMP Core " + bundleVersion;
313 final int majorVersion = major;
314 final int minorVersion = minor;
315 final int microVersion = micro;
316
317 versionInfo = new XMPVersionInfo()
318 {
319 public int getMajor()
320 {
321 return majorVersion;
322 }
323
324 public int getMinor()
325 {
326 return minorVersion;
327 }
328
329 public int getMicro()
330 {
331 return microVersion;
332 }
333
334 public boolean isDebug()
335 {
336 return true;
337 }
338
339 public int getBuild()
340 {
341 return 0;
342 }
343
344 public String getMessage()
345 {
346 return message;
347 }
348
349 public String toString()
350 {
351 return message;
352 }
353 };
354
355 }
356 return versionInfo;
357 }
358 }