Debian Codegraph @

src/com/adobe/internal/xmp/XMPUtils.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.security.NoSuchAlgorithmException;
13 import com.adobe.internal.xmp.impl.Base64;
14 import com.adobe.internal.xmp.impl.ISO8601Converter;
15 import com.adobe.internal.xmp.impl.XMPUtilsImpl;
16 import com.adobe.internal.xmp.options.PropertyOptions;
17 import com.adobe.internal.xmp.options.TemplateOptions;
18
19
20 /**
21 * Utility methods for XMP. I included only those that are different from the
22 * Java default conversion utilities.
23 *
24 * @author Stefan Makswit
25 * @version $Revision$
26 * @since 21.02.2006
27 */
28 public class XMPUtils
29 {
30 /** Private constructor */
31 private XMPUtils()
32 {
33 // EMPTY
34 }
35
36
37 /**
38 * Create a single edit string from an array of strings.
39 *
40 * @param xmp
41 * The XMP object containing the array to be catenated.
42 * @param schemaNS
43 * The schema namespace URI for the array. Must not be null or
44 * the empty string.
45 * @param arrayName
46 * The name of the array. May be a general path expression, must
47 * not be null or the empty string. Each item in the array must
48 * be a simple string value.
49 * @param separator
50 * The string to be used to separate the items in the catenated
51 * string. Defaults to "; ", ASCII semicolon and space
52 * (U+003B, U+0020).
53 * @param quotes
54 * The characters to be used as quotes around array items that
55 * contain a separator. Defaults to '"'
56 * @param allowCommas
57 * Option flag to control the catenation.
58 * @return Returns the string containing the catenated array items.
59 * @throws XMPException Forwards the Exceptions from the metadata processing
60 */
61 public static String catenateArrayItems(XMPMeta xmp, String schemaNS, String arrayName,
62 String separator, String quotes, boolean allowCommas) throws XMPException
63 {
64 return XMPUtilsImpl
65 .catenateArrayItems(xmp, schemaNS, arrayName, separator, quotes, allowCommas);
66 }
67
68
69 /**
70 * Separate a single edit string into an array of strings.
71 *
72 * @param xmp
73 * The XMP object containing the array to be updated.
74 * @param schemaNS
75 * The schema namespace URI for the array. Must not be null or
76 * the empty string.
77 * @param arrayName
78 * The name of the array. May be a general path expression, must
79 * not be null or the empty string. Each item in the array must
80 * be a simple string value.
81 * @param catedStr
82 * The string to be separated into the array items.
83 * @param arrayOptions Option flags to control the separation.
84 * @param preserveCommas Flag if commas shall be preserved
85 * @throws XMPException Forwards the Exceptions from the metadata processing
86 */
87 public static void separateArrayItems(XMPMeta xmp, String schemaNS, String arrayName,
88 String catedStr, PropertyOptions arrayOptions, boolean preserveCommas)
89 throws XMPException
90 {
91 XMPUtilsImpl.separateArrayItems(xmp, schemaNS, arrayName, catedStr, arrayOptions,
92 preserveCommas);
93 }
94
95
96 /**
97 * Remove multiple properties from an XMP object.
98 *
99 * RemoveProperties was created to support the File Info dialog's Delete
100 * button, and has been been generalized somewhat from those specific needs.
101 * It operates in one of three main modes depending on the schemaNS and
102 * propName parameters:
103 *
104 * <ul>
105 * <li> Non-empty <code>schemaNS</code> and <code>propName</code> - The named property is
106 * removed if it is an external property, or if the
107 * flag <code>doAllProperties</code> option is true. It does not matter whether the
108 * named property is an actual property or an alias.
109 *
110 * <li> Non-empty <code>schemaNS</code> and empty <code>propName</code> - The all external
111 * properties in the named schema are removed. Internal properties are also
112 * removed if the flag <code>doAllProperties</code> option is set. In addition,
113 * aliases from the named schema will be removed if the flag <code>includeAliases</code>
114 * option is set.
115 *
116 * <li> Empty <code>schemaNS</code> and empty <code>propName</code> - All external properties in
117 * all schema are removed. Internal properties are also removed if the
118 * flag <code>doAllProperties</code> option is passed. Aliases are implicitly handled
119 * because the associated actuals are internal if the alias is.
120 * </ul>
121 *
122 * It is an error to pass an empty <code>schemaNS</code> and non-empty <code>propName</code>.
123 *
124 * @param xmp
125 * The XMP object containing the properties to be removed.
126 *
127 * @param schemaNS
128 * Optional schema namespace URI for the properties to be
129 * removed.
130 *
131 * @param propName
132 * Optional path expression for the property to be removed.
133 *
134 * @param doAllProperties Option flag to control the deletion: do internal properties in
135 * addition to external properties.
136 *
137 * @param includeAliases Option flag to control the deletion:
138 * Include aliases in the "named schema" case above.
139 * <em>Note:</em> Currently not supported.
140 * @throws XMPException Forwards the Exceptions from the metadata processing
141 */
142 public static void removeProperties(XMPMeta xmp, String schemaNS, String propName,
143 boolean doAllProperties, boolean includeAliases) throws XMPException
144 {
145 XMPUtilsImpl.removeProperties(xmp, schemaNS, propName, doAllProperties, includeAliases);
146 }
147
148
149
150 /**
151 * Alias without the new option <code>deleteEmptyValues</code>.
152 * @param source The source XMP object.
153 * @param dest The destination XMP object.
154 * @param doAllProperties Do internal properties in addition to external properties.
155 * @param replaceOldValues Replace the values of existing properties.
156 * @throws XMPException Forwards the Exceptions from the metadata processing
157 */
158 public static void appendProperties(XMPMeta source, XMPMeta dest, boolean doAllProperties,
159 boolean replaceOldValues) throws XMPException
160 {
161 appendProperties(source, dest, doAllProperties, replaceOldValues, false);
162 }
163
164
165 /**
166 * <p>Append properties from one XMP object to another.
167 *
168 * <p>XMPUtils#appendProperties was created to support the File Info dialog's Append button, and
169 * has been been generalized somewhat from those specific needs. It appends information from one
170 * XMP object (source) to another (dest). The default operation is to append only external
171 * properties that do not already exist in the destination. The flag
172 * <code>doAllProperties</code> can be used to operate on all properties, external and internal.
173 * The flag <code>replaceOldValues</code> option can be used to replace the values
174 * of existing properties. The notion of external
175 * versus internal applies only to top level properties. The keep-or-replace-old notion applies
176 * within structs and arrays as described below.
177 * <ul>
178 * <li>If <code>replaceOldValues</code> is true then the processing is restricted to the top
179 * level properties. The processed properties from the source (according to
180 * <code>doAllProperties</code>) are propagated to the destination,
181 * replacing any existing values.Properties in the destination that are not in the source
182 * are left alone.
183 *
184 * <li>If <code>replaceOldValues</code> is not passed then the processing is more complicated.
185 * Top level properties are added to the destination if they do not already exist.
186 * If they do exist but differ in form (simple/struct/array) then the destination is left alone.
187 * If the forms match, simple properties are left unchanged while structs and arrays are merged.
188 *
189 * <li>If <code>deleteEmptyValues</code> is passed then an empty value in the source XMP causes
190 * the corresponding destination XMP property to be deleted. The default is to treat empty
191 * values the same as non-empty values. An empty value is any of a simple empty string, an array
192 * with no items, or a struct with no fields. Qualifiers are ignored.
193 * </ul>
194 *
195 * <p>The detailed behavior is defined by the following pseudo-code:
196 * <blockquote>
197 * <pre>
198 * appendProperties ( sourceXMP, destXMP, doAllProperties,
199 * replaceOldValues, deleteEmptyValues ):
200 * for all source schema (top level namespaces):
201 * for all top level properties in sourceSchema:
202 * if doAllProperties or prop is external:
203 * appendSubtree ( sourceNode, destSchema, replaceOldValues, deleteEmptyValues )
204 *
205 * appendSubtree ( sourceNode, destParent, replaceOldValues, deleteEmptyValues ):
206 * if deleteEmptyValues and source value is empty:
207 * delete the corresponding child from destParent
208 * else if sourceNode not in destParent (by name):
209 * copy sourceNode's subtree to destParent
210 * else if replaceOld:
211 * delete subtree from destParent
212 * copy sourceNode's subtree to destParent
213 * else:
214 * // Already exists in dest and not replacing, merge structs and arrays
215 * if sourceNode and destNode forms differ:
216 * return, leave the destNode alone
217 * else if form is a struct:
218 * for each field in sourceNode:
219 * AppendSubtree ( sourceNode.field, destNode, replaceOldValues )
220 * else if form is an alt-text array:
221 * copy new items by "xml:lang" value into the destination
222 * else if form is an array:
223 * copy new items by value into the destination, ignoring order and duplicates
224 * </pre>
225 * </blockquote>
226 *
227 * <p><em>Note:</em> appendProperties can be expensive if replaceOldValues is not passed and
228 * the XMP contains large arrays. The array item checking described above is n-squared.
229 * Each source item is checked to see if it already exists in the destination,
230 * without regard to order or duplicates.
231 * <p>Simple items are compared by value and "xml:lang" qualifier, other qualifiers are ignored.
232 * Structs are recursively compared by field names, without regard to field order. Arrays are
233 * compared by recursively comparing all items.
234 *
235 * @param source The source XMP object.
236 * @param dest The destination XMP object.
237 * @param doAllProperties Do internal properties in addition to external properties.
238 * @param replaceOldValues Replace the values of existing properties.
239 * @param deleteEmptyValues Delete destination values if source property is empty.
240 * @throws XMPException Forwards the Exceptions from the metadata processing
241 */
242 public static void appendProperties(XMPMeta source, XMPMeta dest, boolean doAllProperties,
243 boolean replaceOldValues, boolean deleteEmptyValues) throws XMPException
244 {
245 XMPUtilsImpl.appendProperties(source, dest, doAllProperties, replaceOldValues,
246 deleteEmptyValues);
247 }
248
249
250 /**
251 * Convert from string to Boolean.
252 *
253 * @param value
254 * The string representation of the Boolean.
255 * @return The appropriate boolean value for the string. The checked values
256 * for <code>true</code> and <code>false</code> are:
257 * <ul>
258 * <li>{@link XMPConst#TRUESTR} and {@link XMPConst#FALSESTR}
259 * <li>&quot;t&quot; and &quot;f&quot;
260 * <li>&quot;on&quot; and &quot;off&quot;
261 * <li>&quot;yes&quot; and &quot;no&quot;
262 * <li>&quot;value != 0&quot; and &quot;value == 0&quot;
263 * </ul>
264 * @throws XMPException If an empty string is passed.
265 */
266 public static boolean convertToBoolean(String value) throws XMPException
267 {
268 if (value == null || value.length() == 0)
269 {
270 throw new XMPException("Empty convert-string", XMPError.BADVALUE);
271 }
272 value = value.toLowerCase();
273
274 try
275 {
276 // First try interpretation as Integer (anything not 0 is true)
277 return Integer.parseInt(value) != 0;
278 }
279 catch (NumberFormatException e)
280 {
281 return
282 "true".equals(value) ||
283 "t".equals(value) ||
284 "on".equals(value) ||
285 "yes".equals(value);
286 }
287 }
288
289
290 /**
291 * Convert from boolean to string.
292 *
293 * @param value
294 * a boolean value
295 * @return The XMP string representation of the boolean. The values used are
296 * given by the constnts {@link XMPConst#TRUESTR} and
297 * {@link XMPConst#FALSESTR}.
298 */
299 public static String convertFromBoolean(boolean value)
300 {
301 return value ? XMPConst.TRUESTR : XMPConst.FALSESTR;
302 }
303
304
305 /**
306 * Converts a string value to an <code>int</code>.
307 *
308 * @param rawValue
309 * the string value
310 * @return Returns an int.
311 * @throws XMPException
312 * If the <code>rawValue</code> is <code>null</code> or empty or the
313 * conversion fails.
314 */
315 public static int convertToInteger(String rawValue) throws XMPException
316 {
317 try
318 {
319 if (rawValue == null || rawValue.length() == 0)
320 {
321 throw new XMPException("Empty convert-string", XMPError.BADVALUE);
322 }
323 if (rawValue.startsWith("0x"))
324 {
325 return Integer.parseInt(rawValue.substring(2), 16);
326 }
327 else
328 {
329 return Integer.parseInt(rawValue);
330 }
331 }
332 catch (NumberFormatException e)
333 {
334 throw new XMPException("Invalid integer string", XMPError.BADVALUE);
335 }
336 }
337
338
339 /**
340 * Convert from int to string.
341 *
342 * @param value
343 * an int value
344 * @return The string representation of the int.
345 */
346 public static String convertFromInteger(int value)
347 {
348 return String.valueOf(value);
349 }
350
351
352 /**
353 * Converts a string value to a <code>long</code>.
354 *
355 * @param rawValue
356 * the string value
357 * @return Returns a long.
358 * @throws XMPException
359 * If the <code>rawValue</code> is <code>null</code> or empty or the
360 * conversion fails.
361 */
362 public static long convertToLong(String rawValue) throws XMPException
363 {
364 try
365 {
366 if (rawValue == null || rawValue.length() == 0)
367 {
368 throw new XMPException("Empty convert-string", XMPError.BADVALUE);
369 }
370 if (rawValue.startsWith("0x"))
371 {
372 return Long.parseLong(rawValue.substring(2), 16);
373 }
374 else
375 {
376 return Long.parseLong(rawValue);
377 }
378 }
379 catch (NumberFormatException e)
380 {
381 throw new XMPException("Invalid long string", XMPError.BADVALUE);
382 }
383 }
384
385
386 /**
387 * Convert from long to string.
388 *
389 * @param value
390 * a long value
391 * @return The string representation of the long.
392 */
393 public static String convertFromLong(long value)
394 {
395 return String.valueOf(value);
396 }
397
398
399 /**
400 * Converts a string value to a <code>double</code>.
401 *
402 * @param rawValue
403 * the string value
404 * @return Returns a double.
405 * @throws XMPException
406 * If the <code>rawValue</code> is <code>null</code> or empty or the
407 * conversion fails.
408 */
409 public static double convertToDouble(String rawValue) throws XMPException
410 {
411 try
412 {
413 if (rawValue == null || rawValue.length() == 0)
414 {
415 throw new XMPException("Empty convert-string", XMPError.BADVALUE);
416 }
417 else
418 {
419 return Double.parseDouble(rawValue);
420 }
421 }
422 catch (NumberFormatException e)
423 {
424 throw new XMPException("Invalid double string", XMPError.BADVALUE);
425 }
426 }
427
428
429 /**
430 * Convert from long to string.
431 *
432 * @param value
433 * a long value
434 * @return The string representation of the long.
435 */
436 public static String convertFromDouble(double value)
437 {
438 return String.valueOf(value);
439 }
440
441
442 /**
443 * Converts a string value to an <code>XMPDateTime</code>.
444 *
445 * @param rawValue
446 * the string value
447 * @return Returns an <code>XMPDateTime</code>-object.
448 * @throws XMPException
449 * If the <code>rawValue</code> is <code>null</code> or empty or the
450 * conversion fails.
451 */
452 public static XMPDateTime convertToDate(String rawValue) throws XMPException
453 {
454 if (rawValue == null || rawValue.length() == 0)
455 {
456 throw new XMPException("Empty convert-string", XMPError.BADVALUE);
457 }
458 else
459 {
460 return ISO8601Converter.parse(rawValue);
461 }
462 }
463
464
465 /**
466 * Convert from <code>XMPDateTime</code> to string.
467 *
468 * @param value
469 * an <code>XMPDateTime</code>
470 * @return The string representation of the long.
471 */
472 public static String convertFromDate(XMPDateTime value)
473 {
474 return ISO8601Converter.render(value);
475 }
476
477
478 /**
479 * Convert from a byte array to a base64 encoded string.
480 *
481 * @param buffer
482 * the byte array to be converted
483 * @return Returns the base64 string.
484 */
485 public static String encodeBase64(byte[] buffer)
486 {
487 return new String(Base64.encode(buffer));
488 }
489
490
491 /**
492 * Decode from Base64 encoded string to raw data.
493 *
494 * @param base64String
495 * a base64 encoded string
496 * @return Returns a byte array containg the decoded string.
497 * @throws XMPException Thrown if the given string is not property base64 encoded
498 */
499 public static byte[] decodeBase64(String base64String) throws XMPException
500 {
501 try
502 {
503 return Base64.decode(base64String.getBytes());
504 }
505 catch (Throwable e)
506 {
507 throw new XMPException("Invalid base64 string", XMPError.BADVALUE, e);
508 }
509 }
510
511 /**
512 * creates XMP serializations appropriate for a JPEG file.
513 *
514 * The standard XMP in a JPEG file is limited to 64K bytes. This function
515 * serializes the XMP metadata in an XMP object into a string of RDF . If
516 * the data does not fit into the 64K byte limit, it creates a second packet
517 * string with the extended data.
518 *
519 * @param origXMP
520 * The XMP object containing the metadata.
521 *
522 * @param stdStr
523 * A string builder object in which to return the full standard XMP
524 * packet.
525 *
526 * @param extStr
527 * A string builder object in which to return the serialized extended
528 * XMP, empty if not needed.
529 *
530 * @param digestStr
531 * A string builder object in which to return an MD5 digest of the
532 * serialized extended XMP, empty if not needed.
533 *
534 * @throws NoSuchAlgorithmException if fail to find algorithm for MD5
535 * @throws XMPException in case of internal error occurs.
536 *
537 */
538
539 public static void packageForJPEG(XMPMeta origXMP,
540 StringBuilder stdStr,
541 StringBuilder extStr,
542 StringBuilder digestStr) throws NoSuchAlgorithmException, XMPException{
543 XMPUtilsImpl.packageForJPEG(origXMP,stdStr,extStr,digestStr);
544 }
545
546 /**
547 * merges standard and extended XMP retrieved from a JPEG file.
548 *
549 * When an extended partition stores properties that do not fit into the
550 * JPEG file limitation of 64K bytes, this function integrates those
551 * properties back into the same XMP object with those from the standard XMP
552 * packet.
553 *
554 * @param fullXMP
555 * An XMP object which the caller has initialized from the
556 * standard XMP packet in a JPEG file. The extended XMP is added
557 * to this object.
558 *
559 * @param extendedXMP
560 * An XMP object which the caller has initialized from the
561 * extended XMP packet in a JPEG file.
562 *
563 * @throws XMPException
564 * in case of internal error occurs.
565 */
566
567 public static void mergeFromJPEG(XMPMeta fullXMP,
568 XMPMeta extendedXMP) throws XMPException{
569 XMPUtilsImpl.mergeFromJPEG(fullXMP,extendedXMP);
570 }
571
572 /**
573 * modifies a working XMP object according to a template object.
574 *<p>
575 * The XMP template can be used to add, replace or delete properties from
576 * the working XMP object. The actions that you specify determine how the
577 * template is applied. Each action can be applied individually or combined;
578 * if you do not specify any actions, the properties and values in the
579 * working XMP object do not change.
580 *<p>
581 * These actions are available:
582 * <ul>
583 * <li>Clear <code> CLEAR_UNNAMED_PROPERTIES </code> : Deletes top-level
584 * properties. Any top-level property that is present in the template (even
585 * with empty value) is retained. All other top-level properties in the
586 * working object are deleted
587 *
588 * <li>Add <code> ADD_NEW_PROPERTIES </code>: Adds new properties to the
589 * working object if the template properties have values. See additional
590 * detail below.
591 *
592 * <li>Replace <code> REPLACE_EXISTING_PROPERTIES </code>: Replaces the
593 * values of existing top-level properties in the working XMP if the value
594 * forms match those in the template. Properties with empty values in the
595 * template are ignored. If combined with Clear or Add actions, those take
596 * precedence; values are cleared or added, rather than replaced.
597 *
598 * <li>Replace/Delete empty <code> REPLACE_WITH_DELETE_EMPTY </code>:
599 * Replaces values in the same way as the simple Replace action, and also
600 * deletes properties if the value in the template is empty. If combined
601 * with Clear or Add actions, those take precedence; values are cleared or
602 * added, rather than replaced.
603 *
604 * <li>Include internal <code> INCLUDE_INTERNAL_PROPERTIES </code>: Performs
605 * specified action on internal properties as well as external properties.
606 * By default, internal properties are ignored for all actions.
607 * </ul><p>
608 *
609 * The Add behavior depends on the type of property:
610 * <ul>
611 * <li>If a top-level property is not in the working XMP, and has a value in
612 * the template, the property and value are added. Empty properties are not
613 * added.
614 * <li>If a property is in both the working XMP and template, the value
615 * forms must match, otherwise the template is ignored for that property.
616 * <li>If a struct is present in both the working XMP and template, the
617 * individual fields of the template struct are added as appropriate; that
618 * is, the logic is recursively applied to the fields. Struct values are
619 * equivalent if they have the same fields with equivalent values.
620 * <li>If an array is present in both the working XMP and template, items
621 * from the template are added if the value forms match. Array values match
622 * if they have sets of equivalent items, regardless of order.
623 * <li>Alt-text arrays use the \c xml:lang qualifier as a key, adding
624 * languages that are missing.
625 * </ul><p>
626 * Array item checking is n-squared; this can be time-intensive if the
627 * Replace option is not specified. Each source item is checked to see if it
628 * already exists in the destination, without regard to order or duplicates.
629 * Simple items are compared by value and <code> xml:lang </code> qualifier;
630 * other qualifiers are ignored. Structs are recursively compared by field
631 * names, without regard to field order. Arrays are compared by recursively
632 * comparing all items.
633 *
634 * @param workingXMP
635 * The destination XMP object.
636 *
637 * @param templateXMP
638 * The template to apply to the destination XMP object.
639 *
640 * @param options
641 * Option flags to control the copying. If none are specified,
642 * the properties and values in the working XMP do not change. A
643 * logical OR of these bit-flag constants:
644 * <ul>
645 * <li><code> CLEAR_UNNAMED_PROPERTIES </code> Delete anything
646 * that is not in the template
647 * <li><code> ADD_NEW_PROPERTIES </code> Add properties; see
648 * detailed description.
649 * <li><code> REPLACE_EXISTING_PROPERTIES </code> Replace the
650 * values of existing properties.
651 * <li><code> REPLACE_WITH_DELETE_EMPTY </code> Replace the
652 * values of existing properties and delete properties if the new
653 * value is empty.
654 * <li><code> INCLUDE_INTERNAL_PROPERTIES </code> Operate on
655 * internal properties as well as external properties.
656 * </ul>
657 *
658 * @throws XMPException
659 * in case of internal error occurs.
660 */
661
662 static public void applyTemplate ( XMPMeta workingXMP,
663 XMPMeta templateXMP, TemplateOptions options)
664 throws XMPException{
665 XMPUtilsImpl.applyTemplate(workingXMP,templateXMP,options);
666 }
667
668
669 /**
670 *
671 * Replicate a subtree from one XMP object into another, possibly at a
672 * different location.
673 *
674 *
675 * @param source
676 * The source XMP object.
677 *
678 * @param dest
679 * The destination XMP object.
680 *
681 * @param sourceNS
682 * The schema namespace URI for the source subtree.
683 *
684 * @param sourceRoot
685 * The root location for the source subtree. May be a general
686 * path expression, must not be null or the empty string.
687 *
688 * @param destNS
689 * The schema namespace URI for the destination. Defaults to the
690 * source namespace.
691 *
692 * @param destRoot
693 * The root location for the destination. May be a general path
694 * expression. Defaults to the source location.
695 *
696 * @param options
697 * Option flags to control the separation. (For now, this argument is ignored.
698 * 0 should be passed.
699 * @throws XMPException throws an XMPException
700 */
701 public static void duplicateSubtree(XMPMeta source, XMPMeta dest, String sourceNS,
702 String sourceRoot, String destNS, String destRoot, PropertyOptions options) throws XMPException
703 {
704 XMPUtilsImpl.duplicateSubtree(source, dest, sourceNS, sourceRoot, destNS, destRoot, options);
705 }
706
707 }