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.logging.log4j.LogManager;
023import org.apache.logging.log4j.Logger;
024import org.apache.wiki.api.core.Context;
025import org.apache.wiki.api.core.Engine;
026import org.apache.wiki.api.exceptions.NoRequiredPropertyException;
027import org.apache.wiki.api.providers.PageProvider;
028import org.apache.wiki.pages.PageManager;
029import org.apache.wiki.util.ClassUtil;
030
031import java.io.IOException;
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 = LogManager.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            m_provider = ClassUtil.buildInstance( "org.apache.wiki.diff", providerClassName );
061        } catch( final ReflectiveOperationException e ) {
062            log.warn( "Failed loading DiffProvider, will use NullDiffProvider.", e );
063        }
064
065        if( m_provider == null ) {
066            m_provider = new DiffProvider.NullDiffProvider();
067        }
068    }
069
070
071    private void initializeProvider( final Engine engine, final Properties props ) {
072        try {
073            m_provider.initialize( engine, props );
074        } catch( final NoRequiredPropertyException | IOException e ) {
075            log.warn( "Failed initializing DiffProvider, will use NullDiffProvider.", e );
076            m_provider = new DiffProvider.NullDiffProvider(); //doesn't need init'd
077        }
078    }
079
080    /**
081     * Returns valid XHTML string to be used in any way you please.
082     *
083     * @param context        The Wiki Context
084     * @param firstWikiText  The old text
085     * @param secondWikiText the new text
086     * @return XHTML, or empty string, if no difference detected.
087     */
088    @Override
089    public String makeDiff( final Context context, final String firstWikiText, final String secondWikiText ) {
090        String diff;
091        try {
092            diff = m_provider.makeDiffHtml( context, firstWikiText, secondWikiText );
093
094            if( diff == null ) {
095                diff = "";
096            }
097        } catch( final Exception e ) {
098            diff = "Failed to create a diff, check the logs.";
099            log.warn( diff, e );
100        }
101        return diff;
102    }
103
104    /**
105     *  Returns a diff of two versions of a page.
106     *  <p>
107     *  Note that the API was changed in 2.6 to provide a WikiContext object!
108     *
109     *  @param context The WikiContext of the page you wish to get a diff from
110     *  @param version1 Version number of the old page.  If WikiPageProvider.LATEST_VERSION (-1), then uses current page.
111     *  @param version2 Version number of the new page.  If WikiPageProvider.LATEST_VERSION (-1), then uses current page.
112     *
113     *  @return A HTML-ized difference between two pages.  If there is no difference, returns an empty string.
114     */
115    @Override
116    public String getDiff( final Context context, final int version1, final int version2 ) {
117        final String page = context.getPage().getName();
118        String page1 = context.getEngine().getManager( PageManager.class ).getPureText( page, version1 );
119        final String page2 = context.getEngine().getManager( PageManager.class ).getPureText( page, version2 );
120
121        // Kludge to make diffs for new pages to work this way.
122        if( version1 == PageProvider.LATEST_VERSION ) {
123            page1 = "";
124        }
125
126        return makeDiff( context, page1, page2 );
127    }
128
129}
130