-
Notifications
You must be signed in to change notification settings - Fork 0
/
Warehouse.java
1640 lines (1437 loc) · 62.8 KB
/
Warehouse.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2016 Richard Cartwright
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* $Log: Warehouse.java,v $
* Revision 1.8 2011/10/05 17:14:25 vizigoth
* Added support for application metadata plugins, package markers and dynamic metadictionary extraction from AAF files.
*
* Revision 1.7 2011/07/27 12:25:44 vizigoth
* Fixed import warning messages.
*
* Revision 1.6 2011/02/14 22:32:49 vizigoth
* First commit after major sourceforge outage.
*
* Revision 1.5 2011/01/24 14:02:24 vizigoth
* Made property global lookup table public.
*
* Revision 1.4 2011/01/21 17:40:34 vizigoth
* Moved initialization of AAF extendible enumerations and types into the media engine and added public access to the class name aliases.
*
* Revision 1.3 2011/01/19 11:42:36 vizigoth
* Fixes to issues found when writing tests.
*
* Revision 1.2 2011/01/18 09:11:50 vizigoth
* Fixes after writing unit tests.
*
* Revision 1.1 2011/01/13 17:44:26 vizigoth
* Major refactor of the industrial area and improved front-end documentation.
*
*/
package tv.amwa.maj.industry;
import static tv.amwa.maj.io.mxf.MXFConstants.RP210_NAMESPACE;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
import tv.amwa.maj.constant.CodingEquationsType;
import tv.amwa.maj.constant.ColorPrimariesType;
import tv.amwa.maj.constant.CommonConstants;
import tv.amwa.maj.constant.OperationCategoryType;
import tv.amwa.maj.constant.PluginCategoryType;
import tv.amwa.maj.constant.TransferCharacteristicType;
import tv.amwa.maj.constant.UsageType;
import tv.amwa.maj.exception.PropertyNotPresentException;
import tv.amwa.maj.meta.ClassDefinition;
import tv.amwa.maj.meta.ExtensionScheme;
import tv.amwa.maj.meta.MetaDefinition;
import tv.amwa.maj.meta.PropertyDefinition;
import tv.amwa.maj.meta.TypeDefinition;
import tv.amwa.maj.meta.TypeDefinitionObjectReference;
import tv.amwa.maj.meta.TypeDefinitionSet;
import tv.amwa.maj.meta.TypeDefinitionVariableArray;
import tv.amwa.maj.meta.impl.ExtensionSchemeImpl;
import tv.amwa.maj.meta.impl.MetaDefinitionImpl;
import tv.amwa.maj.model.ApplicationObject;
import tv.amwa.maj.model.DataDefinition;
import tv.amwa.maj.model.DefinitionObject;
import tv.amwa.maj.record.AUID;
import tv.amwa.maj.record.impl.AUIDImpl;
/**
* <p>Warehouse with a dynamic inventory of {@linkplain ClassDefinition classes},
* {@linkplain TypeDefinition types}, {@linkplain DefinitionObject definitions} and
* extendible enumeration items. The warehouse is the place to come looking for any
* {@linkplain WeakReference weak referenced} item, by name or identification, known
* to the virtual machine containing the class. Also, new items can be added to the
* inventory of the warehouse at runtime, enabling dynamic and loosely-coupled
* use of the API.</p>
*
* <p>The warehouse is not the place to come to make new values or instantiate objects, or
* to manipulate and test values. Use the {@linkplain Forge forge} to make new things
* and the {@linkplain MediaEngine media engine} to change them.</p>
*
* <p>To find something in the inventory of the warehouse, use:</p>
*
* <ul>
* <li><strong>{@linkplain ClassDefinition media classes}</strong> - Look for a class definition by
* its Java interface, implementing class, name, Java class name or identification with
* {@link #lookForClass(Class)} / {@link #lookForClass(String)} /
* {@link #lookForClass(AUID)}. Note that {@linkplain tv.amwa.maj.meta.PropertyDefinition property
* definitions} are accessible as part of the class that they are a member of.</li>
*
* <li><strong>{@linkplain TypeDefinitions media types}</strong> - Look for a type definition by
* its name or identification with {@link #lookForType(String)} /
* {@link #lookForType(AUID)}.</li>
*
* <li><strong>{@linkplain DefinitionObject definitions}</strong> - Look for a definition by
* its Java interface/implementation or name and the definition name or identification
* with {@link #lookup(Class, String)} / {@link #lookup(Class, AUID)} /
* {@link #lookup(String, String)} / {@link #lookup(String, AUID)}.</li>
*
* <li><strong>{@linkplain ExtendibleEnumerationItem extendible enumerations}</strong> - Look for an
* extendible enumeration by its name with {@link #lookupExtendibleEnumeration(String)} or
* use other methods to query details of the elements of an extendible enumeration:
* {@link #extendibleEnumerationName(AUID)}, {@link #extendibleEnumerationElementName(AUID)},
* {@link #extendibleEnumerationElementValue(String)}, {@link #extendibleEnumerationFullName(AUID)}.</li>
* </ul>
*
* <p>To register to items onto the inventory of the warehouse, use:</p>
*
* <ul>
*
* <li><strong>{@linkplain ClassDefinition media classes}</strong> - The {@link #lookForClass(Class)}
* and {@link #lookForClass(String)} methods will automatically register an unknown class specified
* using an appropriately annotated Java class.</li>
*
* <li><strong>{@linkplain TypeDefinitions media types}</strong> - Use {@link #register(TypeDefinition, String, String)}
* or add a collection of type definitions from static values of a class using
* {@link #registerTypes(Class, String, String)}.</li>
*
* <li><strong>{@linkplain DefinitionObject definitions}</strong> - Use
* {@link #register(DefinitionObject)}.</li>
*
* <li><strong>{@linkplain ExtendibleEnumerationItem extendible enumerations}</strong> - Add new
* extendible enumerations element by element using
* {@link #registerExtendibleEnumerationElement(String, String, AUID)} or from static values of a
* class with {@linkplain ExtendibleEnumerationItem extendible enumeration item annotations}
* using {@link #registerExtendibleEnumerationElements(Class)}.</li>
*
* </ul>
*
* <p>To query the current inventory of the warehouse, lists and counts of the various names and types
* of item are provided:</p>
*
* <ul>
*
* <li><strong>{@linkplain ClassDefinition media classes}</strong> - {@link #getClassInventory()}
* and {@link #countClassDefinitions()}.</li>
*
* <li><strong>{@linkplain TypeDefinitions media types}</strong> - {@link #getTypeInventory()},
* {@link #getTypeDefinitions()} and {@link #countTypeDefinitions()}.</li>
*
* <li><strong>{@linkplain DefinitionObject definitions}</strong> - Generic methods
* {@link #count(Class)} and {@link #inventory(Class)}.</li>
*
* <li><strong>{@linkplain ExtendibleEnumerationItem extendible enumerations}</strong> -
* {@link #getExtendibleEnumerationInventory()} and {@link #countExtendibleEnumerations()}.</li>
* </ul>
*
*
*
* @see TypeDefinitions
* @see tv.amwa.maj.model.Dictionary
* @see tv.amwa.maj.meta.TypeDefinitionExtendibleEnumeration
* @see ExtendibleEnumerationItem
* @see MediaClass
* @see tv.amwa.maj.meta.ClassDefinition
* @see tv.amwa.maj.meta.TypeDefinition
*/
public final class Warehouse {
// This is the authoritative list of class definitions known to this MAJ instance
private static final Map<AUID, ClassDefinition> idToClass =
Collections.synchronizedMap(new HashMap<AUID, ClassDefinition>());
private static Map<Class<?>, ClassDefinition> knownClasses =
Collections.synchronizedMap(new HashMap<Class<?>, ClassDefinition>());
private static Map<String, ClassDefinition> nameToClass =
Collections.synchronizedMap(new HashMap<String, ClassDefinition>());
private static HashMap<String, String> classAliases = new HashMap<String, String>();
private static final Map<AUID, PropertyDefinition> globalPropertyIdTable =
Collections.synchronizedMap(new HashMap<AUID, PropertyDefinition>());
private static Map<String, TypeDefinition> knownTypes =
Collections.synchronizedMap(new HashMap<String, TypeDefinition>());
private static Map<AUID, TypeDefinition> knownTypesByID =
Collections.synchronizedMap(new HashMap<AUID, TypeDefinition>());
static final Class<?>[] typeDefinitionClasses = new Class<?>[] {
TypeDefinitions.class
};
private static Map<String, TreeMap<String, AUID>> extensibleEnumerations =
new HashMap<String, TreeMap<String, AUID>>();
private static Map<AUID, String> idToEnumeration =
new HashMap<AUID, String>();
private static Map<AUID, String> idToElementName =
new HashMap<AUID, String>();
final static Class<?>[] extensibleEnumerationClasses = new Class<?>[] {
OperationCategoryType.class,
PluginCategoryType.class,
TransferCharacteristicType.class,
UsageType.class,
ColorPrimariesType.class,
CodingEquationsType.class
};
static class ExtensionSchemeRecord {
public ExtensionSchemeRecord(
ExtensionScheme extensionScheme) {
schemeID = extensionScheme.getSchemeID();
schemeURI = extensionScheme.getSchemeURI();
try {
preferredPrefix = extensionScheme.getPreferredPrefix();
}
catch (PropertyNotPresentException pnpe) { }
try {
extensionDescription = extensionScheme.getExtensionDescription();
}
catch (PropertyNotPresentException pnpe) { }
}
AUID schemeID;
String schemeURI;
String preferredPrefix = null;
String extensionDescription = null;
public int hashCode() {
return schemeID.hashCode();
}
}
private static Map<AUID, ExtensionSchemeRecord> extensionSchemes =
Collections.synchronizedMap(new HashMap<AUID, ExtensionSchemeRecord>());
public final static void clear() {
knownClasses.clear();
nameToClass.clear();
knownTypes.clear();
knownTypesByID.clear();
extensibleEnumerations.clear();
idToEnumeration.clear();
idToElementName.clear();
idToClass.clear();
globalPropertyIdTable.clear();
}
static {
classAliases.put("Timecode", "TimecodeSegment");
classAliases.put("Edgecode", "EdgeCodeSegment");
classAliases.put("EdgeCode", "EdgeCodeSegment");
classAliases.put("File", "AAFFile");
classAliases.put("FileDescriptor", "AAFFileDescriptor");
classAliases.put("SourceReference", "SourceReferenceSegment");
classAliases.put("AAFObject", "InterchangeObject");
classAliases.put("Object", "InterchangeObject");
}
private Warehouse() {}
/**
* <p>For any given Java class, this method finds the corresponding media class definition or
* creates it if it does not yet exist within the current Java virtual machine. The creation
* of class definitions is done by lazy evaluation as required using Java reflection and annotations.
* All generated values are stored in the warehouse inventory, a static hashtable, so that once
* generated the work is not repeated.</p>
*
* <p>The values returned by this method are only as good as the annotations provided with
* the sourcecode, as labelled using {@link tv.amwa.maj.industry.MediaClass} and {@link tv.amwa.maj.industry.MediaProperty}.
* If an {@link tv.amwa.maj.industry.MediaClass} annotation is not present in the given class, an {@link IllegalArgumentException} is thrown. Any
* problems found with the {@link tv.amwa.maj.industry.MediaProperty} annotations, such as two properties having the same
* name, will also result in an {@link IllegalArgumentException}.</p>
*
* @param mediaClass Java class to find the media class definition of.
* @return Media class definition associated with the given Java class.
*
* @throws NullPointerException The given Java class is <code>null</code>.
* @throws IllegalArgumentException The given Java class is not annotated with {@link MediaClass} or
* the {@link MediaProperty} annotations contain errors.
*
* @see tv.amwa.maj.industry.MediaClass
* @see tv.amwa.maj.industry.MediaProperty
* @see MediaEngine#initializeAAF()
* @see MediaEngine#getClassDefinition(MetadataObject)
*/
public final static ClassDefinition lookForClass(
Class<?> mediaClass)
throws NullPointerException,
IllegalArgumentException {
if (mediaClass == null)
throw new NullPointerException("Cannot make a class definition from a null value.");
if ((!mediaClass.isInterface()) && (mediaClass.getCanonicalName().contains("$")))
mediaClass = mediaClass.getSuperclass();
if (knownClasses.containsKey(mediaClass))
return knownClasses.get(mediaClass);
// If this is the first time the interface has been seen, try and find the class by name
if (mediaClass.isInterface()) {
return lookForClass(mediaClass.getName());
}
ClassDefinition classDef = tv.amwa.maj.meta.impl.ClassDefinitionImpl.forClass(mediaClass);
// Note: Class def is registered in ClassDefinitionImpl
knownClasses.put(mediaClass, classDef);
for ( Class<?> implementing : mediaClass.getInterfaces() )
if (!implementing.getCanonicalName().startsWith("java"))
knownClasses.put(implementing, classDef);
return classDef;
}
public final static void register(
ClassDefinition classDefinition)
throws NullPointerException {
if (classDefinition == null)
throw new NullPointerException("Cannot register a class definition using a null value.");
idToClass.put(classDefinition.getAUID(), classDefinition);
nameToClass.put(classDefinition.getName(), classDefinition);
try {
if (classDefinition.getJavaImplementation() != null) {
if (!(ApplicationObject.class.isAssignableFrom(classDefinition.getJavaImplementation())))
nameToClass.put(classDefinition.getJavaImplementation().getCanonicalName(), classDefinition);
}
nameToClass.put(classDefinition.getSymbol(), classDefinition);
if ((classDefinition.getNamespace() != null) && (classDefinition.getNamespace().length() > 0)) {
nameToClass.put("{" + classDefinition.getNamespace() + "}" + classDefinition.getSymbol(), classDefinition);
nameToClass.put("{" + classDefinition.getNamespace() + "}" + classDefinition.getName(), classDefinition);
}
putAliases(classDefinition);
} catch (PropertyNotPresentException e) {
// No worry.
}
}
/**
* <p>Put class definitions into the class name lookup table only if they do not overwrite
* the defined name or symbol name of another class.</p>
*
* @param severalNames The property definition to check.
*/
private static void putAliases(
ClassDefinition severalNames) {
if (severalNames == null) return;
if (severalNames.getAliases() == null) return;
for ( String alias : severalNames.getAliases() ) {
if (nameToClass.containsKey(alias)) {
ClassDefinition checkBeforeRemoving = nameToClass.get(alias);
if ((alias.equals(checkBeforeRemoving.getName())) ||
(alias.equals(checkBeforeRemoving.getSymbol()))) {
System.err.println("Warning: Cannot use alias " + alias + " for class " +
severalNames.getName() + " because it clashes with another property name or symbol.");
continue;
}
}
classAliases.put(alias, "{" + severalNames.getNamespace() + "}" + severalNames.getSymbol());
classAliases.put("{" + severalNames.getNamespace() + "}" + alias, "{" + severalNames.getNamespace() + "}" + severalNames.getSymbol());
}
}
/**
* <p>Finds and returns a class definition for the given media class name. Firstly, the search takes place using
* the class name specified in the AAF object specification and then by using a Java class name.</p>
*
* <p>The search order used by this method is:</p>
*
* <ol>
* <li>Is the name an alias used in Java for an AAF class name in the specification? In which case,
* substitute the given Java-like name with the AAF alias. Look for the class in the current cache and,
* if present, return it.</li>
* <li>If the given class name already contains a path separator, use it directly for a call to
* {@link java.lang.Class#forName(String)} followed by
* {@link #lookForClass(Class)} with the result. If the given class name represents an interface,
* an attempt is made to find the implementation by naming convention. Return the value.</li>
* <li>If the class name contains no path separator character, prepend MAJ API package names to the
* front of it and call {@link java.lang.Class#forName(String)} followed by
* {@link #lookForClass(Class)} with the result. Return the value.</li>
* </ol>
*
* <p>Once the class has been located and linked into the running virtual machine, the {@link #lookForClass(Class)}
* method is called.</p>
*
* @param name Name of the class definition to find, specified using a name from the AAF object specification,
* a MAJ API class name or a fully qualified java class name.
* @return Class definition for the given name.
*
* @throws NullPointerException The given class name is <code>null</code>.
* @throws IllegalArgumentException The given class name could not be resolved to an AAF class.
*
* @see java.lang.Class#forName(java.lang.String)
*/
public final static ClassDefinition lookForClass(
String name)
throws NullPointerException,
IllegalArgumentException {
if (name == null)
throw new NullPointerException("Cannot retrieve a class definition from a null name.");
if (classAliases.containsKey(name)) name = classAliases.get(name);
ClassDefinition fromName = nameToClass.get(name);
if (fromName != null)
return fromName;
Class<?> guess;
String guessedName = name;
if (name.indexOf('.') >= 0) {
try {
guess = Class.forName(guessedName);
if (guess.isInterface()) {
int lastDot = name.lastIndexOf('.');
guessedName = name.substring(0, lastDot) + ".impl" +
name.substring(lastDot) + "Impl";
guess = Class.forName(guessedName);
}
return lookForClass(guess);
}
catch (Exception e) {
throw new IllegalArgumentException("Unable to find a media class with full pathname " + name + ".", e);
}
}
try {
guessedName = "tv.amwa.maj.model.impl." + name + "Impl";
guess = Class.forName(guessedName);
if (!guessedName.equals(name))
return lookForClass(guess);
else
throw new IllegalArgumentException("Unable to find a media class called " + name);
}
catch (ClassNotFoundException cnfe) {
try {
guessedName = name;
guessedName = "tv.amwa.maj.meta.impl." + name + "Impl";
guess = Class.forName(guessedName);
return lookForClass(guess);
}
catch (Exception e) {
throw new IllegalArgumentException("Unable to find a media class called " + name + ": " + e.getClass().getName() + ": " + e.getMessage());
}
}
catch (Exception e) {
throw new IllegalArgumentException("Unable to find a media class caled " + name + ": "+ e.getClass().getName() + ": " + e.getMessage());
}
}
/**
* <p>Returns a collection of all classes known in this warehouse, by name.</p>
*
* <p>Note this methods excludes some classes registered only for the mechanics of reading
* and writing MXF files.</p>
*
* @return Collection of all classes known in this warehouse, by name.
*
* @see #countClassDefinitions()
*/
public final static Collection<String> getClassInventory() {
SortedSet<String> classNames = new TreeSet<String>();
for ( ClassDefinition currentClass : getClassDefinitions() )
classNames.add(currentClass.getName());
return classNames;
}
public final static Collection<ClassDefinition> getClassDefinitions() {
List<ClassDefinition> classDefintions = new ArrayList<ClassDefinition>();
for(ClassDefinition classDefinition: idToClass.values()) {
// Excluding the MXF incidental stuff
if(!classDefinition.getNamespace().equals(RP210_NAMESPACE)) {
classDefintions.add(classDefinition);
}
}
return classDefintions;
}
/**
* <p>Count of the number of different kinds of class in this warehouse.</p>
*
* <p>Note that this method includes a count of classes registered for the mechanics
* of reading and writing MXF files. To exclude these from the count, use:</p>
*
* <center>{@link #getClassInventory()}<code>.size()</code></center>
*
* @return Count of the number of different kinds of class in this warehouse.
*
* @see #getClassInventory()
*/
public final static int countClassDefinitions() {
return idToClass.size();
}
/**
* <p>Looks up and returns the {@linkplain ClassDefinition class definition} in this warehouse
* with the given identification. If no class definition with the given identification can be
* found, a value of <code>null</code> is returned.</p>
*
* @param identification Identifier for the class.
* @return Instance of class definition representing the class.
*
* @throws NullPointerException Cannot resolve a registered class definition using a <code>null</code>
* value.
*/
public final static ClassDefinition lookForClass(
AUID identification)
throws NullPointerException {
if (identification == null)
throw new NullPointerException("Cannot resolve a registered class definition using a null value.");
byte[] classIdBytes = identification.getAUIDValue();
AUID originalAUID = identification;
if (classIdBytes[13] == 0x53) { // If a local set ID, convert to a class or element instance key
classIdBytes[13] = 0x06;
identification = new AUIDImpl(classIdBytes);
}
if(identification.toString().contains("0d010401.02020000")) {
System.out.println("Found it");
}
ClassDefinition def = idToClass.get(identification);
return def;
}
/**
* <p>Looks up a {@linkplain tv.amwa.maj.model.DefinitionObject definition} by its type and its name
* in the inventory of the warehouse. The definition only has to be known to the Java local machine and
* is not necessarily stored in an AAF {@linkplain tv.amwa.maj.model.Dictionary dictionary}.</p>
*
* <p>For example, to find the correct kind of {@linkplain DataDefinition data definition} for a picture,
* call:</p>
*
* <p>{@code DataDefinition pictureData = Warehouse.lookup(DataDefinition.class, "Picture");}</p>
*
* @param <T> All kinds of {@linkplain DefinitionObject definition objects}.
* @param definitionType Specific kind of definition required.
* @param definitionName Name of the definition of the given kind to find.
* @return Definition corresponding to the given type and name, or <code>null</code> if the
* definition name is not known for the given type.
*
* @throws NullPointerException One or both of the given definition type or definition name
* is/are <code>null</code>.
*
* @see #lookup(Class, AUID)
* @see #lookup(String, AUID)
* @see #lookup(String, String)
* @see WeakReference
* @see tv.amwa.maj.model.Dictionary
*/
@SuppressWarnings("unchecked")
public final static <T extends DefinitionObject> T lookup(
Class<T> definitionType,
String definitionName)
throws NullPointerException {
if (definitionType == null)
throw new NullPointerException("Cannot lookup a definition from a null definition type.");
if (definitionName == null)
throw new NullPointerException("Cannot lookup a definition from a null name.");
if (definitionType.isInterface()) {
ClassDefinition correspondingClass = lookForClass(definitionType);
definitionType = (Class<T>) correspondingClass.getJavaImplementation();
}
try {
Method forName = definitionType.getMethod("forName", String.class);
return (T) forName.invoke(null, definitionName);
}
catch (Exception e) {
return null;
}
}
/**
* <p>Looks up a {@linkplain tv.amwa.maj.model.DefinitionObject definition} by its type and its identification
* in the inventory of the warehouse. The definition only has to be known to the Java local machine and
* is not necessarily stored in an AAF {@linkplain tv.amwa.maj.model.Dictionary dictionary}.</p>
*
* <p>For example, to find the correct kind of {@linkplain DataDefinition data definition} for a picture,
* call:</p>
*
* <pre>{@code
* DataDefinition pictureData = Warehouse.lookup(
* tv.amwa.maj.model.DataDefinition.class,
* Forge.parseAUID("urn:smpte:ul:060x0e2b34.04010101.01030202.01000000"));}</pre>
*
* @param <T> All kinds of {@linkplain DefinitionObject definition objects}.
* @param definitionType Specific kind of definition required.
* @param definitionID Identification of the definition of the given kind to find.
* @return Definition corresponding to the given type and identification, or <code>null</code> if the
* definition name is not known for the given type.
*
* @throws NullPointerException One or both of the given definition type or definition name
* is/are <code>null</code>.
*
* @see #lookup(Class, String)
* @see #lookup(String, AUID)
* @see #lookup(String, String)
* @see WeakReference
* @see tv.amwa.maj.model.Dictionary
*/
@SuppressWarnings("unchecked")
public final static <T extends DefinitionObject> T lookup(
Class<T> definitionType,
AUID definitionID)
throws NullPointerException {
if (definitionType == null)
throw new NullPointerException("Cannot lookup a definition from a null definition type.");
if (definitionID== null)
throw new NullPointerException("Cannot lookup a definition from a null identification.");
if (definitionType.isInterface()) {
ClassDefinition correspondingClass = lookForClass(definitionType);
definitionType = (Class<T>) correspondingClass.getJavaImplementation();
}
try {
Method forName = definitionType.getMethod("forIdentification", AUID.class);
return (T) forName.invoke(null, definitionID);
}
catch (Exception e) {
return null;
}
}
/**
* <p>Looks up a {@linkplain tv.amwa.maj.model.DefinitionObject definition} by its type name and its name
* in the inventory of the warehouse. The definition only has to be known to the Java local machine and
* is not necessarily stored in an AAF {@linkplain tv.amwa.maj.model.Dictionary dictionary}.</p>
*
* <p>For example, to find the correct kind of {@linkplain DataDefinition data definition} for a picture,
* call:</p>
*
* <p>{@code DataDefinition pictureData = Warehouse.lookup("DataDefinition", "Picture");}</p>
*
* @param <T> All kinds of {@linkplain DefinitionObject definition objects}.
* @param definitionTypeName Name of the specific kind of definition required.
* @param definitionName Name of the definition of the given kind to find.
* @return Definition corresponding to the given type name and name, or <code>null</code> if the
* definition name is not known for the given type.
*
* @throws NullPointerException One or both of the given definition type or definition name
* is/are <code>null</code>.
* @throws IllegalArgumentException The given type name does not match one knwon in the class inventory
* or is not a kind of {@linkplain DefinitionObject definition}.
*
* @see #lookup(Class, AUID)
* @see #lookup(String, AUID)
* @see #lookup(Class, String)
* @see WeakReference
* @see tv.amwa.maj.model.Dictionary
*/
@SuppressWarnings("unchecked")
public final static <T extends DefinitionObject> T lookup(
String definitionTypeName,
String definitionName)
throws NullPointerException,
IllegalArgumentException {
if (definitionTypeName == null)
throw new NullPointerException("Cannot lookup a definition using a null definition type name.");
if (definitionName == null)
throw new NullPointerException("Cannot lookup a definition using a null definition name.");
Class<?> correspondingClass = lookForClass(definitionTypeName).getJavaImplementation();
// if (DefinitionObject.class.isAssignableFrom(correspondingClass))
if (correspondingClass.isAssignableFrom(DefinitionObject.class))
throw new IllegalArgumentException("The given class name does not map to a definition.");
return lookup(
(Class<T>) correspondingClass,
definitionName);
}
/**
* <p>Looks up a {@linkplain tv.amwa.maj.model.DefinitionObject definition} by its type name and its
* identification in the inventory of the warehouse. The definition only has to be known to the Java
* local machine and is not necessarily stored in an AAF {@linkplain tv.amwa.maj.model.Dictionary dictionary}.</p>
*
* <p>For example, to find the correct kind of {@linkplain DataDefinition data definition} for a picture,
* call:</p>
*
* <pre>{@code
* DataDefinition pictureData = Warehouse.lookup(
* "DataDefinition",
* Forge.parseAUID("urn:smpte:ul:060x0e2b34.04010101.01030202.01000000"));}</pre>
*
* @param <T> All kinds of {@linkplain DefinitionObject definition objects}.
* @param definitionTypeName Name of the specific kind of definition required.
* @param definitionID Identification of the definition of the given kind to find.
* @return Definition corresponding to the given type name and identification, or <code>null</code> if the
* definition name is not known for the given type.
*
* @throws NullPointerException One or both of the given definition type name or definition identification
* is/are <code>null</code>.
*
* @see #lookup(Class, String)
* @see #lookup(Class, AUID)
* @see #lookup(String, String)
* @see WeakReference
* @see tv.amwa.maj.model.Dictionary
*/
@SuppressWarnings("unchecked")
public final static <T extends DefinitionObject> T lookup(
String definitionTypeName,
AUID definitionID)
throws NullPointerException,
IllegalArgumentException {
if (definitionTypeName == null)
throw new NullPointerException("Cannot lookup a definition using a null definition type name.");
if (definitionID == null)
throw new NullPointerException("Cannot lookup a definition using a null definition name.");
Class<?> correspondingClass = lookForClass(definitionTypeName).getJavaImplementation();
if (correspondingClass.isAssignableFrom(DefinitionObject.class))
throw new IllegalArgumentException("The given class name does not map to a definition.");
return lookup(
(Class<T>) correspondingClass,
definitionID);
}
/**
* <p>Registers the given {@linkplain DefinitionObject definition} with the inventory of this
* warehouse.</p>
*
* @param definition Definition to be placed onto the warehouse inventory.
* @return <code>true</code> if the registration is successful, <code>false</code> if the
* definition is already registered and <code>null</code> if an error occurred and the
* registration failed.
*
* @throws NullPointerException The given definition is <code>null</code>.
*
* @see #lookup(Class, String)
* @see #lookup(Class, AUID)
*/
public final static Boolean register(
DefinitionObject definition)
throws NullPointerException {
if (definition == null)
throw new NullPointerException("Cannot register a new definition using a null value.");
ClassDefinition definitionClassDefinition = lookForClass(definition.getClass());
Class<?> definitionImplementation = definitionClassDefinition.getJavaImplementation();
String registrationName = "register" + definitionClassDefinition.getName();
Boolean alreadyRegistered = null;
try {
Method forName = null;
for ( Class<?> interfaceToCheck : definitionImplementation.getInterfaces() ) {
try {
forName = definitionImplementation.getMethod(registrationName, interfaceToCheck);
}
catch (NoSuchMethodException nsme) { }
if (forName != null) break;
}
alreadyRegistered = (Boolean) forName.invoke(null, definition);
}
catch (Exception e) {
return null;
}
return !alreadyRegistered;
}
/**
* <p>Counts the number of definitions in the inventory of the given definition type.</p>
*
* @param <T> All kinds of {@linkplain DefinitionObject definition types}.
* @param definitionType Definition type to count the number of known definitions for.
* @return Number of definitions in the inventory, or zero if the definition type is not
* known.
*
* @throws NullPointerException The given definition type is <code>null</code>.
*
* @see #countClassDefinitions()
* @see #countTypeDefinitions()
* @see #countExtendibleEnumerations()
* @see #inventory(Class)
*/
@SuppressWarnings("unchecked")
public final static <T extends DefinitionObject> int count(
Class<T> definitionType)
throws NullPointerException {
if (definitionType == null)
throw new NullPointerException("Cannot count definitions from a null definition type.");
if (definitionType.isInterface()) {
ClassDefinition correspondingClass = lookForClass(definitionType);
definitionType = (Class<T>) correspondingClass.getJavaImplementation();
}
try {
Method forName = definitionType.getMethod("count");
return (Integer) forName.invoke(null);
}
catch (Exception e) {
return 0;
}
}
/**
* <p>Returns a collection of the names of the definitions of the given definition type.</p>
*
* @param <T> All kinds of {@linkplain DefinitionObject definition types}.
* @param definitionType Definition type to list the elements of.
* @return List of the names of the definitions of the given type, which is empty is the type is known
* and no definitions are registered and <code>null</code> if the type is not known.
*
* @throws NullPointerException The given definition type is <code>null</code>.
*
* @see #getTypeInventory()
* @see #getClassInventory()
* @see #getExtendibleEnumerationInventory()
* @see #count(Class)
*/
@SuppressWarnings("unchecked")
public final static <T extends DefinitionObject> Collection<String> inventory(
Class<T> definitionType)
throws NullPointerException {
if (definitionType == null)
throw new NullPointerException("Cannot lookup a definition from a null definition type.");
if (definitionType.isInterface()) {
ClassDefinition correspondingClass = lookForClass(definitionType);
definitionType = (Class<T>) correspondingClass.getJavaImplementation();
}
try {
Method forName = definitionType.getMethod("inventory");
return (Collection<String>) forName.invoke(null);
}
catch (Exception e) {
return null;
}
}
/**
* <p>Looks through the given class for public static fields of {@link TypeDefinition type definitions}
* and adds these to the warehouse inventory.</p>
*
* <p>This method picks out static fields in the given class and adds them to the cache of known
* types in this class. Here is an example of the declaration of a {@linkplain tv.amwa.maj.integer.UInt8 UInt8}
* value:</p>
*
* <pre>
* public final static TypeDefinitionInteger UInt8 = new TypeDefinitionInteger(
* new AUID(0x01010100, (short) 0x0000, (short) 0x0000,
* new byte[] { 0x06, 0x0e, 0x2b, 0x34, 0x01, 0x04, 0x01, 0x01 } ),
* "UInt8", (byte) 1, false);
* </pre>
*
* @param typeClass Class to check for static type definition instances.
* @param namespace Namespace in which the type definition is defined.
* @param prefix Preferred prefix for symbols in the given namespace.
*
* @throws NullPointerException The given class is <code>null</code>.
*
* @see #register(TypeDefinition, String, String)
* @see #lookForType(AUID)
* @see #lookForType(String)
* @see tv.amwa.maj.meta.MetaDefinition#getNamespace()
* @see tv.amwa.maj.meta.MetaDefinition#getPrefix()
*/
public final static void registerTypes(
Class<?> typeClass,
String namespace,
String prefix)
throws NullPointerException {
if (typeClass == null)
throw new NullPointerException("Cannot add type definitions from a null class.");
if (namespace == null)
throw new NullPointerException("Cannot add type definitions to the warehouse with a null namespace.");
if (prefix == null)
throw new NullPointerException("Cannot add type definitions to the warehouse with a null prefix.");
for ( Field field : typeClass.getFields() ) {
// System.out.println(field.getName());
int modifiers = field.getModifiers();
if ((!Modifier.isPublic(modifiers)) || (!Modifier.isStatic(modifiers))) continue;
try {
Object value = field.get(null);
if (value instanceof TypeDefinition) {
TypeDefinition foundType = (TypeDefinition) value;
register(foundType, namespace, prefix);
}
}
catch (IllegalAccessException iae) { /* If you can't access it, don't add it! */ }
}
/* for ( String typeName : knownTypes.keySet() )
if (typeName.contains("SourceReference")) System.out.println(typeName); */
}
/**
* <p>Finds the type definition for the given name from the warehouse inventory. Type definition names are
* the same as used in the AAF specification, reference implementation meta dictionary or SMPTE
* type dictionary, for example:</p>
*
* <ul>
* <li><code>UInt32</code> is a unique name;</li>
* <li><code>Int8Array</code> is a unique name;</li>
* <li><code>TypeDefinitionWeakReferenceVector</code> returns the same type as:
* <ul>
* <li><code>WeakReferenceVector of TypeDefinition</code> and</li>
* <li><code>WeakReferenceVector<TypeDefinition></code>;</li>
* </ul></li>
* <li><code>CompositionPackageStrongReference</code> returns the same type as:
* <ul>
* <li><code>StrongReference to CompositionPackage</code> and</li>
* <li><code>StrongReference<CompositionPackage></code></li>
* </ul></li>
* </ul>
*
* <p>If a corresponding type cannot be found, this method returns <code>null</code>.</p>
*
* @param typeName Name of the type definition to check for.
* @return Type definition corresponding to the given name.
*
* @throws NullPointerException The given type name is <code>null</code>.
*
* @see #lookForClass(AUID)
* @see #getTypeInventory()
*/
public final static TypeDefinition lookForType(
String typeName)
throws NullPointerException {
if (typeName == null)
throw new NullPointerException("Cannot find a type definition with a null name value.");
if (!knownTypes.containsKey(typeName))
registerTypes(TypeDefinitions.class, CommonConstants.AAF_XML_NAMESPACE, CommonConstants.AAF_XML_PREFIX);
return knownTypes.get(typeName);
}
/**
* <p>Finds the type definition with the given {@linkplain tv.amwa.maj.record.AUID AUID} identification
* from the warehouse inventory. If a type definition matching the given identification cannot be found,
* <code>null</code> is returned.</p>
*
* @param identification AUID to use to lookup a type definition.
* @return Type definition matching the identification.
*
* @throws NullPointerException The given type identifier is <code>null</code>.
*
* @see #lookForType(String)
* @see #getTypeInventory()
*/
public final static TypeDefinition lookForType(
AUID identification)
throws NullPointerException {
if (identification == null)
throw new NullPointerException("Cannot find a type definition with a null identification value.");
return knownTypesByID.get(identification);
}
/**
* <p>Count of the number of different type definitions in this warehouse.</p>
*