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 com.flexive.shared.content.FxPK;
036:
037: import java.util.Collection;
038: import java.util.regex.Matcher;
039:
040: /**
041: * <p>
042: * A content URI route.It allows to both map
043: * external URIs containing instance PKs to internal JSF pages AND to generate
044: * those external URIs based on an instance PK.
045: * </p>
046: * <p>
047: * Supported placeholders:
048: * <table>
049: * <tr>
050: * <th>${id}</td>
051: * <td>The content ID</td>
052: * </tr>
053: * <tr>
054: * <th>${pk}</td>
055: * <td>The content PK</td>
056: * </tr>
057: * </p>
058: *
059: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
060: * @version $Rev: 1 $
061: */
062: public class ContentURIRoute extends URIRoute {
063: // private static final transient Log LOG = LogFactory.getLog(ContentURIMapper.class);
064: static {
065: PARAMETERS.put("id", "([0-9]+)");
066: PARAMETERS.put("pk", "([0-9]+\\.(?:[0-9]+|LIVE|MAX))");
067: }
068:
069: private final String typeName;
070:
071: /**
072: * Create a new content URI mapper with the given format and for the given type name.
073: *
074: * @param target the target URI
075: * @param format the format string
076: * @param typeName the type for which this mapper should be applied.
077: * @see com.flexive.shared.structure.FxType
078: */
079: public ContentURIRoute(String target, String format, String typeName) {
080: super (target, format);
081: this .typeName = typeName;
082: }
083:
084: public String getTypeName() {
085: return typeName;
086: }
087:
088: /**
089: * {@inheritDoc}
090: */
091: @Override
092: public ContentURIMatcher getMatcher(String uri) {
093: final Matcher matcher = pattern.matcher(uri);
094: matcher.find();
095: return new ContentURIMatcher(this , uri, matcher);
096: }
097:
098: public String getMappedUri(FxPK pk) {
099: return replaceUriParameter(replaceUriParameter(format, "id",
100: String.valueOf(pk.getId())), "pk", pk.toString());
101: }
102:
103: /**
104: * Returns the first route matching the given typename, or null when no route was found.
105: *
106: * @param routes the routes to be searched
107: * @param typeName the typename to be searched for
108: * @return the first route matching the given typename, or null when no route was found.
109: */
110: public static ContentURIRoute findForType(
111: Collection<? extends ContentURIRoute> routes,
112: String typeName) {
113: for (ContentURIRoute route : routes) {
114: if (typeName.equalsIgnoreCase(route.getTypeName())) {
115: return route;
116: }
117: }
118: return null;
119: }
120: }
|