Fix some bugs in remote/sync (still not complete)
[jvcard.git] / src / be / nikiroo / jvcard / remote / Command.java
1 package be.nikiroo.jvcard.remote;
2
3 public class Command {
4 public enum Verb {
5 /** VERSION of the protocol */
6 VERSION,
7 /** TIME of the remote server in milliseconds since the Unix epoch */
8 TIME,
9 /** STOP the communication (client stops) */
10 STOP,
11 /**
12 * LIST all the contacts on the remote server that contain the search
13 * term, or all contacts if no search term given
14 */
15 LIST,
16 /** HELP about the protocol for interactive access */
17 HELP,
18 /** SELECT a resource (a card) to work on */
19 SELECT,
20 /** GET a remote card */
21 GET_CARD,
22 /**
23 * PUT mode activation toggle for a card on the remote server (you can
24 * issue *_CONTACT commands when in PUT mode)
25 */
26 PUT_CARD,
27 /** POST a new card to the remote server */
28 POST_CARD,
29 /** DELETE an existing contact from the remote server */
30 DELETE_CARD,
31 /** GET a remote contact */
32 GET_CONTACT,
33 /**
34 * PUT mode activation toggle for a contact on the remote server (you
35 * can issue *_DATA commands when in PUT mode)
36 */
37 PUT_CONTACT,
38 /** POST a new contact to the remote server */
39 POST_CONTACT,
40 /** DELETE an existing contact from the remote server */
41 DELETE_CONTACT,
42 /** GET a remote data */
43 GET_DATA,
44 /** POST a new data to the remote server */
45 POST_DATA,
46 /** DELETE an existing data from the remote server */
47 DELETE_DATA,
48 }
49
50 private Verb verb;
51 private int version;
52 private String param;
53
54 /**
55 * Create a new, empty {@link Command} with the given {@link Verb} and
56 * version.
57 *
58 * @param verb
59 * the {@link Verb}
60 * @param version
61 * the version
62 */
63 public Command(Verb verb, int version) {
64 this(verb, null, version);
65 }
66
67 /**
68 * Create a new, empty {@link Command} with the given {@link Verb} and
69 * version.
70 *
71 * @param verb
72 * the {@link Verb}
73 * @param version
74 * the version
75 */
76 public Command(Verb verb, String param, int version) {
77 this.verb = verb;
78 this.version = version;
79 this.param = param;
80 }
81
82 /**
83 * Read a command line (starting with a {@link Verb}) and process its
84 * content here in a more readable format.
85 *
86 * @param input
87 * the command line
88 * @param version
89 * the version (which can be overrided by a {@link Verb#VERSION}
90 * command)
91 */
92 public Command(String input, int version) {
93 this.version = version;
94
95 if (input != null) {
96 String v = input;
97 int indexSp = input.indexOf(" ");
98 if (indexSp >= 0) {
99 v = input.substring(0, indexSp);
100 }
101
102 for (Verb verb : Verb.values()) {
103 if (v.equals(verb.name())) {
104 this.verb = verb;
105 }
106 }
107
108 if (verb != null) {
109 String param = null;
110 if (indexSp >= 0)
111 param = input.substring(indexSp + 1);
112
113 this.param = param;
114
115 if (verb == Verb.VERSION) {
116 try {
117 version = Integer.parseInt(param);
118 } catch (NumberFormatException e) {
119 e.printStackTrace();
120 }
121 }
122 }
123 }
124 }
125
126 /**
127 * Return the version
128 *
129 * @return the version
130 */
131 public int getVersion() {
132 return version;
133 }
134
135 /**
136 * Return the {@link Verb}
137 *
138 * @return the {@link Verb}
139 */
140 public Verb getVerb() {
141 return verb;
142 }
143
144 /**
145 * Return the parameter of this {@link Command} if any.
146 *
147 * @return the parameter or NULL
148 */
149 public String getParam() {
150 return param;
151 }
152
153 @Override
154 public String toString() {
155 if (verb == null)
156 return "[null command]";
157
158 switch (verb) {
159 case VERSION:
160 return verb.name() + " " + version;
161 default:
162 return verb.name() + (param == null ? "" : " " + param);
163 }
164 }
165 }