001: /*
002: * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions
006: * are met:
007: *
008: * - Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: *
011: * - Redistribution in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in
013: * the documentation and/or other materials provided with the
014: * distribution.
015: *
016: * Neither the name of Sun Microsystems, Inc. or the names of
017: * contributors may be used to endorse or promote products derived
018: * from this software without specific prior written permission.
019: *
020: * This software is provided "AS IS," without a warranty of any
021: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
022: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
023: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
024: * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
025: * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
026: * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
027: * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
028: * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
029: * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
030: * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
031: * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
032: *
033: * You acknowledge that Software is not designed, licensed or intended
034: * for use in the design, construction, operation or maintenance of
035: * any nuclear facility.
036: */
037:
038: package com.sun.portal.portlet.samples.bookmarkportlet;
039:
040: class FormNameEncoder implements TypeEncoder {
041:
042: /** The html object names have to be unique within a document. An
043: * example where a problem will arise if the above condition is not
044: * met is, if you have two bookmark channels in the users desktop,
045: * and if the templates come from the provider directory
046: * ( i.e. BookmarkProvider) then the html form name is same for both
047: * the channels and opening a new url by typing in to the channel's
048: * text field will fail because the javascript target name is ambiguous.
049: * To avoid this, the html form is named after the channel name.
050: * HTML special characters in the channel name is encoded using this
051: * static method.
052: **/
053:
054: public String encode(String text) {
055:
056: StringBuffer escaped = new StringBuffer();
057: for (int i = 0; i < text.length(); i++) {
058: char c = text.charAt(i);
059: switch (c) {
060: case '!':
061: case '#':
062: case '%':
063: case '\'':
064: case ')':
065: case '+':
066: case '-':
067: case '/':
068: case ';':
069: case '=':
070: case '?':
071: case '\\':
072: case '^':
073: case '`':
074: case '|':
075: case '~':
076: case '"':
077: case '$':
078: case '&':
079: case '(':
080: case '*':
081: case ',':
082: case '.':
083: case ':':
084: case '<':
085: case '>':
086: case '@':
087: case '[':
088: case ']':
089: case '{':
090: case '}':
091: case ' ':
092: escaped.append("_");
093: continue;
094: default:
095: escaped.append(c);
096: continue;
097: }
098: }
099: return escaped.toString();
100: }
101: }
|