1 package net.sf.plb4jedit.plb;
2
3 import java.io.File;
4 import java.io.FileNotFoundException;
5 import java.io.IOException;
6 import java.util.*;
7
8 /***
9 * Represents a plb include. Keeps track of the objects of interest in an include, which are global
10 * accessible variable and label definitions and a list of other included includes. <br/>
11 *
12 * Instances should be received by the IncludeCache, so there is no public constructor.
13 *
14 * @author Peter Schaefer
15 */
16 public class Include {
17 /*** label/variable names starting with LOCAL_DEF are "local" ones */
18 private static String LOCAL_DEF = "#";
19 /*** physical File of this include */
20 private File inc;
21 /*** list of includes included by this include */
22 private List includes;
23 /*** a Map of all global variables in this include. Key is variable name in lower case.
24 * value is a Variable object.
25 */
26 private Map globalVariables;
27 /***
28 * a Map of all global accessible labels in this include. Key is the label name in lower case.
29 * Value is a Value object.
30 */
31 private Map globalLabels;
32
33 /***
34 * constructs an Include object from the file specified by its absolute path.
35 * <br/>
36 * Take advantage of include caching and get instances via getInstance!
37 * @param absolutePath to the location of an include
38 * @throws FileNotFoundException if a File specified by absolutePath can not be instantiated
39 * @throws IOException if a File specified by absolutePath can not be instantiated
40 */
41 protected Include(String absolutePath)
42 throws FileNotFoundException, IOException {
43 scan(absolutePath);
44 }
45
46 /***
47 * gives access to Include Instances via an IncludeCache.
48 * @param absolutePath to the location of an include
49 * @return
50 * @throws FileNotFoundException
51 * @throws IOException
52 */
53 public static Include getInstance(String absolutePath)
54 throws FileNotFoundException, IOException {
55 return IncludeCache.getInstance().get(absolutePath);
56 }
57
58 /***
59 * helper which constructs an include object via a PlbSource-object of the include,
60 * which gives access to label and variable definitions.
61 *
62 * @param absolutePath
63 * @throws FileNotFoundException
64 * @throws IOException
65 */
66 private void scan(String absolutePath)
67 throws FileNotFoundException, IOException {
68 inc = new File(absolutePath);
69 if (!inc.exists()) {
70 throw new FileNotFoundException(
71 "not a valid include " + absolutePath);
72 }
73 includes = new ArrayList();
74 globalVariables = new HashMap();
75 globalLabels = new HashMap();
76 PlbSource source = new PlbSource(inc);
77 for (Iterator it = source.getIncludes().iterator(); it.hasNext();) {
78 SourceLine line = (SourceLine) it.next();
79 includes.add(line.getRest());
80 }
81 for (Iterator it = source.getLabels().iterator(); it.hasNext();) {
82 SourceLine line = (SourceLine) it.next();
83 String label = line.getLabel();
84 if (!label.startsWith(LOCAL_DEF)) {
85 globalLabels.put(
86 label.toLowerCase(),
87 new Label(
88 inc.getAbsolutePath(),
89 line.getLineNumber(),
90 label,
91 line.getRest()));
92 }
93 }
94 for (Iterator it = source.getVariables().iterator(); it.hasNext();) {
95 SourceLine line = (SourceLine) it.next();
96 String variable = line.getLabel();
97 if (!variable.startsWith(LOCAL_DEF)) {
98 globalVariables.put(
99 variable.toLowerCase(),
100 new Variable(
101 inc.getAbsolutePath(),
102 line.getLineNumber(),
103 variable,
104 line.getRest()));
105 }
106 }
107 source = null;
108 }
109
110 /***
111 * main method for testing purpose.<br/>
112 * Constructs an include object for the given absolute path as first command line
113 * argument. Prints out all incs, vars and labels found in the include.
114 *
115 * @param args absolute path to an include.
116 * @throws FileNotFoundException
117 * @throws IOException
118 */
119 public static void main(String[] args)
120 throws FileNotFoundException, IOException {
121 /* for this test only. Otherwise it is recommende to use the IncludeCache functionality*/
122 Include inc = new Include(args[0]);
123 for (Iterator it = inc.getIncs().iterator(); it.hasNext();) {
124 System.out.println("** inc ** " + it.next());
125 }
126 for (Iterator it = inc.getGlobalLabels().keySet().iterator();
127 it.hasNext();
128 ) {
129 System.out.println("** label ** " + it.next());
130 }
131 for (Iterator it = inc.getGlobalVariables().keySet().iterator();
132 it.hasNext();
133 ) {
134 System.out.println("** var ** " + it.next());
135 }
136 }
137
138 /***
139 * @return a map of all global Labels of this include
140 */
141 public Map getGlobalLabels() {
142 return globalLabels;
143 }
144
145 /***
146 * @return a map of all global variables of this include
147 */
148 public Map getGlobalVariables() {
149 return globalVariables;
150 }
151
152 /***
153 * @return a list of all includes included in this include
154 */
155 public List getIncs() {
156 return includes;
157 }
158
159 }
This page was automatically generated by Maven