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