001: //** Copyright Statement ***************************************************
002: //The Salmon Open Framework for Internet Applications (SOFIA)
003: // Copyright (C) 1999 - 2002, Salmon LLC
004: //
005: // This program is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU General Public License version 2
007: // as published by the Free Software Foundation;
008: //
009: // This program is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: // GNU General Public License for more details.
013: //
014: // You should have received a copy of the GNU General Public License
015: // along with this program; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: //
018: // For more information please visit http://www.salmonllc.com
019: //** End Copyright Statement ***************************************************
020: package com.salmonllc.html;
021:
022: /////////////////////////
023: //$Archive: /SOFIA/SourceCode/com/salmonllc/html/HtmlBackLink.java $
024: //$Author: Srufle $
025: //$Revision: 7 $
026: //$Modtime: 4/15/03 1:57a $
027: /////////////////////////
028:
029: import java.util.Vector;
030:
031: import com.salmonllc.html.events.PageEvent;
032: import com.salmonllc.html.events.PageListener;
033: import com.salmonllc.util.MessageLog;
034:
035: /**
036: * This type gives you the ability to put a back link on a page and setup the rules for when and where it goes back.
037: */
038: public class HtmlBackLink extends HtmlContainer implements PageListener {
039: private HtmlLink _backLink;
040: private HtmlText _backLinkTextComp;
041: private String _backLinkHref;
042: private String _overRideHref;
043: private String _refererHref;
044:
045: //
046: private Vector _backLinkExclusions = new Vector();
047: private Vector _validBackLinks = new Vector();
048: //
049: private boolean STRIP_PARAMS = true;
050: private boolean OVERRIDE_REFER = false;
051: private boolean CHECK_VALID = false;
052: private boolean CHECK_EXCLUSIONS = false;
053:
054: /**
055: * Simple Constructor.
056: * @param page HtmlPage reference.
057: */
058: public HtmlBackLink(HtmlPage p) {
059: this ("FRAME_WORK", p);
060: }
061:
062: /**
063: * Constructor.
064: * @param name Name of back link.
065: * @param page HtmlPage reference.
066: */
067: public HtmlBackLink(String name, HtmlPage p) {
068: this (name, null, HtmlText.FONT_LINK, null, p);
069: }
070:
071: /**
072: * This method adds a URL to the list of places you WOULD NOT like to go back to.
073: * @param URL java.lang.String
074: */
075: public void addBackLinkExclusionURL(String URL) {
076: String savedHref = "";
077: String testStr = URL;
078:
079: // Strip query strings and get the name portion.
080: int pos = testStr.indexOf('?');
081: if (pos > -1) {
082: testStr = testStr.substring(0, pos);
083: }
084:
085: //
086: pos = testStr.lastIndexOf('/');
087: if (pos > -1) {
088: testStr = testStr.substring(pos + 1);
089: }
090:
091: // check all the valid back links.
092: // if the refer is in the list then you can NOT go back to it
093: // so just leave it alone.
094: for (int i = 0; i < _validBackLinks.size(); i++) {
095: savedHref = (String) _validBackLinks.elementAt(i);
096: if (savedHref.equalsIgnoreCase(testStr)) {
097: // URL you are trying to add is already in the valid list
098: return;
099: } else {
100: if (savedHref.indexOf(testStr) > -1) {
101: // URL you are trying to add is already in the valid list
102: return;
103: }
104: }
105: }
106:
107: // dont want to add it more than once
108: int index = _backLinkExclusions.indexOf(URL);
109: if (index == -1) {
110: _backLinkExclusions.addElement(URL);
111: }
112: }
113:
114: /**
115: * This method adds a URL to the list of places you WOULD like to go back to.
116: * @param URL java.lang.String
117: */
118: public void addValidBackLinkURL(String URL) {
119: String savedHref = "";
120: String testStr = URL;
121:
122: // Strip query strings and get the name portion.
123: int pos = testStr.indexOf('?');
124: if (pos > -1) {
125: testStr = testStr.substring(0, pos);
126: }
127:
128: //
129: pos = testStr.lastIndexOf('/');
130: if (pos > -1) {
131: testStr = testStr.substring(pos + 1);
132: }
133:
134: // check all the excluded back links.
135: // if the refer is in the list then you can NOT go back to it
136: // so just leave it alone.
137: for (int i = 0; i < _backLinkExclusions.size(); i++) {
138: savedHref = (String) _backLinkExclusions.elementAt(i);
139: if (savedHref.equalsIgnoreCase(testStr)) {
140: // URL you are trying to add is already in the exclusion list
141: return;
142: } else {
143: if (savedHref.indexOf(testStr) > -1) {
144: // URL you are trying to add is already in the exclusion list
145: return;
146: }
147: }
148: }
149:
150: // dont want to add it more than once
151: int index = _validBackLinks.indexOf(URL);
152: if (index == -1) {
153: _validBackLinks.addElement(URL);
154: } else {
155: _validBackLinks.setElementAt(URL, index);
156: }
157: }
158:
159: public void pageRequested(PageEvent p) throws Exception {
160: if (!getPage().isReferredByCurrentPage()) {
161: setBackLink();
162: }
163: }
164:
165: public void pageRequestEnd(PageEvent p) throws Exception {
166: }
167:
168: public void pageSubmitEnd(PageEvent p) {
169: }
170:
171: public void pageSubmitted(PageEvent p) {
172: }
173:
174: /**
175: * This method will remove a URL from the list of place you WOULD NOT like to go back to.
176: * @param URL java.lang.String
177: */
178: public void removeBackLinkExclusionURL(String URL) {
179: if (_backLinkExclusions == null) {
180: return;
181: }
182: int index = _backLinkExclusions.indexOf(URL);
183: if (index != -1) {
184: _backLinkExclusions.removeElementAt(index);
185: }
186: }
187:
188: /**
189: * This method removes a URL from the list of places you WOULD like to go back to.
190: * @param URL java.lang.String
191: */
192: public void removeValidBackLinkURL(String URL) {
193: if (_validBackLinks == null) {
194: return;
195: }
196: int index = _validBackLinks.indexOf(URL);
197: if (index != -1) {
198: _validBackLinks.removeElementAt(index);
199: }
200: }
201:
202: /**
203: * This method figures out from what has been setup what the back link HREF will be.
204: */
205: private void setBackLink() {
206: if (_backLink == null)
207: return;
208:
209: // Get URL of source page making current request (the "referer").
210: _refererHref = getPage().getCurrentRequest().getHeader(
211: "referer");
212: if (_refererHref == null) {
213: // This was the first page, so nothing to do.
214: return;
215: }
216: try {
217: java.net.URL u = new java.net.URL(_refererHref);
218: _refererHref = u.getFile();
219: } catch (java.net.MalformedURLException e) {
220: // Should never happen.
221: MessageLog.writeErrorMessage(e, this );
222: return;
223: }
224:
225: // Strip query strings and get the name portion.
226: int pos = -1;
227: if (STRIP_PARAMS) {
228: pos = _refererHref.indexOf('?');
229: if (pos > -1)
230: _refererHref = _refererHref.substring(0, pos);
231: }
232:
233: //get just the file portion
234: pos = _refererHref.lastIndexOf('/');
235: if (pos > -1) {
236: _refererHref = _refererHref.substring(pos + 1);
237: }
238: boolean setReferer = true;
239: if (OVERRIDE_REFER) {
240: _backLinkHref = _overRideHref;
241: } else {
242: // make referer the backLink
243: _backLinkHref = _refererHref;
244:
245: // then do the checks to replace. If none on the checks are done it will have the default behavior;
246: String savedHref = "";
247: if (CHECK_VALID) {
248: // check all the valid back links.
249: // if the refer is in the list then you can got back to it
250: setReferer = false;
251: for (int i = 0; i < _validBackLinks.size(); i++) {
252: savedHref = (String) _validBackLinks.elementAt(i);
253: if (savedHref.equalsIgnoreCase(_refererHref)) {
254: setReferer = true;
255: _backLinkHref = _refererHref;
256: break;
257: } else {
258: if (savedHref.indexOf(_refererHref) > -1) {
259: setReferer = true;
260: _backLinkHref = savedHref;
261: break;
262: }
263: }
264: }
265: } // end CHECK_VALID
266:
267: if (CHECK_EXCLUSIONS) {
268: // check all the excluded back links.
269: // if the refer is in the list then you can NOT go back to it
270: // so just leave it alone.
271: for (int i = 0; i < _backLinkExclusions.size(); i++) {
272: savedHref = (String) _backLinkExclusions
273: .elementAt(i);
274: if (savedHref.equalsIgnoreCase(_refererHref)) {
275: setReferer = false;
276: break;
277: } else {
278: if (savedHref.indexOf(_refererHref) > -1) {
279: setReferer = false;
280: break;
281: }
282: }
283: }
284: } // end CHECK_EXCLUSIONS
285:
286: }
287:
288: //
289: if (setReferer) {
290: _backLink.setHref(_backLinkHref);
291: }
292: }
293:
294: /**
295: * This method will set the back link text for you. This is helpfull when the backlink should say one thing in one mode and another in another mode.
296: */
297: public void setBackLinkText(String text) {
298: _backLinkTextComp.setText(text);
299: }
300:
301: /**
302: * This method sets whether back link exclusions are checked.
303: * default is CHECK_EXCLUSIONS = false
304: */
305: public void setCheckExclusions(boolean toggle) {
306: CHECK_EXCLUSIONS = toggle;
307: }
308:
309: /**
310: * This method sets whether valid back links are checked.
311: * default is CHECK_VALID = false
312: */
313: public void setCheckValid(boolean toggle) {
314: CHECK_VALID = toggle;
315: }
316:
317: /**
318: * This method lets you override where the back link will go.
319: */
320: public void setOverRideBackLinkHref(String href) {
321: if (href == null) {
322: // href that was passed was null just return
323: return;
324: }
325:
326: _overRideHref = href;
327: }
328:
329: /**
330: * This method sets whether the back link should look at the override backlink.
331: * default is OVERRIDE_REFER = false
332: */
333: public void setOverRideRefer(boolean toggle) {
334: OVERRIDE_REFER = toggle;
335: }
336:
337: /**
338: * This method sets whether we should strip parameters when figureing out default back links.
339: * default is STRIP_PARAMS = true
340: */
341: public void setStripParams(boolean toggle) {
342: STRIP_PARAMS = toggle;
343: }
344:
345: /**
346: * This method allows you to get the current back link href. This is helpfull if you have to add parameters to your back link.
347: * @return java.lang.String
348: */
349: public String getBackLinkHref() {
350: return _backLinkHref;
351: }
352:
353: /**
354: * This method allows you to get the refering page href.
355: * @return java.lang.String
356: */
357: public String getRefererHref() {
358: return _refererHref;
359: }
360:
361: /**
362: * Constructor.
363: * @param name Name of back link.
364: * @param backLinkText Text to put in link. if null is passed in 'Return' is provided.
365: * @param page HtmlPage reference.
366: */
367: public HtmlBackLink(String name, String backLinkText, HtmlPage p) {
368: this (name, backLinkText, HtmlText.FONT_LINK, null, p);
369:
370: }
371:
372: /**
373: * Constructor.
374: * @param name Name of back link.
375: * @param backLinkText Text to put in link. if null is passed in 'Return' is provided.
376: * @param backLinkFont This allows you to provide a link font otherwise the default LinkFont is used.
377: * @param backLinkImage HtmlImage reference to be used as a back image.
378: * @param page HtmlPage reference.
379: */
380: public HtmlBackLink(String name, String backLinkText,
381: String backLinkFont, HtmlImage backLinkImage, HtmlPage p) {
382: super (name, p);
383: _backLinkHref = "";
384: _backLink = new HtmlLink(name + "_BACK_LINK", _backLinkHref, p);
385:
386: // add passed in image for the backlink
387: if (backLinkImage != null) {
388: _backLink.add(backLinkImage);
389: }
390:
391: // create text comp to put in link. Using passed in text and LinkFont
392: if (backLinkText == null) {
393: backLinkText = "Return";
394: }
395: _backLinkTextComp = new HtmlText(backLinkText, backLinkFont, p);
396: _backLink.add(_backLinkTextComp);
397:
398: //
399:
400: p.addPageListener(this );
401:
402: //
403: add(_backLink);
404: }
405:
406: /**
407: * Constructor.
408: * @param name Name of back link.
409: * @param backLinkText Text to put in link. if null is passed in 'Return' is provided.
410: * @param backLinkImage HtmlImage reference to be used as a back image.
411: * @param page HtmlPage reference.
412: */
413: public HtmlBackLink(String name, String backLinkText,
414: HtmlImage backLinkImage, HtmlPage p) {
415: this(name, backLinkText, HtmlText.FONT_LINK, backLinkImage, p);
416:
417: }
418: }
|