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  import java.util.Arrays;
20  import java.util.Collections;
21  import java.util.HashSet;
22  import java.util.Set;
23  
24  /**
25   * XMP property definition. Each instance of this class defines a single
26   * metadata property like "dc:format". In addition to the property name,
27   * the {@link ValueType value type} and category (internal or external)
28   * of the property are included in the property definition. The available
29   * choice values are also stored for open and closed choice value types.
30   *
31   * @since Apache Tika 0.7
32   */
33  public final class Property {
34  
35      public static enum ValueType {
36          BOOLEAN, OPEN_CHOICE, CLOSED_CHOICE, DATE, INTEGER, LOCALE,
37          MIME_TYPE, PROPER_NAME, RATIONAL, REAL, TEXT, URI, URL, XPATH
38      }
39  
40      private final String name;
41  
42      private final boolean internal;
43  
44      private final ValueType valueType;
45  
46      /**
47       * The available choices for the open and closed choice value types.
48       */
49      private final Set<String> choices;
50  
51      private Property(
52              String name, boolean internal,
53              ValueType valueType, String[] choices) {
54          this.name = name;
55          this.internal = internal;
56          this.valueType = valueType;
57          if (choices != null) {
58              this.choices = Collections.unmodifiableSet(
59                      new HashSet<String>(Arrays.asList(choices)));
60          } else {
61              this.choices = null;
62          }
63      }
64  
65      public String getName() {
66          return name;
67      }
68  
69      public boolean isInternal() {
70          return internal;
71      }
72  
73      public boolean isExternal() {
74          return !internal;
75      }
76  
77      public ValueType getValueType() {
78          return valueType;
79      }
80  
81      /**
82       * Returns the (immutable) set of choices for the values of this property.
83       * Only defined for {@link ValueType#OPEN_CHOICE open} and
84       * {@link ValueType#CLOSED_CHOICE closed choice} value types.
85       *
86       * @return available choices, or <code>null</code>
87       */
88      public Set<String> getChoices() {
89          return choices;
90      }
91  
92      private Property( String name, boolean internal, ValueType valueType) {
93          this(name, internal, valueType, null);
94      }
95  
96      public static Property internalBoolean(String name) {
97          return new Property(name, true, ValueType.BOOLEAN);
98      }
99  
100     public static Property internalClosedChoise(
101             String name, String... choices) {
102         return new Property(name, true, ValueType.CLOSED_CHOICE, choices);
103     }
104 
105     public static Property internalDate(String name) {
106         return new Property(name, true, ValueType.DATE);
107     }
108 
109     public static Property internalInteger(String name) {
110         return new Property(name, true, ValueType.INTEGER);
111     }
112 
113     public static Property internalRational(String name) {
114         return new Property(name, true, ValueType.RATIONAL);
115     }
116 
117     public static Property internalOpenChoise(
118             String name, String... choices) {
119         return new Property(name, true, ValueType.OPEN_CHOICE, choices);
120     }
121     public static Property internalReal(String name) {
122         return new Property(name, true, ValueType.REAL);
123     }
124 
125     public static Property internalText(String name) {
126         return new Property(name, true, ValueType.TEXT);
127     }
128 
129     public static Property internalURI(String name) {
130         return new Property(name, true, ValueType.URI);
131     }
132 
133     public static Property externalClosedChoise(
134             String name, String... choices) {
135         return new Property(name, false, ValueType.CLOSED_CHOICE, choices);
136     }
137 
138     public static Property externalDate(String name) {
139         return new Property(name, false, ValueType.DATE);
140     }
141 
142     public static Property externalInteger(String name) {
143         return new Property(name, false, ValueType.INTEGER);
144     }
145 
146     public static Property externalText(String name) {
147         return new Property(name, false, ValueType.TEXT);
148     }
149 
150 }