001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2008
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.shared;
034:
035: import com.flexive.shared.content.FxPK;
036: import com.flexive.shared.search.FxPaths;
037: import org.apache.commons.lang.StringUtils;
038:
039: /**
040: * Provides the default mapper for content URIs. The format method takes a FxPK identifying an
041: * object and returns an URI for the given object.
042: *
043: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
044: * @version $Rev: 181 $
045: */
046: public class ContentLinkFormatter {
047: public static final String DEFAULT_ADMIN_CONTENT = "<a href=\"adm/content/contentEditor.jsf?action=editInstance&pk=%{id}.%{version}\">%{id}.%{version}</a>";
048: public static final String DEFAULT_ADMIN_ITEM = "<a href=\"adm/content/contentEditor.jsf?action=editInstance&id=%{id}\">%{caption}</a>";
049:
050: private static final ContentLinkFormatter INSTANCE = new ContentLinkFormatter();
051: private static final int MAX_CAPTION_LEN = 20;
052: private static final String PATH_DELIM = " / ";
053:
054: /**
055: * Protected c'tor to avoid instantiation
056: */
057: protected ContentLinkFormatter() {
058: }
059:
060: public static ContentLinkFormatter getInstance() {
061: return INSTANCE;
062: }
063:
064: /**
065: * <p>Uses the given format string to create a hyperlink for the given primary key.
066: * Supported placeholders are:</p>
067: * <table>
068: * <tr>
069: * <th>%{pk}</th>
070: * <td>The primary key in "dot" notation. For example, new FxPK(42, 1)
071: * results in "42.1" being substituted in the URI.</td>
072: * </tr>
073: * <tr>
074: * <th>%{id}</th>
075: * <td>The object ID.</td>
076: * </tr>
077: * <tr>
078: * <th>%{version}</th>
079: * <td>The object version.</td>
080: * </tr>
081: * </table>
082: *
083: * @param formatString the input format string. For example, <code>"/content/%{id}.html"</code>
084: * @param pk the content PK to be formatted
085: * @return the resulting hyperlink
086: */
087: public String format(String formatString, FxPK pk) {
088: return StringUtils.defaultString(formatString,
089: DEFAULT_ADMIN_CONTENT).replace("%{id}",
090: String.valueOf(pk.getId())).replace("%{version}",
091: String.valueOf(pk.getVersion())).replace("%{pk}",
092: pk.toString());
093: }
094:
095: /**
096: * <p>Uses the given format string to create an URI for the given tree path items.
097: * If more than one path is contained in the given parameter, the paths are joined with
098: * a ',' character.
099: * Supported placeholders are:</p>
100: * <table>
101: * <tr>
102: * <th>%{pk}</th>
103: * <td>The primary key in "dot" notation. For example, new FxPK(42, 1)
104: * results in "42.1" being substituted in the URI.</td>
105: * </tr>
106: * <tr>
107: * <th>%{id}</th>
108: * <td>The object ID.</td>
109: * </tr>
110: * <tr>
111: * <th>%{version}</th>
112: * <td>The object version.</td>
113: * </tr>
114: * <tr>
115: * <th>%{nodeId}</th>
116: * <td>The tree node ID.</td>
117: * </tr>
118: * <tr>
119: * <th>%{caption}</th>
120: * <td>The item caption (will be URL-escaped).</td>
121: * </tr>
122: * </table>
123: *
124: * @param formatString the input format string. For example, <code>"/content/%{id}.html"</code>
125: * @param paths the tree paths to be formatted
126: * @return the resulting URI string
127: */
128: public String format(String formatString, FxPaths paths) {
129: StringBuilder out = new StringBuilder(255);
130: for (FxPaths.Path path : paths.getPaths()) {
131: out.append(out.length() > 0 ? ", " : "");
132: for (FxPaths.Item item : path.getItems()) {
133: out.append(format(formatString, item)).append(
134: PATH_DELIM);
135: }
136: if (!path.getItems().isEmpty()) {
137: out.delete(out.length() - PATH_DELIM.length(), out
138: .length());
139: }
140: }
141: return out.toString();
142: }
143:
144: protected String format(String formatString, FxPaths.Item item) {
145: final String result = format(
146: StringUtils.defaultString(formatString,
147: DEFAULT_ADMIN_ITEM),
148: new FxPK(item.getReferenceId())).replace("%{nodeId}",
149: String.valueOf(item.getNodeId()));
150: return result.replace("%{caption}", StringUtils.abbreviate(item
151: .getCaption(), MAX_CAPTION_LEN));
152: }
153: }
|