summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/support/texosquery/TeXOSQuery.java
blob: dca8fa7c32d7b4974d61b64c5f5d4bdc07b285f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
package com.dickimawbooks.texosquery;

import java.io.BufferedReader;
import java.util.Locale;
import java.util.Calendar;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Main class.
 * @author Nicola Talbot
 * @version 1.1
 * @since 1.0
 */
public class TeXOSQuery {

    private static final String VERSION_NUMBER = "1.1";
    private static final String VERSION_DATE = "2016-07-14";
    private static final char BACKSLASH = '\\';
    private static final char FORWARDSLASH = '/';
    private static final long ZERO = 0L;
    
    /**
     * Escapes hash from input string.
     * @param string Input string.
     * @return String with hash escaped.
     */
    private static String escapeHash(String string) {
        return string.replaceAll("#", "\\\\#");
    }

    /**
     * Gets a string representation of the provided locale.
     * @param locale The provided locale.
     * @return String representation.
     */
    private static String getLocale(Locale locale) {
        return getLocale(locale, false);
    }

    /**
     * Gets a string representation of the provided locale, converting the code
     * set if possible.
     * @param locale The provided locale.
     * @param convertCodeset Boolean value to convert the code set.
     * @return String representation.
     */
    private static String getLocale(Locale locale, boolean convertCodeset) {
        
        String identifier = "";

        if (locale != null) {
            
            String language = locale.getLanguage();

            if (language != null) {
                identifier = language;
            }

            String country = locale.getCountry();

            if ((country != null) &&
                    (!"".equals(country))) {
                
                if ("".equals(identifier)) {
                    identifier = country;
                } else {
                    identifier = identifier.concat("-").concat(country);
                }
                
            }

            String codeset = System.getProperty("file.encoding", "UTF-8");

            if ((codeset != null) &&
                    (!"".equals(codeset))) {
                
                if (convertCodeset) {
                    codeset = codeset.toLowerCase().replaceAll("-", "");
                }

                identifier = identifier.concat(".").concat(codeset);
            }

            String script = locale.getScript();

            if ((script != null) &&
                    (!"".equals(script))) {
                identifier = identifier.concat("@").concat(escapeHash(script));
            }

        }

        return identifier;
    }

    /**
     * Gets the OS name.
     * @return The OS name as string.
     */
    private static String getOSname() {
        return getSystemProperty("os.name");
    }

    /**
     * Gets the OS architecture.
     * @return The OS architecture as string.
     */
    private static String getOSarch() {
        return getSystemProperty("os.arch");
    }

    /**
     * Gets the OS version.
     * @return The OS version as string.
     */
    private static String getOSversion() {
        return getSystemProperty("os.version");
    }

    /**
     * Gets the user home.
     * @return The user home as string.
     */
    private static String getUserHome() {
        return getSystemProperty("user.home");
    }

    /*
   * 
     */
    /**
     * Converts the filename string to TeX path. Since this is designed to work
     * within TeX, backslashes in paths need to be replaced with forward
     * slashes.
     * @param filename The filename string.
     * @return TeX path.
     */
    private static String toTeXPath(String filename) {
        
        String path = "";
        
        if (filename != null) {
            if (File.separatorChar == BACKSLASH) {
                filename = filename.replaceAll("\\\\", "/");
            }
            path = escapeHash(filename);
        }
        
        return path;
    }

    /**
     * Converts the TeX path back to the original representation.
     * @param filename The filename string.
     * @return The original representation.
     */
    private static String fromTeXPath(String filename) {
        
        if (File.separatorChar != FORWARDSLASH) {
            filename = filename.replaceAll("/", File.separator);
        }

        return filename;
    }

    /**
     * Gets a file representation from a filename string.
     * @param filename Filename string.
     * @return File representation 
     */
    private static File fileFromTeXPath(String filename) {
        
        filename = fromTeXPath(filename);
        File file = new File(filename);

        if (!file.exists() && file.getParent() == null) {

            try {
                Process process = new ProcessBuilder(
                        "kpsewhich",
                        filename
                ).start();

                if (process.waitFor() == 0) {
                
                    InputStream stream = process.getInputStream();
                    
                    if (stream != null) {
                        
                        BufferedReader reader = new BufferedReader(
                                new InputStreamReader(stream)
                        );

                        String line = reader.readLine();
                        reader.close();

                        if ((line != null) && (!"".equals(line))) {
                            file = new File(fromTeXPath(line));
                        }
                    }
                }
            } catch (IOException exception1) {
                // quack quack
            } catch (InterruptedException exception2) {
                // quack quack
            }
        }

        return file;
    }

