001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2007
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.faces;
034:
035: import java.util.*;
036:
037: /**
038: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
039: * @version $Rev: 1 $
040: */
041: public class URIRouteCollection<T extends URIRoute> {
042: // target --> route lookup table
043: private final Map<String, T> routes = new HashMap<String, T>();
044:
045: public URIRouteCollection(List<T> routes) {
046: for (T mapper : routes) {
047: this .routes.put(mapper.getTarget(), mapper);
048: }
049: }
050:
051: /**
052: * Returns a matcher for the given URI, or null if no matcher was found.
053: *
054: * @param uri the URI to be matched
055: * @return a matcher for the given URI, or null if no matcher was found.
056: */
057: public URIMatcher getMatcher(String uri) {
058: for (Map.Entry<String, T> entry : routes.entrySet()) {
059: final URIMatcher matcher = entry.getValue().getMatcher(uri);
060: if (matcher.isMatched()) {
061: return matcher;
062: }
063: }
064: return null;
065: }
066:
067: /**
068: * Returns a route for the given target URI.
069: *
070: * @param targetUri the target URI to be matched
071: * @return a route for the given target URI, or null if none exists.
072: */
073: public T findForTarget(String targetUri) {
074: return routes.get(targetUri);
075: }
076:
077: /**
078: * Returns a route for the given external URI.
079: *
080: * @param uri the external URI to be matched
081: * @return a route for the given external URI, or null if none exists.
082: */
083: public T findForUri(String uri) {
084: for (Map.Entry<String, T> entry : routes.entrySet()) {
085: final URIMatcher matcher = entry.getValue().getMatcher(uri);
086: if (matcher.isMatched()) {
087: return entry.getValue();
088: }
089: }
090: return null;
091: }
092:
093: /**
094: * Returns all available routes.
095: *
096: * @return all available routes.
097: */
098: public Collection<T> getRoutes() {
099: return Collections.unmodifiableCollection(routes.values());
100: }
101: }
|