Search in sources :

Example 6 with SiteRenderingContext

use of org.apache.maven.doxia.siterenderer.SiteRenderingContext in project maven-plugins by apache.

the class SiteMojo method renderLocale.

private void renderLocale(Locale locale, List<MavenReportExecution> reports) throws IOException, RendererException, MojoFailureException, MojoExecutionException {
    SiteRenderingContext context = createSiteRenderingContext(locale);
    // MSITE-723 add generated site directory, in case some content has been put in pre-site phase
    context.addSiteDirectory(generatedSiteDirectory);
    context.setInputEncoding(getInputEncoding());
    context.setOutputEncoding(getOutputEncoding());
    context.setValidate(validate);
    if (validate) {
        getLog().info("Validation is switched on, xml input documents will be validated!");
    }
    File outputDir = getOutputDirectory(locale);
    Map<String, DocumentRenderer> documents = locateDocuments(context, reports, locale);
    // copy resources
    siteRenderer.copyResources(context, outputDir);
    // 1. render Doxia documents first
    List<DocumentRenderer> reportDocuments = renderDoxiaDocuments(documents, context, outputDir, false);
    // prepare external reports
    for (MavenReportExecution mavenReportExecution : reports) {
        MavenReport report = mavenReportExecution.getMavenReport();
        report.setReportOutputDirectory(outputDir);
    }
    siteRenderer.render(reportDocuments, context, outputDir);
    if (generateSitemap) {
        getLog().info("Generating Sitemap.");
        new SiteMap(getOutputEncoding(), i18n).generate(context.getDecoration(), generatedSiteDirectory, locale);
    }
    // 3. Generated docs must be (re-)done afterwards as they are often generated by reports
    context.getSiteDirectories().clear();
    context.addSiteDirectory(generatedSiteDirectory);
    Map<String, DocumentRenderer> generatedDocuments = siteRenderer.locateDocumentFiles(context);
    renderDoxiaDocuments(generatedDocuments, context, outputDir, true);
    // copy generated resources also
    siteRenderer.copyResources(context, outputDir);
}
Also used : MavenReportExecution(org.apache.maven.reporting.exec.MavenReportExecution) MavenReport(org.apache.maven.reporting.MavenReport) SiteRenderingContext(org.apache.maven.doxia.siterenderer.SiteRenderingContext) File(java.io.File) DocumentRenderer(org.apache.maven.doxia.siterenderer.DocumentRenderer) DoxiaDocumentRenderer(org.apache.maven.doxia.siterenderer.DoxiaDocumentRenderer)

Example 7 with SiteRenderingContext

use of org.apache.maven.doxia.siterenderer.SiteRenderingContext in project maven-plugins by apache.

the class AbstractChangesReport method execute.

public void execute() throws MojoExecutionException {
    if (!canGenerateReport()) {
        return;
    }
    // TODO: push to a helper? Could still be improved by taking more of the site information from the site plugin
    Writer writer = null;
    try {
        DecorationModel model = new DecorationModel();
        model.setBody(new Body());
        Map<String, String> attributes = new HashMap<String, String>();
        attributes.put("outputEncoding", getOutputEncoding());
        Locale locale = Locale.getDefault();
        SiteRenderingContext siteContext = siteRenderer.createContextForSkin(getSkinArtifactFile(), attributes, model, getName(locale), locale);
        siteContext.setOutputEncoding(getOutputEncoding());
        RenderingContext context = new RenderingContext(outputDirectory, getOutputName() + ".html");
        SiteRendererSink sink = new SiteRendererSink(context);
        generate(sink, null, locale);
        outputDirectory.mkdirs();
        File file = new File(outputDirectory, getOutputName() + ".html");
        writer = new OutputStreamWriter(new FileOutputStream(file), getOutputEncoding());
        siteRenderer.generateDocument(writer, sink, siteContext);
        writer.close();
        writer = null;
        siteRenderer.copyResources(siteContext, new File(project.getBasedir(), "src/site/resources"), outputDirectory);
    } catch (RendererException e) {
        throw new MojoExecutionException("An error has occurred in " + getName(Locale.ENGLISH) + " report generation.", e);
    } catch (IOException e) {
        throw new MojoExecutionException("An error has occurred in " + getName(Locale.ENGLISH) + " report generation.", e);
    } catch (MavenReportException e) {
        throw new MojoExecutionException("An error has occurred in " + getName(Locale.ENGLISH) + " report generation.", e);
    } finally {
        IOUtils.closeQuietly(writer);
    }
}
Also used : Locale(java.util.Locale) RenderingContext(org.apache.maven.doxia.sink.render.RenderingContext) SiteRenderingContext(org.apache.maven.doxia.siterenderer.SiteRenderingContext) DecorationModel(org.apache.maven.doxia.site.decoration.DecorationModel) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) HashMap(java.util.HashMap) IOException(java.io.IOException) RendererException(org.apache.maven.doxia.siterenderer.RendererException) SiteRenderingContext(org.apache.maven.doxia.siterenderer.SiteRenderingContext) SiteRendererSink(org.apache.maven.doxia.siterenderer.sink.SiteRendererSink) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) Body(org.apache.maven.doxia.site.decoration.Body) File(java.io.File) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) MavenReportException(org.apache.maven.reporting.MavenReportException)

