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*/
019
020package org.apache.wiki.diff;
021
022import org.apache.log4j.Logger;
023import org.apache.wiki.api.core.Context;
024import org.apache.wiki.api.core.Engine;
025import org.apache.wiki.api.exceptions.NoRequiredPropertyException;
026import org.apache.wiki.api.providers.PageProvider;
027import org.apache.wiki.pages.PageManager;
028import org.apache.wiki.util.ClassUtil;
029
030import java.io.IOException;
031import java.lang.reflect.InvocationTargetException;
032import java.util.Properties;
033
034
035/**
036 * Load, initialize and delegate to the DiffProvider that will actually do the work.
037 */
038public class DefaultDifferenceManager implements DifferenceManager {
039
040    private static final Logger log = Logger.getLogger( DefaultDifferenceManager.class );
041
042    private DiffProvider m_provider;
043
044    /**
045     * Creates a new DifferenceManager for the given engine.
046     *
047     * @param engine The Engine.
048     * @param props  A set of properties.
049     */
050    public DefaultDifferenceManager( final Engine engine, final Properties props ) {
051        loadProvider( props );
052        initializeProvider( engine, props );
053
054        log.info( "Using difference provider: " + m_provider.getProviderInfo() );
055    }
056
057    private void loadProvider( final Properties props ) {
058        final String providerClassName = props.getProperty( PROP_DIFF_PROVIDER, TraditionalDiffProvider.class.getName() );
059        try {
060            final Class< ? > providerClass = ClassUtil.findClass("org.apache.wiki.diff", providerClassName );
061            m_provider = (DiffProvider) providerClass.getDeclaredConstructor().newInstance();
062        } catch( final ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e ) {
063            log.warn("Failed loading DiffProvider, will use NullDiffProvider.", e);
064        }
065
066        if( m_provider == null ) {
067            m_provider = new DiffProvider.NullDiffProvider();
068        }
069    }
070
071
072    private void initializeProvider( final Engine engine, final Properties props ) {
073        try {
074            m_provider.initialize( engine, props );
075        } catch( final NoRequiredPropertyException | IOException e ) {
076            log.warn( "Failed initializing DiffProvider, will use NullDiffProvider.", e );
077            m_provider = new DiffProvider.NullDiffProvider(); //doesn't need init'd
078        }
079    }
080
081    /**
082     * Returns valid XHTML string to be used in any way you please.
083     *
084     * @param context        The Wiki Context
085     * @param firstWikiText  The old text
086     * @param secondWikiText the new text
087     * @return XHTML, or empty string, if no difference detected.
088     */
089    @Override
090    public String makeDiff( final Context context, final String firstWikiText, final String secondWikiText ) {
091        String diff;
092        try {
093            diff = m_provider.makeDiffHtml( context, firstWikiText, secondWikiText );
094
095            if( diff == null ) {
096                diff = "";
097            }
098        } catch( final Exception e ) {
099            diff = "Failed to create a diff, check the logs.";
100            log.warn( diff, e );
101        }
102        return diff;
103    }
104
105    /**
106     *  Returns a diff of two versions of a page.
107     *  <p>
108     *  Note that the API was changed in 2.6 to provide a WikiContext object!
109     *
110     *  @param context The WikiContext of the page you wish to get a diff from
111     *  @param version1 Version number of the old page.  If WikiPageProvider.LATEST_VERSION (-1), then uses current page.
112     *  @param version2 Version number of the new page.  If WikiPageProvider.LATEST_VERSION (-1), then uses current page.
113     *
114     *  @return A HTML-ized difference between two pages.  If there is no difference, returns an empty string.
115     */
116    @Override
117    public String getDiff( final Context context, final int version1, final int version2 ) {
118        final String page = context.getPage().getName();
119        String page1 = context.getEngine().getManager( PageManager.class ).getPureText( page, version1 );
120        final String page2 = context.getEngine().getManager( PageManager.class ).getPureText( page, version2 );
121
122        // Kludge to make diffs for new pages to work this way.
123        if( version1 == PageProvider.LATEST_VERSION ) {
124            page1 = "";
125        }
126
127        return makeDiff( context, page1, page2 );
128    }
129
130}
131