KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > core > CoreScript


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.core;
21
22 /**
23  * Encapsulates a script fragment that will be added to every page in
24  * SSL-Explorer user interface. For example, the Xtra plugin has its own
25  * JavaScript libraries that need to be placed on every page for the
26  * virtual keyboard to work. The plugin creates an instance of this
27  * class and registers it using {@link com.sslexplorer.core.CoreServlet#addPageScript(CoreScript)}.
28  * The list of registered scripts is then iterated over on ever.
29  * <p>
30  * Currently a script may be placed in one of three places, in the
31  * {@link #PAGE_HEADER}, {@link #AFTER_BODY_START} or {@link #BEFORE_BODY_END}.
32  *
33  * @author Brett Smith <brett@3sp.com>
34  * @since 0.2
35  */

36
37 public class CoreScript {
38
39     /**
40      * Type that places the script in the page header
41      */

42     public final static int PAGE_HEADER = 0;
43     
44     /**
45      * Type that places the script just after the &lt;body&gt; tag.
46      */

47     public final static int AFTER_BODY_START = 1;
48
49     /**
50      * Type that places the script just before the end &lt;body&gt; tag.
51      */

52     public final static int BEFORE_BODY_END = 2;
53     
54     // Private instance variables
55

56     private String JavaDoc language;
57     private String JavaDoc path;
58     private String JavaDoc script;
59     private String JavaDoc type;
60     private int position;
61
62     /**
63      * Constructor that by default places the script after the start body tag. Either
64      * the path of the script text must be supplied although you may omit either.
65      *
66      * @param language script language, e.g. Javascript
67      * @param path the path to script. This may be <code>null</code> if the <i>script</i> argument is supplied
68      * @param script the actual script fragment as a string. This may be <code>null</code> if the <i>path</i> argument is supplied.
69      * @param type mime type. May be omitted.
70      */

71     public CoreScript(String JavaDoc language, String JavaDoc path, String JavaDoc script, String JavaDoc type) {
72         this(AFTER_BODY_START, language, path, script, type);
73     }
74
75     /**
76      * Constructor that by default places the script after the start body tag. Either
77      * the path of the script text must be supplied although you may omit either.
78      *
79      * @param position may be one of {@link #PAGE_HEADER}, {@link #AFTER_BODY_START} or {@link #BEFORE_BODY_END}
80      * @param language script language, e.g. Javascript
81      * @param path the path to script. This may be <code>null</code> if the <i>script</i> argument is supplied
82      * @param script the actual script fragment as a string. This may be <code>null</code> if the <i>path</i> argument is supplied.
83      * @param type mime type. May be omitted.
84      */

85     public CoreScript(int position, String JavaDoc language, String JavaDoc path, String JavaDoc script, String JavaDoc type) {
86         super();
87         this.position = position;
88         this.language = language;
89         this.path = path;
90         this.script = script;
91         this.type = type;
92     }
93
94     /**
95      * Get the language or <code>null</code> if not specified. If not specified
96      * the generated HTML will not contain the <i>language</i> attribute.
97      *
98      * @return the language.
99      */

100     public String JavaDoc getLanguage() {
101         return language;
102     }
103
104     /**
105      * Set the language or <code>null</code>. If null the generated HTML will
106      * not contain the <i>language</i> attribute.
107      *
108      * @param language The language to set.
109      */

110     public void setLanguage(String JavaDoc language) {
111         this.language = language;
112     }
113
114     /**
115      * Get the path to the script or <code>null</code> if the path is not specified.
116      *
117      * @return returns the path.
118      */

119     public String JavaDoc getPath() {
120         return path;
121     }
122
123     /**
124      * Set the path to script.
125      *
126      * @param path the path to set
127      */

128     public void setPath(String JavaDoc path) {
129         this.path = path;
130     }
131
132     /**
133      * Get the script fragment text.
134      *
135      * @return returns the script fragment text.
136      */

137     public String JavaDoc getScript() {
138         return script;
139     }
140
141     /**
142      * Set the script fragment text.
143      *
144      * @param script The script fragment to set.
145      */

146     public void setScript(String JavaDoc script) {
147         this.script = script;
148     }
149
150     /**
151      * Get the MIME type or <code>null</code> if not specified.
152      *
153      * @return Returns the type.
154      */

155     public String JavaDoc getType() {
156         return type;
157     }
158
159     /**
160      * Set the MIME type of <code>null</code> to not specify.
161      *
162      * @param type The type to set.
163      */

164     public void setType(String JavaDoc type) {
165         this.type = type;
166     }
167
168     /**
169      * Get the HTML to render for this script
170      *
171      * @return HTML
172      */

173     public String JavaDoc getRenderedHTML() {
174         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
175         buf.append("<script");
176         if(language != null && !language.equals("")) {
177             buf.append(" language=\"");
178             buf.append(language);
179             buf.append("\"");
180         }
181         if(type != null && !type.equals("")) {
182             buf.append(" language=\"");
183             buf.append(language);
184             buf.append("\"");
185         }
186         if(path != null && !path.equals("")) {
187             buf.append(" SRC=\"");
188             buf.append(path);
189             buf.append("\"");
190         }
191         if(script != null && !script.equals("")) {
192             buf.append(">");
193             buf.append(script);
194             buf.append("</script>");
195         }
196         else {
197             buf.append("/>");
198         }
199         return buf.toString();
200     }
201
202     /**
203      * Return the position the script should be place at. Will be one
204      * of {@link #PAGE_HEADER}, {@link #AFTER_BODY_START} or {@link #BEFORE_BODY_END}.
205      *
206      * @return the position.
207      */

208     public int getPosition() {
209         return position;
210     }
211
212     /**
213      * Set the position the script should be place at. Will be one
214      * of {@link #PAGE_HEADER}, {@link #AFTER_BODY_START} or {@link #BEFORE_BODY_END}.
215      *
216      * @param position the position.
217      */

218     public void setPosition(int position) {
219         this.position = position;
220     }
221 }
222
Popular Tags