View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.tika.metadata;
18  
19  // JDK imports
20  import java.util.Enumeration;
21  import java.util.HashMap;
22  import java.util.Map;
23  import java.util.Properties;
24  
25  /**
26   * A multi-valued metadata container.
27   * 
28   * 
29   * 
30   */
31  public class Metadata implements CreativeCommons, DublinCore, HttpHeaders,
32      MSOffice, TikaMetadataKeys, TikaMimeKeys {
33  
34  
35    /**
36     * A map of all metadata attributes.
37     */
38    private Map<String, String[]> metadata = null;
39  
40    /**
41     * Constructs a new, empty metadata.
42     */
43    public Metadata() {
44      metadata = new HashMap<String, String[]>();
45    }
46  
47    /**
48     * Returns true if named value is multivalued.
49     * 
50     * @param name
51     *          name of metadata
52     * @return true is named value is multivalued, false if single value or null
53     */
54    public boolean isMultiValued(final String name) {
55      return metadata.get(name) != null && metadata.get(name).length > 1;
56    }
57  
58    /**
59     * Returns an array of the names contained in the metadata.
60     * 
61     * @return Metadata names
62     */
63    public String[] names() {
64      return metadata.keySet().toArray(new String[metadata.keySet().size()]);
65    }
66  
67    /**
68     * Get the value associated to a metadata name. If many values are assiociated
69     * to the specified name, then the first one is returned.
70     * 
71     * @param name
72     *          of the metadata.
73     * @return the value associated to the specified metadata name.
74     */
75    public String get(final String name) {
76      String[] values = metadata.get(name);
77      if (values == null) {
78        return null;
79      } else {
80        return values[0];
81      }
82    }
83  
84    /**
85     * Get the values associated to a metadata name.
86     * 
87     * @param name
88     *          of the metadata.
89     * @return the values associated to a metadata name.
90     */
91    public String[] getValues(final String name) {
92      return _getValues(name);
93    }
94  
95    private String[] _getValues(final String name) {
96      String[] values = metadata.get(name);
97      if (values == null) {
98        values = new String[0];
99      }
100     return values;
101   }
102 
103   /**
104    * Add a metadata name/value mapping. Add the specified value to the list of
105    * values associated to the specified metadata name.
106    * 
107    * @param name
108    *          the metadata name.
109    * @param value
110    *          the metadata value.
111    */
112   public void add(final String name, final String value) {
113     String[] values = metadata.get(name);
114     if (values == null) {
115       set(name, value);
116     } else {
117       String[] newValues = new String[values.length + 1];
118       System.arraycopy(values, 0, newValues, 0, values.length);
119       newValues[newValues.length - 1] = value;
120       metadata.put(name, newValues);
121     }
122   }
123 
124   /**
125    * Copy All key-value pairs from properties.
126    * 
127    * @param properties
128    *          properties to copy from
129    */
130   public void setAll(Properties properties) {
131     Enumeration names = properties.propertyNames();
132     while (names.hasMoreElements()) {
133       String name = (String) names.nextElement();
134       metadata.put(name, new String[] { properties.getProperty(name) });
135     }
136   }
137 
138   /**
139    * Set metadata name/value. Associate the specified value to the specified
140    * metadata name. If some previous values were associated to this name, they
141    * are removed.
142    * 
143    * @param name
144    *          the metadata name.
145    * @param value
146    *          the metadata value.
147    */
148   public void set(String name, String value) {
149     metadata.put(name, new String[] { value });
150   }
151 
152   /**
153    * Remove a metadata and all its associated values.
154    * 
155    * @param name
156    *          metadata name to remove
157    */
158   public void remove(String name) {
159     metadata.remove(name);
160   }
161 
162   /**
163    * Returns the number of metadata names in this metadata.
164    * 
165    * @return number of metadata names
166    */
167   public int size() {
168     return metadata.size();
169   }
170 
171   public boolean equals(Object o) {
172 
173     if (o == null) {
174       return false;
175     }
176 
177     Metadata other = null;
178     try {
179       other = (Metadata) o;
180     } catch (ClassCastException cce) {
181       return false;
182     }
183 
184     if (other.size() != size()) {
185       return false;
186     }
187 
188     String[] names = names();
189     for (int i = 0; i < names.length; i++) {
190       String[] otherValues = other._getValues(names[i]);
191       String[] thisValues = _getValues(names[i]);
192       if (otherValues.length != thisValues.length) {
193         return false;
194       }
195       for (int j = 0; j < otherValues.length; j++) {
196         if (!otherValues[j].equals(thisValues[j])) {
197           return false;
198         }
199       }
200     }
201     return true;
202   }
203 
204   public String toString() {
205     StringBuffer buf = new StringBuffer();
206     String[] names = names();
207     for (int i = 0; i < names.length; i++) {
208       String[] values = _getValues(names[i]);
209       for (int j = 0; j < values.length; j++) {
210         buf.append(names[i]).append("=").append(values[j]).append(" ");
211       }
212     }
213     return buf.toString();
214   }
215 
216 }