View Javadoc

1   // Copyright 2006-2007, by the California Institute of Technology.
2   // ALL RIGHTS RESERVED. United States Government Sponsorship acknowledged.
3   // Any commercial use must be negotiated with the Office of Technology Transfer
4   // at the California Institute of Technology.
5   //
6   // This software is subject to U. S. export control laws and regulations
7   // (22 C.F.R. 120-130 and 15 C.F.R. 730-774). To the extent that the software
8   // is subject to U.S. export control laws and regulations, the recipient has
9   // the responsibility to obtain export licenses or other export authority as
10  // may be required before exporting such information to foreign countries or
11  // providing access to foreign nationals.
12  //
13  // $Id$ 
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          //Test and warn if filename is mixed case.
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          //Go through the list of directories and see if pointed to file can be resolved
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              //Check to see if this is the right URL for the file
72              try {
73                  fileURL.openStream();
74                  resolvedURL = fileURL;
75              } catch (IOException ioEx) {
76                  //Ignore this must not be the path to the pointed file
77              }
78              
79              //Check to see if we can find the file as upper case
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                      //Ignore this must not be the path to the pointed file
89                  }
90              }
91  
92              //Check to see if we can find the file as lower case
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                     //Ignore this must not be the path to the pointed file
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         //The name of the file will be the first element in the sequence or the value if not 
116         //contained in a sequence
117         if (value instanceof Sequence)
118             filename = ((Sequence) value).get(0).toString();
119         else
120             filename = value.toString();
121         
122         return filename;
123     }
124 }