    /**
     * Gets the current working directory.
     * @return The current working directory.
     */
    private static String getCwd() {
        return getSystemProperty("user.dir");
    }

    /**
     * Gets the temporary directory.
     * @return Temporary directory.
     */
    private static String getTmpDir() {
        return getSystemProperty("java.io.tmpdir");
    }

    /**
     * Gets the current date.
     * @return The current date.
     */
    private static String pdfnow() {
        return pdfDate(Calendar.getInstance());
    }

    /**
     * Gets the date in an specific format.
     * @param calendar A calendar object.
     * @return Date in an specific format.
     */
    private static String pdfDate(Calendar calendar) {
        String tz = String.format("%1$tz", calendar);
        return String.format(
                "D:%1$tY%1$tm%1td%1$tH%1$tM%1$tS%2$s'%3$s'",
                calendar,
                tz.substring(0, 3),
                tz.substring(3)
        );
    }

    /**
     * Gets the date from a file.
     * @param file File.
     * @return The date in an specific format.
     */
    private static String pdfDate(File file) {
        
        String date = "";
        
        try {
            long millisecs = file.lastModified();
            
            if (millisecs > ZERO) {
                
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(millisecs);
                date = pdfDate(calendar);
                
            }
        } catch (SecurityException exception) {
            // quack quack
        }

        return date;
    }

    /**
     * Gets the file length.
     * @param file The file.
     * @return The length as a string.
     */
    private static String getFileLength(File file) {
        
        String output = "";
        
        try {
            long length = file.length();

            if (length > ZERO) {
                output = String.format("%d", length);
            }
        } catch (SecurityException exception) {
            // quack quack
        }

        return output;
    }

    /**
     * Gets the list of files from a directory.
     * @param separator Separator.
     * @param directory Directory.
     * @return List as a string.
     */
    private static String getFileList(String separator, File directory) {
        
        StringBuilder builder = new StringBuilder();
        
        if (directory.isDirectory()) {
        
            try {
                
                String[] list = directory.list();
                if (list != null) {
                    
                    for (int i = 0; i < list.length; i++) {
                        
                        if (i > 0) {
                            builder.append(separator);
                        }
                        
                        builder.append(escapeHash(list[i]));
                        
                    }
                }
                
            } catch (SecurityException exception) {
                // quack quack
            }
        }

        return builder.toString();
    }

    /**
     * Gets a filtered list of files from directory.
     * @param separator Separator.
     * @param regex Regular expression.
     * @param directory Directory.
     * @return Filtered list as string.
     */
    private static String getFilterFileList(String separator,
            final String regex, File directory) {
        
        StringBuilder builder = new StringBuilder();
        
        if (directory.isDirectory()) {
      
            if ((regex == null) || ("".equals(regex))) {
                builder.append(getFileList(separator, directory));
            }
            else {        
                try {
                    String[] list = directory.list(new FilenameFilter() {
                        @Override
                        public boolean accept(File dir, String name) {
                            return name.matches(regex);
                        }
                    });

                    if (list != null) {
                        
                        for (int i = 0; i < list.length; i++) {
                        
                            if (i > 0) {
                                builder.append(separator);
                            }
                            
                            builder.append(escapeHash(list[i]));
                        }
                        
                    }
                } catch (SecurityException exception) {
                    // quack quack
                }
            }
        }

        return builder.toString();
    }

    /**
     * Gets the file URI.
     * @param file The file.
     * @return The URI.
     */
    private static String fileURI(File file) {
        
        String uri = "";
        
        if (file.exists()) {
            try {
                uri = file.toURI().toString();
            } catch (SecurityException exception) {
                // quack quack
            }
        }

        return uri;
    }

    /**
     * Gets the file path.
     * @param file The file.
     * @return The path.
     */
    private static String filePath(File file) {
        
        String path = "";
        
        if (file.exists()) {
            try {
                path = toTeXPath(file.getCanonicalPath());
            } catch (SecurityException exception1) {
                // quack quack
            } catch (IOException exception2) {
                // quack quack
            }
        }

        return path;
    }

    /**
     * Gets the path for the file's parent.
     * @param file The file.
     * @return The path.
     */
    private static String parentPath(File file) {
        
        String path = "";
        
        if (file.exists()) {
            try {
                path = toTeXPath(file.getCanonicalFile().getParent());
            } catch (SecurityException exception1) {
                // quack quack
            } catch (IOException exception2) {
                // quack quack
            }
        }

        return path;
    }