Example 8 with SiteRenderingContext

use of org.apache.maven.doxia.siterenderer.SiteRenderingContext in project maven-plugins by apache.

the class SiteRunMojo method createWebApplication.

private WebAppContext createWebApplication() throws MojoExecutionException {
    File webXml = new File(tempWebappDirectory, "WEB-INF/web.xml");
    webXml.getParentFile().mkdirs();
    InputStream inStream = null;
    FileOutputStream outStream = null;
    try {
        inStream = getClass().getResourceAsStream("/run/web.xml");
        outStream = new FileOutputStream(webXml);
        IOUtil.copy(inStream, outStream);
        outStream.close();
        outStream = null;
        inStream.close();
        inStream = null;
    } catch (FileNotFoundException e) {
        throw new MojoExecutionException("Unable to construct temporary webapp for running site", e);
    } catch (IOException e) {
        throw new MojoExecutionException("Unable to construct temporary webapp for running site", e);
    } finally {
        IOUtil.close(outStream);
        IOUtil.close(inStream);
    }
    WebAppContext webapp = new WebAppContext();
    webapp.setContextPath("/");
    webapp.setResourceBase(tempWebappDirectory.getAbsolutePath());
    webapp.setAttribute(DoxiaFilter.SITE_RENDERER_KEY, siteRenderer);
    webapp.getInitParams().put("org.mortbay.jetty.servlet.Default.useFileMappedBuffer", "false");
    // For external reports
    project.getReporting().setOutputDirectory(tempWebappDirectory.getAbsolutePath());
    for (MavenReportExecution mavenReportExecution : getReports()) {
        mavenReportExecution.getMavenReport().setReportOutputDirectory(tempWebappDirectory);
    }
    // TODO: is it sane to call getReports() method a second time?
    List<MavenReportExecution> reports = getReports();
    List<Locale> localesList = getLocales();
    webapp.setAttribute(DoxiaFilter.LOCALES_LIST_KEY, localesList);
    // Default is first in the list
    Locale defaultLocale = localesList.get(0);
    Locale.setDefault(defaultLocale);
    try {
        Map<String, DoxiaBean> i18nDoxiaContexts = new HashMap<String, DoxiaBean>();
        for (Locale locale : localesList) {
            SiteRenderingContext i18nContext = createSiteRenderingContext(locale);
            i18nContext.setInputEncoding(getInputEncoding());
            i18nContext.setOutputEncoding(getOutputEncoding());
            SiteRenderingContext i18nGeneratedSiteContext = createSiteRenderingContext(locale);
            i18nGeneratedSiteContext.setInputEncoding(getInputEncoding());
            i18nGeneratedSiteContext.setOutputEncoding(getOutputEncoding());
            i18nGeneratedSiteContext.getSiteDirectories().clear();
            Map<String, DocumentRenderer> i18nDocuments = locateDocuments(i18nContext, reports, locale);
            DoxiaBean doxiaBean;
            if (defaultLocale.equals(locale)) {
                i18nGeneratedSiteContext.addSiteDirectory(generatedSiteDirectory);
                doxiaBean = new DoxiaBean(i18nContext, i18nDocuments, i18nGeneratedSiteContext);
            } else {
                i18nGeneratedSiteContext.addSiteDirectory(new File(generatedSiteDirectory, locale.getLanguage()));
                doxiaBean = new DoxiaBean(i18nContext, i18nDocuments, i18nGeneratedSiteContext);
            }
            i18nDoxiaContexts.put(locale.getLanguage(), doxiaBean);
            if (defaultLocale.equals(locale)) {
                i18nDoxiaContexts.put("default", doxiaBean);
            }
            if (defaultLocale.equals(locale)) {
                siteRenderer.copyResources(i18nContext, tempWebappDirectory);
            } else {
                siteRenderer.copyResources(i18nContext, new File(tempWebappDirectory, locale.getLanguage()));
            }
        }
        webapp.setAttribute(DoxiaFilter.I18N_DOXIA_CONTEXTS_KEY, i18nDoxiaContexts);
    } catch (Exception e) {
        throw new MojoExecutionException("Unable to set up webapp", e);
    }
    return webapp;
}
Also used : Locale(java.util.Locale) MavenReportExecution(org.apache.maven.reporting.exec.MavenReportExecution) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) HashMap(java.util.HashMap) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) DocumentRenderer(org.apache.maven.doxia.siterenderer.DocumentRenderer) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) FileNotFoundException(java.io.FileNotFoundException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) WebAppContext(org.mortbay.jetty.webapp.WebAppContext) SiteRenderingContext(org.apache.maven.doxia.siterenderer.SiteRenderingContext) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 9 with SiteRenderingContext

