001: /*
002: JSPWiki - a JSP-based WikiWiki clone.
003:
004: Copyright (C) 2001-2005 Janne Jalkanen (Janne.Jalkanen@iki.fi)
005:
006: This program is free software; you can redistribute it and/or modify
007: it under the terms of the GNU Lesser General Public License as published by
008: the Free Software Foundation; either version 2.1 of the License, or
009: (at your option) any later version.
010:
011: This program is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: GNU Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public License
017: along with this program; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: package com.ecyrd.jspwiki.diff;
022:
023: import java.io.IOException;
024: import java.util.Properties;
025:
026: import org.apache.log4j.Logger;
027:
028: import com.ecyrd.jspwiki.NoRequiredPropertyException;
029: import com.ecyrd.jspwiki.WikiContext;
030: import com.ecyrd.jspwiki.WikiEngine;
031: import com.ecyrd.jspwiki.util.ClassUtil;
032:
033: /**
034: * Load, initialize and delegate to the DiffProvider that will actually do the work.
035: *
036: * @author John Volkar
037: */
038: public class DifferenceManager {
039: private static final Logger log = Logger
040: .getLogger(DifferenceManager.class);
041:
042: /** Property value for storing a diff provider. Value is {@value}. */
043: public static final String PROP_DIFF_PROVIDER = "jspwiki.diffProvider";
044:
045: private DiffProvider m_provider;
046:
047: /**
048: * Creates a new DifferenceManager for the given engine.
049: *
050: * @param engine The WikiEngine.
051: * @param props A set of properties.
052: */
053: public DifferenceManager(WikiEngine engine, Properties props) {
054: loadProvider(props);
055:
056: initializeProvider(engine, props);
057:
058: log.info("Using difference provider: "
059: + m_provider.getProviderInfo());
060: }
061:
062: private void loadProvider(Properties props) {
063: String providerClassName = props.getProperty(
064: PROP_DIFF_PROVIDER, TraditionalDiffProvider.class
065: .getName());
066:
067: try {
068: Class providerClass = ClassUtil.findClass(
069: "com.ecyrd.jspwiki.diff", providerClassName);
070: m_provider = (DiffProvider) providerClass.newInstance();
071: } catch (ClassNotFoundException e) {
072: log
073: .warn(
074: "Failed loading DiffProvider, will use NullDiffProvider.",
075: e);
076: } catch (InstantiationException e) {
077: log
078: .warn(
079: "Failed loading DiffProvider, will use NullDiffProvider.",
080: e);
081: } catch (IllegalAccessException e) {
082: log
083: .warn(
084: "Failed loading DiffProvider, will use NullDiffProvider.",
085: e);
086: }
087:
088: if (null == m_provider) {
089: m_provider = new DiffProvider.NullDiffProvider();
090: }
091: }
092:
093: private void initializeProvider(WikiEngine engine, Properties props) {
094: try {
095: m_provider.initialize(engine, props);
096: } catch (NoRequiredPropertyException e1) {
097: log
098: .warn(
099: "Failed initializing DiffProvider, will use NullDiffProvider.",
100: e1);
101: m_provider = new DiffProvider.NullDiffProvider(); //doesn't need init'd
102: } catch (IOException e1) {
103: log
104: .warn(
105: "Failed initializing DiffProvider, will use NullDiffProvider.",
106: e1);
107: m_provider = new DiffProvider.NullDiffProvider(); //doesn't need init'd
108: }
109: }
110:
111: /**
112: * Returns valid XHTML string to be used in any way you please.
113: *
114: * @param context The Wiki Context
115: * @param firstWikiText The old text
116: * @param secondWikiText the new text
117: * @return XHTML, or empty string, if no difference detected.
118: */
119: public String makeDiff(WikiContext context, String firstWikiText,
120: String secondWikiText) {
121: String diff = null;
122: try {
123: diff = m_provider.makeDiffHtml(context, firstWikiText,
124: secondWikiText);
125:
126: if (diff == null)
127: diff = "";
128: } catch (Exception e) {
129: diff = "Failed to create a diff, check the logs.";
130: log.warn(diff, e);
131: }
132: return diff;
133: }
134: }
|