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    import com.liferay.portlet.wiki.util.WikiUtil;
031    
032    import java.io.IOException;
033    import java.io.InputStream;
034    
035    import java.util.Collection;
036    import java.util.Collections;
037    import java.util.HashMap;
038    import java.util.Map;
039    import java.util.Properties;
040    
041    import javax.portlet.PortletURL;
042    
043    /**
044     * @author Jorge Ferrer
045     */
046    public class JSPWikiEngine implements WikiEngine {
047    
048            public String convert(
049                            com.liferay.portlet.wiki.model.WikiPage page, PortletURL portletURL)
050                    throws PageContentException {
051    
052                    try {
053                            return convert(page);
054                    }
055                    catch (WikiException we) {
056                            throw new PageContentException(we);
057                    }
058            }
059    
060            public Map<String, Boolean> getOutgoingLinks(
061                            com.liferay.portlet.wiki.model.WikiPage page)
062                    throws PageContentException {
063    
064                    if (Validator.isNull(page.getContent())) {
065                            return Collections.EMPTY_MAP;
066                    }
067    
068                    try {
069                            LiferayJSPWikiEngine engine = getEngine(page.getNodeId());
070    
071                            WikiPage jspWikiPage = LiferayPageProvider.toJSPWikiPage(
072                                    page, engine);
073    
074                            Collection<String> titles = engine.scanWikiLinks(
075                                    jspWikiPage, WikiUtil.encodeJSPWikiContent(page.getContent()));
076    
077                            Map<String, Boolean> links = new HashMap<String, Boolean>();
078    
079                            for (String title : titles) {
080                                    if (title.startsWith("[[")) {
081                                            title = title.substring(2);
082                                    }
083                                    else if (title.startsWith("[")) {
084                                            title = title.substring(1);
085                                    }
086    
087                                    if (title.endsWith("]]")) {
088                                            title = title.substring(title.length() - 2, title.length());
089                                    }
090                                    else if (title.startsWith("[")) {
091                                            title = title.substring(title.length() - 1, title.length());
092                                    }
093    
094                                    Boolean existsObj = links.get(title);
095    
096                                    if (existsObj == null) {
097                                            if (WikiPageLocalServiceUtil.getPagesCount(
098                                                            page.getNodeId(), title, true) > 0) {
099    
100                                                    existsObj = Boolean.TRUE;
101                                            }
102                                            else {
103                                                    existsObj = Boolean.FALSE;
104                                            }
105    
106                                            links.put(title.toLowerCase(), existsObj);
107                                    }
108                            }
109    
110                            return links;
111                    }
112                    catch (SystemException se) {
113                            throw new PageContentException(se);
114                    }
115                    catch (WikiException we) {
116                            throw new PageContentException(we);
117                    }
118            }
119    
120            public void setInterWikiConfiguration(String interWikiConfiguration) {
121            }
122    
123            public void setMainConfiguration(String mainConfiguration) {
124                    setProperties(mainConfiguration);
125            }
126    
127            public boolean validate(long nodeId, String newContent) {
128                    return true;
129            }
130    
131            protected String convert(com.liferay.portlet.wiki.model.WikiPage page)
132                    throws WikiException {
133    
134                    String content = WikiUtil.encodeJSPWikiContent(page.getContent());
135    
136                    if (Validator.isNull(content)) {
137                            return StringPool.BLANK;
138                    }
139    
140                    com.ecyrd.jspwiki.WikiEngine engine = getEngine(page.getNodeId());
141    
142                    WikiPage jspWikiPage = LiferayPageProvider.toJSPWikiPage(page, engine);
143    
144                    WikiContext wikiContext = new WikiContext(engine, jspWikiPage);
145    
146                    return engine.textToHTML(wikiContext, content);
147            }
148    
149            protected LiferayJSPWikiEngine getEngine(long nodeId)
150                    throws WikiException {
151    
152                    LiferayJSPWikiEngine engine = _engines.get(nodeId);
153    
154                    if (engine == null) {
155                            Properties nodeProps = new Properties(_props);
156    
157                            nodeProps.setProperty("nodeId", String.valueOf(nodeId));
158    
159                            String appName = nodeProps.getProperty("jspwiki.applicationName");
160    
161                            nodeProps.setProperty(
162                                    "jspwiki.applicationName", appName + " for node " + nodeId);
163    
164                            engine = new LiferayJSPWikiEngine(nodeProps);
165    
166                            _engines.put(nodeId, engine);
167                    }
168    
169                    return engine;
170            }
171    
172            protected synchronized void setProperties(String configuration) {
173                    _props = new Properties();
174    
175                    InputStream is = new UnsyncByteArrayInputStream(
176                            configuration.getBytes());
177    
178                    try {
179                            _props.load(is);
180                    }
181                    catch (IOException ioe) {
182                            _log.error(ioe, ioe);
183                    }
184            }
185    
186            private static Log _log = LogFactoryUtil.getLog(JSPWikiEngine.class);
187    
188            private Properties _props;
189            private Map<Long, LiferayJSPWikiEngine> _engines =
190                    new HashMap<Long, LiferayJSPWikiEngine>();
191    
192    }