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 import org.gjt.sp.jedit.jEdit;
9 import org.gjt.sp.util.Log;
10
11 /***
12 * Description: manages Plb includes of a program to access all interesting resources of
13 * a program (other includes, variable declaration, label definitions). <br/>
14 * Notice that on an include not everything may be resolved because the resources are always relative
15 * to the source-file and contain everything defined in the souce file and its includes (recursively).
16 *
17 * @author Peter Schaefer
18 * @version $Header: /cvsroot/plb4jedit/plb4jedit/Plb/src/java/net/sf/plb4jedit/plb/ResourceManager.java,v 1.1 2003/11/04 17:40:31 skopp Exp $
19 */
20 public class ResourceManager {
21 private PathBroker pathBroker;
22 private Set includes;
23 private TreeMap variables;
24 private TreeMap labels;
25 private static IncludeCache cache;
26 private ResourceManager() {
27 }
28
29 /***
30 * constructs a ResourceManager from a PlbSource and a PLB_PATH. This path is
31 * used to resolve all includes found in the source.
32 * @param plbPath PLB_PATH to search for includes.
33 * @param program absolute path of the source file name.
34 * @throws FileNotFoundException if source can not be resolved or an include is not found in the path
35 * @throws IOException if source can not be resolved or an include is not found in the path
36 */
37 public ResourceManager(String plbPath, PlbSource program)
38 throws FileNotFoundException, IOException {
39 cache = IncludeCache.getInstance();
40 includes = new TreeSet();
41 variables = new TreeMap();
42 labels = new TreeMap();
43 pathBroker = new PathBroker(plbPath, program.getPath());
44 fillOwn(program);
45 resolveIncludes();
46 }
47
48 private void resolveIncludes() {
49 Stack todo = new Stack();
50 for (Iterator it = includes.iterator(); it.hasNext();) {
51 todo.push(it.next());
52
53 }
54 while (!todo.isEmpty()) {
55 handleOneInclude(todo);
56 }
57 }
58
59 private void handleOneInclude(Stack todo) {
60 String incPath = (String) todo.pop();
61 Include inc;
62 try {
63 inc = cache.get(incPath);
64 } catch (FileNotFoundException e) {
65 Log.log(Log.ERROR, this, "handleOneInclude: " + e.getMessage());
66 jEdit.getActiveView().getStatus().setMessage(
67 "Error parsing " + incPath);
68 return;
69 } catch (IOException e) {
70 return;
71 }
72 labels.putAll(inc.getGlobalLabels());
73 variables.putAll(inc.getGlobalVariables());
74 for (Iterator it = inc.getIncs().iterator(); it.hasNext();) {
75 String id;
76 String next = (String) it.next();
77 try {
78 id = pathBroker.searchInclude(next).getAbsolutePath();
79 } catch (FileNotFoundException e1) {
80 Log.log(
81 Log.ERROR,
82 this,
83 "handleOneInclude: searchInclude-> "
84 + e1.getMessage()
85 + incPath
86 + "/"
87 + next);
88 jEdit.getActiveView().getStatus().setMessage(
89 "Error " + incPath + "/" + next);
90 return;
91 }
92 if (!includes.contains(id)) {
93 todo.push(id);
94 includes.add(id);
95 }
96 }
97 }
98
99 /***
100 * searches for a Variable name and returns a Variable object which gives access to
101 * the source file and line-number where this variable is defined.
102 * @param varname to search for
103 * @return the variable with the name varname or null if varname is not found.
104 */
105 public Variable searchVariable(String varname) {
106 return (Variable) getVariables().get(varname);
107 }
108
109 /***
110 * searches for a label name and returns a label object, which gives access to the source
111 * file and line number where this label is defined.
112 * @param label to search for
113 * @return the Label with the given name or null if the label cann't be found.
114 */
115 public Label searchLabel(String label) {
116 return (Label) getLabels().get(label);
117 }
118
119 private void fillOwn(PlbSource source) {
120 for (Iterator it = source.getIncludes().iterator(); it.hasNext();) {
121 SourceLine line = (SourceLine) it.next();
122 File incFile;
123 try {
124 incFile = pathBroker.searchInclude(line.getRest());
125 } catch (FileNotFoundException e) {
126 Log.log(Log.ERROR,this,"fillOwn: " + e.getMessage() + "\n" + line.getRest()+"/"+line.getLine());
127 jEdit.getActiveView().getStatus().setMessage("Error: can't find " + line.getRest());
128 return;
129 }
130 String absPath = incFile.getAbsolutePath();
131 includes.add(absPath);
132 }
133 for (Iterator it = source.getLabels().iterator(); it.hasNext();) {
134 SourceLine line = (SourceLine) it.next();
135 String label = line.getLabel();
136 labels.put(
137 label.toLowerCase(),
138 new Label(
139 source.getPath(),
140 line.getLineNumber(),
141 label,
142 line.getRest()));
143 }
144
145 for (Iterator it = source.getVariables().iterator(); it.hasNext();) {
146 SourceLine line = (SourceLine) it.next();
147 String variable = line.getLabel();
148
149 variables.put(
150 variable.toLowerCase(),
151 new Variable(
152 source.getPath(),
153 line.getLineNumber(),
154 variable,
155 line.getRest()));
156
157 }
158 }
159
160 /***
161 * @return a set of inlcudes includes by this file.
162 */
163 public Set getIncludes() {
164 return includes;
165 }
166
167 /***
168 * @return containing all labels defined in the source file and its includes
169 */
170 public TreeMap getLabels() {
171 return labels;
172 }
173
174 /***
175 * @return containing all variables defined in the source file and its includes
176 */
177 public TreeMap getVariables() {
178 return variables;
179 }
180
181 }
This page was automatically generated by Maven