001/*
002    Licensed to the Apache Software Foundation (ASF) under one
003    or more contributor license agreements.  See the NOTICE file
004    distributed with this work for additional information
005    regarding copyright ownership.  The ASF licenses this file
006    to you under the Apache License, Version 2.0 (the
007    "License"); you may not use this file except in compliance
008    with the License.  You may obtain a copy of the License at
009
010       http://www.apache.org/licenses/LICENSE-2.0
011
012    Unless required by applicable law or agreed to in writing,
013    software distributed under the License is distributed on an
014    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015    KIND, either express or implied.  See the License for the
016    specific language governing permissions and limitations
017    under the License.
018 */
019package org.apache.wiki.render;
020
021import org.apache.logging.log4j.LogManager;
022import org.apache.wiki.StringTransmutator;
023import org.apache.wiki.api.core.Context;
024import org.apache.wiki.api.core.Page;
025import org.apache.wiki.api.engine.Initializable;
026import org.apache.wiki.api.engine.RenderApi;
027import org.apache.wiki.api.providers.PageProvider;
028import org.apache.wiki.event.WikiEventListener;
029import org.apache.wiki.modules.InternalModule;
030import org.apache.wiki.parser.MarkupParser;
031import org.apache.wiki.parser.WikiDocument;
032
033import java.io.IOException;
034
035
036/**
037 *  This class provides a facade towards the differing rendering routines.  You should use the routines in this manager
038 *  instead of the ones in Engine, if you don't want the different side effects to occur - such as WikiFilters.
039 *  <p>
040 *  This class also manages a rendering cache, i.e. documents are stored between calls. You may control the cache by
041 *  tweaking the ehcache.xml file.
042 *  <p>
043 *
044 *  @since  2.4
045 */
046public interface RenderingManager extends RenderApi, WikiEventListener, InternalModule, Initializable {
047
048    /** markup parser property. */
049    String PROP_PARSER = "jspwiki.renderingManager.markupParser";
050
051    /** default renderer property. */
052    String PROP_RENDERER = "jspwiki.renderingManager.renderer";
053
054    /** default wysiwyg renderer property. */
055    String PROP_WYSIWYG_RENDERER = "jspwiki.renderingManager.renderer.wysiwyg";
056
057    String PROP_BEAUTIFYTITLE = "jspwiki.breakTitleWithSpaces";
058
059    /** Name of the regular page cache. */
060    String DOCUMENTCACHE_NAME = "jspwiki.renderingCache";
061
062    /**
063     *  Beautifies the title of the page by appending spaces in suitable places, if the user has so decreed in the properties when
064     *  constructing this Engine.  However, attachment names are only beautified by the name.
065     *
066     *  @param title The title to beautify
067     *  @return A beautified title (or, if beautification is off, returns the title without modification)
068     *  @since 1.7.11, moved to PageManager on 2.11.0
069     */
070    String beautifyTitle( String title );
071
072    /**
073     *  Beautifies the title of the page by appending non-breaking spaces in suitable places.  This is really suitable only for HTML output,
074     *  as it uses the &amp;nbsp; -character.
075     *
076     *  @param title The title to beautify
077     *  @return A beautified title.
078     *  @since 2.1.127
079     */
080    String beautifyTitleNoBreak( String title );
081
082    /**
083     *  Returns the wiki Parser
084     *  @param pagedata the page data
085     *  @return A MarkupParser instance.
086     */
087    MarkupParser getParser( Context context, String pagedata );
088
089    /**
090     *  Returns a cached document, if one is found.
091     *
092     * @param context the wiki context
093     * @param pagedata the page data
094     * @return the rendered wiki document
095     */
096    WikiDocument getRenderedDocument( Context context, String pagedata );
097
098    /**
099     * Returns a WikiRenderer instance, initialized with the given context and doc. The object is an XHTMLRenderer,
100     * unless overridden in jspwiki.properties with PROP_RENDERER.
101     *
102     * @param context The WikiContext
103     * @param doc The document to render
104     * @return A WikiRenderer for this document, or null, if no such renderer could be instantiated.
105     */
106    WikiRenderer getRenderer( Context context, WikiDocument doc );
107
108    /**
109     * Returns a WikiRenderer instance meant for WYSIWYG editing, initialized with the given
110     * context and doc. The object is an WysiwygEditingRenderer, unless overridden
111     * in jspwiki.properties with PROP_WYSIWYG_RENDERER.
112     *
113     * @param context The WikiContext
114     * @param doc The document to render
115     * @return A WikiRenderer instance meant for WYSIWYG editing, for this document, or null, if no such renderer could be instantiated.
116     */
117    WikiRenderer getWysiwygRenderer( Context context, WikiDocument doc );
118
119    /**
120     *  Simply renders a WikiDocument to a String.  This version does not get the document from the cache - in fact, it does
121     *  not cache the document at all.  This is very useful, if you have something that you want to render outside the caching
122     *  routines.  Because the cache is based on full pages, and the cache keys are based on names, use this routine if you're
123     *  rendering anything for yourself.
124     *
125     *  @param context The WikiContext to render in
126     *  @param doc A proper WikiDocument
127     *  @return Rendered HTML.
128     *  @throws IOException If the WikiDocument is poorly formed.
129     */
130    String getHTML( Context context, WikiDocument doc ) throws IOException;
131
132    /**
133     *  Returns the converted HTML of the page using a different context than the default context.
134     *
135     *  @param  context A WikiContext in which you wish to render this page in.
136     *  @param  page WikiPage reference.
137     *  @return HTML-rendered version of the page.
138     */
139    String getHTML( Context context, Page page );
140
141    /**
142     *  Returns the converted HTML of the page's specific version. The version must be a positive integer, otherwise the current
143     *  version is returned.
144     *
145     *  @param pagename WikiName of the page to convert.
146     *  @param version Version number to fetch
147     *  @return HTML-rendered page text.
148     */
149    String getHTML( String pagename, int version );
150
151    /**
152     *   Convenience method for rendering, using the default parser and renderer.  Note that you can't use this method
153     *   to do any arbitrary rendering, as the pagedata MUST be the data from the that the WikiContext refers to - this
154     *   method caches the HTML internally, and will return the cached version.  If the pagedata is different from what
155     *   was cached, will re-render and store the pagedata into the internal cache.
156     *
157     *   @param context the wiki context
158     *   @param pagedata the page data
159     *   @return XHTML data.
160     */
161    default String getHTML( final Context context, final String pagedata ) {
162        try {
163            final WikiDocument doc = getRenderedDocument( context, pagedata );
164            return getHTML( context, doc );
165        } catch( final IOException e ) {
166            LogManager.getLogger( RenderingManager.class ).error("Unable to parse", e );
167        }
168
169        return null;
170    }
171
172    /**
173     *  Returns the converted HTML of the page.
174     *
175     *  @param page WikiName of the page to convert.
176     *  @return HTML-rendered version of the page.
177     */
178    default String getHTML( final String page ) {
179        return getHTML( page, PageProvider.LATEST_VERSION );
180    }
181
182    /**
183     *  Helper method for doing the HTML translation.
184     *
185     *  @param context The WikiContext in which to do the conversion
186     *  @param pagedata The data to render
187     *  @param localLinkHook Is called whenever a wiki link is found
188     *  @param extLinkHook   Is called whenever an external link is found
189     *  @param parseAccessRules Parse the access rules if we encounter them
190     *  @param justParse Just parses the pagedata, does not actually render.  In this case, this methods an empty string.
191     *  @return HTML-rendered page text.
192     */
193    String textToHTML( Context context,
194                       String pagedata,
195                       StringTransmutator localLinkHook,
196                       StringTransmutator extLinkHook,
197                       StringTransmutator attLinkHook,
198                       boolean parseAccessRules,
199                       boolean justParse );
200
201    /**
202     *  Just convert WikiText to HTML.
203     *
204     *  @param context The WikiContext in which to do the conversion
205     *  @param pagedata The data to render
206     *  @param localLinkHook Is called whenever a wiki link is found
207     *  @param extLinkHook   Is called whenever an external link is found
208     *
209     *  @return HTML-rendered page text.
210     */
211    default String textToHTML( final Context context,
212                               final String pagedata,
213                               final StringTransmutator localLinkHook,
214                               final StringTransmutator extLinkHook ) {
215        return textToHTML( context, pagedata, localLinkHook, extLinkHook, null, true, false );
216    }
217
218    /**
219     *  Just convert WikiText to HTML.
220     *
221     *  @param context The WikiContext in which to do the conversion
222     *  @param pagedata The data to render
223     *  @param localLinkHook Is called whenever a wiki link is found
224     *  @param extLinkHook   Is called whenever an external link is found
225     *  @param attLinkHook   Is called whenever an attachment link is found
226     *  @return HTML-rendered page text.
227     */
228    default String textToHTML( final Context context,
229                               final String pagedata,
230                               final StringTransmutator localLinkHook,
231                               final StringTransmutator extLinkHook,
232                               final StringTransmutator attLinkHook ) {
233        return textToHTML( context, pagedata, localLinkHook, extLinkHook, attLinkHook, true, false );
234    }
235
236}