1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.wiki.engines.jspwiki;
24  
25  import com.ecyrd.jspwiki.WikiContext;
26  import com.ecyrd.jspwiki.WikiException;
27  import com.ecyrd.jspwiki.WikiPage;
28  
29  import com.liferay.portal.SystemException;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portlet.wiki.PageContentException;
33  import com.liferay.portlet.wiki.engines.WikiEngine;
34  import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
35  
36  import java.io.ByteArrayInputStream;
37  import java.io.IOException;
38  import java.io.InputStream;
39  
40  import java.util.Collection;
41  import java.util.Collections;
42  import java.util.HashMap;
43  import java.util.Map;
44  import java.util.Properties;
45  
46  import javax.portlet.PortletURL;
47  
48  import org.apache.commons.logging.Log;
49  import org.apache.commons.logging.LogFactory;
50  
51  /**
52   * <a href="JSPWikiEngine.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Jorge Ferrer
55   *
56   */
57  public class JSPWikiEngine implements WikiEngine {
58  
59      public String convert(
60              com.liferay.portlet.wiki.model.WikiPage page, PortletURL portletURL)
61          throws PageContentException {
62  
63          try {
64              return convert(page);
65          }
66          catch (WikiException we) {
67              throw new PageContentException(we);
68          }
69      }
70  
71      public Map<String, Boolean> getOutgoingLinks(
72              com.liferay.portlet.wiki.model.WikiPage page)
73          throws PageContentException {
74  
75          if (Validator.isNull(page.getContent())) {
76              return Collections.EMPTY_MAP;
77          }
78  
79          try {
80              LiferayJSPWikiEngine engine = getEngine(page.getNodeId());
81  
82              WikiPage jspWikiPage = LiferayPageProvider.toJSPWikiPage(
83                  page, engine);
84  
85              Collection<String> titles = engine.scanWikiLinks(
86                  jspWikiPage, page.getContent());
87  
88              Map<String, Boolean> links = new HashMap<String, Boolean>();
89  
90              for (String title : titles) {
91                  if (title.startsWith("[[")) {
92                      title = title.substring(2);
93                  }
94                  else if (title.startsWith("[")) {
95                      title = title.substring(1);
96                  }
97  
98                  if (title.endsWith("]]")) {
99                      title = title.substring(title.length() - 2, title.length());
100                 }
101                 else if (title.startsWith("[")) {
102                     title = title.substring(title.length() - 1, title.length());
103                 }
104 
105                 Boolean existsObj = links.get(title);
106 
107                 if (existsObj == null) {
108                     if (WikiPageLocalServiceUtil.getPagesCount(
109                             page.getNodeId(), title, true) > 0) {
110 
111                         existsObj = Boolean.TRUE;
112                     }
113                     else {
114                         existsObj = Boolean.FALSE;
115                     }
116 
117                     links.put(title, existsObj);
118                 }
119             }
120 
121             return links;
122         }
123         catch (SystemException se) {
124             throw new PageContentException(se);
125         }
126         catch (WikiException we) {
127             throw new PageContentException(we);
128         }
129     }
130 
131     public void setInterWikiConfiguration(String interWikiConfiguration) {
132     }
133 
134     public void setMainConfiguration(String mainConfiguration) {
135         setProperties(mainConfiguration);
136     }
137 
138     public boolean validate(long nodeId, String newContent) {
139         return true;
140     }
141 
142     protected String convert(com.liferay.portlet.wiki.model.WikiPage page)
143         throws WikiException {
144 
145         String content = page.getContent();
146 
147         if (Validator.isNull(content)) {
148             return StringPool.BLANK;
149         }
150 
151         com.ecyrd.jspwiki.WikiEngine engine = getEngine(page.getNodeId());
152 
153         WikiPage jspWikiPage = LiferayPageProvider.toJSPWikiPage(page, engine);
154 
155         WikiContext wikiContext = new WikiContext(engine, jspWikiPage);
156 
157         return engine.textToHTML(wikiContext, content);
158     }
159 
160     protected LiferayJSPWikiEngine getEngine(long nodeId)
161         throws WikiException {
162 
163         LiferayJSPWikiEngine engine = _engines.get(nodeId);
164 
165         if (engine == null) {
166             Properties nodeProps = new Properties(_props);
167 
168             nodeProps.setProperty("nodeId", String.valueOf(nodeId));
169 
170             String appName = nodeProps.getProperty("jspwiki.applicationName");
171 
172             nodeProps.setProperty(
173                 "jspwiki.applicationName", appName + " for node " + nodeId);
174 
175             engine = new LiferayJSPWikiEngine(nodeProps);
176 
177             _engines.put(nodeId, engine);
178         }
179 
180         return engine;
181     }
182 
183     protected synchronized void setProperties(String configuration) {
184         _props = new Properties();
185 
186         InputStream is = new ByteArrayInputStream(configuration.getBytes());
187 
188         try {
189             _props.load(is);
190         }
191         catch (IOException ioe) {
192             _log.error(ioe, ioe);
193         }
194     }
195 
196     private static Log _log = LogFactory.getLog(JSPWikiEngine.class);
197 
198     private Properties _props;
199     private Map<Long, LiferayJSPWikiEngine> _engines =
200         new HashMap<Long, LiferayJSPWikiEngine>();
201 
202 }