001: /*
002: * Copyright Javelin Software, All rights reserved.
003: */
004:
005: package com.javelin.swinglets;
006:
007: import java.awt.*;
008: import java.util.*;
009: import java.io.*;
010: import java.net.*;
011: import javax.servlet.*;
012:
013: /**
014: * SInclude lets you do server side includes easily
015: *
016: * @author Robin Sharp
017: */
018:
019: public class SInclude extends SComponent {
020: /**
021: * Construct an unqualified include.
022: * <p>
023: * The url or URL or File or Class, Path mut be set.
024: */
025: public SInclude() {
026: }
027:
028: /**
029: * Construct a fully qualified include from a url.
030: */
031: public SInclude(String url) {
032: this .url = getSwingletManager().getRealPath() + url;
033: }
034:
035: /**
036: * Construct a fully qualified include from a file.
037: */
038: public SInclude(File file) {
039: this .file = file;
040: }
041:
042: /**
043: * Construct a fully qualified include from a url.
044: */
045: public SInclude(URL url) {
046: this .url = url.toExternalForm();
047: }
048:
049: /**
050: * Construct a fully qualified include from a class and path.
051: */
052: public SInclude(Class relativeClass, String path) {
053: this .relativeClass = relativeClass;
054: this .path = path;
055: }
056:
057: /**
058: * Is this a file
059: */
060: public boolean isFile() {
061: return file != null;
062: }
063:
064: /**
065: * Is this a URL
066: */
067: public boolean isURL() {
068: return url != null;
069: }
070:
071: /**
072: * Is this a relative file
073: */
074: public boolean isRelative() {
075: return relativeClass != null && path != null;
076: }
077:
078: /**
079: * Get the URL
080: */
081: public String getUrl() {
082: return url;
083: }
084:
085: /**
086: * Set the URL
087: */
088: public SInclude setUrl(URL url) {
089: return setUrl(url.toExternalForm());
090: }
091:
092: /**
093: * Set the URL
094: */
095: public SInclude setUrl(String url) {
096: this .url = url;
097:
098: this .file = null;
099: this .relativeClass = null;
100: this .path = null;
101:
102: return this ;
103: }
104:
105: /**
106: * Get the File
107: */
108: public File getFile() {
109: return file;
110: }
111:
112: /**
113: * Set the File.
114: */
115: public SInclude setFile(File file) {
116: this .file = file;
117:
118: this .url = null;
119: this .relativeClass = null;
120: this .path = null;
121:
122: return this ;
123: }
124:
125: /**
126: * Get the Relative Class
127: */
128: public Class getRelativeClass() {
129: return relativeClass;
130: }
131:
132: /**
133: * Set the Relative Class.
134: */
135: public SInclude setRelativeClass(Class relativeClass) {
136: this .relativeClass = relativeClass;
137:
138: this .url = null;
139: this .file = null;
140:
141: return this ;
142: }
143:
144: /**
145: * Get the Path.
146: */
147: public String getRelativePath() {
148: return path;
149: }
150:
151: /**
152: * Set the Path.
153: */
154: public SInclude setPath(String path) {
155: this .path = path;
156:
157: this .url = null;
158: this .file = null;
159:
160: return this ;
161: }
162:
163: /**
164: * By default the output is included as is, exact copy, char by char.
165: * But if verbatim is set to false then the text is readin/writeout line by line
166: */
167: public SInclude setVerbatim(boolean verbatim) {
168: this .verbatim = verbatim;
169: return this ;
170: }
171:
172: /**
173: * Is the output readin/writeout char by char.
174: */
175: public boolean isVerbatim() {
176: return verbatim;
177: }
178:
179: /**
180: * By default the output is included as is, and the look and feel
181: * interprets the text. If preformatted is true the look and feel
182: * does not intrepret the text. (e.g. <PRE> in html).
183: */
184: public SInclude setPreformatted(boolean preformatted) {
185: this .preformatted = preformatted;
186: return this ;
187: }
188:
189: /**
190: * Is the include prefromatted.
191: */
192: public boolean isPreformatted() {
193: return preformatted;
194: }
195:
196: /**
197: * Returns the name of the L&F class that renders this component.
198: */
199: public Class getUIClass() {
200: return SInclude.class;
201: }
202:
203: public String toString() {
204: BufferedReader in = null;
205: StringBuffer srcBuf;
206:
207: try {
208: if (file != null) {
209: in = new BufferedReader(new FileReader(file));
210: } else if (url != null) {
211: java.net.URL realUrl = new URL(url);
212: in = new BufferedReader(new InputStreamReader(realUrl
213: .openStream()));
214: } else if (relativeClass != null && path != null) {
215: in = new BufferedReader(new InputStreamReader(
216: relativeClass.getResourceAsStream(path)));
217: } else {
218: return "";
219: }
220:
221: srcBuf = new StringBuffer(1024);
222: if (verbatim) {
223: int val;
224: while ((val = in.read()) != -1) {
225: srcBuf.append((char) val);
226: }
227: } else {
228: String s;
229: while ((s = in.readLine()) != null) {
230: srcBuf.append(s).append((char) '\n');
231: }
232: }
233: return srcBuf.toString();
234: } catch (Exception ex) {
235: //ex.printStackTrace();
236: } finally {
237: if (in != null) {
238: try {
239: in.close();
240: } catch (Exception ex2) {
241:
242: }
243: }
244: }
245:
246: return "";
247: }
248:
249: // PROTECTED ////////////////////////////////////////////////////
250:
251: protected String url;
252: protected File file;
253: protected Class relativeClass;
254: protected String path;
255:
256: boolean verbatim = true;
257: boolean preformatted = false;
258:
259: }
|