1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package gov.nasa.pds.tools.label;
17
18 import gov.nasa.pds.tools.logging.ToolsLogRecord;
19
20 import java.io.IOException;
21 import java.net.URL;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26
27 /***
28 * This class represents a pointer statement that references an external file.
29 * @author pramirez
30 * @version $Revision$
31 *
32 */
33 public class ExternalPointer extends PointerStatement {
34 private static Logger log = Logger.getLogger(ExternalPointer.class.getName());
35
36 /***
37 * Constructs a pointer statment that references an external file.
38 * @param lineNumber of statement
39 * @param identifier of statement
40 * @param value of the assignment
41 */
42 public ExternalPointer(int pointerType, int lineNumber, String identifier, Value value) {
43 super(pointerType, lineNumber, identifier, value);
44 }
45
46 /***
47 * This method resolves the URL to the file pointed to by looking in the given include paths. Will throw
48 * an error if it can not resolve an URL. Otherwise the URL is returned.
49 * @param includePaths are the directories in which to look for the file
50 * @return URL to the pointed to file
51 * @throws IOException Thrown if pointed to file can not be resolved
52 */
53 public URL resolveURL(List includePaths) throws IOException {
54 URL resolvedURL = null;
55 String filename = "";
56
57 filename = getFile();
58
59
60 if (!filename.equals(filename.toLowerCase()) && !filename.equals(filename.toUpperCase()))
61 log.log(new ToolsLogRecord(Level.WARNING, "A pointer reference should not contain mixed case.",
62 getFilename(), context, lineNumber));
63
64
65 for (Iterator i = includePaths.iterator(); i.hasNext() && resolvedURL == null;) {
66 URL baseURL = (URL) i.next();
67 String url = baseURL.toString();
68 if (!url.endsWith("/"))
69 url += "/";
70 URL fileURL = new URL(url + filename);
71
72 try {
73 fileURL.openStream();
74 resolvedURL = fileURL;
75 } catch (IOException ioEx) {
76
77 }
78
79
80 if (resolvedURL == null) {
81 fileURL = new URL(url + filename.toUpperCase());
82 try {
83 fileURL.openStream();
84 log.log(new ToolsLogRecord(Level.WARNING, "In order to resolve the pointer the filename " +
85 "had to be forced to upper case.", getFilename(), context, lineNumber));
86 resolvedURL = fileURL;
87 } catch (IOException ioEx) {
88
89 }
90 }
91
92
93 if (resolvedURL == null) {
94 fileURL = new URL(url + filename.toLowerCase());
95 try {
96 fileURL.openStream();
97 log.log(new ToolsLogRecord(Level.WARNING, "In order to resolve the pointer the filename " +
98 "had to be forced lower case.", getFilename(), context, lineNumber));
99 resolvedURL = fileURL;
100 } catch (IOException ioEx) {
101
102 }
103 }
104 }
105
106 if (resolvedURL == null)
107 throw new IOException("Could not find referenced pointer " + filename);
108
109 return resolvedURL;
110 }
111
112 public String getFile() {
113 String filename = "";
114
115
116
117 if (value instanceof Sequence)
118 filename = ((Sequence) value).get(0).toString();
119 else
120 filename = value.toString();
121
122 return filename;
123 }
124 }