1 //
2 // CamlFileParser.java
3 // CocoDonkey
4 // $Id: CamlData.java,v 1.1 2002/08/21 21:51:31 fortun Exp $
5 //
6 // Created by Fred Bonzoun on Tue Aug 20 2002.
7 // Copyright (c) 2002 Bonzoun. All rights reserved.
8 //
9 // This library is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU Lesser General Public License as published
11 // by the Free Software Foundation; either version 2.1 of the License, or
12 // (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 //
23
24 package net.bonzoun.cocodonkey;
25
26 /***
27 * Class that encpsulates data that come from a file saved by Caml
28 ***/
29 public abstract class CamlData {
30
31 private static final CamlData empty = new CamlData.Data("");
32
33 public String getString() {
34 return toString();
35 }
36
37 public int size() {
38 return -1;
39 }
40
41 public int getInt() {
42 return parseInt(getString());
43 }
44
45 public double getDouble() {
46 return parseDouble(getString());
47 }
48
49 public CamlData get(int i) {
50 return empty;
51 }
52
53 public String getString(int i) {
54 return get(i).toString();
55 }
56
57 public int getInt(int i) {
58 return parseInt(getString(i));
59 }
60
61 public double getDouble(int i) {
62 return parseDouble(getString(i));
63 }
64
65 public boolean getBoolean(int i) {
66 return getString(i).equals("true");
67 }
68
69 public CamlData get(String key) {
70 return empty;
71 }
72
73 public String getString(String key) {
74 return get(key).toString();
75 }
76
77 public int getInt(String key) {
78 return parseInt(getString(key));
79 }
80
81 public double getDouble(String key) {
82 return parseDouble(getString(key));
83 }
84
85 public boolean getBoolean(String key) {
86 return getString(key).equals("true");
87 }
88
89 private static int parseInt(String s) {
90 try {
91 return Integer.parseInt(s);
92 }
93 catch(NumberFormatException e) {
94 return -1;
95 }
96 }
97
98 private static double parseDouble(String s) {
99 try {
100 return Double.parseDouble(s);
101 }
102 catch(NumberFormatException e) {
103 return -1.;
104 }
105 }
106
107 public static class List extends CamlData {
108 private java.util.List list;
109
110 public List(java.util.List value) {
111 list = value;
112 }
113
114 public int size() {
115 return list.size();
116 }
117
118 public String toString() {
119 return list.toString();
120 }
121
122 public CamlData get(int i) {
123 if (i>=0 && i<list.size())
124 return (CamlData)list.get(i);
125 else
126 return empty;
127 }
128 }
129
130 public static class Map extends CamlData {
131 private java.util.Map map;
132
133 public Map(java.util.Map value) {
134 map = value;
135 }
136
137 public int size() {
138 return map.size();
139 }
140
141 public String toString() {
142 return map.toString();
143 }
144
145 public CamlData get(String key) {
146 Object value = map.get(key);
147 if (value!=null)
148 return (CamlData)value;
149 else
150 return empty;
151 }
152 }
153
154 public static class Data extends CamlData {
155 private String string;
156
157 public Data(String value) {
158 string = value;
159 }
160
161 public String toString() {
162 return string;
163 }
164 }
165 }
166
167 // $Log: CamlData.java,v $
168 // Revision 1.1 2002/08/21 21:51:31 fortun
169 // *** empty log message ***
170 //