Prefix.java revision 1220
1187N/A/*
1187N/A * CDDL HEADER START
1187N/A *
1187N/A * The contents of this file are subject to the terms of the
1187N/A * Common Development and Distribution License (the "License").
1187N/A * You may not use this file except in compliance with the License.
1187N/A *
1187N/A * See LICENSE.txt included in this distribution for the specific
1187N/A * language governing permissions and limitations under the License.
1187N/A *
1187N/A * When distributing Covered Code, include this CDDL HEADER in each
1187N/A * file and include the License file at LICENSE.txt.
1187N/A * If applicable, add the following below this CDDL HEADER, with the
1187N/A * fields enclosed by brackets "[]" replaced with your own identifying
1187N/A * information: Portions Copyright [yyyy] [name of copyright owner]
1187N/A *
1187N/A * CDDL HEADER END
1187N/A */
1281N/A
1187N/A/*
1187N/A * Copyright (c) 2011 Jens Elkner.
1187N/A */
1187N/Apackage org.opensolaris.opengrok.web;
1187N/A
1187N/Aimport java.util.Map;
1187N/Aimport java.util.TreeMap;
1281N/A
1281N/A/**
1281N/A * URL Prefixes usually tied to a certain servlet.
1187N/A *
1187N/A * @author Jens Elkner
1187N/A * @version $Revision$
1281N/A */
1281N/Apublic enum Prefix {
1281N/A /** unknown prefix */
1187N/A UNKNOWN(""),
1187N/A /** a cross reference */
1187N/A XREF_P("/xref"),
1187N/A /** a show cross reference, i.e. add Line and Navigation toggle button in
1187N/A * the menu bar */
1281N/A XREF_S("/xr"),
1187N/A /** show more lines. If a search result set for a file matches more lines
1187N/A * than a given limit (default: 10), only the first <i>limit</i> lines gets
1281N/A * shown as an "[all ...]" link, which can be used to show all matching
1281N/A * lines. The servlet path of this link starts with this prefix. */
1281N/A MORE_P("/more"),
1187N/A /** reserved (not used) */
1187N/A MORE_S("/mo"),
1187N/A /** diff to previous version (link prefix) */
1281N/A DIFF_P("/diff"),
1281N/A /** reserved (not used) */
1281N/A DIFF_S("/di"),
1281N/A /** reserved (not used) */
1281N/A HIST_P("/hist"),
1281N/A /** reserved (not used) */
1281N/A HIST_S("/hi"),
1281N/A /** show the history for a file (link prefix) */
1281N/A HIST_L("/history"),
1281N/A /** RSS XML Feed of latest changes (link prefix) */
1281N/A RSS_P("/rss"),
1281N/A /** Download file (link prefix) */
1187N/A RAW_P("/raw"),
1281N/A /** full blown search from main page or top bar (link prefix) */
1281N/A SEARCH_P("/search"),
1281N/A /** search from cross reference, can lead to direct match (which opens
1187N/A * directly) or to a matches Summary page */
1187N/A SEARCH_R("/s"),
1187N/A /** opensearch description page */
1281N/A SEARCH_O("/opensearch"),
1281N/A /** related source file or directory not found/unavailable/ignored */
1281N/A NOT_FOUND("/enoent"),
1281N/A /** misc error occured */
1281N/A ERROR("/error")
1281N/A ;
1281N/A private String prefix;
1281N/A private Prefix(String prefix) {
1281N/A this.prefix = prefix;
1281N/A }
1187N/A
1187N/A /**
1187N/A * Get the string used as prefix.
1281N/A * @return the prefix
1281N/A */
1281N/A @Override
1281N/A public String toString() {
1281N/A return prefix;
1281N/A }
1281N/A
1281N/A // should be sufficient for now
1281N/A private static Map<String, Prefix> lookupTable;
1281N/A static {
1281N/A lookupTable = new TreeMap<String, Prefix>();
1281N/A for (Prefix p : Prefix.values()) {
1281N/A lookupTable.put(p.toString(), p);
1281N/A }
1281N/A }
1281N/A
1281N/A /**
1281N/A * Get the prefix of the given path.
1281N/A * @param servletPath path to check
1281N/A * @return {@link Prefix#UNKNOWN} if <var>path</var> is {@code null} or has
1281N/A * no or unknown prefix, the corresponding prefix otherwise.
1281N/A * @see #toString()
1281N/A */
1254N/A public static Prefix get(String servletPath) {
1281N/A if (servletPath == null || servletPath.length() < 3
1281N/A || servletPath.charAt(0) != '/')
1281N/A {
1187N/A return UNKNOWN;
1187N/A }
1187N/A int idx = servletPath.indexOf('/', 1);
1281N/A if (idx > 0) {
1187N/A servletPath = servletPath.substring(0, idx);
1281N/A }
1187N/A Prefix p = lookupTable.get(servletPath);
1187N/A return p == null ? UNKNOWN : p;
1281N/A }
1187N/A}
1187N/A