Debian Codegraph @

src/com/adobe/internal/xmp/impl/XMPSchemaRegistryImpl.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 import java.util.ArrayList;
13 import java.util.Collections;
14 import java.util.HashMap;
15 import java.util.Iterator;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.TreeMap;
19 import java.util.regex.Pattern;
20
21 import com.adobe.internal.xmp.XMPConst;
22 import com.adobe.internal.xmp.XMPError;
23 import com.adobe.internal.xmp.XMPException;
24 import com.adobe.internal.xmp.XMPSchemaRegistry;
25 import com.adobe.internal.xmp.options.AliasOptions;
26 import com.adobe.internal.xmp.properties.XMPAliasInfo;
27
28
29 /**
30 * The schema registry handles the namespaces, aliases and global options for the XMP Toolkit. There
31 * is only one single instance used by the toolkit.
32 *
33 * @author Stefan Makswit
34 * @version $Revision$
35 * @since 27.01.2006
36 */
37 public final class XMPSchemaRegistryImpl implements XMPSchemaRegistry, XMPConst
38 {
39 /** a map from a namespace URI to its registered prefix */
40 private Map namespaceToPrefixMap = new HashMap();
41
42 /** a map from a prefix to the associated namespace URI */
43 private Map prefixToNamespaceMap = new HashMap();
44
45 /** a map of all registered aliases.
46 * The map is a relationship from a qname to an <code>XMPAliasInfo</code>-object. */
47 private Map aliasMap = new HashMap();
48 /** The pattern that must not be contained in simple properties */
49 private Pattern p = Pattern.compile("[/*?\\[\\]]");
50
51
52 /**
53 * Performs the initialisation of the registry with the default namespaces, aliases and global
54 * options.
55 */
56 public XMPSchemaRegistryImpl()
57 {
58 try
59 {
60 registerStandardNamespaces();
61 registerStandardAliases();
62 }
63 catch (XMPException e)
64 {
65 throw new RuntimeException("The XMPSchemaRegistry cannot be initialized!");
66 }
67 }
68
69
70 // ---------------------------------------------------------------------------------------------
71 // Namespace Functions
72
73
74 /**
75 * @see XMPSchemaRegistry#registerNamespace(String, String)
76 */
77 public synchronized String registerNamespace(String namespaceURI, String suggestedPrefix)
78 throws XMPException
79 {
80 ParameterAsserts.assertSchemaNS(namespaceURI);
81 ParameterAsserts.assertPrefix(suggestedPrefix);
82
83 if (suggestedPrefix.charAt(suggestedPrefix.length() - 1) != ':')
84 {
85 suggestedPrefix += ':';
86 }
87
88 if (!Utils.isXMLNameNS(suggestedPrefix.substring(0,
89 suggestedPrefix.length() - 1)))
90 {
91 throw new XMPException("The prefix is a bad XML name", XMPError.BADXML);
92 }
93
94 String registeredPrefix = (String) namespaceToPrefixMap.get(namespaceURI);
95 String registeredNS = (String) prefixToNamespaceMap.get(suggestedPrefix);
96 if (registeredPrefix != null)
97 {
98 // Return the actual prefix
99 return registeredPrefix;
100 }
101 else
102 {
103 if (registeredNS != null)
104 {
105 // the namespace is new, but the prefix is already engaged,
106 // we generate a new prefix out of the suggested
107 String generatedPrefix = suggestedPrefix;
108 for (int i = 1; prefixToNamespaceMap.containsKey(generatedPrefix); i++)
109 {
110 generatedPrefix = suggestedPrefix
111 .substring(0, suggestedPrefix.length() - 1)
112 + "_" + i + "_:";
113 }
114 suggestedPrefix = generatedPrefix;
115 }
116 prefixToNamespaceMap.put(suggestedPrefix, namespaceURI);
117 namespaceToPrefixMap.put(namespaceURI, suggestedPrefix);
118
119 // Return the suggested prefix
120 return suggestedPrefix;
121 }
122 }
123
124
125 /**
126 * @see XMPSchemaRegistry#deleteNamespace(String)
127 */
128 public synchronized void deleteNamespace(String namespaceURI)
129 {
130 String prefixToDelete = getNamespacePrefix(namespaceURI);
131 if (prefixToDelete != null)
132 {
133 namespaceToPrefixMap.remove(namespaceURI);
134 prefixToNamespaceMap.remove(prefixToDelete);
135 }
136 }
137
138
139 /**
140 * @see XMPSchemaRegistry#getNamespacePrefix(String)
141 */
142 public synchronized String getNamespacePrefix(String namespaceURI)
143 {
144 return (String) namespaceToPrefixMap.get(namespaceURI);
145 }
146
147
148 /**
149 * @see XMPSchemaRegistry#getNamespaceURI(String)
150 */
151 public synchronized String getNamespaceURI(String namespacePrefix)
152 {
153 if (namespacePrefix != null && !namespacePrefix.endsWith(":"))
154 {
155 namespacePrefix += ":";
156 }
157 return (String) prefixToNamespaceMap.get(namespacePrefix);
158 }
159
160
161 /**
162 * @see XMPSchemaRegistry#getNamespaces()
163 */
164 public synchronized Map getNamespaces()
165 {
166 return Collections.unmodifiableMap(new TreeMap(namespaceToPrefixMap));
167 }
168
169
170 /**
171 * @see XMPSchemaRegistry#getPrefixes()
172 */
173 public synchronized Map getPrefixes()
174 {
175 return Collections.unmodifiableMap(new TreeMap(prefixToNamespaceMap));
176 }
177
178
179 /**
180 * Register the standard namespaces of schemas and types that are included in the XMP
181 * Specification and some other Adobe private namespaces.
182 * Note: This method is not lock because only called by the constructor.
183 *
184 * @throws XMPException Forwards processing exceptions
185 */
186 private void registerStandardNamespaces() throws XMPException
187 {
188 // register standard namespaces
189 registerNamespace(NS_XML, "xml");
190 registerNamespace(NS_RDF, "rdf");
191 registerNamespace(NS_DC, "dc");
192 registerNamespace(NS_IPTCCORE, "Iptc4xmpCore");
193 registerNamespace(NS_IPTCEXT, "Iptc4xmpExt");
194 registerNamespace(NS_DICOM, "DICOM");
195 registerNamespace(NS_PLUS, "plus");
196
197 // register Adobe standard namespaces
198 registerNamespace(NS_X, "x");
199 registerNamespace(NS_IX, "iX");
200
201 registerNamespace(NS_XMP, "xmp");
202 registerNamespace(NS_XMP_RIGHTS, "xmpRights");
203 registerNamespace(NS_XMP_MM, "xmpMM");
204 registerNamespace(NS_XMP_BJ, "xmpBJ");
205 registerNamespace(NS_XMP_NOTE, "xmpNote");
206
207 registerNamespace(NS_PDF, "pdf");
208 registerNamespace(NS_PDFX, "pdfx");
209 registerNamespace(NS_PDFX_ID, "pdfxid");
210 registerNamespace(NS_PDFA_SCHEMA, "pdfaSchema");
211 registerNamespace(NS_PDFA_PROPERTY, "pdfaProperty");
212 registerNamespace(NS_PDFA_TYPE, "pdfaType");
213 registerNamespace(NS_PDFA_FIELD, "pdfaField");
214 registerNamespace(NS_PDFA_ID, "pdfaid");
215 registerNamespace(NS_PDFA_EXTENSION, "pdfaExtension");
216 registerNamespace(NS_PHOTOSHOP, "photoshop");
217 registerNamespace(NS_PSALBUM, "album");
218 registerNamespace(NS_EXIF, "exif");
219 registerNamespace(NS_EXIFX, "exifEX");
220 registerNamespace(NS_EXIF_AUX, "aux");
221 registerNamespace(NS_TIFF, "tiff");
222 registerNamespace(NS_PNG, "png");
223 registerNamespace(NS_JPEG, "jpeg");
224 registerNamespace(NS_JP2K, "jp2k");
225 registerNamespace(NS_CAMERARAW, "crs");
226 registerNamespace(NS_ADOBESTOCKPHOTO, "bmsp");
227 registerNamespace(NS_CREATOR_ATOM, "creatorAtom");
228 registerNamespace(NS_ASF, "asf");
229 registerNamespace(NS_WAV, "wav");
230 registerNamespace(NS_BWF, "bext");
231 registerNamespace(NS_RIFFINFO, "riffinfo");
232 registerNamespace(NS_SCRIPT, "xmpScript");
233 registerNamespace(NS_TXMP, "txmp");
234 registerNamespace(NS_SWF, "swf");
235 registerNamespace(NS_CCV, "ccv");
236
237 // register Adobe private namespaces
238 registerNamespace(NS_DM, "xmpDM");
239 registerNamespace(NS_TRANSIENT, "xmpx");
240 // <#AdobePrivate>
241 // The Plain XMP format is disabled
242 // registerNamespace(NS_PXMP, "pxmp");
243 // </#AdobePrivate>
244
245 // register Adobe standard type namespaces
246 registerNamespace(TYPE_TEXT, "xmpT");
247 registerNamespace(TYPE_PAGEDFILE, "xmpTPg");
248 registerNamespace(TYPE_GRAPHICS, "xmpG");
249 registerNamespace(TYPE_IMAGE, "xmpGImg");
250 registerNamespace(TYPE_FONT, "stFnt");
251 registerNamespace(TYPE_DIMENSIONS, "stDim");
252 registerNamespace(TYPE_RESOURCEEVENT, "stEvt");
253 registerNamespace(TYPE_RESOURCEREF, "stRef");
254 registerNamespace(TYPE_ST_VERSION, "stVer");
255 registerNamespace(TYPE_ST_JOB, "stJob");
256 registerNamespace(TYPE_MANIFESTITEM, "stMfs");
257 registerNamespace(TYPE_IDENTIFIERQUAL, "xmpidq");
258 }
259
260
261
262 // ---------------------------------------------------------------------------------------------
263 // Alias Functions
264
265
266 /**
267 * @see XMPSchemaRegistry#resolveAlias(String, String)
268 */
269 public synchronized XMPAliasInfo resolveAlias(String aliasNS, String aliasProp)
270 {
271 String aliasPrefix = getNamespacePrefix(aliasNS);
272 if (aliasPrefix == null)
273 {
274 return null;
275 }
276
277 return (XMPAliasInfo) aliasMap.get(aliasPrefix + aliasProp);
278 }
279
280
281 /**
282 * @see XMPSchemaRegistry#findAlias(java.lang.String)
283 */
284 public synchronized XMPAliasInfo findAlias(String qname)
285 {
286 return (XMPAliasInfo) aliasMap.get(qname);
287 }
288
289
290 /**
291 * @see XMPSchemaRegistry#findAliases(String)
292 */
293 public synchronized XMPAliasInfo[] findAliases(String aliasNS)
294 {
295 String prefix = getNamespacePrefix(aliasNS);
296 List result = new ArrayList();
297 if (prefix != null)
298 {
299 for (Iterator it = aliasMap.keySet().iterator(); it.hasNext();)
300 {
301 String qname = (String) it.next();
302 if (qname.startsWith(prefix))
303 {
304 result.add(findAlias(qname));
305 }
306 }
307
308 }
309 return (XMPAliasInfo[]) result.toArray(new XMPAliasInfo[result.size()]);
310 }
311
312
313 /**
314 * Associates an alias name with an actual name.
315 * <p>
316 * Define a alias mapping from one namespace/property to another. Both
317 * property names must be simple names. An alias can be a direct mapping,
318 * where the alias and actual have the same data type. It is also possible
319 * to map a simple alias to an item in an array. This can either be to the
320 * first item in the array, or to the 'x-default' item in an alt-text array.
321 * Multiple alias names may map to the same actual, as long as the forms
322 * match. It is a no-op to reregister an alias in an identical fashion.
323 * Note: This method is not locking because only called by registerStandardAliases
324 * which is only called by the constructor.
325 * Note2: The method is only package-private so that it can be tested with unittests
326 *
327 * @param aliasNS
328 * The namespace URI for the alias. Must not be null or the empty
329 * string.
330 * @param aliasProp
331 * The name of the alias. Must be a simple name, not null or the
332 * empty string and not a general path expression.
333 * @param actualNS
334 * The namespace URI for the actual. Must not be null or the
335 * empty string.
336 * @param actualProp
337 * The name of the actual. Must be a simple name, not null or the
338 * empty string and not a general path expression.
339 * @param aliasForm
340 * Provides options for aliases for simple aliases to array
341 * items. This is needed to know what kind of array to create if
342 * set for the first time via the simple alias. Pass
343 * <code>XMP_NoOptions</code>, the default value, for all
344 * direct aliases regardless of whether the actual data type is
345 * an array or not (see {@link AliasOptions}).
346 * @throws XMPException
347 * for inconsistant aliases.
348 */
349 synchronized void registerAlias(String aliasNS, String aliasProp, final String actualNS,
350 final String actualProp, final AliasOptions aliasForm) throws XMPException
351 {
352 ParameterAsserts.assertSchemaNS(aliasNS);
353 ParameterAsserts.assertPropName(aliasProp);
354 ParameterAsserts.assertSchemaNS(actualNS);
355 ParameterAsserts.assertPropName(actualProp);
356
357 // FfF: if we need the decoration with [1] or
358 // FfF: [?xml:lang="x-default"] for array forms
359
360 // Fix the alias options
361 final AliasOptions aliasOpts = aliasForm != null ?
362 new AliasOptions(XMPNodeUtils.verifySetOptions(
363 aliasForm.toPropertyOptions(), null).getOptions()) :
364 new AliasOptions();
365
366 if (p.matcher(aliasProp).find() || p.matcher(actualProp).find())
367 {
368 throw new XMPException("Alias and actual property names must be simple",
369 XMPError.BADXPATH);
370 }
371
372 // check if both namespaces are registered
373 final String aliasPrefix = getNamespacePrefix(aliasNS);
374 final String actualPrefix = getNamespacePrefix(actualNS);
375 if (aliasPrefix == null)
376 {
377 throw new XMPException("Alias namespace is not registered", XMPError.BADSCHEMA);
378 }
379 else if (actualPrefix == null)
380 {
381 throw new XMPException("Actual namespace is not registered",
382 XMPError.BADSCHEMA);
383 }
384
385 String key = aliasPrefix + aliasProp;
386
387 // check if alias is already existing
388 if (aliasMap.containsKey(key))
389 {
390 throw new XMPException("Alias is already existing", XMPError.BADPARAM);
391 }
392 else if (aliasMap.containsKey(actualPrefix + actualProp))
393 {
394 throw new XMPException(
395 "Actual property is already an alias, use the base property",
396 XMPError.BADPARAM);
397 }
398
399 XMPAliasInfo aliasInfo = new XMPAliasInfo()
400 {
401 /**
402 * @see XMPAliasInfo#getNamespace()
403 */
404 public String getNamespace()
405 {
406 return actualNS;
407 }
408
409 /**
410 * @see XMPAliasInfo#getPrefix()
411 */
412 public String getPrefix()
413 {
414 return actualPrefix;
415 }
416
417 /**
418 * @see XMPAliasInfo#getPropName()
419 */
420 public String getPropName()
421 {
422 return actualProp;
423 }
424
425 /**
426 * @see XMPAliasInfo#getAliasForm()
427 */
428 public AliasOptions getAliasForm()
429 {
430 return aliasOpts;
431 }
432
433 public String toString()
434 {
435 return actualPrefix + actualProp + " NS(" + actualNS + "), FORM ("
436 + getAliasForm() + ")";
437 }
438 };
439
440 aliasMap.put(key, aliasInfo);
441 }
442
443
444 /**
445 * @see XMPSchemaRegistry#getAliases()
446 */
447 public synchronized Map getAliases()
448 {
449 return Collections.unmodifiableMap(new TreeMap(aliasMap));
450 }
451
452
453 /**
454 * Register the standard aliases.
455 * Note: This method is not lock because only called by the constructor.
456 *
457 * @throws XMPException If the registrations of at least one alias fails.
458 */
459 private void registerStandardAliases() throws XMPException
460 {
461 AliasOptions aliasToArrayOrdered = new AliasOptions().setArrayOrdered(true);
462 AliasOptions aliasToArrayAltText = new AliasOptions().setArrayAltText(true);
463
464
465 // Aliases from XMP to DC.
466 registerAlias(NS_XMP, "Author", NS_DC, "creator", aliasToArrayOrdered);
467 registerAlias(NS_XMP, "Authors", NS_DC, "creator", null);
468 registerAlias(NS_XMP, "Description", NS_DC, "description", null);
469 registerAlias(NS_XMP, "Format", NS_DC, "format", null);
470 registerAlias(NS_XMP, "Keywords", NS_DC, "subject", null);
471 registerAlias(NS_XMP, "Locale", NS_DC, "language", null);
472 registerAlias(NS_XMP, "Title", NS_DC, "title", null);
473 registerAlias(NS_XMP_RIGHTS, "Copyright", NS_DC, "rights", null);
474
475 // Aliases from PDF to DC and XMP.
476 registerAlias(NS_PDF, "Author", NS_DC, "creator", aliasToArrayOrdered);
477 registerAlias(NS_PDF, "BaseURL", NS_XMP, "BaseURL", null);
478 registerAlias(NS_PDF, "CreationDate", NS_XMP, "CreateDate", null);
479 registerAlias(NS_PDF, "Creator", NS_XMP, "CreatorTool", null);
480 registerAlias(NS_PDF, "ModDate", NS_XMP, "ModifyDate", null);
481 registerAlias(NS_PDF, "Subject", NS_DC, "description", aliasToArrayAltText);
482 registerAlias(NS_PDF, "Title", NS_DC, "title", aliasToArrayAltText);
483
484 // Aliases from PHOTOSHOP to DC and XMP.
485 registerAlias(NS_PHOTOSHOP, "Author", NS_DC, "creator", aliasToArrayOrdered);
486 registerAlias(NS_PHOTOSHOP, "Caption", NS_DC, "description", aliasToArrayAltText);
487 registerAlias(NS_PHOTOSHOP, "Copyright", NS_DC, "rights", aliasToArrayAltText);
488 registerAlias(NS_PHOTOSHOP, "Keywords", NS_DC, "subject", null);
489 registerAlias(NS_PHOTOSHOP, "Marked", NS_XMP_RIGHTS, "Marked", null);
490 registerAlias(NS_PHOTOSHOP, "Title", NS_DC, "title", aliasToArrayAltText);
491 registerAlias(NS_PHOTOSHOP, "WebStatement", NS_XMP_RIGHTS, "WebStatement", null);
492
493 // Aliases from TIFF and EXIF to DC and XMP.
494 registerAlias(NS_TIFF, "Artist", NS_DC, "creator", aliasToArrayOrdered);
495 registerAlias(NS_TIFF, "Copyright", NS_DC, "rights", null);
496 registerAlias(NS_TIFF, "DateTime", NS_XMP, "ModifyDate", null);
497 registerAlias(NS_EXIF, "DateTimeDigitized", NS_XMP, "CreateDate", null );
498 registerAlias(NS_TIFF, "ImageDescription", NS_DC, "description", null);
499 registerAlias(NS_TIFF, "Software", NS_XMP, "CreatorTool", null);
500
501 // Aliases from PNG (Acrobat ImageCapture) to DC and XMP.
502 registerAlias(NS_PNG, "Author", NS_DC, "creator", aliasToArrayOrdered);
503 registerAlias(NS_PNG, "Copyright", NS_DC, "rights", aliasToArrayAltText);
504 registerAlias(NS_PNG, "CreationTime", NS_XMP, "CreateDate", null);
505 registerAlias(NS_PNG, "Description", NS_DC, "description", aliasToArrayAltText);
506 registerAlias(NS_PNG, "ModificationTime", NS_XMP, "ModifyDate", null);
507 registerAlias(NS_PNG, "Software", NS_XMP, "CreatorTool", null);
508 registerAlias(NS_PNG, "Title", NS_DC, "title", aliasToArrayAltText);
509 }
510 }