    /**
     * Prints the syntax usage.
     */
    private static void syntax() {
        System.out.println("Usage: texosquery <option>...");

        System.out.println();
        System.out.println("Cross-platform OS query application");
        System.out.println("for use with TeX's shell escape.");
        System.out.println();
        System.out.println("Each query displays the result in a single line.");
        System.out.println("A blank line is printed if the requested");
        System.out.println("information is unavailable.");

        System.out.println();
        System.out.println("-h or --help\tDisplay this help message and exit");
        System.out.println("-v or --version\tDisplay version information and exit");
        System.out.println();
        System.out.println("General:");
        System.out.println();
        System.out.println("-L or --locale\t\tDisplay locale information");
        System.out.println("-l or --locale-lcs\tAs --locale but codeset ");
        System.out.println("\t\t\tin lowercase with hyphens stripped");
        System.out.println("-c or --cwd\t\tDisplay current working directory");
        System.out.println("-m or --userhome\tDisplay user's home directory");
        System.out.println("-t or --tmpdir\t\tDisplay temporary directory");
        System.out.println("-o or --osname\t\tDisplay OS name");
        System.out.println("-r or --osversion\tDisplay OS version");
        System.out.println("-a or --osarch\t\tDisplay OS architecture");
        System.out.println("-n or --pdfnow\t\tDisplay current date-time in PDF format");

        System.out.println();
        System.out.println("File Queries:");
        System.out.println();
        System.out.println("Paths should use / for the directory divider.");
        System.out.println();
        System.out.println("-d <file> or --pdfdate <file>");
        System.out.println("  Display date stamp of <file> in PDF format");
        System.out.println();
        System.out.println("-s <file> or --filesize <file>");
        System.out.println("  Display size of <file> in bytes");
        System.out.println();
        System.out.println("-i <sep> <dir> or --list <sep> <dir>");
        System.out.println("  Display list of all files in <dir> separated by <sep>");
        System.out.println();
        System.out.println("-f <sep> <regex> <dir> or --filterlist <sep> <regex> <dir>");
        System.out.println("  Display list of files in <dir> that match <regex> separated by <sep>");
        System.out.println();
        System.out.println("-u <file> or --uri <file>");
        System.out.println("  Display the URI of <file>");
        System.out.println();
        System.out.println("-p <file> or --path <file>");
        System.out.println("  Display the canonical path of <file>");
        System.out.println();
        System.out.println("-e <file> or --dirname <file>");
        System.out.println("  Display the canonical path of the parent of <file>");
    }

    /**
     * Prints the version.
     */
    private static void version() {
        System.out.println(String.format("texosquery %s %s", VERSION_NUMBER,
                VERSION_DATE));
        System.out.println("Copyright 2016 Nicola Talbot");
        System.out.println("License LPPL 1.3+ (http://ctan.org/license/lppl1.3)");
    }

    /**
     * Prints the information with optional grouping.
     * @param group Determines whether to add grouping
     * @param info Information to print
     */ 
   private static void print(boolean group, String info)
   {
      if (group)
      {
         System.out.println(String.format("{%s}", info));
      }
      else
      {
         System.out.println(info);
      }
   }

