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 WikiPlugin {
024 public static final String DEFAULT_USAGE_MESSAGE = "Missing parameter 'source', 'url' or 'attachment' or missing body. Valid parameters are:"
025 + PluginParameterChecker.createValidParameterHtmlTable();
026
027 public String execute(WikiContext context, Map params) throws PluginException {
028 new PluginParameterChecker().checkParametersSupported(params);
029
030 final String printVersion = ParameterUtilities.getParameter(params, PluginParameter.PRINT_VERSION);
031 if (printVersion != null) {
032 return "<b>" + Version.getJava2HtmlConverterTitle() + "</b>";
033 }
034
035 JavaSourceConversionOptions conversionOptions;
036 try {
037 conversionOptions = new PluginConversionOptionsParser().parseConversionOptions(params);
038 }
039 catch (final IllegalConfigurationException e) {
040 throw new PluginException(e.getMessage(), e);
041 }
042
043 String converterName = ParameterUtilities.getParameter(params, PluginParameter.CONVERTER);
044 if (converterName == null) {
045 converterName = JavaSourceConverterProvider.getDefaultConverterName();
046 }
047 else {
048 if (JavaSourceConverterProvider.getJavaSourceConverterByName(converterName) == null) {
049 throw new PluginException("Unknown converter '" + converterName + "'."); //$NON-NLS-1$ //$NON-NLS-2$
050 }
051 }
052
053 final String urlString = ParameterUtilities.getParameter(params, PluginParameter.URL);
054 if (urlString != null) {
055 return convertFileFromUrl(urlString, conversionOptions, context, converterName);
056 }
057
058 final String source = ParameterUtilities.getParameter(params, PluginParameter.SOURCE);
059 if (source != null) {
060 return convert(source, new JavaSourceConversionSettings(conversionOptions, converterName));
061 }
062 String body = ParameterUtilities.getParameter(params, PluginParameter._BODY);
063 if (body != null) {
064 body = removeLeadingNewLine(body);
065 return convert(body, new JavaSourceConversionSettings(conversionOptions, converterName));
066 }
067 final String attachment = ParameterUtilities.getParameter(params, PluginParameter.ATTACHMENT);
068 if (attachment != null) {
069 new PluginSecurityManager(context).checkValidAttachmentUrlPart(attachment);
070 final URL url = createAttachmentUrl(context, attachment);
071 try {
072 return convert(url, new JavaSourceConversionSettings(conversionOptions, converterName));
073 }
074 catch (final IOException e) {
075 throw new PluginException("Unable to read Url '" + urlString + "'", e); //$NON-NLS-1$//$NON-NLS-2$
076 }
077 }
078
079 throw new PluginException(DEFAULT_USAGE_MESSAGE);
080 }
081
082 private String removeLeadingNewLine(String body) {
083 if (body.length() > 0 && body.charAt(0) == '\n') {
084 return body.substring(1);
085 }
086 else {
087 return body;
088 }
089 }
090
091 private URL createAttachmentUrl(WikiContext context, String attachment) throws PluginException {
092 final String baseUrl = context.getEngine().getBaseURL();
093 if (baseUrl == null || baseUrl.length() == 0) {
094 throw new PluginException(
095 "Unable to determine the base url for this wiki. Please contact the wiki administrator to adjust the settings."); //$NON-NLS-1$
096 }
097 try {
098 return new URL(baseUrl + "attach/" + context.getPage().getName() + "/" + attachment); //$NON-NLS-1$ //$NON-NLS-2$
099 }
100 catch (final MalformedURLException e) {
101 throw new PluginException("Unable to build attachment URL", e); //$NON-NLS-1$
102 }
103 }
104
105 private String convertFileFromUrl(
106 String urlString,
107 JavaSourceConversionOptions conversionOptions,
108 WikiContext context,
109 String converterName) throws PluginException {
110 URL url = null;
111 try {
112 url = new URL(urlString);
113 }
114 catch (final MalformedURLException e) {
115 throw new PluginException("Unable to open url '" + urlString + "'", e); //$NON-NLS-1$ //$NON-NLS-2$
116 }
117
118 new PluginSecurityManager(context).checkUrlAccessEnabled(url);
119
120 try {
121 return convert(url, new JavaSourceConversionSettings(conversionOptions, converterName));
122 }
123 catch (final IOException e) {
124 throw new PluginException("Unable to read Url '" + url + "'", e); //$NON-NLS-1$ //$NON-NLS-2$
125 }
126 }
127 }
|