1 package be
.nikiroo
.jvcard
.launcher
;
4 import java
.io
.IOException
;
5 import java
.lang
.reflect
.Field
;
6 import java
.net
.Socket
;
7 import java
.nio
.charset
.Charset
;
8 import java
.util
.LinkedList
;
11 import be
.nikiroo
.jvcard
.Card
;
12 import be
.nikiroo
.jvcard
.parsers
.Format
;
13 import be
.nikiroo
.jvcard
.remote
.Command
;
14 import be
.nikiroo
.jvcard
.remote
.SimpleSocket
;
15 import be
.nikiroo
.jvcard
.resources
.Bundles
;
16 import be
.nikiroo
.jvcard
.resources
.StringUtils
;
17 import be
.nikiroo
.jvcard
.resources
.Trans
;
18 import be
.nikiroo
.jvcard
.resources
.Trans
.StringId
;
21 * This class contains the runnable Main method. It will parse the user supplied
22 * parameters and take action based upon those. Most of the time, it will start
29 static public final String APPLICATION_TITLE
= "jVcard";
30 static public final String APPLICATION_VERSION
= "1.0-beta2-dev";
32 static private final int ERR_NO_FILE
= 1;
33 static private final int ERR_SYNTAX
= 2;
34 static private final int ERR_INTERNAL
= 3;
35 static private Trans transService
;
38 * Translate the given {@link StringId}.
43 * @return the translation
45 static public String
trans(StringId id
) {
46 return transService
.trans(id
);
50 * Check if unicode characters should be used.
52 * @return TRUE to allow unicode
54 static public boolean isUnicode() {
55 return transService
.isUnicode();
59 * Start the application.
62 * The returned exit codes are:
64 * <li>1: no files to open</li>
65 * <li>2: invalid syntax</li>
66 * <li>3: internal error</li>
71 * the parameters (see <tt>--help</tt> to know which are
74 public static void main(String
[] args
) {
75 Boolean textMode
= null;
76 boolean noMoreParams
= false;
77 boolean filesTried
= false;
79 // get the "system default" language to help translate the --help
81 String language
= null;
82 transService
= new Trans(language
);
84 boolean unicode
= transService
.isUnicode();
85 String i18nDir
= null;
86 List
<String
> files
= new LinkedList
<String
>();
88 for (int index
= 0; index
< args
.length
; index
++) {
89 String arg
= args
[index
];
90 if (!noMoreParams
&& arg
.equals("--")) {
92 } else if (!noMoreParams
&& arg
.equals("--help")) {
94 .println("TODO: implement some help text.\n"
95 + "Usable switches:\n"
96 + "\t--: stop looking for switches\n"
97 + "\t--help: this here thingy\n"
98 + "\t--lang LANGUAGE: choose the language, for instance en_GB\n"
99 + "\t--tui: force pure text mode even if swing treminal is available\n"
100 + "\t--gui: force swing terminal mode\n"
101 + "\t--noutf: force non-utf8 mode if you need it\n"
102 + "\t--config DIRECTORY: force the given directory as a CONFIG_DIR\n"
103 + "\t--server PORT: start a remoting server instead of a client\n"
104 + "\t--i18n DIR: generate the translation file for the given language (can be \"\") to/from the .properties given dir\n"
105 + "everyhing else is either a file to open or a directory to open\n"
106 + "(we will only open 1st level files in given directories)\n"
107 + "('jvcard://hostname:8888/file' links -- or without 'file' -- are also ok)\n");
109 } else if (!noMoreParams
&& arg
.equals("--tui")) {
111 } else if (!noMoreParams
&& arg
.equals("--gui")) {
113 } else if (!noMoreParams
&& arg
.equals("--noutf")) {
115 transService
.setUnicode(unicode
);
116 } else if (!noMoreParams
&& arg
.equals("--lang")) {
118 if (index
>= args
.length
) {
119 System
.err
.println("Syntax error: no language given");
120 System
.exit(ERR_SYNTAX
);
124 language
= args
[index
];
125 transService
= new Trans(language
);
126 transService
.setUnicode(unicode
);
127 } else if (!noMoreParams
&& arg
.equals("--config")) {
129 if (index
>= args
.length
) {
131 .println("Syntax error: no config directory given");
132 System
.exit(ERR_SYNTAX
);
136 Bundles
.setDirectory(args
[index
]);
137 transService
= new Trans(language
);
138 transService
.setUnicode(unicode
);
139 } else if (!noMoreParams
&& arg
.equals("--server")) {
141 if (index
>= args
.length
) {
142 System
.err
.println("Syntax error: no port given");
143 System
.exit(ERR_SYNTAX
);
148 port
= Integer
.parseInt(args
[index
]);
149 } catch (NumberFormatException e
) {
150 System
.err
.println("Invalid port number: " + args
[index
]);
151 System
.exit(ERR_SYNTAX
);
154 } else if (!noMoreParams
&& arg
.equals("--i18n")) {
156 if (index
>= args
.length
) {
158 .println("Syntax error: no .properties directory given");
159 System
.exit(ERR_SYNTAX
);
163 i18nDir
= args
[index
];
166 files
.addAll(open(arg
));
175 if (port
!= null && files
.size() > 0) {
177 .println("Invalid syntax: you cannot both use --server and provide card files");
178 System
.exit(ERR_SYNTAX
);
179 } else if (i18nDir
!= null && files
.size() > 0) {
181 .println("Invalid syntax: you cannot both use --i18n and provide card files");
182 System
.exit(ERR_SYNTAX
);
183 } else if (port
!= null && i18nDir
!= null) {
185 .println("Invalid syntax: you cannot both use --server and --i18n");
186 System
.exit(ERR_SYNTAX
);
187 } else if (i18nDir
!= null && language
== null) {
189 .println("Invalid syntax: you cannot use --i18n without --lang");
190 System
.exit(ERR_SYNTAX
);
191 } else if (port
== null && i18nDir
== null && files
.size() == 0) {
192 if (files
.size() == 0 && !filesTried
) {
193 files
.addAll(open("."));
196 if (files
.size() == 0) {
197 System
.err
.println("No files to open");
198 System
.exit(ERR_NO_FILE
);
206 Optional
.runServer(port
);
207 } catch (Exception e
) {
208 if (e
instanceof IOException
) {
210 .println("I/O Exception: Cannot start the server");
212 System
.err
.println("Remoting support not available");
213 System
.exit(ERR_INTERNAL
);
216 } else if (i18nDir
!= null) {
218 Trans
.generateTranslationFile(i18nDir
, language
);
219 } catch (IOException e
) {
221 .println("I/O Exception: Cannot create/update a language in directory: "
226 Optional
.startTui(textMode
, files
);
227 } catch (Exception e
) {
228 if (e
instanceof IOException
) {
230 .println("I/O Exception: Cannot start the program with the given cards");
232 System
.err
.println("TUI support not available");
233 System
.exit(ERR_INTERNAL
);
240 * Return the {@link Card} corresponding to the given resource name -- a
241 * file or a remote jvcard URL
244 * a filename or a remote jvcard url with named resource (e.g.:
245 * <tt>jvcard://localhost:4444/coworkers.vcf</tt>)
247 * @return the {@link Card}
249 * @throws IOException
250 * in case of IO error or remoting not available
252 static public Card
getCard(String input
) throws IOException
{
253 boolean remote
= false;
254 Format format
= Format
.Abook
;
256 if (ext
.contains(".")) {
257 String tab
[] = ext
.split("\\.");
258 if (tab
.length
> 1 && tab
[tab
.length
- 1].equalsIgnoreCase("vcf")) {
259 format
= Format
.VCard21
;
263 if (input
.contains("://")) {
264 format
= Format
.VCard21
;
271 card
= Optional
.syncCard(input
);
273 card
= new Card(new File(input
), format
);
275 } catch (IOException ioe
) {
277 } catch (Exception e
) {
278 throw new IOException("Remoting support not available", e
);
285 * Open the given path and add all its files if it is a directory or just
286 * this one if not to the returned list.
291 * @return the list of opened files
293 static private List
<String
> open(String path
) {
294 List
<String
> files
= new LinkedList
<String
>();
296 if (path
!= null && path
.startsWith("jvcard://")) {
297 if (path
.endsWith("/")) {
298 files
.addAll(list(path
));
303 File file
= new File(path
);
305 if (file
.isDirectory()) {
306 for (File subfile
: file
.listFiles()) {
307 if (!subfile
.isDirectory())
308 files
.add(subfile
.getAbsolutePath());
311 files
.add(file
.getAbsolutePath());
314 System
.err
.println("File or directory not found: \"" + path
323 * List all the available {@link Card}s on the given network location (which
324 * is expected to be a jVCard remote server, obviously).
327 * the jVCard remote server path (e.g.:
328 * <tt>jvcard://localhost:4444/</tt>)
330 * @return the list of {@link Card}s
332 static private List
<String
> list(String path
) {
333 List
<String
> files
= new LinkedList
<String
>();
336 String host
= path
.split("\\:")[1].substring(2);
337 int port
= Integer
.parseInt(path
.split("\\:")[2].replaceAll("/$",
339 SimpleSocket s
= new SimpleSocket(new Socket(host
, port
),
343 s
.sendCommand(Command
.LIST_CARD
);
344 for (String p
: s
.receiveBlock()) {
346 + p
.substring(StringUtils
.fromTime(0).length() + 1));
349 } catch (Exception e
) {
357 * Really, really ask for UTF-8 encoding.
359 static private void utf8() {
361 System
.setProperty("file.encoding", "UTF-8");
362 Field charset
= Charset
.class.getDeclaredField("defaultCharset");
363 charset
.setAccessible(true);
364 charset
.set(null, null);
365 } catch (SecurityException e
) {
366 } catch (NoSuchFieldException e
) {
367 } catch (IllegalArgumentException e
) {
368 } catch (IllegalAccessException e
) {