KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > bsf > dbline > Buffer


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2002 The Apache Software Foundation. All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if
20  * any, must include the following acknowlegement:
21  * "This product includes software developed by the
22  * Apache Software Foundation (http://www.apache.org/)."
23  * Alternately, this acknowlegement may appear in the software itself,
24  * if and wherever such third-party acknowlegements normally appear.
25  *
26  * 4. The names "Apache BSF", "Apache", and "Apache Software Foundation"
27  * must not be used to endorse or promote products derived from
28  * this software without prior written permission. For written
29  * permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache"
32  * nor may "Apache" appear in their names without prior written
33  * permission of the Apache Group.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many individuals
50  * on behalf of the Apache Software Foundation and was originally created by
51  * Sanjiva Weerawarana and others at International Business Machines
52  * Corporation. For more information on the Apache Software Foundation,
53  * please see <http://www.apache.org/>.
54  */

55
56 package org.apache.bsf.dbline;
57
58 import java.io.*;
59 import java.util.*;
60 import org.apache.bsf.*;
61
62 /**
63  * A buffer represents a document such as a JSP.
64  * Note that lines start at zero, so the first line
65  * has a line number of zero.
66  *
67  * A document is described by its URI as known in
68  * the servlet engine.
69  * The URI includes the Web application prefix.
70  * For instance: /examples/jsp/cal/cal1.jsp
71  * with /examples being the application prefix.
72  *
73  * Note: the name is the shortname, that is, the last
74  * name of the URI. This allows easy manipulation
75  * from the command line in the debugger.
76  *
77  * @author: Olivier Gruber
78  */

79
80 public class Buffer {
81
82   String JavaDoc m_uri; // Document URI in the servlet engine.
83
String JavaDoc m_name; // Short name for this document.
84
String JavaDoc m_filename;
85   
86   String JavaDoc m_lines[]; // actual text of the document once loaded.
87
Vector m_breakpoints; // known breakpoints in this document.
88

89   int m_currentLine; // current line when listing this buffer out on
90
// the console.
91

92 // Vector m_fnOrScripts;
93

94     public Buffer(String JavaDoc uri,String JavaDoc filename,File file) {
95         int index;
96         m_filename = filename;
97         m_lines = loadAndParse2(file);
98         index = m_filename.lastIndexOf(File.separatorChar);
99         m_name = m_filename.substring(index+1);
100         m_uri= uri;
101
102 // m_fnOrScripts = new Vector();
103
m_breakpoints = new Vector();
104     }
105     
106     public void addBreakpoint(BreakPoint bp) {
107         m_breakpoints.addElement(bp);
108     }
109     
110     ///////////////////////////////////////////////
111
StringBuffer JavaDoc buildFnOrScript(int start,int end) {
112
113         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
114         String JavaDoc line;
115   
116         if (start<0) start = 0;
117         if (end>m_lines.length) end = m_lines.length;
118         
119         for (int l = start; l < end ; l++) {
120             line = m_lines[l];
121             buf.append(line+"\n");
122         }
123         return buf;
124     }
125     ///////////////////////////////////////////////
126
/*
127     public FnOrScript exec(int start, int end) {
128         FnOrScript fnOrScript;
129         try {
130             fnOrScript = new FnOrScript(this,start,end);
131             m_fnOrScripts.addElement(fnOrScript);
132             return fnOrScript;
133         } catch (Throwable t) {
134             t.printStackTrace();
135             return null;
136         }
137     }
138     */

139     ///////////////////////////////////////////////
140
public static Buffer factory(String JavaDoc filename, String JavaDoc uri) {
141         int index;
142         Buffer buffer=null;
143         char sep = File.separatorChar;
144
145         filename = filename.replace('/',sep);
146         filename = filename.trim();
147
148         File file = new File(filename);
149         if (file.exists()) {
150             buffer = new Buffer(uri,filename,file);
151         } else {
152             System.out.println("File "+filename+" does not exist.");
153         }
154         return buffer;
155     }
156     ///////////////////////////////////////////////
157
public BreakPoint getBreakpoint(int id) {
158         BreakPoint bp;
159         Enumeration e;
160         e = m_breakpoints.elements();
161         while (e.hasMoreElements()) {
162             bp = (BreakPoint)e.nextElement();
163             if (bp.m_id==id) return bp;
164         }
165         return null;
166     }
167     ///////////////////////////////////////////////
168
public BreakPoint removeBreakpoint(int id) {
169         BreakPoint bp = getBreakpoint(id);
170         m_breakpoints.removeElement(bp);
171         return bp;
172     }
173     ///////////////////////////////////////////////
174
public Enumeration getBreakpoints() {
175         return m_breakpoints.elements();
176     }
177     ///////////////////////////////////////////////
178
public int getCurrentLine() {
179       return m_currentLine;
180     }
181     public void setCurrentLine(int lineno) {
182         m_currentLine = lineno;
183     }
184     ///////////////////////////////////////////////
185
public String JavaDoc getFileName() { return m_filename; }
186     ///////////////////////////////////////////////
187
public int getLineCount() {
188         return m_lines.length;
189     }
190     ///////////////////////////////////////////////
191
public String JavaDoc getLine(int lineno) {
192         try {
193             return m_lines[lineno];
194         } catch (Throwable JavaDoc t) {
195             return null;
196         }
197     }
198     ///////////////////////////////////////////////
199
public String JavaDoc getName() { return m_name; }
200     ///////////////////////////////////////////////
201
public String JavaDoc getURI() { return m_uri; }
202
203
204     ///////////////////////////////////////////////
205
private String JavaDoc[] loadAndParse2(File file) {
206         int b, size, bytes_read = 0;
207         int count;
208         FileInputStream in;
209         byte bytes[];
210         String JavaDoc buf, line;
211         Vector lines;
212         StringTokenizer lineTokenizer;
213
214         lines = new Vector();
215     
216         try {
217             // read in the bytes...
218
size = (int) file.length();
219             in = new FileInputStream(file);
220             bytes = new byte[size];
221             count = 0;
222             while (bytes_read < size) {
223                 b = in.read();
224                 bytes[count++] = (byte)b;
225                 bytes_read++;
226                 if (b==(int)'\n') {
227                     line = new String JavaDoc(bytes,0,count-1);
228                     lines.addElement(line);
229                     count=0;
230                 }
231             }
232         } catch (Throwable JavaDoc t) {
233             t.printStackTrace();
234             return null;
235         }
236         String JavaDoc tmp[] = new String JavaDoc[lines.size()];
237         System.arraycopy(lines.toArray(),0,tmp,0,tmp.length);
238         return tmp;
239     }
240
241 }
242
Popular Tags