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  /***
19   * This class hides the construction of pointers. It helps determine the exact type of pointer and
20   * constructs it. If modifications need to be made to this behavior it will be hidden here.
21   * 
22   * @author pramirez
23   * @version $Revision$
24   * 
25   */
26  public class PointerStatementFactory implements PointerType {
27  	
28      public static PointerStatement newInstance(int line, String identifier, Value value) {
29      	int pointerType = resolvePointerType(identifier, value);
30      	PointerStatement newPointer = null;
31      	
32      	if (pointerType == DATA_LOCATION) {
33      		newPointer = new DataLocationPointer(line, identifier, value);
34      	} else if (pointerType == INCLUDE) {
35      		if (identifier.equals(CATALOG))
36      			newPointer = new CatalogPointer(line, identifier, value);
37      		else 
38      			newPointer = new IncludePointer(line, identifier, value);
39      	} else if (pointerType == DESCRIPTION) {
40      		newPointer = new DescriptionPointer(line, identifier, value);
41      	} else
42      		newPointer = new PointerStatement(UNDEFINED, line, identifier, value);
43      	
44      	return newPointer;
45      }
46      
47      private static int resolvePointerType(String identifier, Value value) {
48          int pointerType = UNDEFINED;
49          for (int i = 0; i < DESCRIPTION_NAMES.length && pointerType == UNDEFINED; i++) {
50              if (identifier.endsWith(DESCRIPTION_NAMES[i]))
51                  pointerType = DESCRIPTION;
52          }
53          if (value instanceof TextString) {
54  	        for (int i = 0; i < INCLUDE_NAMES.length && pointerType == UNDEFINED; i++) {
55  	        	if (identifier.endsWith(INCLUDE_NAMES[i]))
56  	        		pointerType = INCLUDE;
57  	        }
58  	        if (identifier.equals(CATALOG))
59  	        	pointerType = INCLUDE;
60          }
61          if (pointerType == UNDEFINED)
62              pointerType = DATA_LOCATION;
63          
64          return pointerType;
65      }
66  }