This is an automated email from the ASF dual-hosted git repository. juanpablo pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/jspwiki.git
commit 0b57c00950c20e00864268c9ac0e546604990caa Author: juanpablo <[email protected]> AuthorDate: Mon Dec 30 20:09:01 2019 +0100 code format and fixes suggested by IntelliJ --- .../src/main/java/org/apache/wiki/WikiEngine.java | 154 +++++++++------------ .../org/apache/wiki/render/CreoleRenderer.java | 122 ++++++---------- .../wiki/render/CustomXMLOutputProcessor.java | 6 +- .../org/apache/wiki/render/RenderingManager.java | 152 +++++++++----------- .../java/org/apache/wiki/render/WikiRenderer.java | 19 ++- .../apache/wiki/render/WysiwygEditingRenderer.java | 111 ++++++--------- .../java/org/apache/wiki/render/XHTMLRenderer.java | 34 ++--- 7 files changed, 236 insertions(+), 362 deletions(-) diff --git a/jspwiki-main/src/main/java/org/apache/wiki/WikiEngine.java b/jspwiki-main/src/main/java/org/apache/wiki/WikiEngine.java index 9db2f4e..8d32d26 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/WikiEngine.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/WikiEngine.java @@ -26,7 +26,6 @@ import org.apache.wiki.api.engine.AdminBeanManager; import org.apache.wiki.api.engine.FilterManager; import org.apache.wiki.api.engine.PluginManager; import org.apache.wiki.api.exceptions.FilterException; -import org.apache.wiki.api.exceptions.NoSuchVariableException; import org.apache.wiki.api.exceptions.ProviderException; import org.apache.wiki.api.exceptions.WikiException; import org.apache.wiki.attachment.Attachment; @@ -1290,7 +1289,7 @@ public class WikiEngine { * @return String of WikiText. * @since 2.1.13. */ - public String getPureText( WikiPage page ) + public String getPureText( final WikiPage page ) { return getPureText( page.getName(), page.getVersion() ); } @@ -1303,16 +1302,9 @@ public class WikiEngine { * @param page WikiPage reference. * @return HTML-rendered version of the page. */ - - public String getHTML( WikiContext context, WikiPage page ) - { - String pagedata = null; - - pagedata = getPureText( page.getName(), page.getVersion() ); - - String res = textToHTML( context, pagedata ); - - return res; + public String getHTML( final WikiContext context, final WikiPage page ) { + final String pagedata = getPureText( page.getName(), page.getVersion() ); + return textToHTML( context, pagedata ); } /** @@ -1321,7 +1313,7 @@ public class WikiEngine { * @param page WikiName of the page to convert. * @return HTML-rendered version of the page. */ - public String getHTML( String page ) + public String getHTML( final String page ) { return getHTML( page, WikiPageProvider.LATEST_VERSION ); } @@ -1335,17 +1327,11 @@ public class WikiEngine { * @param version Version number to fetch * @return HTML-rendered page text. */ - public String getHTML( String pagename, int version ) - { - WikiPage page = getPage( pagename, version ); - - WikiContext context = new WikiContext( this, - page ); + public String getHTML( final String pagename, final int version ) { + final WikiPage page = getPage( pagename, version ); + final WikiContext context = new WikiContext( this, page ); context.setRequestContext( WikiContext.NONE ); - - String res = getHTML( context, page ); - - return res; + return getHTML( context, page ); } /** @@ -1355,58 +1341,53 @@ public class WikiEngine { * @param context The WikiContext in which the page is to be rendered * @return Rendered page text */ - public String textToHTML( WikiContext context, String pagedata ) - { + public String textToHTML( final WikiContext context, String pagedata ) { String result = ""; - boolean runFilters = "true".equals(m_variableManager.getValue(context,PROP_RUNFILTERS,"true")); + final boolean runFilters = "true".equals(m_variableManager.getValue(context,PROP_RUNFILTERS,"true")); - StopWatch sw = new StopWatch(); + final StopWatch sw = new StopWatch(); sw.start(); - try - { - if( runFilters ) + try { + if( runFilters ) { pagedata = m_filterManager.doPreTranslateFiltering( context, pagedata ); + } result = m_renderingManager.getHTML( context, pagedata ); - if( runFilters ) + if( runFilters ) { result = m_filterManager.doPostTranslateFiltering( context, result ); - } - catch( FilterException e ) - { + } + } catch( final FilterException e ) { + log.error( "page filter threw exception: ", e ); // FIXME: Don't yet know what to do } sw.stop(); - if( log.isDebugEnabled() ) - log.debug("Page "+context.getRealPage().getName()+" rendered, took "+sw ); + if( log.isDebugEnabled() ) { + log.debug( "Page " + context.getRealPage().getName() + " rendered, took " + sw ); + } return result; } /** - * Protected method that signals that the WikiEngine will be - * shut down by the servlet container. It is called by - * {@link WikiServlet#destroy()}. When this method is called, - * it fires a "shutdown" WikiEngineEvent to all registered - * listeners. + * Protected method that signals that the WikiEngine will be shut down by the servlet container. It is called by + * {@link WikiServlet#destroy()}. When this method is called, it fires a "shutdown" WikiEngineEvent to all registered listeners. */ - protected void shutdown() - { + protected void shutdown() { fireEvent( WikiEngineEvent.SHUTDOWN ); m_filterManager.destroy(); } /** - * Reads a WikiPageful of data from a String and returns all links - * internal to this Wiki in a Collection. + * Reads a WikiPageful of data from a String and returns all links internal to this Wiki in a Collection. * * @param page The WikiPage to scan * @param pagedata The page contents * @return a Collection of Strings */ - public Collection< String > scanWikiLinks( WikiPage page, String pagedata ) { - LinkCollector localCollector = new LinkCollector(); + public Collection< String > scanWikiLinks( final WikiPage page, final String pagedata ) { + final LinkCollector localCollector = new LinkCollector(); textToHTML( new WikiContext( this, page ), pagedata, @@ -1430,11 +1411,10 @@ public class WikiEngine { * @return HTML-rendered page text. */ - public String textToHTML( WikiContext context, - String pagedata, - StringTransmutator localLinkHook, - StringTransmutator extLinkHook ) - { + public String textToHTML( final WikiContext context, + final String pagedata, + final StringTransmutator localLinkHook, + final StringTransmutator extLinkHook ) { return textToHTML( context, pagedata, localLinkHook, extLinkHook, null, true, false ); } @@ -1449,12 +1429,11 @@ public class WikiEngine { * @return HTML-rendered page text. */ - public String textToHTML( WikiContext context, - String pagedata, - StringTransmutator localLinkHook, - StringTransmutator extLinkHook, - StringTransmutator attLinkHook ) - { + public String textToHTML( final WikiContext context, + final String pagedata, + final StringTransmutator localLinkHook, + final StringTransmutator extLinkHook, + final StringTransmutator attLinkHook ) { return textToHTML( context, pagedata, localLinkHook, extLinkHook, attLinkHook, true, false ); } @@ -1466,68 +1445,61 @@ public class WikiEngine { * @param localLinkHook Is called whenever a wiki link is found * @param extLinkHook Is called whenever an external link is found * @param parseAccessRules Parse the access rules if we encounter them - * @param justParse Just parses the pagedata, does not actually render. In this case, - * this methods an empty string. + * @param justParse Just parses the pagedata, does not actually render. In this case, this methods an empty string. * @return HTML-rendered page text. - */ - private String textToHTML( WikiContext context, + private String textToHTML( final WikiContext context, String pagedata, - StringTransmutator localLinkHook, - StringTransmutator extLinkHook, - StringTransmutator attLinkHook, - boolean parseAccessRules, - boolean justParse ) - { + final StringTransmutator localLinkHook, + final StringTransmutator extLinkHook, + final StringTransmutator attLinkHook, + final boolean parseAccessRules, + final boolean justParse ) { String result = ""; - if( pagedata == null ) - { + if( pagedata == null ) { log.error("NULL pagedata to textToHTML()"); return null; } - boolean runFilters = "true".equals(m_variableManager.getValue(context,PROP_RUNFILTERS,"true")); + final boolean runFilters = "true".equals(m_variableManager.getValue(context,PROP_RUNFILTERS,"true")); - try - { - StopWatch sw = new StopWatch(); + try { + final StopWatch sw = new StopWatch(); sw.start(); - if( runFilters && m_filterManager != null ) + if( runFilters && m_filterManager != null ) { pagedata = m_filterManager.doPreTranslateFiltering( context, pagedata ); + } - MarkupParser mp = m_renderingManager.getParser( context, pagedata ); + final MarkupParser mp = m_renderingManager.getParser( context, pagedata ); mp.addLocalLinkHook( localLinkHook ); mp.addExternalLinkHook( extLinkHook ); mp.addAttachmentLinkHook( attLinkHook ); - if( !parseAccessRules ) mp.disableAccessRules(); + if( !parseAccessRules ) { + mp.disableAccessRules(); + } - WikiDocument doc = mp.parse(); + final WikiDocument doc = mp.parse(); - // // In some cases it's better just to parse, not to render - // - if( !justParse ) - { + if( !justParse ) { result = m_renderingManager.getHTML( context, doc ); - if( runFilters && m_filterManager != null ) + if( runFilters && m_filterManager != null ) { result = m_filterManager.doPostTranslateFiltering( context, result ); + } } sw.stop(); - if( log.isDebugEnabled() ) - log.debug("Page "+context.getRealPage().getName()+" rendered, took "+sw ); - } - catch( IOException e ) - { + if( log.isDebugEnabled() ) { + log.debug( "Page " + context.getRealPage().getName() + " rendered, took " + sw ); + } + } catch( final IOException e ) { log.error( "Failed to scan page data: ", e ); - } - catch( FilterException e ) - { + } catch( final FilterException e ) { log.error( "page filter threw exception: ", e ); // FIXME: Don't yet know what to do } diff --git a/jspwiki-main/src/main/java/org/apache/wiki/render/CreoleRenderer.java b/jspwiki-main/src/main/java/org/apache/wiki/render/CreoleRenderer.java index 9700228..93f8255 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/render/CreoleRenderer.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/render/CreoleRenderer.java @@ -18,11 +18,6 @@ */ package org.apache.wiki.render; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - import org.apache.wiki.WikiContext; import org.apache.wiki.parser.PluginContent; import org.apache.wiki.parser.WikiDocument; @@ -30,14 +25,17 @@ import org.jdom2.Content; import org.jdom2.Element; import org.jdom2.Text; +import java.util.ArrayList; +import java.util.List; + /** * Implements DOM-to-Creole rendering. * <p> * FIXME: This class is not yet completely done. * */ -public class CreoleRenderer extends WikiRenderer -{ +public class CreoleRenderer extends WikiRenderer { + private static final String IMG_START = "{{"; private static final String IMG_END = "}}"; private static final String PLUGIN_START = "<<"; @@ -76,67 +74,51 @@ public class CreoleRenderer extends WikiRenderer private int m_listCount = 0; private char m_listChar = 'x'; - private List<PluginContent> m_plugins = new ArrayList<PluginContent>(); + private final List< PluginContent > m_plugins = new ArrayList<>(); /** * Creates a new Creole Renderer. - * */ - public CreoleRenderer( WikiContext ctx, WikiDocument doc ) + public CreoleRenderer( final WikiContext ctx, final WikiDocument doc ) { super( ctx, doc ); } /** * Renders an element into the StringBuilder given - * @param ce - * @param sb + * @param ce element to render + * @param sb stringbuilder holding the element render */ - private void renderElement( Element ce, StringBuilder sb ) - { + private void renderElement( final Element ce, final StringBuilder sb ) { String endEl = EMPTY_STRING; - for( int i = 0; i < ELEMENTS.length; i+=3 ) - { - if( ELEMENTS[i].equals(ce.getName()) ) - { + for( int i = 0; i < ELEMENTS.length; i+=3 ) { + if( ELEMENTS[i].equals(ce.getName()) ) { sb.append( ELEMENTS[i+1] ); endEl = ELEMENTS[i+2]; } } - if( UL.equals(ce.getName()) ) - { + if( UL.equals(ce.getName()) ) { m_listCount++; m_listChar = '*'; - } - else if( OL.equals(ce.getName()) ) - { + } else if( OL.equals(ce.getName()) ) { m_listCount++; m_listChar = '#'; - } - else if( LI.equals(ce.getName()) ) - { + } else if( LI.equals(ce.getName()) ) { for(int i = 0; i < m_listCount; i++ ) sb.append( m_listChar ); sb.append( ONE_SPACE ); - } - else if( A.equals(ce.getName()) ) - { - String href = ce.getAttributeValue( HREF_ATTRIBUTE ); - String text = ce.getText(); - - if( href.equals(text) ) - { - sb.append( HREF_START + href + HREF_END ); - } - else - { - sb.append( HREF_START + href+ HREF_DELIMITER + text +HREF_END); + } else if( A.equals( ce.getName() ) ) { + final String href = ce.getAttributeValue( HREF_ATTRIBUTE ); + final String text = ce.getText(); + + if( href.equals( text ) ) { + sb.append( HREF_START ).append( href ).append( HREF_END ); + } else { + sb.append( HREF_START ).append( href ).append( HREF_DELIMITER ).append( text ).append( HREF_END); } // Do not render anything else return; - } - else if( PRE.equals(ce.getName()) ) - { + } else if( PRE.equals( ce.getName() ) ) { sb.append( PRE_START ); sb.append( ce.getText() ); sb.append( PRE_END ); @@ -144,43 +126,27 @@ public class CreoleRenderer extends WikiRenderer return; } - // // Go through the children - // - for( Iterator< Content > i = ce.getContent().iterator(); i.hasNext(); ) - { - Content c = i.next(); - - if( c instanceof PluginContent ) - { - PluginContent pc = (PluginContent)c; - - if( pc.getPluginName().equals( PLUGIN_IMAGE ) ) - { - sb.append( IMG_START + pc.getParameter( PARAM_SRC ) + IMG_END ); - } - else - { + for( final Content c : ce.getContent() ) { + if( c instanceof PluginContent ) { + final PluginContent pc = ( PluginContent )c; + + if( pc.getPluginName().equals( PLUGIN_IMAGE ) ) { + sb.append( IMG_START ).append( pc.getParameter( PARAM_SRC ) ).append( IMG_END ); + } else { m_plugins.add(pc); - sb.append( PLUGIN_START + pc.getPluginName() + ONE_SPACE + m_plugins.size() + PLUGIN_END ); + sb.append( PLUGIN_START ).append( pc.getPluginName() ).append( ONE_SPACE ).append( m_plugins.size() ).append( PLUGIN_END ); } - } - else if( c instanceof Text ) - { - sb.append( ((Text)c).getText() ); - } - else if( c instanceof Element ) - { - renderElement( (Element)c, sb ); + } else if( c instanceof Text ) { + sb.append( ( ( Text )c ).getText() ); + } else if( c instanceof Element ) { + renderElement( ( Element )c, sb ); } } - if( UL.equals( ce.getName() ) || OL.equals( ce.getName() ) ) - { + if( UL.equals( ce.getName() ) || OL.equals( ce.getName() ) ) { m_listCount--; - } - else if( P.equals( ce.getName() ) ) - { + } else if( P.equals( ce.getName() ) ) { sb.append( LINEBREAK ); } @@ -190,18 +156,12 @@ public class CreoleRenderer extends WikiRenderer /** * {@inheritDoc} */ - public String getString() throws IOException - { - StringBuilder sb = new StringBuilder(1000); - - Element ce = m_document.getRootElement(); + public String getString() { + final StringBuilder sb = new StringBuilder(1000); + final Element ce = m_document.getRootElement(); - // // Traverse through the entire tree of everything. - // - renderElement( ce, sb ); - return sb.toString(); } diff --git a/jspwiki-main/src/main/java/org/apache/wiki/render/CustomXMLOutputProcessor.java b/jspwiki-main/src/main/java/org/apache/wiki/render/CustomXMLOutputProcessor.java index 9a52483..da68a9d 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/render/CustomXMLOutputProcessor.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/render/CustomXMLOutputProcessor.java @@ -33,10 +33,10 @@ import java.io.Writer; */ public class CustomXMLOutputProcessor extends AbstractXMLOutputProcessor { - protected void attributeEscapedEntitiesFilter(final Writer out, - final FormatStack fstack, final String value) throws IOException { + protected void attributeEscapedEntitiesFilter( final Writer out, + final FormatStack fstack, final String value ) throws IOException { - write(out, Format.escapeAttribute(fstack.getEscapeStrategy(), value)); + write( out, Format.escapeAttribute( fstack.getEscapeStrategy(), value ) ); } diff --git a/jspwiki-main/src/main/java/org/apache/wiki/render/RenderingManager.java b/jspwiki-main/src/main/java/org/apache/wiki/render/RenderingManager.java index e11d190..8a59271 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/render/RenderingManager.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/render/RenderingManager.java @@ -18,12 +18,9 @@ */ package org.apache.wiki.render; -import java.io.IOException; -import java.io.StringReader; -import java.lang.reflect.Constructor; -import java.util.Collection; -import java.util.Properties; - +import net.sf.ehcache.Cache; +import net.sf.ehcache.CacheManager; +import net.sf.ehcache.Element; import org.apache.log4j.Logger; import org.apache.wiki.WikiContext; import org.apache.wiki.WikiEngine; @@ -40,34 +37,33 @@ import org.apache.wiki.parser.WikiDocument; import org.apache.wiki.providers.WikiPageProvider; import org.apache.wiki.util.ClassUtil; -import net.sf.ehcache.Cache; -import net.sf.ehcache.CacheManager; -import net.sf.ehcache.Element; +import java.io.IOException; +import java.io.StringReader; +import java.lang.reflect.Constructor; +import java.util.Collection; +import java.util.Properties; /** - * This class provides a facade towards the differing rendering routines. You should - * use the routines in this manager instead of the ones in WikiEngine, if you don't - * want the different side effects to occur - such as WikiFilters. + * This class provides a facade towards the differing rendering routines. You should use the routines in this manager + * instead of the ones in WikiEngine, if you don't want the different side effects to occur - such as WikiFilters. * <p> - * This class also manages a rendering cache, i.e. documents are stored between calls. - * You may control the cache by tweaking the ehcache.xml file. + * This class also manages a rendering cache, i.e. documents are stored between calls. You may control the cache by + * tweaking the ehcache.xml file. * <p> * * @since 2.4 */ -public class RenderingManager implements WikiEventListener, InternalModule -{ - private static Logger log = Logger.getLogger( RenderingManager.class ); +public class RenderingManager implements WikiEventListener, InternalModule { - private int m_cacheExpiryPeriod = 24*60*60; // This can be relatively long + private static final Logger log = Logger.getLogger( RenderingManager.class ); - private WikiEngine m_engine; + private final int m_cacheExpiryPeriod = 24*60*60; // This can be relatively long + private final CacheManager m_cacheManager = CacheManager.getInstance(); + private WikiEngine m_engine; private boolean m_useCache = true; - private CacheManager m_cacheManager = CacheManager.getInstance(); - /** The capacity of the caches, if you want something else, tweak ehcache.xml. */ private static final int DEFAULT_CACHESIZE = 1000; private static final String VERSION_DELIMITER = "::"; @@ -79,10 +75,10 @@ public class RenderingManager implements WikiEventListener, InternalModule public static final String DEFAULT_PARSER = JSPWikiMarkupParser.class.getName(); /** The name of the default renderer. */ - public static final String DEFAULT_RENDERER = XHTMLRenderer.class.getName(); + public static final String DEFAULT_RENDERER = XHTMLRenderer.class.getName(); /** The name of the default WYSIWYG renderer. */ - public static final String DEFAULT_WYSIWYG_RENDERER = WysiwygEditingRenderer.class.getName(); + public static final String DEFAULT_WYSIWYG_RENDERER = WysiwygEditingRenderer.class.getName(); /** Stores the WikiDocuments that have been cached. */ private Cache m_documentCache; @@ -116,11 +112,8 @@ public class RenderingManager implements WikiEventListener, InternalModule * @param properties A list of properties to get parameters from. * @throws WikiException If the manager could not be initialized. */ - public void initialize( WikiEngine engine, Properties properties ) - throws WikiException - { + public void initialize( final WikiEngine engine, final Properties properties ) throws WikiException { m_engine = engine; - m_markupParserClass = properties.getProperty( PROP_PARSER, DEFAULT_PARSER ); if( !ClassUtil.assignable( m_markupParserClass, MarkupParser.class.getName() ) ) { log.warn( m_markupParserClass + " does not subclass " + MarkupParser.class.getName() + " reverting to default markup parser." ); @@ -128,17 +121,16 @@ public class RenderingManager implements WikiEventListener, InternalModule } log.info( "Using " + m_markupParserClass + " as markup parser." ); - m_useCache = "true".equals(properties.getProperty(PageManager.PROP_USECACHE)); - - if (m_useCache) { - String documentCacheName = engine.getApplicationName() + "." + DOCUMENTCACHE_NAME; + m_useCache = "true".equals( properties.getProperty( PageManager.PROP_USECACHE ) ); + if( m_useCache ) { + final String documentCacheName = engine.getApplicationName() + "." + DOCUMENTCACHE_NAME; if (m_cacheManager.cacheExists(documentCacheName)) { m_documentCache = m_cacheManager.getCache(documentCacheName); } else { - log.info("cache with name " + documentCacheName + " not found in ehcache.xml, creating it with defaults."); - m_documentCache = new Cache(documentCacheName, DEFAULT_CACHESIZE, false, false, m_cacheExpiryPeriod, m_cacheExpiryPeriod); - m_cacheManager.addCache(m_documentCache); + log.info( "cache with name " + documentCacheName + " not found in ehcache.xml, creating it with defaults." ); + m_documentCache = new Cache( documentCacheName, DEFAULT_CACHESIZE, false, false, m_cacheExpiryPeriod, m_cacheExpiryPeriod ); + m_cacheManager.addCache( m_documentCache ); } } @@ -177,10 +169,10 @@ public class RenderingManager implements WikiEventListener, InternalModule * @param pagedata the page data * @return A MarkupParser instance. */ - public MarkupParser getParser( WikiContext context, String pagedata ) { + public MarkupParser getParser( final WikiContext context, final String pagedata ) { try { return ClassUtil.getMappedObject( m_markupParserClass, context, new StringReader( pagedata ) ); - } catch( ReflectiveOperationException | IllegalArgumentException e ) { + } catch( final ReflectiveOperationException | IllegalArgumentException e ) { log.error( "unable to get an instance of " + m_markupParserClass + " (" + e.getMessage() + "), returning default markup parser.", e ); return new JSPWikiMarkupParser( context, new StringReader( pagedata ) ); } @@ -192,18 +184,17 @@ public class RenderingManager implements WikiEventListener, InternalModule * @param context the wiki context * @param pagedata the page data * @return the rendered wiki document - * @throws IOException If rendering cannot be accomplished */ // FIXME: The cache management policy is not very good: deleted/changed pages should be detected better. - protected WikiDocument getRenderedDocument( WikiContext context, String pagedata ) throws IOException { - String pageid = context.getRealPage().getName() + VERSION_DELIMITER + - context.getRealPage().getVersion() + VERSION_DELIMITER + - context.getVariable( RenderingManager.VAR_EXECUTE_PLUGINS ); + protected WikiDocument getRenderedDocument( final WikiContext context, final String pagedata ) { + final String pageid = context.getRealPage().getName() + VERSION_DELIMITER + + context.getRealPage().getVersion() + VERSION_DELIMITER + + context.getVariable( RenderingManager.VAR_EXECUTE_PLUGINS ); if( useCache( context ) ) { - Element element = m_documentCache.get( pageid ); + final Element element = m_documentCache.get( pageid ); if ( element != null ) { - WikiDocument doc = (WikiDocument) element.getObjectValue(); + final WikiDocument doc = ( WikiDocument )element.getObjectValue(); // // This check is needed in case the different filters have actually changed the page data. @@ -220,48 +211,45 @@ public class RenderingManager implements WikiEventListener, InternalModule } // Refresh the data content - // try { - MarkupParser parser = getParser( context, pagedata ); - WikiDocument doc = parser.parse(); + final MarkupParser parser = getParser( context, pagedata ); + final WikiDocument doc = parser.parse(); doc.setPageData( pagedata ); if( useCache( context ) ) { m_documentCache.put( new Element( pageid, doc ) ); } return doc; - } catch( IOException ex ) { + } catch( final IOException ex ) { log.error( "Unable to parse", ex ); } return null; } - boolean useCache( WikiContext context ) { + boolean useCache( final WikiContext context ) { return m_useCache && WikiContext.VIEW.equals( context.getRequestContext() ); } /** - * Simply renders a WikiDocument to a String. This version does not get the document - * from the cache - in fact, it does not cache the document at all. This is - * very useful, if you have something that you want to render outside the caching - * routines. Because the cache is based on full pages, and the cache keys are - * based on names, use this routine if you're rendering anything for yourself. + * Simply renders a WikiDocument to a String. This version does not get the document from the cache - in fact, it does + * not cache the document at all. This is very useful, if you have something that you want to render outside the caching + * routines. Because the cache is based on full pages, and the cache keys are based on names, use this routine if you're + * rendering anything for yourself. * * @param context The WikiContext to render in * @param doc A proper WikiDocument * @return Rendered HTML. * @throws IOException If the WikiDocument is poorly formed. */ - public String getHTML( WikiContext context, WikiDocument doc ) throws IOException - { + public String getHTML( final WikiContext context, final WikiDocument doc ) throws IOException { final Boolean wysiwygVariable = ( Boolean )context.getVariable( WYSIWYG_EDITOR_MODE ); final boolean wysiwygEditorMode; if( wysiwygVariable != null ) { - wysiwygEditorMode = wysiwygVariable.booleanValue(); + wysiwygEditorMode = wysiwygVariable; } else { wysiwygEditorMode = false; } - WikiRenderer rend; + final WikiRenderer rend; if( wysiwygEditorMode ) { rend = getWysiwygRenderer( context, doc ); } else { @@ -272,15 +260,14 @@ public class RenderingManager implements WikiEventListener, InternalModule } /** - * Returns a WikiRenderer instance, initialized with the given - * context and doc. The object is an XHTMLRenderer, unless overridden - * in jspwiki.properties with PROP_RENDERER. + * Returns a WikiRenderer instance, initialized with the given context and doc. The object is an XHTMLRenderer, + * unless overridden in jspwiki.properties with PROP_RENDERER. * * @param context The WikiContext * @param doc The document to render * @return A WikiRenderer for this document, or null, if no such renderer could be instantiated. */ - public WikiRenderer getRenderer( WikiContext context, WikiDocument doc ) { + public WikiRenderer getRenderer( final WikiContext context, final WikiDocument doc ) { final Object[] params = { context, doc }; return getRenderer( params, m_rendererConstructor ); } @@ -292,47 +279,38 @@ public class RenderingManager implements WikiEventListener, InternalModule * * @param context The WikiContext * @param doc The document to render - * @return A WikiRenderer instance meant for WYSIWYG editing, for this document, or null, if - * no such renderer could be instantiated. + * @return A WikiRenderer instance meant for WYSIWYG editing, for this document, or null, if no such renderer could be instantiated. */ - public WikiRenderer getWysiwygRenderer( WikiContext context, WikiDocument doc ) { + public WikiRenderer getWysiwygRenderer( final WikiContext context, final WikiDocument doc ) { final Object[] params = { context, doc }; return getRenderer( params, m_rendererWysiwygConstructor ); } @SuppressWarnings("unchecked") - private < T extends WikiRenderer > T getRenderer( Object[] params, Constructor<?> rendererConstructor ) { - T rval = null; - + private < T extends WikiRenderer > T getRenderer( final Object[] params, final Constructor<?> rendererConstructor ) { try { - rval = (T)rendererConstructor.newInstance( params ); + return ( T )rendererConstructor.newInstance( params ); } catch( final Exception e ) { log.error( "Unable to create WikiRenderer", e ); } - return rval; + return null; } /** - * Convenience method for rendering, using the default parser and renderer. Note that - * you can't use this method to do any arbitrary rendering, as the pagedata MUST - * be the data from the that the WikiContext refers to - this method caches the HTML - * internally, and will return the cached version. If the pagedata is different - * from what was cached, will re-render and store the pagedata into the internal cache. + * Convenience method for rendering, using the default parser and renderer. Note that you can't use this method + * to do any arbitrary rendering, as the pagedata MUST be the data from the that the WikiContext refers to - this + * method caches the HTML internally, and will return the cached version. If the pagedata is different from what + * was cached, will re-render and store the pagedata into the internal cache. * * @param context the wiki context * @param pagedata the page data * @return XHTML data. */ - public String getHTML( WikiContext context, String pagedata ) - { - try - { - WikiDocument doc = getRenderedDocument( context, pagedata ); - + public String getHTML( final WikiContext context, final String pagedata ) { + try { + final WikiDocument doc = getRenderedDocument( context, pagedata ); return getHTML( context, doc ); - } - catch( IOException e ) - { + } catch( final IOException e ) { log.error("Unable to parse",e); } @@ -347,21 +325,21 @@ public class RenderingManager implements WikiEventListener, InternalModule * @see org.apache.wiki.event.WikiEventListener#actionPerformed(org.apache.wiki.event.WikiEvent) */ @Override - public void actionPerformed( WikiEvent event ) { + public void actionPerformed( final WikiEvent event ) { log.debug( "event received: " + event.toString() ); if( m_useCache ) { if( ( event instanceof WikiPageEvent ) && ( event.getType() == WikiPageEvent.POST_SAVE_BEGIN ) ) { if( m_documentCache != null ) { - String pageName = ( ( WikiPageEvent ) event ).getPageName(); + final String pageName = ( ( WikiPageEvent ) event ).getPageName(); m_documentCache.remove( pageName ); - Collection< String > referringPages = m_engine.getReferenceManager().findReferrers( pageName ); + final Collection< String > referringPages = m_engine.getReferenceManager().findReferrers( pageName ); // // Flush also those pages that refer to this page (if an nonexistent page // appears, we need to flush the HTML that refers to the now-existent page) // if( referringPages != null ) { - for( String page : referringPages ) { + for( final String page : referringPages ) { if( log.isDebugEnabled() ) { log.debug( "Flushing latest version of " + page ); } diff --git a/jspwiki-main/src/main/java/org/apache/wiki/render/WikiRenderer.java b/jspwiki-main/src/main/java/org/apache/wiki/render/WikiRenderer.java index 209266b..0dae868 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/render/WikiRenderer.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/render/WikiRenderer.java @@ -18,22 +18,20 @@ */ package org.apache.wiki.render; -import java.io.IOException; - import org.apache.wiki.WikiContext; import org.apache.wiki.parser.WikiDocument; +import java.io.IOException; + /** - * Provides an interface to the basic rendering engine. - * This class is an abstract class instead of an interface because - * it is expected that rendering capabilities are increased at some - * point, and I would hate if renderers broke. This class allows - * some sane defaults to be implemented. + * Provides an interface to the basic rendering engine. This class is an abstract class instead of an interface because + * it is expected that rendering capabilities are increased at some point, and I would hate if renderers broke. + * This class allows some sane defaults to be implemented. * * @since 2.4 */ -public abstract class WikiRenderer -{ +public abstract class WikiRenderer { + protected WikiContext m_context; protected WikiDocument m_document; @@ -46,8 +44,7 @@ public abstract class WikiRenderer * @param context A WikiContext in which the rendering will take place. * @param doc The WikiDocument which shall be rendered. */ - protected WikiRenderer( WikiContext context, WikiDocument doc ) - { + protected WikiRenderer( final WikiContext context, final WikiDocument doc ) { m_context = context; m_document = doc; doc.setContext( context ); // Make sure it is set diff --git a/jspwiki-main/src/main/java/org/apache/wiki/render/WysiwygEditingRenderer.java b/jspwiki-main/src/main/java/org/apache/wiki/render/WysiwygEditingRenderer.java index cb915a7..bb65bc2 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/render/WysiwygEditingRenderer.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/render/WysiwygEditingRenderer.java @@ -18,10 +18,6 @@ */ package org.apache.wiki.render; -import java.io.IOException; -import java.io.StringWriter; -import java.util.Iterator; - import org.apache.wiki.WikiContext; import org.apache.wiki.htmltowiki.XHtmlToWikiConfig; import org.apache.wiki.parser.MarkupParser; @@ -31,19 +27,20 @@ import org.jdom2.Element; import org.jdom2.output.Format; import org.jdom2.output.XMLOutputter; +import java.io.IOException; +import java.io.StringWriter; +import java.util.Iterator; + /** - * Implements a WikiRendered that outputs XHTML in a format that is suitable - * for use by a WYSIWYG XHTML editor. + * Implements a WikiRenderer that outputs XHTML in a format that is suitable for use by a WYSIWYG XHTML editor. * * @since 2.5 */ -public class WysiwygEditingRenderer - extends WikiRenderer -{ +public class WysiwygEditingRenderer extends WikiRenderer { private static final String A_ELEMENT = "a"; private static final String IMG_ELEMENT = "img"; -// private static final String PRE_ELEMENT = "pre"; + // private static final String PRE_ELEMENT = "pre"; private static final String CLASS_ATTRIBUTE = "class"; private static final String HREF_ATTRIBUTE = "href"; private static final String TITLE_ATTRIBUTE = "title"; @@ -55,7 +52,7 @@ public class WysiwygEditingRenderer * @param context A WikiContext in which the rendering will take place. * @param doc The WikiDocument which shall be rendered. */ - public WysiwygEditingRenderer( WikiContext context, WikiDocument doc ) + public WysiwygEditingRenderer( final WikiContext context, final WikiDocument doc ) { super( context, doc ); } @@ -64,34 +61,27 @@ public class WysiwygEditingRenderer * Recursively walk the XHTML DOM tree and manipulate specific elements to * make them better for WYSIWYG editing. */ - private void processChildren(Element baseElement) - { - for( Iterator< Element > itr = baseElement.getChildren().iterator(); itr.hasNext(); ) - { - Element element = itr.next(); - String elementName = element.getName().toLowerCase(); - Attribute classAttr = element.getAttribute( CLASS_ATTRIBUTE ); - - if( elementName.equals( A_ELEMENT ) ) - { - if( classAttr != null ) - { - String classValue = classAttr.getValue(); - Attribute hrefAttr = element.getAttribute( HREF_ATTRIBUTE ); - - XHtmlToWikiConfig wikiConfig = new XHtmlToWikiConfig( m_context ); + private void processChildren( final Element baseElement ) { + for( final Iterator< Element > itr = baseElement.getChildren().iterator(); itr.hasNext(); ) { + final Element element = itr.next(); + final String elementName = element.getName().toLowerCase(); + final Attribute classAttr = element.getAttribute( CLASS_ATTRIBUTE ); + + if( elementName.equals( A_ELEMENT ) ) { + if( classAttr != null ) { + final String classValue = classAttr.getValue(); + final Attribute hrefAttr = element.getAttribute( HREF_ATTRIBUTE ); + final XHtmlToWikiConfig wikiConfig = new XHtmlToWikiConfig( m_context ); // Get the url for wiki page link - it's typically "Wiki.jsp?page=MyPage" // or when using the ShortURLConstructor option, it's "wiki/MyPage" . - String wikiPageLinkUrl = wikiConfig.getWikiJspPage(); - String editPageLinkUrl = wikiConfig.getEditJspPage(); + final String wikiPageLinkUrl = wikiConfig.getWikiJspPage(); + final String editPageLinkUrl = wikiConfig.getEditJspPage(); //if( classValue.equals( WIKIPAGE ) // || ( hrefAttr != null && hrefAttr.getValue().startsWith( wikiPageLinkUrl ) ) ) if( //classValue.equals( WIKIPAGE ) && - ( hrefAttr != null ) - && ( hrefAttr.getValue().startsWith( wikiPageLinkUrl ) ) ) - { + ( hrefAttr != null ) && ( hrefAttr.getValue().startsWith( wikiPageLinkUrl ) ) ) { // Remove the leading url string so that users will only see the // wikipage's name when editing an existing wiki link. // For example, change "Wiki.jsp?page=MyPage" to just "MyPage". @@ -106,15 +96,11 @@ public class WysiwygEditingRenderer // to this wiki string: "TargetPage#Heading2". hrefAttr.setValue( newHref.replaceFirst( LINKS_SOURCE, LINKS_TRANSLATION ) ); - } - else if( //classValue.equals( EDITPAGE ) && - ( hrefAttr != null ) - && ( hrefAttr.getValue().startsWith( editPageLinkUrl ) ) ) - { - - Attribute titleAttr = element.getAttribute( TITLE_ATTRIBUTE ); - if( titleAttr != null ) - { + } else if( //classValue.equals( EDITPAGE ) && + ( hrefAttr != null ) && ( hrefAttr.getValue().startsWith( editPageLinkUrl ) ) ) { + + final Attribute titleAttr = element.getAttribute( TITLE_ATTRIBUTE ); + if( titleAttr != null ) { // remove the title since we don't want to eventually save the default undefined page title. titleAttr.detach(); } @@ -123,30 +109,20 @@ public class WysiwygEditingRenderer newHref = m_context.getEngine().decodeName( newHref ); hrefAttr.setValue( newHref ); - } - - else if( classValue.equals( MarkupParser.HASHLINK ) ) - { + } else if( classValue.equals( MarkupParser.HASHLINK ) ) { itr.remove(); //remove element without disturbing the ongoing iteration continue; //take next iteration of the for loop } } - } // end of check for "a" element - - else if ( elementName.equals( IMG_ELEMENT ) ) - { - if( classAttr != null ) - { - String classValue = classAttr.getValue(); - - if( classValue.equals( MarkupParser.OUTLINK ) ) - { - itr.remove(); //remove element without disturbing the ongoing iteration - continue; //take next iteration of the for loop + // end of check for "a" element + } else if ( elementName.equals( IMG_ELEMENT ) ) { + if( classAttr != null ) { + final String classValue = classAttr.getValue(); + if( classValue.equals( MarkupParser.OUTLINK ) ) { + itr.remove(); // remove element without disturbing the ongoing iteration + continue; // take next iteration of the for loop } - } - } processChildren( element ); @@ -156,20 +132,16 @@ public class WysiwygEditingRenderer /** * {@inheritDoc} */ - public String getString() - throws IOException - { - Element rootElement = m_document.getRootElement(); + public String getString() throws IOException { + final Element rootElement = m_document.getRootElement(); processChildren( rootElement ); m_document.setContext( m_context ); - CustomXMLOutputProcessor processor = new CustomXMLOutputProcessor(); - XMLOutputter output = new XMLOutputter(processor); - - StringWriter out = new StringWriter(); - - Format fmt = Format.getRawFormat(); + final CustomXMLOutputProcessor processor = new CustomXMLOutputProcessor(); + final XMLOutputter output = new XMLOutputter(processor); + final StringWriter out = new StringWriter(); + final Format fmt = Format.getRawFormat(); fmt.setExpandEmptyElements( false ); fmt.setLineSeparator( LINEBREAK ); @@ -178,4 +150,5 @@ public class WysiwygEditingRenderer return out.toString(); } + } diff --git a/jspwiki-main/src/main/java/org/apache/wiki/render/XHTMLRenderer.java b/jspwiki-main/src/main/java/org/apache/wiki/render/XHTMLRenderer.java index b54fe02..49e6a4d 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/render/XHTMLRenderer.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/render/XHTMLRenderer.java @@ -18,14 +18,13 @@ */ package org.apache.wiki.render; -import java.io.IOException; -import java.io.StringWriter; - +import org.apache.wiki.WikiContext; +import org.apache.wiki.parser.WikiDocument; import org.jdom2.output.Format; import org.jdom2.output.XMLOutputter; -import org.apache.wiki.WikiContext; -import org.apache.wiki.parser.WikiDocument; +import java.io.IOException; +import java.io.StringWriter; /** * Implements a WikiRendered that outputs XHTML. Because the internal DOM @@ -34,9 +33,8 @@ import org.apache.wiki.parser.WikiDocument; * * @since 2.4 */ -public class XHTMLRenderer - extends WikiRenderer -{ +public class XHTMLRenderer extends WikiRenderer { + private static final String LINEBREAK = "\n"; /** @@ -45,7 +43,7 @@ public class XHTMLRenderer * @param context A WikiContext in which the rendering will take place. * @param doc The WikiDocument which shall be rendered. */ - public XHTMLRenderer( WikiContext context, WikiDocument doc ) + public XHTMLRenderer( final WikiContext context, final WikiDocument doc ) { super( context, doc ); } @@ -53,24 +51,20 @@ public class XHTMLRenderer /** * {@inheritDoc} */ - public String getString() - throws IOException - { + public String getString() throws IOException { m_document.setContext( m_context ); - CustomXMLOutputProcessor processor = new CustomXMLOutputProcessor(); - XMLOutputter output = new XMLOutputter(processor); - - StringWriter out = new StringWriter(); - - Format fmt = Format.getRawFormat(); + final CustomXMLOutputProcessor processor = new CustomXMLOutputProcessor(); + final XMLOutputter output = new XMLOutputter(processor); + final StringWriter out = new StringWriter(); + final Format fmt = Format.getRawFormat(); fmt.setExpandEmptyElements( false ); fmt.setLineSeparator( LINEBREAK ); output.setFormat( fmt ); output.outputElementContent( m_document.getRootElement(), out ); - String result = out.toString(); - return result; + return out.toString(); } + }
