Improved query string handling
[stereo.git] / DAAPLib / src / util / queryparser / Token.java
blobd03edae67f18831445e487a74314bc7b27102067
1 package util.queryparser;
3 import interfaces.Element;
5 import java.math.BigInteger;
7 import daap.DAAPConstants;
9 public class Token implements Filter {
11 public final boolean falseify;
12 public final String property;
13 public final String value;
15 public Token(String t) {
16 System.out.println("token: " + t);
17 String p;
19 int i = t.indexOf(':');
20 if (i > 0) {
21 p = t.substring(0,i);
22 value = t.substring(i+1);
24 else {
25 p = t;
26 value = "";
29 if (p.charAt(p.length()-1) == '!') {
30 falseify = true;
31 property = p.substring(0, p.length()-1);
33 else {
34 falseify = false;
35 property = p;
39 public boolean check(Element t) {
41 Integer code = DAAPConstants.shortCodes.get(property);
43 if (code != null) {
45 Object tval = null;
46 Object pval = t.getTag(code);
48 int ae00 = 0x61650000;
49 if ((0xFFFF0000 & code) == ae00) {
50 //TODO we ignore apple codes because mt-daapd doesn't implement them
51 //this should change?
52 return true;
54 switch (DAAPConstants.types.get(code)) {
55 case 1: tval = Byte.parseByte(value); break;
56 case 5: tval = Integer.parseInt(value); break;
57 //using big integer ensures unsigned longs are parsed correctly
58 case 7: tval = new BigInteger(value).longValue(); break;
59 case 9: tval = value; break; //string
60 default:
61 throw new IllegalArgumentException("unknown or unimplemented type: "
62 + DAAPConstants.types.get(code) + " for " + property);
65 return (pval != null && tval.equals(pval)) ^ falseify;
67 else {
68 throw new IllegalArgumentException("unknown property: " + property);
72 public String toString() {
73 return "'"+property+(falseify?"!":"")+":"+value+"'";