001: /**
002: * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, version 2.1, dated February 1999.
003: *
004: * This program is free software; you can redistribute it and/or modify
005: * it under the terms of the latest version of the GNU Lesser General
006: * Public License as published by the Free Software Foundation;
007: *
008: * This program is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
011: * GNU Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public License
014: * along with this program (LICENSE.txt); if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
016: */package org.jamwiki.utils;
017:
018: import java.util.Iterator;
019: import java.util.Map;
020: import org.apache.commons.lang.StringUtils;
021: import org.jamwiki.WikiConfiguration;
022:
023: /**
024: * Class for controlling "namespace". Namespaces allow the organization of
025: * wiki topics by dividing topics into groups. A namespace will precede the
026: * topic, such as "Namespace:Topic". Namespaces can be customized by
027: * modifying the configuration values in
028: * <code>/WEB-INF/classes/jamwiki-configuration.xml</code>.
029: *
030: * @see org.jamwiki.WikiConfiguration
031: */
032: public class NamespaceHandler {
033:
034: /** Logger */
035: private static final WikiLogger logger = WikiLogger
036: .getLogger(NamespaceHandler.class.getName());
037:
038: public static final String NAMESPACE_SEPARATOR = ":";
039: public static final String NAMESPACE_SPECIAL = initializeNamespace(
040: "special", false);
041: public static final String NAMESPACE_COMMENTS = initializeNamespace(
042: "main", true);
043: public static final String NAMESPACE_IMAGE = initializeNamespace(
044: "image", false);
045: public static final String NAMESPACE_IMAGE_COMMENTS = initializeNamespace(
046: "image", true);
047: public static final String NAMESPACE_CATEGORY = initializeNamespace(
048: "category", false);
049: public static final String NAMESPACE_CATEGORY_COMMENTS = initializeNamespace(
050: "category", true);
051: public static final String NAMESPACE_JAMWIKI = initializeNamespace(
052: "jamwiki", false);
053: public static final String NAMESPACE_JAMWIKI_COMMENTS = initializeNamespace(
054: "jamwiki", true);
055: public static final String NAMESPACE_TEMPLATE = initializeNamespace(
056: "template", false);
057: public static final String NAMESPACE_TEMPLATE_COMMENTS = initializeNamespace(
058: "template", true);
059: public static final String NAMESPACE_USER = initializeNamespace(
060: "user", false);
061: public static final String NAMESPACE_USER_COMMENTS = initializeNamespace(
062: "user", true);
063: public static final String NAMESPACE_HELP = initializeNamespace(
064: "help", false);
065: public static final String NAMESPACE_HELP_COMMENTS = initializeNamespace(
066: "help", true);
067:
068: /**
069: *
070: */
071: private NamespaceHandler() {
072: }
073:
074: /**
075: *
076: */
077: public static String getCommentsNamespace(String namespace) {
078: if (StringUtils.isBlank(namespace)) {
079: // main namespace
080: return NAMESPACE_COMMENTS;
081: }
082: Map namespaces = WikiConfiguration.getInstance()
083: .getNamespaces();
084: Iterator iterator = namespaces.keySet().iterator();
085: while (iterator.hasNext()) {
086: String key = (String) iterator.next();
087: String[] values = (String[]) namespaces.get(key);
088: String main = values[0];
089: String comments = values[1];
090: if (namespace.equals(NAMESPACE_SPECIAL)) {
091: return NAMESPACE_SPECIAL;
092: }
093: if (namespace.equals(main)
094: || (comments != null && namespace.equals(comments))) {
095: return comments;
096: }
097: }
098: // unrecognized namespace
099: return NAMESPACE_COMMENTS + NAMESPACE_SEPARATOR + namespace;
100: }
101:
102: /**
103: *
104: */
105: public static String getMainNamespace(String namespace) {
106: if (StringUtils.isBlank(namespace)) {
107: // main namespace
108: return "";
109: }
110: Map namespaces = WikiConfiguration.getInstance()
111: .getNamespaces();
112: Iterator iterator = namespaces.keySet().iterator();
113: while (iterator.hasNext()) {
114: String key = (String) iterator.next();
115: String[] values = (String[]) namespaces.get(key);
116: String main = values[0];
117: String comments = values[1];
118: if (namespace.equals(main)
119: || (comments != null && namespace.equals(comments))) {
120: return main;
121: }
122: }
123: // unrecognized namespace
124: return namespace;
125: }
126:
127: /**
128: *
129: */
130: private static final String initializeNamespace(String name,
131: boolean isComments) {
132: Map namespaces = WikiConfiguration.getInstance()
133: .getNamespaces();
134: Iterator iterator = namespaces.keySet().iterator();
135: while (iterator.hasNext()) {
136: String key = (String) iterator.next();
137: String[] values = (String[]) namespaces.get(key);
138: String main = values[0];
139: String comments = values[1];
140: if (name.equals(key)) {
141: return (isComments) ? comments : main;
142: }
143: }
144: logger.warning("Namespace not found in configuration: " + name);
145: return null;
146: }
147: }
|