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