Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/drew/metadata/Metadata.java


1   /*
2    * Metadata.java
3    *
4    * This class is public domain software - that is, you can do whatever you want
5    * with it, and include it software that is licensed under the GNU or the
6    * BSD license, or whatever other licence you choose, including proprietary
7    * closed source licenses.  Similarly, I release this Java version under the
8    * same license, though I do ask that you leave this header in tact.
9    *
10   * If you make modifications to this code that you think would benefit the
11   * wider community, please send me a copy and I'll post it on my site.
12   *
13   * If you make use of this code, I'd appreciate hearing about it.
14   *   drew.noakes@drewnoakes.com
15   * Latest version of this software kept at
16   *   http://drewnoakes.com/
17   *
18   * Created on 28 April 2002, 17:40
19   * Modified 04 Aug 2002
20   * - Adjusted javadoc
21   * - Added
22   * Modified 29 Oct 2002 (v1.2)
23   * - Stored IFD directories in separate tag-spaces
24   * - iterator() now returns an Iterator over a list of TagValue objects
25   * - More get*Description() methods to detail GPS tags, among others
26   * - Put spaces between words of tag name for presentation reasons (they had no
27   *   significance in compound form)
28   */
29  package com.drew.metadata;
30  
31  import java.util.ArrayList;
32  import java.util.HashMap;
33  import java.util.Iterator;
34  import java.io.Serializable;
35  
36  /**
37   * Result from an exif extraction operation, containing all tags, their
38   * values and support for retrieving them.
39   * @author  Drew Noakes http://drewnoakes.com
40   */
41  public final class Metadata implements Serializable
42  {
43      /**
44       *
45       */
46      private HashMap directoryMap;
47  
48      /**
49       * List of Directory objects set against this object.  Keeping a list handy makes
50       * creation of an Iterator and counting tags simple.
51       */
52      private final ArrayList directoryList;
53  
54      /**
55       * Creates a new instance of Metadata.  Package private.
56       */
57      public Metadata()
58      {
59          directoryMap = new HashMap();
60          directoryList = new ArrayList();
61      }
62  
63  
64  // OTHER METHODS
65  
66      /**
67       * Creates an Iterator over the tag types set against this image, preserving the order
68       * in which they were set.  Should the same tag have been set more than once, it's first
69       * position is maintained, even though the final value is used.
70       * @return an Iterator of tag types set for this image
71       */
72      public Iterator getDirectoryIterator()
73      {
74          return directoryList.iterator();
75      }
76  
77      /**
78       * Returns a count of unique directories in this metadata collection.
79       * @return the number of unique directory types set for this metadata collection
80       */
81      public int getDirectoryCount()
82      {
83          return directoryList.size();
84      }
85  
86      /**
87       *
88       * @param type
89       * @return
90       */
91      public Directory getDirectory(Class type)
92      {
93          if (!Directory.class.isAssignableFrom(type)) {
94              throw new RuntimeException("Class type passed to getDirectory must be an implementation of com.drew.metadata.Directory");
95          }
96          // check if we've already issued this type of directory
97          if (directoryMap.containsKey(type)) {
98              return (Directory)directoryMap.get(type);
99          }
100         Object directory;
101         try {
102             directory = type.newInstance();
103         } catch (Exception e) {
104             throw new RuntimeException("Cannot instantiate provided Directory type: " + type.toString());
105         }
106         // store the directory in case it's requested later
107         directoryMap.put(type, directory);
108         directoryList.add(directory);
109         return (Directory)directory;
110     }
111 
112     /**
113      * Indicates whether a given directory type has been created in this metadata
114      * repository.  Directories are created by calling getDirectory(Class).
115      * @param type the Directory type
116      * @return true if the metadata directory has been created
117      */
118     public boolean containsDirectory(Class type)
119     {
120         return directoryMap.containsKey(type);
121     }
122 }