1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.wiki.engines.jspwiki;
16  
17  import com.ecyrd.jspwiki.WikiContext;
18  import com.ecyrd.jspwiki.WikiException;
19  import com.ecyrd.jspwiki.WikiPage;
20  
21  import com.liferay.portal.kernel.exception.SystemException;
22  import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
23  import com.liferay.portal.kernel.log.Log;
24  import com.liferay.portal.kernel.log.LogFactoryUtil;
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portlet.wiki.PageContentException;
28  import com.liferay.portlet.wiki.engines.WikiEngine;
29  import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
30  
31  import java.io.IOException;
32  import java.io.InputStream;
33  
34  import java.util.Collection;
35  import java.util.Collections;
36  import java.util.HashMap;
37  import java.util.Map;
38  import java.util.Properties;
39  
40  import javax.portlet.PortletURL;
41  
42  /**
43   * <a href="JSPWikiEngine.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Jorge Ferrer
46   */
47  public class JSPWikiEngine implements WikiEngine {
48  
49      public String convert(
50              com.liferay.portlet.wiki.model.WikiPage page, PortletURL portletURL)
51          throws PageContentException {
52  
53          try {
54              return convert(page);
55          }
56          catch (WikiException we) {
57              throw new PageContentException(we);
58          }
59      }
60  
61      public Map<String, Boolean> getOutgoingLinks(
62              com.liferay.portlet.wiki.model.WikiPage page)
63          throws PageContentException {
64  
65          if (Validator.isNull(page.getContent())) {
66              return Collections.EMPTY_MAP;
67          }
68  
69          try {
70              LiferayJSPWikiEngine engine = getEngine(page.getNodeId());
71  
72              WikiPage jspWikiPage = LiferayPageProvider.toJSPWikiPage(
73                  page, engine);
74  
75              Collection<String> titles = engine.scanWikiLinks(
76                  jspWikiPage, page.getContent());
77  
78              Map<String, Boolean> links = new HashMap<String, Boolean>();
79  
80              for (String title : titles) {
81                  if (title.startsWith("[[")) {
82                      title = title.substring(2);
83                  }
84                  else if (title.startsWith("[")) {
85                      title = title.substring(1);
86                  }
87  
88                  if (title.endsWith("]]")) {
89                      title = title.substring(title.length() - 2, title.length());
90                  }
91                  else if (title.startsWith("[")) {
92                      title = title.substring(title.length() - 1, title.length());
93                  }
94  
95                  Boolean existsObj = links.get(title);
96  
97                  if (existsObj == null) {
98                      if (WikiPageLocalServiceUtil.getPagesCount(
99                              page.getNodeId(), title, true) > 0) {
100 
101                         existsObj = Boolean.TRUE;
102                     }
103                     else {
104                         existsObj = Boolean.FALSE;
105                     }
106 
107                     links.put(title.toLowerCase(), existsObj);
108                 }
109             }
110 
111             return links;
112         }
113         catch (SystemException se) {
114             throw new PageContentException(se);
115         }
116         catch (WikiException we) {
117             throw new PageContentException(we);
118         }
119     }
120 
121     public void setInterWikiConfiguration(String interWikiConfiguration) {
122     }
123 
124     public void setMainConfiguration(String mainConfiguration) {
125         setProperties(mainConfiguration);
126     }
127 
128     public boolean validate(long nodeId, String newContent) {
129         return true;
130     }
131 
132     protected String convert(com.liferay.portlet.wiki.model.WikiPage page)
133         throws WikiException {
134 
135         String content = page.getContent();
136 
137         if (Validator.isNull(content)) {
138             return StringPool.BLANK;
139         }
140 
141         com.ecyrd.jspwiki.WikiEngine engine = getEngine(page.getNodeId());
142 
143         WikiPage jspWikiPage = LiferayPageProvider.toJSPWikiPage(page, engine);
144 
145         WikiContext wikiContext = new WikiContext(engine, jspWikiPage);
146 
147         return engine.textToHTML(wikiContext, content);
148     }
149 
150     protected LiferayJSPWikiEngine getEngine(long nodeId)
151         throws WikiException {
152 
153         LiferayJSPWikiEngine engine = _engines.get(nodeId);
154 
155         if (engine == null) {
156             Properties nodeProps = new Properties(_props);
157 
158             nodeProps.setProperty("nodeId", String.valueOf(nodeId));
159 
160             String appName = nodeProps.getProperty("jspwiki.applicationName");
161 
162             nodeProps.setProperty(
163                 "jspwiki.applicationName", appName + " for node " + nodeId);
164 
165             engine = new LiferayJSPWikiEngine(nodeProps);
166 
167             _engines.put(nodeId, engine);
168         }
169 
170         return engine;
171     }
172 
173     protected synchronized void setProperties(String configuration) {
174         _props = new Properties();
175 
176         InputStream is = new UnsyncByteArrayInputStream(
177             configuration.getBytes());
178 
179         try {
180             _props.load(is);
181         }
182         catch (IOException ioe) {
183             _log.error(ioe, ioe);
184         }
185     }
186 
187     private static Log _log = LogFactoryUtil.getLog(JSPWikiEngine.class);
188 
189     private Properties _props;
190     private Map<Long, LiferayJSPWikiEngine> _engines =
191         new HashMap<Long, LiferayJSPWikiEngine>();
192 
193 }