1 /*
2 * $Header: /cvsroot/plb4jedit/plb4jedit/Plb/src/java/net/sf/plb4jedit/plb/PlbSource.java,v 1.1 2003/11/04 17:40:31 skopp Exp $
3 *
4 * Copyright 2001 Riege Software International, All Rights Reserved.
5 *
6 * This software is the proprietary information of Riege Software International
7 * Use is subject to license terms.
8 *
9 */
10 package net.sf.plb4jedit.plb;
11
12 import java.io.*;
13 import java.util.*;
14
15 import org.gjt.sp.jedit.MiscUtilities;
16 import org.gjt.sp.util.Log;
17
18 /***
19 * Description: Anaylyze PLB source and give access to its structure (variables, labels, includes).
20 *
21 * @author Peter Schaefer
22 * @version $Header: /cvsroot/plb4jedit/plb4jedit/Plb/src/java/net/sf/plb4jedit/plb/PlbSource.java,v 1.1 2003/11/04 17:40:31 skopp Exp $
23 */
24
25 public class PlbSource {
26 final public static String cvsId = "$Header: /cvsroot/plb4jedit/plb4jedit/Plb/src/java/net/sf/plb4jedit/plb/PlbSource.java,v 1.1 2003/11/04 17:40:31 skopp Exp $";
27 private String path;
28 private String version;
29
30 private List source; // list of SourceLines
31
32 /* a list with includes defined inside this source */
33 private List includes;
34
35 private List vars;
36 private Map varLabels; // map varLabelName to SourceLine
37 private List labels;
38 private Map executionLabels; // map executionLabel to SourceLine
39 private LinkedList records; // Stack of record definition a variable resides in
40
41 private PlbSource() {}
42
43 public PlbSource(String[] lines, String path) {
44 Log.log(Log.DEBUG,this,"PlbSource for " + path + " " + lines.length + " lines");
45 this.path = path;
46 init();
47 source = new ArrayList(lines.length);
48 for (int i = 0; i < lines.length; i++) {
49 addLine(lines[i],i+1);
50 }
51 sortLists();
52 fillMaps();
53 }
54
55 public PlbSource(File f) throws FileNotFoundException, IOException {
56 path = f.getAbsolutePath();
57 init();
58 FileReader fr = new FileReader(f);
59 LineNumberReader lineReader = new LineNumberReader(fr);
60
61 source = new LinkedList();
62 String line;
63 while ((line = lineReader.readLine()) != null) {
64 addLine(line,lineReader.getLineNumber());
65 }
66 sortLists();
67 fillMaps();
68 }
69
70 private void init() {
71 version = "";
72 vars = new LinkedList();
73 includes = new LinkedList();
74 labels = new LinkedList();
75 varLabels = new HashMap();
76 executionLabels = new HashMap();
77 records = new LinkedList();
78 }
79
80 private void addLine(String line, int lineNumber) {
81 SourceLine sourceLine = new SourceLine(line,lineNumber,records);
82 source.add(sourceLine);
83 if (sourceLine.isRecordDef()) {
84 this.records.add(sourceLine.getLabel());
85 } else if (sourceLine.isRecordEnd()) {
86 this.records.removeLast();
87 }
88 if (sourceLine.isInclude()) {
89 includes.add(sourceLine);
90 }
91 if (sourceLine.isVarDef()) {
92 vars.add(sourceLine);
93 if (sourceLine.getLabel().equals("number") &&
94 sourceLine.getKeyword().equals("init"))
95 {
96 version = sourceLine.getRest().replaceAll("\"","").trim();
97 }
98 } else if (sourceLine.isLabel()) {
99 labels.add(sourceLine);
100 }
101 }
102
103 private void sortLists() {
104 Collections.sort(vars,SourceLine.LABEL_ORDERED);
105 Collections.sort(includes,SourceLine.REST_ORDERED);
106 Collections.sort(labels,SourceLine.LABEL_ORDERED);
107 }
108
109 private void fillMaps() {
110 for (Iterator it = vars.iterator(); it.hasNext();) {
111 SourceLine line = (SourceLine)it.next();
112 varLabels.put(line.getLabel().toLowerCase(),line);
113 }
114 for (Iterator it = labels.iterator(); it.hasNext();) {
115 SourceLine line = (SourceLine)it.next();
116 executionLabels.put(line.getLabel().toLowerCase(),line);
117 }
118 }
119
120 public void print() {
121 for (Iterator it = source.iterator(); it.hasNext();) {
122 System.out.println(it.next().toString());
123 }
124 }
125
126 public List getVariables() {
127 return vars;
128 }
129
130 public List getIncludes() {
131 return includes;
132 }
133
134 public List getLabels() {
135 return labels;
136 }
137
138 public String getPath() {
139 return path;
140 }
141
142 public String getVersion() {
143 return version;
144 }
145
146 public boolean isInclude() {
147 //Log.log(Log.DEBUG,this,"isInclude ? ");
148 return (!definesVariable("$version"));
149 }
150
151 public boolean isRecordInclude() { return isInclude() && dirEquals("rcd"); }
152 public boolean isIoInclude() { return isInclude() && dirEquals("io"); }
153 public boolean isMiscInclude() { return isInclude() && dirEquals("misc"); }
154
155 private boolean dirEquals(String dir) {
156 return MiscUtilities.getFileName(MiscUtilities.getParentOfPath(path)).equals(dir+File.separator);
157 }
158
159 public int getNumberOfLines() {
160 return source.size();
161 }
162
163 public boolean definesVariable(String variable) {
164 //Log.log(Log.DEBUG,this,"defines Variable: "+variable + " "+varLabels.size());
165 return varLabels.containsKey(variable);
166 }
167
168 public boolean definesLabel(String label) {
169 //Log.log(Log.DEBUG,this,"defines Label: "+ label + " "+executionLabels.size());
170 return executionLabels.containsKey(label);
171 }
172
173 public ResourceManager getResources(String plbPath ) throws FileNotFoundException, IOException {
174 ResourceManager manager = new ResourceManager(plbPath,this);
175 return manager;
176 }
177
178 public static void printMap(Map m) {
179 for (Iterator it = m.keySet().iterator(); it.hasNext();) {
180 String key = (String) it.next();
181 System.out.println(key +"=" + m.get(key));
182 }
183 }
184
185 public static void main(String[] args) throws Exception {
186 if (args.length != 2) {
187 System.out.println("Usage: plb.PlbSourceAnalyzer source.dbs plbpath");
188 System.exit(0);
189 }
190 System.out.println("Source: " + args[0]);
191 long start = System.currentTimeMillis();
192 PlbSource source = new PlbSource(new File(args[0]));
193 System.out.println("PLB_PATH: " + args[1]);
194 ResourceManager resources = source.getResources(args[1]);
195 long end = System.currentTimeMillis();
196
197 for (Iterator it = resources.getIncludes().iterator(); it.hasNext();) {
198 System.out.println(it.next());
199 }
200
201 /*for (Iterator it = resources.getLabels().keySet().iterator(); it.hasNext(); ) {
202 String label = (String) it.next();
203 System.out.println(">" + resources.getLabels().get(label));
204 }*/
205
206 Map search = resources.getVariables().subMap("rrf","rrfzzzzzz");
207 printMap(search);
208 System.out.println("s$uword->"+resources.searchVariable("s$uwordT"));
209
210 System.out.println("Number of labels: " + resources.getLabels().size());
211 System.out.println("Number of variables: " + resources.getVariables().size());
212 System.out.println("Number of includes: " + resources.getIncludes().size());
213 System.out.println("Parsing took " + (end-start) + " milliseconds.");
214 }
215 }
This page was automatically generated by Maven