    /**
     * Main method.
     * @param args Command line arguments.
     */
    public static void main(String[] args) {
        if (args.length == 0) {
            System.err.println("Missing argument. Try texosquery --help");
            System.exit(1);
        }

        boolean group = false;

        for (int i = 0, n=args.length-1; i < args.length; i++) {
            if (args[i].equals("-L") || args[i].equals("--locale")) {
                if (i < n) group = true;

                print(group, getLocale(Locale.getDefault()));
            } else if (args[i].equals("-l") || args[i].equals("--locale-lcs")) {
                if (i < n) group = true;

                print(group, getLocale(Locale.getDefault(), true));
            } else if (args[i].equals("-c") || args[i].equals("--cwd")) {
                if (i < n) group = true;

                print(group, getCwd());
            } else if (args[i].equals("-m") || args[i].equals("--userhome")) {
                if (i < n) group = true;

                print(group, getUserHome());
            } else if (args[i].equals("-t") || args[i].equals("--tmpdir")) {
                if (i < n) group = true;

                print(group, getTmpDir());
            } else if (args[i].equals("-r") || args[i].equals("--osversion")) {
                if (i < n) group = true;

                print(group, getOSversion());
            } else if (args[i].equals("-a") || args[i].equals("--osarch")) {
                if (i < n) group = true;

                print(group, getOSarch());
            } else if (args[i].equals("-o") || args[i].equals("--osname")) {
                if (i < n) group = true;

                print(group, getOSname());
            } else if (args[i].equals("-n") || args[i].equals("--pdfnow")) {
                if (i < n) group = true;

                print(group, pdfnow());
            } else if (args[i].equals("-d") || args[i].equals("--pdfdate")) {
                i++;

                if (i >= args.length) {
                    System.err.println(
                            String.format("filename expected after %s", args[i - 1]));
                    System.exit(1);
                }

                if (i < n) group = true;

                if ("".equals(args[i])) {
                    print(group, "");
                } else {
                    print(group, pdfDate(fileFromTeXPath(args[i])));
                }
            } else if (args[i].equals("-s") || args[i].equals("--filesize")) {
                i++;

                if (i >= args.length) {
                    System.err.println(
                            String.format("filename expected after %s", args[i - 1]));
                    System.exit(1);
                }

                if (i < n) group = true;

                if ("".equals(args[i])) {
                    print(group, "");
                } else {
                    print(group, getFileLength(fileFromTeXPath(args[i])));
                }
            } else if (args[i].equals("-i") || args[i].equals("--list")) {
                i++;

                if (i >= args.length) {
                    System.err.println(
                            String.format("separator and directory name expected after %s",
                                    args[i - 1]));
                    System.exit(1);
                }

                i++;

                if (i >= args.length) {
                    System.err.println(
                            String.format("directory name expected after %s %s",
                                    args[i - 2], args[i - 1]));
                    System.exit(1);
                }

                if (i < n) group = true;

                if ("".equals(args[i])) {
                    print(group, "");
                } else {
                    print(group, getFileList(args[i - 1],
                            new File(fromTeXPath(args[i]))));
                }
            } else if (args[i].equals("-f") || args[i].equals("--filterlist")) {
                i++;

                if (i >= args.length) {
                    System.err.println(
                            String.format(
                                    "separator, regex and directory name expected after %s",
                                    args[i - 1]));
                    System.exit(1);
                }

                i++;

                if (i >= args.length) {
                    System.err.println(
                            String.format("regex and directory name expected after %s %s",
                                    args[i - 2], args[i - 1]));
                    System.exit(1);
                }

                i++;

                if (i >= args.length) {
                    System.err.println(
                            String.format("directory name expected after %s %s",
                                    args[i - 3], args[i - 2], args[i - 1]));
                    System.exit(1);
                }

                if (i < n) group = true;

                if ("".equals(args[i])) {
                    print(group, "");
                } else {
                    print(group, getFilterFileList(
                            args[i - 2], args[i - 1], new File(fromTeXPath(args[i]))));
                }
            } else if (args[i].equals("-u") || args[i].equals("--uri")) {
                i++;

                if (i >= args.length) {
                    System.err.println(
                            String.format("filename expected after %s", args[i - 1]));
                    System.exit(1);
                }

                if (i < n) group = true;

                if ("".equals(args[i])) {
                    print(group, "");
                } else {
                    print(group, fileURI(fileFromTeXPath(args[i])));
                }
            }
            else if (args[i].equals("-p") || args[i].equals("--path"))
            {
                i++;

                if (i >= args.length) {
                    System.err.println(
                            String.format("filename expected after %s", args[i - 1]));
                    System.exit(1);
                }

                if (i < n) group = true;

                if ("".equals(args[i])) {
                    print(group, "");
                } else {
                    print(group, filePath(fileFromTeXPath(args[i])));
                }
            }
            else if (args[i].equals("-e") || args[i].equals("--dirname"))
            {
                i++;

                if (i >= args.length) {
                    System.err.println(
                            String.format("filename expected after %s", args[i - 1]));
                    System.exit(1);
                }

                if (i < n) group = true;

                if ("".equals(args[i])) {
                    print(group, "");
                } else {
                    print(group, 
                     parentPath(fileFromTeXPath(args[i])));
                }
            }
            else if (args[i].equals("-h") || args[i].equals("--help")) {
                syntax();
                System.exit(0);
            } else if (args[i].equals("-v") || args[i].equals("--version")) {
                version();
                System.exit(0);
            } else {
                System.err.println(String.format("unknown option '%s'", args[i]));
                System.exit(1);
            }
        }
    }
    
    /**
     * Gets the system property.
     * @param key Key.
     * @return Value;
     */
    private static String getSystemProperty(String key) {
        
        String value = "";
        
        try {
            value = System.getProperty(key, "");
        }
        catch (SecurityException exception) {
            // quack quack
        }
        
        return value;
        
    }

}