001: /**
002: * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, version 2.1, dated February 1999.
003: *
004: * This program is free software; you can redistribute it and/or modify
005: * it under the terms of the latest version of the GNU Lesser General
006: * Public License as published by the Free Software Foundation;
007: *
008: * This program is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
011: * GNU Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public License
014: * along with this program (LICENSE.txt); if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
016: */package org.jamwiki.parser;
017:
018: import java.lang.reflect.Constructor;
019: import java.util.Locale;
020: import org.apache.commons.lang.ClassUtils;
021: import org.jamwiki.Environment;
022: import org.jamwiki.WikiBase;
023: import org.jamwiki.model.Topic;
024: import org.jamwiki.utils.WikiLogger;
025:
026: /**
027: * This class provides utility methods for use with the parser functions.
028: */
029: public class ParserUtil {
030:
031: private static final WikiLogger logger = WikiLogger
032: .getLogger(ParserUtil.class.getName());
033:
034: /**
035: * Using the system parser, parse system content.
036: *
037: * @param parserInput A ParserInput object that contains parser
038: * configuration information.
039: * @param parserOutput A ParserOutput object that will hold metadata
040: * output. If this parameter is <code>null</code> then metadata generated
041: * during parsing will not be available to the calling method.
042: * @param content The raw topic content that is to be parsed.
043: * @return The parsed content.
044: * @throws Exception Thrown if there are any parsing errors.
045: */
046: public static String parse(ParserInput parserInput,
047: ParserOutput parserOutput, String content) throws Exception {
048: if (content == null) {
049: return null;
050: }
051: if (parserOutput == null) {
052: parserOutput = new ParserOutput();
053: }
054: AbstractParser parser = parserInstance(parserInput);
055: return parser.parseHTML(parserOutput, content);
056: }
057:
058: /**
059: * Retrieve a default ParserOutput object for a given topic name. Note that
060: * the content has almost no parsing performed on it other than to generate
061: * parser output metadata.
062: *
063: * @param content The raw topic content.
064: * @return Returns a minimal ParserOutput object initialized primarily with
065: * parser metadata such as links.
066: * @throws Exception Thrown if a parser error occurs.
067: */
068: public static ParserOutput parserOutput(String content,
069: String virtualWiki, String topicName) throws Exception {
070: ParserInput parserInput = new ParserInput();
071: parserInput.setVirtualWiki(virtualWiki);
072: parserInput.setTopicName(topicName);
073: parserInput.setAllowSectionEdit(false);
074: return ParserUtil.parseMetadata(parserInput, content);
075: }
076:
077: /**
078: * This method provides a way to parse content and set all output metadata,
079: * such as link values used by the search engine.
080: *
081: * @param parserInput A ParserInput object that contains parser configuration
082: * information.
083: * @param content The raw topic content that is to be parsed.
084: * @return Returns a ParserOutput object with minimally parsed topic content
085: * and other parser output fields set.
086: * @throws Exception Thrown if there are any parsing errors.
087: */
088: public static ParserOutput parseMetadata(ParserInput parserInput,
089: String content) throws Exception {
090: AbstractParser parser = parserInstance(parserInput);
091: ParserOutput parserOutput = new ParserOutput();
092: parser.parseMetadata(parserOutput, content);
093: return parserOutput;
094: }
095:
096: /**
097: * Perform a bare minimum of parsing as required prior to saving a topic
098: * to the database. In general this method will simply parse signature
099: * tags are return.
100: *
101: * @param parserInput A ParserInput object that contains parser configuration
102: * information.
103: * @param raw The raw Wiki syntax to be converted into HTML.
104: * @return The parsed content.
105: * @throws Exception Thrown if any error occurs during parsing.
106: */
107: public static String parseMinimal(ParserInput parserInput,
108: String raw) throws Exception {
109: AbstractParser parser = parserInstance(parserInput);
110: return parser.parseMinimal(raw);
111: }
112:
113: /**
114: * Utility method to retrieve an instance of the current system parser.
115: *
116: * @param parserInput A ParserInput object that contains parser configuration
117: * information.
118: * @return An instance of the system parser.
119: * @throws Exception Thrown if a parser instance can not be instantiated.
120: */
121: private static AbstractParser parserInstance(ParserInput parserInput)
122: throws Exception {
123: String parserClass = Environment
124: .getValue(Environment.PROP_PARSER_CLASS);
125: logger.fine("Using parser: " + parserClass);
126: Class clazz = ClassUtils.getClass(parserClass);
127: Class[] parameterTypes = new Class[1];
128: parameterTypes[0] = ClassUtils
129: .getClass("org.jamwiki.parser.ParserInput");
130: Constructor constructor = clazz.getConstructor(parameterTypes);
131: Object[] initArgs = new Object[1];
132: initArgs[0] = parserInput;
133: return (AbstractParser) constructor.newInstance(initArgs);
134: }
135:
136: /**
137: * Given a topic name, return the parser-specific syntax to indicate a page
138: * redirect.
139: *
140: * @param topicName The name of the topic that is being redirected to.
141: * @return A string containing the syntax indicating a redirect.
142: * @throws Exception Thrown if a parser instance cannot be instantiated or
143: * if any other parser error occurs.
144: */
145: public static String parserRedirectContent(String topicName)
146: throws Exception {
147: AbstractParser parser = parserInstance(null);
148: return parser.buildRedirectContent(topicName);
149: }
150:
151: /**
152: * When editing a section of a topic, this method provides a way of slicing
153: * out a given section of the raw topic content.
154: *
155: * @param context The servlet context.
156: * @param locale The locale for which the content is being parsed.
157: * @param virtualWiki The virtual wiki for the topic being parsed.
158: * @param topicName The name of the topic being parsed.
159: * @param targetSection The section to be sliced and returned.
160: * @return Returns the raw topic content for the target section.
161: * @throws Exception Thrown if a parser error occurs.
162: */
163: public static String parseSlice(String context, Locale locale,
164: String virtualWiki, String topicName, int section)
165: throws Exception {
166: Topic topic = WikiBase.getDataHandler().lookupTopic(
167: virtualWiki, topicName, false, null);
168: if (topic == null || topic.getTopicContent() == null) {
169: return null;
170: }
171: ParserInput parserInput = new ParserInput();
172: parserInput.setContext(context);
173: parserInput.setLocale(locale);
174: parserInput.setTopicName(topicName);
175: parserInput.setVirtualWiki(virtualWiki);
176: AbstractParser parser = ParserUtil.parserInstance(parserInput);
177: ParserOutput parserOutput = new ParserOutput();
178: return parser.parseSlice(parserOutput, topic.getTopicContent(),
179: section);
180: }
181:
182: /**
183: * When editing a section of a topic, this method provides a way of splicing
184: * an edited section back into the raw topic content.
185: *
186: * @param parserOutput A ParserOutput object containing parser
187: * metadata output.
188: * @param context The servlet context.
189: * @param locale The locale for which the content is being parsed.
190: * @param virtualWiki The virtual wiki for the topic being parsed.
191: * @param topicName The name of the topic being parsed.
192: * @param targetSection The section to be sliced and returned.
193: * @param replacementText The edited content that is to be spliced back into
194: * the raw topic.
195: * @return The raw topic content including the new replacement text.
196: * @throws Exception Thrown if a parser error occurs.
197: */
198: public static String parseSplice(ParserOutput parserOutput,
199: String context, Locale locale, String virtualWiki,
200: String topicName, int targetSection, String replacementText)
201: throws Exception {
202: Topic topic = WikiBase.getDataHandler().lookupTopic(
203: virtualWiki, topicName, false, null);
204: if (topic == null || topic.getTopicContent() == null) {
205: return null;
206: }
207: ParserInput parserInput = new ParserInput();
208: parserInput.setContext(context);
209: parserInput.setLocale(locale);
210: parserInput.setTopicName(topicName);
211: parserInput.setVirtualWiki(virtualWiki);
212: AbstractParser parser = ParserUtil.parserInstance(parserInput);
213: return parser
214: .parseSplice(parserOutput, topic.getTopicContent(),
215: targetSection, replacementText);
216: }
217: }
|