001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.wiki.util;
022:
023: import com.efsol.friki.PageRepository;
024:
025: import com.liferay.portal.kernel.portlet.LiferayPortletURL;
026: import com.liferay.portal.kernel.util.StringPool;
027: import com.liferay.portal.kernel.util.StringUtil;
028: import com.liferay.util.Http;
029:
030: import java.io.IOException;
031: import java.io.StringReader;
032: import java.io.StringWriter;
033:
034: import java.util.HashMap;
035: import java.util.Iterator;
036: import java.util.Map;
037: import java.util.StringTokenizer;
038:
039: import javax.portlet.PortletURL;
040:
041: import org.stringtree.factory.memory.MapStringRepository;
042:
043: /**
044: * <a href="WikiUtil.java.html"><b><i>View Source</i></b></a>
045: *
046: * @author Brian Wing Shun Chan
047: *
048: */
049: public class WikiUtil {
050:
051: public static String convert(NodeFilter filter, String content)
052: throws IOException {
053:
054: return _instance._convert(filter, content);
055: }
056:
057: public static NodeFilter getFilter(long nodeId) {
058: return _instance._getFilter(null, nodeId);
059: }
060:
061: public static NodeFilter getFilter(PortletURL portletURL,
062: long nodeId) {
063: return _instance._getFilter(portletURL, nodeId);
064: }
065:
066: private WikiUtil() {
067: try {
068: ClassLoader classLoader = getClass().getClassLoader();
069:
070: _spec = Http.URLtoString(classLoader
071: .getResource("wiki.transform"));
072: _remoteNames = _buildRemoteNamesMap(Http
073: .URLtoString(classLoader
074: .getResource("intermap.txt")));
075: } catch (IOException ioe) {
076: ioe.printStackTrace();
077: }
078: }
079:
080: private Map _buildRemoteNamesMap(String names) {
081: Map remoteNames = new HashMap();
082:
083: StringTokenizer st = new StringTokenizer(names, "\n");
084:
085: while (st.hasMoreTokens()) {
086: String line = st.nextToken().trim();
087:
088: int sep = line.indexOf(StringPool.SPACE);
089:
090: if (sep > 0) {
091: String name = line.substring(0, sep);
092: String url = line.substring(sep + 1);
093:
094: remoteNames.put(name, url);
095: }
096: }
097:
098: return remoteNames;
099: }
100:
101: private String _convert(NodeFilter filter, String content)
102: throws IOException {
103:
104: if (content == null) {
105: return StringPool.BLANK;
106: }
107:
108: StringWriter out = new StringWriter();
109:
110: filter.filter(new StringReader(content), out);
111:
112: String newContent = out.toString();
113:
114: String portletURLToString = StringPool.BLANK;
115:
116: LiferayPortletURL portletURL = (LiferayPortletURL) filter
117: .getPortletURL();
118:
119: if (portletURL != null) {
120: portletURL.setParameter("nodeId", String.valueOf(filter
121: .getNodeId()));
122:
123: Iterator itr = filter.getTitles().keySet().iterator();
124:
125: while (itr.hasNext()) {
126: String title = (String) itr.next();
127:
128: portletURL.setParameter("title", title, false);
129:
130: portletURLToString = portletURL.toString();
131:
132: newContent = StringUtil.replace(newContent,
133: "[$BEGIN_PAGE_TITLE$]" + title
134: + "[$END_PAGE_TITLE$]",
135: portletURLToString);
136: }
137: }
138:
139: return newContent;
140: }
141:
142: private NodeFilter _getFilter(PortletURL portletURL, long nodeId) {
143: MapStringRepository context = new MapStringRepository();
144: NodeRepository nodeRepository = new NodeRepository(nodeId);
145: PageRepository pageRepository = new PageRepository(
146: nodeRepository);
147:
148: NodeFilter filter = new NodeFilter(context, pageRepository,
149: _remoteNames, _spec, nodeRepository, portletURL, nodeId);
150:
151: return filter;
152: }
153:
154: private static WikiUtil _instance = new WikiUtil();
155:
156: private String _spec;
157: private Map _remoteNames;
158:
159: }
|