source: josm/trunk/src/org/openstreetmap/josm/data/projection/datum/NTV2Proj4DirGridShiftFileSource.java@ 14159

Last change on this file since 14159 was 13647, checked in by Don-vip, 6 years ago

see #16204 - Allow to start and close JOSM in WebStart sandbox mode (where every external access is denied). This was very useful to reproduce some very tricky bugs that occured in real life but were almost impossible to diagnose.

File size: 2.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.projection.datum;
3
4import java.io.File;
5import java.io.IOException;
6import java.io.InputStream;
7import java.nio.file.Files;
8import java.nio.file.InvalidPathException;
9import java.util.Arrays;
10import java.util.Collections;
11import java.util.List;
12
13import org.openstreetmap.josm.tools.Logging;
14import org.openstreetmap.josm.tools.Platform;
15import org.openstreetmap.josm.tools.PlatformVisitor;
16import org.openstreetmap.josm.tools.Utils;
17
18/**
19 * Shift file source that scans the common data directories of the proj4 library.
20 * @since 12777
21 */
22public final class NTV2Proj4DirGridShiftFileSource implements NTV2GridShiftFileSource, PlatformVisitor<List<File>> {
23
24 private NTV2Proj4DirGridShiftFileSource() {
25 // hide constructor
26 }
27
28 // lazy initialization
29 private static class InstanceHolder {
30 static final NTV2Proj4DirGridShiftFileSource INSTANCE = new NTV2Proj4DirGridShiftFileSource();
31 }
32
33 /**
34 * Get the singleton instance of this class.
35 * @return the singleton instance of this class
36 */
37 public static NTV2Proj4DirGridShiftFileSource getInstance() {
38 return InstanceHolder.INSTANCE;
39 }
40
41 @Override
42 public InputStream getNTV2GridShiftFile(String gridFileName) {
43 File grid = null;
44 // Check is the grid is installed in default PROJ.4 directories
45 for (File dir : Platform.determinePlatform().accept(this)) {
46 File file = new File(dir, gridFileName);
47 if (file.exists() && file.isFile()) {
48 grid = file;
49 break;
50 }
51 }
52 // If not, search into PROJ_LIB directory
53 if (grid == null) {
54 String projLib = Utils.getSystemProperty("PROJ_LIB");
55 if (projLib != null && !projLib.isEmpty()) {
56 File dir = new File(projLib);
57 if (dir.exists() && dir.isDirectory()) {
58 File file = new File(dir, gridFileName);
59 if (file.exists() && file.isFile()) {
60 grid = file;
61 }
62 }
63 }
64 }
65 if (grid != null) {
66 try {
67 return Files.newInputStream(grid.getAbsoluteFile().toPath());
68 } catch (IOException | InvalidPathException ex) {
69 Logging.warn("Unable to open NTV2 grid shift file: " + grid);
70 Logging.debug(ex);
71 }
72 }
73 return null;
74 }
75
76 @Override
77 public List<File> visitUnixoid() {
78 return Arrays.asList(new File("/usr/local/share/proj"), new File("/usr/share/proj"));
79 }
80
81 @Override
82 public List<File> visitWindows() {
83 return Arrays.asList(new File("C:\\PROJ\\NAD"));
84 }
85
86 @Override
87 public List<File> visitOsx() {
88 return Collections.emptyList();
89 }
90}
Note: See TracBrowser for help on using the repository browser.