use of org.apache.maven.doxia.siterenderer.SiteRenderingContext in project maven-plugins by apache.

the class DoxiaFilter method doFilter.

/**
     * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
     */
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    HttpServletRequest req = (HttpServletRequest) servletRequest;
    // ----------------------------------------------------------------------
    // Handle the servlet path
    // ----------------------------------------------------------------------
    String path = req.getServletPath();
    // welcome file
    if (path.endsWith("/")) {
        path += "index.html";
    }
    // Remove the /
    path = path.substring(1);
    // Handle locale request
    SiteRenderingContext context;
    Map<String, DocumentRenderer> documents;
    SiteRenderingContext generatedSiteContext;
    String localeWanted = null;
    for (Locale locale : localesList) {
        if (path.startsWith(locale.getLanguage() + "/")) {
            localeWanted = locale.toString();
            path = path.substring(locale.getLanguage().length() + 1);
        }
    }
    if (localeWanted == null) {
        DoxiaBean defaultDoxiaBean = i18nDoxiaContexts.get("default");
        if (defaultDoxiaBean == null) {
            throw new ServletException("No doxia bean found for the default locale");
        }
        context = defaultDoxiaBean.getContext();
        documents = defaultDoxiaBean.getDocuments();
        generatedSiteContext = defaultDoxiaBean.getGeneratedSiteContext();
    } else {
        DoxiaBean i18nDoxiaBean = i18nDoxiaContexts.get(localeWanted);
        if (i18nDoxiaBean == null) {
            throw new ServletException("No doxia bean found for the locale " + localeWanted);
        }
        context = i18nDoxiaBean.getContext();
        documents = i18nDoxiaBean.getDocuments();
        generatedSiteContext = i18nDoxiaBean.getGeneratedSiteContext();
    }
    // ----------------------------------------------------------------------
    if (documents.containsKey(path)) {
        try {
            DocumentRenderer renderer = documents.get(path);
            renderer.renderDocument(servletResponse.getWriter(), siteRenderer, context);
            if (renderer instanceof ReportDocumentRenderer) {
                ReportDocumentRenderer reportDocumentRenderer = (ReportDocumentRenderer) renderer;
                if (reportDocumentRenderer.isExternalReport()) {
                    try {
                        filterChain.doFilter(servletRequest, servletResponse);
                    } catch (Exception e) {
                        throw new ServletException(e);
                    }
                }
            }
            return;
        } catch (RendererException e) {
            throw new ServletException(e);
        }
    } else if (generatedSiteContext != null) {
        try {
            Map<String, DocumentRenderer> locateDocuments = siteRenderer.locateDocumentFiles(generatedSiteContext);
            if (locateDocuments.containsKey(path)) {
                DocumentRenderer renderer = locateDocuments.get(path);
                renderer.renderDocument(servletResponse.getWriter(), siteRenderer, generatedSiteContext);
                return;
            }
        } catch (RendererException e) {
            throw new ServletException(e);
        }
    }
    filterChain.doFilter(servletRequest, servletResponse);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Locale(java.util.Locale) ServletException(javax.servlet.ServletException) RendererException(org.apache.maven.doxia.siterenderer.RendererException) SiteRenderingContext(org.apache.maven.doxia.siterenderer.SiteRenderingContext) ReportDocumentRenderer(org.apache.maven.plugins.site.render.ReportDocumentRenderer) DocumentRenderer(org.apache.maven.doxia.siterenderer.DocumentRenderer) ReportDocumentRenderer(org.apache.maven.plugins.site.render.ReportDocumentRenderer) Map(java.util.Map) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) RendererException(org.apache.maven.doxia.siterenderer.RendererException)

