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 MimeType type = null;
28
29 private int priority = 50;
30
31 private Clause clause = null;
32
33 Magic() {
34 this(50);
35 }
36
37 Magic(int priority) {
38 this.priority = priority;
39 }
40
41 void setType(MimeType type) {
42 this.type = type;
43 }
44
45 MimeType getType() {
46 return type;
47 }
48
49 int getPriority() {
50 return priority;
51 }
52
53 void setClause(Clause clause) {
54 this.clause = clause;
55 }
56
57 public boolean eval(byte[] data) {
58 return clause.eval(data);
59 }
60
61 public int size() {
62 return clause.size();
63 }
64
65 public String toString() {
66 StringBuffer buf = new StringBuffer();
67 buf.append("[").append(priority).append("/").append(clause).append("]");
68 return buf.toString();
69 }
70
71 public int compareTo(Magic o) {
72 int diff = o.priority - priority;
73 if (diff == 0) {
74 diff = o.size() - size();
75 }
76 return diff;
77 }
78
79 }