001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package com.sun.jsfcl.app;
043:
044: import java.util.HashSet;
045: import java.util.Set;
046:
047: /**
048: * <p>Default implementation of {@link PageBeanMapper} that corresponds to
049: * the mapping performed by the IDE at design time. This implementation
050: * uses the following rules:</p>
051: * <ul>
052: * <li>Strip the leading '/' character (if any).</li>
053: * <li>String the trailing extension (if any).</li>
054: * <li>Replace any '/' characters with '$' characters</li>
055: * <li>If the resulting string matches the name of one of the implicit
056: * variables recognized by JSF EL expressions, prefix it with '_'.
057: * </ul>
058: */
059: public class PageBeanMapperImpl implements PageBeanMapper {
060:
061: // ------------------------------------------------------- Static Variables
062:
063: /**
064: * <p>The set of reserved identifiers recognized by the JavaServer Faces
065: * expression language machinery.</p>
066: */
067: private static Set reserved = new HashSet();
068:
069: static {
070: reserved.add("applicationScope"); //NOI18N
071: reserved.add("cookies"); //NOI18N
072: reserved.add("facesContext"); //NOI18N
073: reserved.add("header"); //NOI18N
074: reserved.add("headerValues"); //NOI18N
075: reserved.add("initParam"); //NOI18N
076: reserved.add("param"); //NOI18N
077: reserved.add("paramValues"); //NOI18N
078: reserved.add("requestScope"); //NOI18N
079: reserved.add("sessionScope"); //NOI18N
080: reserved.add("view"); //NOI18N
081: };
082:
083: // ------------------------------------------------- PageBeanMapper Methods
084:
085: /**
086: * <p>Map the specified view identifier (which will be the context relative
087: * path of a JSP page) to the managed bean name of the corresponding
088: * page bean (which must extend {@link AbstractPageBean}).</p>
089: *
090: * @param viewId View identifier of the JSP page to be mapped
091: */
092: public String mapViewId(String viewId) {
093:
094: if ((viewId == null) || (viewId.length() < 1)) {
095: return viewId;
096: }
097:
098: // Strip any leading '/' character
099: if (viewId.charAt(0) == '/') { //NOI18N
100: viewId = viewId.substring(1);
101: }
102:
103: // Strip any trailing extension
104: int slash = viewId.lastIndexOf('/'); //NOI18N
105: int period = viewId.lastIndexOf('.'); //NOI18N
106: if (period >= 0) {
107: if ((slash < 0) || (period > slash)) {
108: viewId = viewId.substring(0, period);
109: }
110: }
111:
112: // Replace remaining '/' characters with '$'
113: viewId = viewId.replace('/', '$'); //NOI18N
114:
115: // Prefix with '_' if necessary, and return
116: if (reserved.contains(viewId)) {
117: return "_" + viewId;
118: } else {
119: return viewId;
120: }
121:
122: }
123:
124: }
|