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.mime;
18  
19  /**
20   * Defines a magic for a MimeType. A magic is made of one or several
21   * MagicClause.
22   * 
23   * 
24   */
25  class Magic implements Clause, Comparable<Magic> {
26  
27      private final MimeType type;
28  
29      private int priority = 50;
30  
31      private Clause clause = null;
32  
33      Magic(MimeType type) {
34          this.type = type;
35      }
36  
37      MimeType getType() {
38          return type;
39      }
40  
41      void setPriority(int priority) {
42          this.priority = priority;
43      }
44  
45      int getPriority() {
46          return priority;
47      }
48  
49      void setClause(Clause clause) {
50          this.clause = clause;
51      }
52  
53      public boolean eval(byte[] data) {
54          return clause.eval(data);
55      }
56  
57      public int size() {
58          return clause.size();
59      }
60  
61      public String toString() {
62          StringBuffer buf = new StringBuffer();
63          buf.append("[").append(priority).append("/").append(clause).append("]");
64          return buf.toString();
65      }
66  
67      public int compareTo(Magic o) {
68          int diff = o.priority - priority;
69          if (diff == 0) {
70              diff = o.size() - size();
71          }
72          if (diff == 0) {
73              diff = o.toString().compareTo(toString());
74          }
75          return diff;
76      }
77  
78  }