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

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

enable new PMD rule AvoidFileStream - see https://pmd.github.io/pmd-6.0.0/pmd_rules_java_performance.html#avoidfilestream / https://bugs.openjdk.java.net/browse/JDK-8080225 for details

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