Debian Codegraph @

src/com/adobe/internal/xmp/impl/Utils.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.impl;
11
12
13 import java.util.HashSet;
14 import java.util.Set;
15
16 import com.adobe.internal.xmp.XMPConst;
17
18
19 /**
20 * Utility functions for the XMPToolkit implementation.
21 *
22 * @author Stefan Makswit
23 * @version $Revision$
24 * @since 06.06.2006
25 */
26 public class Utils implements XMPConst
27 {
28 /** segments of a UUID */
29 public static final int UUID_SEGMENT_COUNT = 4;
30 /** length of a UUID */
31 public static final int UUID_LENGTH = 32 + UUID_SEGMENT_COUNT;
32 /** table of XML name start chars (<= 0xFF) */
33 private static boolean[] xmlNameStartChars;
34 /** table of XML name chars (<= 0xFF) */
35 private static boolean[] xmlNameChars;
36 /** init char tables */
37 static
38 {
39 initCharTables();
40 }
41
42
43 /**
44 * Private constructor
45 */
46 private Utils()
47 {
48 // EMPTY
49 }
50
51
52 /**
53 * Normalize an xml:lang value so that comparisons are effectively case
54 * insensitive as required by RFC 3066 (which superceeds RFC 1766). The
55 * normalization rules:
56 * <ul>
57 * <li> The primary subtag is lower case, the suggested practice of ISO 639.
58 * <li> All 2 letter secondary subtags are upper case, the suggested
59 * practice of ISO 3166.
60 * <li> All other subtags are lower case.
61 * </ul>
62 *
63 * @param value
64 * raw value
65 * @return Returns the normalized value.
66 */
67 public static String normalizeLangValue(String value)
68 {
69 // don't normalize x-default
70 if (XMPConst.X_DEFAULT.equals(value))
71 {
72 return value;
73 }
74
75 int subTag = 1;
76 StringBuffer buffer = new StringBuffer();
77
78 for (int i = 0; i < value.length(); i++)
79 {
80 switch (value.charAt(i))
81 {
82 case '-':
83 case '_':
84 // move to next subtag and convert underscore to hyphen
85 buffer.append('-');
86 subTag++;
87 break;
88 case ' ':
89 // remove spaces
90 break;
91 default:
92 // convert second subtag to uppercase, all other to lowercase
93 if (subTag != 2)
94 {
95 buffer.append(Character.toLowerCase(value.charAt(i)));
96 }
97 else
98 {
99 buffer.append(Character.toUpperCase(value.charAt(i)));
100 }
101 }
102
103 }
104 return buffer.toString();
105 }
106
107
108 /**
109 * Split the name and value parts for field and qualifier selectors:
110 * <ul>
111 * <li>[qualName="value"] - An element in an array of structs, chosen by a
112 * field value.
113 * <li>[?qualName="value"] - An element in an array, chosen by a qualifier
114 * value.
115 * </ul>
116 * The value portion is a string quoted by ''' or '"'. The value may contain
117 * any character including a doubled quoting character. The value may be
118 * empty. <em>Note:</em> It is assumed that the expression is formal
119 * correct
120 *
121 * @param selector
122 * the selector
123 * @return Returns an array where the first entry contains the name and the
124 * second the value.
125 */
126 static String[] splitNameAndValue(String selector)
127 {
128 // get the name
129 int eq = selector.indexOf('=');
130 int pos = 1;
131 if (selector.charAt(pos) == '?')
132 {
133 pos++;
134 }
135 String name = selector.substring(pos, eq);
136
137 // get the value
138 pos = eq + 1;
139 char quote = selector.charAt(pos);
140 pos++;
141 int end = selector.length() - 2; // quote and ]
142 StringBuffer value = new StringBuffer(end - eq);
143 while (pos < end)
144 {
145 value.append(selector.charAt(pos));
146 pos++;
147 if (selector.charAt(pos) == quote)
148 {
149 // skip one quote in value
150 pos++;
151 }
152 }
153 return new String[] { name, value.toString() };
154 }
155
156 static Set<String> EXTERNAL_XMPDM_PROPS = new HashSet<String>(){{
157 add("xmpDM:album");
158 add( "xmpDM:altTapeName");
159 add("xmpDM:altTimecode");
160 add("xmpDM:artist");
161 add("xmpDM:cameraAngle");
162 add("xmpDM:cameraLabel");
163 add("xmpDM:cameraModel");
164 add("xmpDM:cameraMove");
165 add("xmpDM:client");
166 add("xmpDM:comment");
167 add("xmpDM:composer");
168 add("xmpDM:director");
169 add( "xmpDM:directorPhotography");
170 add("xmpDM:engineer");
171 add( "xmpDM:genre");
172 add( "xmpDM:good");
173 add("xmpDM:instrument");
174 add( "xmpDM:logComment");
175 add("xmpDM:projectName");
176 add("xmpDM:releaseDate");
177 add("xmpDM:scene");
178 add("xmpDM:shotDate");
179 add("xmpDM:shotDay");
180 add("xmpDM:shotLocation");
181 add("xmpDM:shotName");
182 add("xmpDM:shotNumber");
183 add("xmpDM:shotSize");
184 add("xmpDM:speakerPlacement");
185 add("xmpDM:takeNumber");
186 add("xmpDM:tapeName");
187 add("xmpDM:trackNumber");
188 add("xmpDM:videoAlphaMode");
189 add("xmpDM:videoAlphaPremultipleColor");
190 }};
191
192
193
194 /**
195 *
196 * @param schema
197 * a schema namespace
198 * @param prop
199 * an XMP Property
200 * @return Returns true if the property is defined as &quot;Internal
201 * Property&quot;, see XMP Specification.
202 */
203 static boolean isInternalProperty(String schema, String prop)
204 {
205 boolean isInternal = false;
206
207 if (NS_DC.equals(schema))
208 {
209 if ("dc:format".equals(prop) || "dc:language".equals(prop))
210 {
211 isInternal = true;
212 }
213 }
214 else if (NS_XMP.equals(schema))
215 {
216 if ("xmp:BaseURL".equals(prop) || "xmp:CreatorTool".equals(prop)
217 || "xmp:Format".equals(prop) || "xmp:Locale".equals(prop)
218 || "xmp:MetadataDate".equals(prop) || "xmp:ModifyDate".equals(prop))
219 {
220 isInternal = true;
221 }
222 }
223 else if (NS_PDF.equals(schema))
224 {
225 if ("pdf:BaseURL".equals(prop) || "pdf:Creator".equals(prop)
226 || "pdf:ModDate".equals(prop) || "pdf:PDFVersion".equals(prop)
227 || "pdf:Producer".equals(prop))
228 {
229 isInternal = true;
230 }
231 }
232 else if (NS_TIFF.equals(schema))
233 {
234 isInternal = true;
235 if ("tiff:ImageDescription".equals(prop) || "tiff:Artist".equals(prop)
236 || "tiff:Copyright".equals(prop))
237 {
238 isInternal = false;
239 }
240 }
241 else if (NS_EXIF.equals(schema))
242 {
243 isInternal = true;
244 if ("exif:UserComment".equals(prop))
245 {
246 isInternal = false;
247 }
248 }
249 else if (NS_EXIF_AUX.equals(schema))
250 {
251 isInternal = true;
252 }
253 else if (NS_PHOTOSHOP.equals(schema))
254 {
255 if ("photoshop:ICCProfile".equals(prop) || "photoshop:TextLayers".equals(prop))
256 {
257 isInternal = true;
258 }
259 }
260 else if(NS_DM.equals(schema)){
261 isInternal = ! EXTERNAL_XMPDM_PROPS.contains(prop);
262 }
263 else if (NS_CAMERARAW.equals(schema))
264 {
265 isInternal = true;
266 }
267 else if(NS_SCRIPT.equals(schema)){
268 isInternal = true;
269 if(("xmpScript:action".equals(prop)) || ("xmpScript:character".equals(prop))
270 ||("xmpScript:dialog".equals(prop))||("xmpScript:sceneSetting".equals(prop))
271 ||("xmpScript:sceneTimeOfDay".equals(prop)))
272 isInternal = false;
273 }
274 else if(NS_BWF.equals(schema)){
275 if("bext:version".equals(prop))
276 isInternal = true;
277 }
278 else if (NS_ADOBESTOCKPHOTO.equals(schema))
279 {
280 isInternal = true;
281 }
282 else if (NS_XMP_MM.equals(schema))
283 {
284 isInternal = true;
285 }
286 else if (TYPE_TEXT.equals(schema))
287 {
288 isInternal = true;
289 }
290 else if (TYPE_PAGEDFILE.equals(schema))
291 {
292 isInternal = true;
293 }
294 else if (TYPE_GRAPHICS.equals(schema))
295 {
296 isInternal = true;
297 }
298 else if (TYPE_IMAGE.equals(schema))
299 {
300 isInternal = true;
301 }
302 else if (TYPE_FONT.equals(schema))
303 {
304 isInternal = true;
305 }
306
307 return isInternal;
308 }
309
310
311 /**
312 * Check some requirements for an UUID:
313 * <ul>
314 * <li>Length of the UUID is 32</li>
315 * <li>The Delimiter count is 4 and all the 4 delimiter are on their right
316 * position (8,13,18,23)</li>
317 * </ul>
318 *
319 * FfF: in Java 1.5 replace by java.util.UUID
320 *
321 * @param uuid uuid to test
322 * @return true - this is a well formed UUID, false - UUID has not the expected format
323 */
324
325 static boolean checkUUIDFormat(String uuid)
326 {
327 boolean result = true;
328 int delimCnt = 0;
329 int delimPos = 0;
330
331 if (uuid == null)
332 {
333 return false;
334 }
335
336 for (delimPos = 0; delimPos < uuid.length(); delimPos++)
337 {
338 if (uuid.charAt(delimPos) == '-')
339 {
340 delimCnt++;
341 result = result &&
342 (delimPos == 8 || delimPos == 13 || delimPos == 18 || delimPos == 23);
343 }
344 }
345
346 return result && UUID_SEGMENT_COUNT == delimCnt && UUID_LENGTH == delimPos;
347 }
348
349
350 /**
351 * Simple check for valid XMLNames. Within ASCII range<br>
352 * ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6]<br>
353 * are accepted, above all characters (which is not entirely
354 * correct according to the XML Spec.
355 *
356 * @param name an XML Name
357 * @return Return <code>true</code> if the name is correct.
358 */
359 public static boolean isXMLName(String name)
360 {
361 if (name.length() > 0 && !isNameStartChar(name.charAt(0)))
362 {
363 return false;
364 }
365 for (int i = 1; i < name.length(); i++)
366 {
367 if (!isNameChar(name.charAt(i)))
368 {
369 return false;
370 }
371 }
372 return true;
373 }
374
375
376 /**
377 * Checks if the value is a legal "unqualified" XML name, as
378 * defined in the XML Namespaces proposed recommendation.
379 * These are XML names, except that they must not contain a colon.
380 * @param name the value to check
381 * @return Returns true if the name is a valid "unqualified" XML name.
382 */
383 public static boolean isXMLNameNS(String name)
384 {
385 if (name.length() > 0 && (!isNameStartChar(name.charAt(0)) || name.charAt(0) == ':'))
386 {
387 return false;
388 }
389 for (int i = 1; i < name.length(); i++)
390 {
391 if (!isNameChar(name.charAt(i)) || name.charAt(i) == ':')
392 {
393 return false;
394 }
395 }
396 return true;
397 }
398
399
400 /**
401 * @param c a char
402 * @return Returns true if the char is an ASCII control char.
403 */
404 static boolean isControlChar(char c)
405 {
406 return (c <= 0x1F || c == 0x7F) &&
407 c != 0x09 && c != 0x0A && c != 0x0D;
408 }
409
410
411 /**
412 * Serializes the node value in XML encoding. Its used for tag bodies and
413 * attributes.<br>
414 * <em>Note:</em> The attribute is always limited by quotes,
415 * thats why <code>&amp;apos;</code> is never serialized.<br>
416 * <em>Note:</em> Control chars are written unescaped, but if the user uses others than tab, LF
417 * and CR the resulting XML will become invalid.
418 * @param value a string
419 * @param forAttribute flag if string is attribute value (need to additional escape quotes)
420 * @param escapeWhitespaces Decides if LF, CR and TAB are escaped.
421 * @return Returns the value ready for XML output.
422 */
423 public static String escapeXML(String value, boolean forAttribute, boolean escapeWhitespaces)
424 {
425 // quick check if character are contained that need special treatment
426 boolean needsEscaping = false;
427 for (int i = 0; i < value.length (); i++)
428 {
429 char c = value.charAt (i);
430 if (
431 c == '<' || c == '>' || c == '&' || // XML chars
432 (escapeWhitespaces && (c == '\t' || c == '\n' || c == '\r')) ||
433 (forAttribute && c == '"'))
434 {
435 needsEscaping = true;
436 break;
437 }
438 }
439
440 if (!needsEscaping)
441 {
442 // fast path
443 return value;
444 }
445 else
446 {
447 // slow path with escaping
448 StringBuffer buffer = new StringBuffer(value.length() * 4 / 3);
449 for (int i = 0; i < value.length (); i++)
450 {
451 char c = value.charAt (i);
452 if (!(escapeWhitespaces && (c == '\t' || c == '\n' || c == '\r')))
453 {
454 switch (c)
455 {
456 // we do what "Canonical XML" expects
457 // AUDIT: &apos; not serialized as only outer qoutes are used
458 case '<': buffer.append("&lt;"); continue;
459 case '>': buffer.append("&gt;"); continue;
460 case '&': buffer.append("&amp;"); continue;
461 case '"': buffer.append(forAttribute ? "&quot;" : "\""); continue;
462 default: buffer.append(c); continue;
463 }
464 }
465 else
466 {
467 // write control chars escaped,
468 // if there are others than tab, LF and CR the xml will become invalid.
469 buffer.append("&#x");
470 buffer.append(Integer.toHexString(c).toUpperCase());
471 buffer.append(';');
472 }
473 }
474 return buffer.toString();
475 }
476 }
477
478
479 /**
480 * Replaces the ASCII control chars with a space.
481 *
482 * @param value
483 * a node value
484 * @return Returns the cleaned up value
485 */
486 static String removeControlChars(String value)
487 {
488 StringBuffer buffer = new StringBuffer(value);
489 for (int i = 0; i < buffer.length(); i++)
490 {
491 if (isControlChar(buffer.charAt(i)))
492 {
493 buffer.setCharAt(i, ' ');
494 }
495 }
496 return buffer.toString();
497 }
498
499
500 /**
501 * Simple check if a character is a valid XML start name char.
502 * All characters according to the XML Spec 1.1 are accepted:
503 * http://www.w3.org/TR/xml11/#NT-NameStartChar
504 *
505 * @param ch a character
506 * @return Returns true if the character is a valid first char of an XML name.
507 */
508 private static boolean isNameStartChar(char ch)
509 {
510 return
511 (ch <= 0xFF && xmlNameStartChars[ch]) ||
512 (ch >= 0x100 && ch <= 0x2FF) ||
513 (ch >= 0x370 && ch <= 0x37D) ||
514 (ch >= 0x37F && ch <= 0x1FFF) ||
515 (ch >= 0x200C && ch <= 0x200D) ||
516 (ch >= 0x2070 && ch <= 0x218F) ||
517 (ch >= 0x2C00 && ch <= 0x2FEF) ||
518 (ch >= 0x3001 && ch <= 0xD7FF) ||
519 (ch >= 0xF900 && ch <= 0xFDCF) ||
520 (ch >= 0xFDF0 && ch <= 0xFFFD) ||
521 (ch >= 0x10000 && ch <= 0xEFFFF);
522 }
523
524
525 /**
526 * Simple check if a character is a valid XML name char
527 * (every char except the first one), according to the XML Spec 1.1:
528 * http://www.w3.org/TR/xml11/#NT-NameChar
529 *
530 * @param ch a character
531 * @return Returns true if the character is a valid char of an XML name.
532 */
533 private static boolean isNameChar(char ch)
534 {
535 return
536 (ch <= 0xFF && xmlNameChars[ch]) ||
537 isNameStartChar(ch) ||
538 (ch >= 0x300 && ch <= 0x36F) ||
539 (ch >= 0x203F && ch <= 0x2040);
540 }
541
542
543 /**
544 * Initializes the char tables for the chars 0x00-0xFF for later use,
545 * according to the XML 1.1 specification
546 * http://www.w3.org/TR/xml11
547 */
548 private static void initCharTables()
549 {
550 xmlNameChars = new boolean[0x0100];
551 xmlNameStartChars = new boolean[0x0100];
552
553 for (char ch = 0; ch < xmlNameChars.length; ch++)
554 {
555 xmlNameStartChars[ch] =
556 ch == ':' ||
557 ('A' <= ch && ch <= 'Z') ||
558 ch == '_' ||
559 ('a' <= ch && ch <= 'z') ||
560 (0xC0 <= ch && ch <= 0xD6) ||
561 (0xD8 <= ch && ch <= 0xF6) ||
562 (0xF8 <= ch && ch <= 0xFF);
563
564 xmlNameChars[ch] =
565 xmlNameStartChars[ch] ||
566 ch == '-' ||
567 ch == '.' ||
568 ('0' <= ch && ch <= '9') ||
569 ch == 0xB7;
570 }
571 }
572
573 }