001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.wiki.engines.jspwiki;
016    
017    import com.ecyrd.jspwiki.WikiContext;
018    import com.ecyrd.jspwiki.WikiException;
019    import com.ecyrd.jspwiki.WikiPage;
020    
021    import com.liferay.portal.kernel.exception.SystemException;
022    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
023    import com.liferay.portal.kernel.log.Log;
024    import com.liferay.portal.kernel.log.LogFactoryUtil;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portlet.wiki.PageContentException;
028    import com.liferay.portlet.wiki.engines.WikiEngine;
029    import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
030    
031    import java.io.IOException;
032    import java.io.InputStream;
033    
034    import java.util.Collection;
035    import java.util.Collections;
036    import java.util.HashMap;
037    import java.util.Map;
038    import java.util.Properties;
039    
040    import javax.portlet.PortletURL;
041    
042    /**
043     * @author Jorge Ferrer
044     */
045    public class JSPWikiEngine implements WikiEngine {
046    
047            public String convert(
048                            com.liferay.portlet.wiki.model.WikiPage page, PortletURL portletURL)
049                    throws PageContentException {
050    
051                    try {
052                            return convert(page);
053                    }
054                    catch (WikiException we) {
055                            throw new PageContentException(we);
056                    }
057            }
058    
059            public Map<String, Boolean> getOutgoingLinks(
060                            com.liferay.portlet.wiki.model.WikiPage page)
061                    throws PageContentException {
062    
063                    if (Validator.isNull(page.getContent())) {
064                            return Collections.EMPTY_MAP;
065                    }
066    
067                    try {
068                            LiferayJSPWikiEngine engine = getEngine(page.getNodeId());
069    
070                            WikiPage jspWikiPage = LiferayPageProvider.toJSPWikiPage(
071                                    page, engine);
072    
073                            Collection<String> titles = engine.scanWikiLinks(
074                                    jspWikiPage, page.getContent());
075    
076                            Map<String, Boolean> links = new HashMap<String, Boolean>();
077    
078                            for (String title : titles) {
079                                    if (title.startsWith("[[")) {
080                                            title = title.substring(2);
081                                    }
082                                    else if (title.startsWith("[")) {
083                                            title = title.substring(1);
084                                    }
085    
086                                    if (title.endsWith("]]")) {
087                                            title = title.substring(title.length() - 2, title.length());
088                                    }
089                                    else if (title.startsWith("[")) {
090                                            title = title.substring(title.length() - 1, title.length());
091                                    }
092    
093                                    Boolean existsObj = links.get(title);
094    
095                                    if (existsObj == null) {
096                                            if (WikiPageLocalServiceUtil.getPagesCount(
097                                                            page.getNodeId(), title, true) > 0) {
098    
099                                                    existsObj = Boolean.TRUE;
100                                            }
101                                            else {
102                                                    existsObj = Boolean.FALSE;
103                                            }
104    
105                                            links.put(title.toLowerCase(), existsObj);
106                                    }
107                            }
108    
109                            return links;
110                    }
111                    catch (SystemException se) {
112                            throw new PageContentException(se);
113                    }
114                    catch (WikiException we) {
115                            throw new PageContentException(we);
116                    }
117            }
118    
119            public void setInterWikiConfiguration(String interWikiConfiguration) {
120            }
121    
122            public void setMainConfiguration(String mainConfiguration) {
123                    setProperties(mainConfiguration);
124            }
125    
126            public boolean validate(long nodeId, String newContent) {
127                    return true;
128            }
129    
130            protected String convert(com.liferay.portlet.wiki.model.WikiPage page)
131                    throws WikiException {
132    
133                    String content = page.getContent();
134    
135                    if (Validator.isNull(content)) {
136                            return StringPool.BLANK;
137                    }
138    
139                    com.ecyrd.jspwiki.WikiEngine engine = getEngine(page.getNodeId());
140    
141                    WikiPage jspWikiPage = LiferayPageProvider.toJSPWikiPage(page, engine);
142    
143                    WikiContext wikiContext = new WikiContext(engine, jspWikiPage);
144    
145                    return engine.textToHTML(wikiContext, content);
146            }
147    
148            protected LiferayJSPWikiEngine getEngine(long nodeId)
149                    throws WikiException {
150    
151                    LiferayJSPWikiEngine engine = _engines.get(nodeId);
152    
153                    if (engine == null) {
154                            Properties nodeProps = new Properties(_props);
155    
156                            nodeProps.setProperty("nodeId", String.valueOf(nodeId));
157    
158                            String appName = nodeProps.getProperty("jspwiki.applicationName");
159    
160                            nodeProps.setProperty(
161                                    "jspwiki.applicationName", appName + " for node " + nodeId);
162    
163                            engine = new LiferayJSPWikiEngine(nodeProps);
164    
165                            _engines.put(nodeId, engine);
166                    }
167    
168                    return engine;
169            }
170    
171            protected synchronized void setProperties(String configuration) {
172                    _props = new Properties();
173    
174                    InputStream is = new UnsyncByteArrayInputStream(
175                            configuration.getBytes());
176    
177                    try {
178                            _props.load(is);
179                    }
180                    catch (IOException ioe) {
181                            _log.error(ioe, ioe);
182                    }
183            }
184    
185            private static Log _log = LogFactoryUtil.getLog(JSPWikiEngine.class);
186    
187            private Properties _props;
188            private Map<Long, LiferayJSPWikiEngine> _engines =
189                    new HashMap<Long, LiferayJSPWikiEngine>();
190    
191    }