Example 10 with SiteRenderingContext

use of org.apache.maven.doxia.siterenderer.SiteRenderingContext in project maven-plugins by apache.

the class AbstractProjectInfoReport method execute.

@Override
public void execute() throws MojoExecutionException {
    if (!canGenerateReport()) {
        return;
    }
    // TODO: push to a helper? Could still be improved by taking more of the site information from the site plugin
    Writer writer = null;
    try {
        String filename = getOutputName() + ".html";
        DecorationModel model = new DecorationModel();
        model.setBody(new Body());
        Map<String, Object> attributes = new HashMap<String, Object>();
        attributes.put("outputEncoding", "UTF-8");
        attributes.put("project", project);
        Locale locale = Locale.getDefault();
        Artifact defaultSkin = siteTool.getDefaultSkinArtifact(localRepository, project.getRemoteArtifactRepositories());
        SiteRenderingContext siteContext = siteRenderer.createContextForSkin(defaultSkin.getFile(), attributes, model, getName(locale), locale);
        RenderingContext context = new RenderingContext(outputDirectory, filename);
        SiteRendererSink sink = new SiteRendererSink(context);
        generate(sink, null, locale);
        outputDirectory.mkdirs();
        writer = new OutputStreamWriter(new FileOutputStream(new File(outputDirectory, filename)), "UTF-8");
        siteRenderer.generateDocument(writer, sink, siteContext);
        siteRenderer.copyResources(siteContext, new File(project.getBasedir(), "src/site/resources"), outputDirectory);
        writer.close();
        writer = null;
    } catch (RendererException e) {
        throw new MojoExecutionException("An error has occurred in " + getName(Locale.ENGLISH) + " report generation.", e);
    } catch (IOException e) {
        throw new MojoExecutionException("An error has occurred in " + getName(Locale.ENGLISH) + " report generation.", e);
    } catch (SiteToolException e) {
        throw new MojoExecutionException("An error has occurred in " + getName(Locale.ENGLISH) + " report generation.", e);
    } catch (MavenReportException e) {
        throw new MojoExecutionException("An error has occurred in " + getName(Locale.ENGLISH) + " report generation.", e);
    } finally {
        IOUtil.close(writer);
    }
}
Also used : Locale(java.util.Locale) RenderingContext(org.apache.maven.doxia.siterenderer.RenderingContext) SiteRenderingContext(org.apache.maven.doxia.siterenderer.SiteRenderingContext) DecorationModel(org.apache.maven.doxia.site.decoration.DecorationModel) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) HashMap(java.util.HashMap) IOException(java.io.IOException) Artifact(org.apache.maven.artifact.Artifact) SiteToolException(org.apache.maven.doxia.tools.SiteToolException) RendererException(org.apache.maven.doxia.siterenderer.RendererException) SiteRenderingContext(org.apache.maven.doxia.siterenderer.SiteRenderingContext) SiteRendererSink(org.apache.maven.doxia.siterenderer.sink.SiteRendererSink) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) Body(org.apache.maven.doxia.site.decoration.Body) File(java.io.File) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) MavenReportException(org.apache.maven.reporting.MavenReportException)

Aggregations

SiteRenderingContext (org.apache.maven.doxia.siterenderer.SiteRenderingContext)11 DecorationModel (org.apache.maven.doxia.site.decoration.DecorationModel)7 File (java.io.File)6 IOException (java.io.IOException)5 Writer (java.io.Writer)5 RendererException (org.apache.maven.doxia.siterenderer.RendererException)5 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)5 HashMap (java.util.HashMap)4 Locale (java.util.Locale)4 DocumentRenderer (org.apache.maven.doxia.siterenderer.DocumentRenderer)4 FileOutputStream (java.io.FileOutputStream)3 SiteToolException (org.apache.maven.doxia.tools.SiteToolException)3 OutputStreamWriter (java.io.OutputStreamWriter)2 Map (java.util.Map)2 Artifact (org.apache.maven.artifact.Artifact)2 Body (org.apache.maven.doxia.site.decoration.Body)2 RenderingContext (org.apache.maven.doxia.siterenderer.RenderingContext)2 SiteRendererSink (org.apache.maven.doxia.siterenderer.sink.SiteRendererSink)2 MojoFailureException (org.apache.maven.plugin.MojoFailureException)2 MavenReport (org.apache.maven.reporting.MavenReport)2