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 java.io.IOException;
19  import java.net.URL;
20  import java.util.ArrayList;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import gov.nasa.pds.tools.label.parser.LabelParser;
25  import gov.nasa.pds.tools.label.parser.LabelParserFactory;
26  import gov.nasa.pds.tools.label.parser.ParseException;
27  import gov.nasa.pds.tools.label.validate.Status;
28  
29  /***
30   * This class represents a pointer that is a set of external statements that can and should
31   * be included in label containing this statement when performing validation.
32   * @author pramirez
33   * @version $Revision$
34   * 
35   */
36  public class IncludePointer extends PointerStatement implements PointerType, Status {
37      protected List statements;
38      protected boolean loaded = false;
39      protected String loadStatus = null;
40      protected int numErrors;
41      protected int numWarnings;
42  
43      /***
44       * Constructs a pointer that can be resolved to a set of statements.
45       * @param lineNumber of statement
46       * @param identifier of statement
47       * @param value assigned to statement
48       */
49      public IncludePointer(int lineNumber, String identifier, Value value) {
50          super(INCLUDE, lineNumber, identifier, value);
51          statements = new ArrayList();
52          numWarnings = 0;
53          numErrors = 0;
54          loadStatus = UNKNOWN;
55  		this.externalReference = true;
56      }
57      
58      /***
59       * This method attempts to load the referenced statements. If unsuccessful will throw an error.
60       * Once loaded the statements are held in the class so they may be accessed at a later time.
61       * @param includePaths An list of {@link URL} in which to search for the referenced file
62       * @throws ParseException thrown if the file can not be properly loaded
63       * @throws IOException thrown if file can not be accessed
64       */
65      public synchronized void loadReferencedStatements(List includePaths) throws ParseException, IOException {
66          if (!loaded) {
67          	for (Iterator n = getExternalFileReferences().iterator(); n.hasNext();) {
68          		String file = (String) n.next();
69  	            URL labelURL = URLResolver.resolveURL(includePaths, this, file);
70  	            loaded = true;
71  	            LabelParser parser = LabelParserFactory.getInstance().newLabelParser();
72  	            for (Iterator i = includePaths.iterator(); i.hasNext();)
73  	                parser.addIncludePath((URL) i.next());
74  	            String labelContext = context;
75  	            if (labelContext == null)
76  	                labelContext = filename;
77  	            Label label = parser.parsePartial(labelContext, labelURL);
78  	            loadStatus = label.getStatus();
79  	            statements = label.getStatements();
80  	            numErrors = label.getNumErrors();
81  	            numWarnings = label.getNumWarnings();
82          	}
83          }
84      }
85      
86      /***
87       * Retrieves the list of statements pointed to by this structure pointer
88       * @return external list of statements
89       */
90      public List getStatements() {
91          return statements;
92      }
93      
94      /***
95       * Indicates whether or not the statements pointed to have been loaded.
96       * @return flag indicating load status
97       */
98      public boolean isLoaded() {
99          return loaded;
100     }
101     
102     public String getLoadStatus() {
103         return loadStatus;
104     }
105     
106     public int getNumErrors() {return numErrors;}
107     public int getNumWarnings() {return numWarnings;}
108 
109 }