001: package de.java2html.plugin.jspwiki;
002:
003: import java.io.IOException;
004: import java.net.MalformedURLException;
005: import java.net.URL;
006: import java.util.Map;
007:
008: import com.ecyrd.jspwiki.WikiContext;
009: import com.ecyrd.jspwiki.plugin.PluginException;
010: import com.ecyrd.jspwiki.plugin.WikiPlugin;
011:
012: import de.java2html.JavaSourceConversionSettings;
013: import de.java2html.Version;
014: import de.java2html.converter.JavaSourceConverterProvider;
015: import de.java2html.options.JavaSourceConversionOptions;
016: import de.java2html.plugin.AbstractJava2HtmlPlugin;
017: import de.java2html.plugin.ParameterUtilities;
018: import de.java2html.util.IllegalConfigurationException;
019:
020: /**
021: * @author Markus Gebhard
022: */
023: public class Java2HtmlPlugin extends AbstractJava2HtmlPlugin implements
024: WikiPlugin {
025: public static final String DEFAULT_USAGE_MESSAGE = "Missing parameter 'source', 'url' or 'attachment' or missing body. Valid parameters are:"
026: + PluginParameterChecker.createValidParameterHtmlTable();
027:
028: public String execute(WikiContext context, Map params)
029: throws PluginException {
030: new PluginParameterChecker().checkParametersSupported(params);
031:
032: String printVersion = ParameterUtilities.getParameter(params,
033: PluginParameter.PRINT_VERSION);
034: if (printVersion != null) {
035: return "<b>" + Version.getJava2HtmlConverterTitle()
036: + "</b>";
037: }
038:
039: JavaSourceConversionOptions conversionOptions;
040: try {
041: conversionOptions = new PluginConversionOptionsParser()
042: .parseConversionOptions(params);
043: } catch (IllegalConfigurationException e) {
044: throw new PluginException(e.getMessage(), e);
045: }
046:
047: String converterName = ParameterUtilities.getParameter(params,
048: PluginParameter.CONVERTER);
049: if (converterName == null) {
050: converterName = JavaSourceConverterProvider
051: .getDefaultConverterName();
052: } else {
053: if (JavaSourceConverterProvider
054: .getJavaSourceConverterByName(converterName) == null) {
055: throw new PluginException(
056: "Unknown converter '" + converterName + "'."); //$NON-NLS-1$ //$NON-NLS-2$
057: }
058: }
059:
060: String urlString = ParameterUtilities.getParameter(params,
061: PluginParameter.URL);
062: if (urlString != null) {
063: return convertFileFromUrl(urlString, conversionOptions,
064: context, converterName);
065: }
066:
067: String source = ParameterUtilities.getParameter(params,
068: PluginParameter.SOURCE);
069: if (source != null) {
070: return convert(source, new JavaSourceConversionSettings(
071: conversionOptions, converterName));
072: }
073: String body = ParameterUtilities.getParameter(params,
074: PluginParameter._BODY);
075: if (body != null) {
076: body = removeLeadingNewLine(body);
077: return convert(body, new JavaSourceConversionSettings(
078: conversionOptions, converterName));
079: }
080: String attachment = ParameterUtilities.getParameter(params,
081: PluginParameter.ATTACHMENT);
082: if (attachment != null) {
083: new PluginSecurityManager(context)
084: .checkValidAttachmentUrlPart(attachment);
085: URL url = createAttachmentUrl(context, attachment);
086: try {
087: return convert(url, new JavaSourceConversionSettings(
088: conversionOptions, converterName));
089: } catch (IOException e) {
090: throw new PluginException(
091: "Unable to read Url '" + urlString + "'", e); //$NON-NLS-1$//$NON-NLS-2$
092: }
093: }
094:
095: throw new PluginException(DEFAULT_USAGE_MESSAGE);
096: }
097:
098: private String removeLeadingNewLine(String body) {
099: if (body.length() > 0 && body.charAt(0) == '\n') {
100: return body.substring(1);
101: } else {
102: return body;
103: }
104: }
105:
106: private URL createAttachmentUrl(WikiContext context,
107: String attachment) throws PluginException {
108: String baseUrl = context.getEngine().getBaseURL();
109: if (baseUrl == null || baseUrl.length() == 0) {
110: throw new PluginException(
111: "Unable to determine the base url for this wiki. Please contact the wiki administrator to adjust the settings."); //$NON-NLS-1$
112: }
113: try {
114: return new URL(
115: baseUrl
116: + "attach?page=" + context.getPage().getName() + "/" + attachment); //$NON-NLS-1$ //$NON-NLS-2$
117: } catch (MalformedURLException e) {
118: throw new PluginException(
119: "Unable to build attachment URL", e); //$NON-NLS-1$
120: }
121: }
122:
123: private String convertFileFromUrl(String urlString,
124: JavaSourceConversionOptions conversionOptions,
125: WikiContext context, String converterName)
126: throws PluginException {
127: URL url = null;
128: try {
129: url = new URL(urlString);
130: } catch (MalformedURLException e) {
131: throw new PluginException(
132: "Unable to open url '" + urlString + "'", e); //$NON-NLS-1$ //$NON-NLS-2$
133: }
134:
135: new PluginSecurityManager(context).checkUrlAccessEnabled(url);
136:
137: try {
138: return convert(url, new JavaSourceConversionSettings(
139: conversionOptions, converterName));
140: } catch (IOException e) {
141: throw new PluginException(
142: "Unable to read Url '" + url + "'", e); //$NON-NLS-1$ //$NON-NLS-2$
143: }
144: }
145: }
|