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 java.io.IOException;
023import java.util.Properties;
024
025import org.apache.log4j.Logger;
026import org.apache.wiki.WikiContext;
027import org.apache.wiki.WikiEngine;
028import org.apache.wiki.api.exceptions.NoRequiredPropertyException;
029import org.apache.wiki.util.ClassUtil;
030
031
032/**
033 * Load, initialize and delegate to the DiffProvider that will actually do the work.
034 */
035public class DifferenceManager {
036    private static final Logger log = Logger.getLogger(DifferenceManager.class);
037
038    /**
039     * Property value for storing a diff provider.  Value is {@value}.
040     */
041    public static final String PROP_DIFF_PROVIDER = "jspwiki.diffProvider";
042
043    private DiffProvider m_provider;
044
045    /**
046     * Creates a new DifferenceManager for the given engine.
047     *
048     * @param engine The WikiEngine.
049     * @param props  A set of properties.
050     */
051    public DifferenceManager(WikiEngine engine, Properties props) {
052        loadProvider(props);
053
054        initializeProvider(engine, props);
055
056        log.info("Using difference provider: " + m_provider.getProviderInfo());
057    }
058
059    private void loadProvider(Properties props) {
060        String providerClassName = props.getProperty(PROP_DIFF_PROVIDER,
061                TraditionalDiffProvider.class.getName());
062
063        try {
064            Class<?> providerClass = ClassUtil.findClass("org.apache.wiki.diff", providerClassName);
065            m_provider = (DiffProvider) providerClass.newInstance();
066        } catch (ClassNotFoundException e) {
067            log.warn("Failed loading DiffProvider, will use NullDiffProvider.", e);
068        } catch (InstantiationException e) {
069            log.warn("Failed loading DiffProvider, will use NullDiffProvider.", e);
070        } catch (IllegalAccessException e) {
071            log.warn("Failed loading DiffProvider, will use NullDiffProvider.", e);
072        }
073
074        if (null == m_provider) {
075            m_provider = new DiffProvider.NullDiffProvider();
076        }
077    }
078
079
080    private void initializeProvider(WikiEngine engine, Properties props) {
081        try {
082            m_provider.initialize(engine, props);
083        } catch (NoRequiredPropertyException e1) {
084            log.warn("Failed initializing DiffProvider, will use NullDiffProvider.", e1);
085            m_provider = new DiffProvider.NullDiffProvider(); //doesn't need init'd
086        } catch (IOException e1) {
087            log.warn("Failed initializing DiffProvider, will use NullDiffProvider.", e1);
088            m_provider = new DiffProvider.NullDiffProvider(); //doesn't need init'd
089        }
090    }
091
092    /**
093     * Returns valid XHTML string to be used in any way you please.
094     *
095     * @param context        The Wiki Context
096     * @param firstWikiText  The old text
097     * @param secondWikiText the new text
098     * @return XHTML, or empty string, if no difference detected.
099     */
100    public String makeDiff(WikiContext context, String firstWikiText, String secondWikiText) {
101        String diff = null;
102        try {
103            diff = m_provider.makeDiffHtml(context, firstWikiText, secondWikiText);
104
105            if (diff == null) {
106                diff = "";
107            }
108        } catch (Exception e) {
109            diff = "Failed to create a diff, check the logs.";
110            log.warn(diff, e);
111        }
112        return diff;
113    }
114}
115