Search in sources :

Example 61 with BuildException

use of org.apache.tools.ant.BuildException in project mylyn.docs by eclipse.

the class MarkupToHtmlTask method processFile.

/**
 * process the file
 *
 * @param baseDir
 * @param source
 * @return the lightweight markup, or null if the file was not written
 * @throws BuildException
 */
protected String processFile(MarkupLanguage markupLanguage, final File baseDir, final File source) throws BuildException {
    // $NON-NLS-1$
    log(MessageFormat.format(Messages.getString("MarkupToHtmlTask.14"), source), Project.MSG_VERBOSE);
    String markupContent = null;
    String name = source.getName();
    if (name.lastIndexOf('.') != -1) {
        name = name.substring(0, name.lastIndexOf('.'));
    }
    File htmlOutputFile = computeHtmlFile(source, name);
    if (!htmlOutputFile.exists() || overwrite || htmlOutputFile.lastModified() < source.lastModified()) {
        if (markupContent == null) {
            markupContent = readFully(source);
        }
        performValidation(source, markupContent);
        Writer writer;
        try {
            // $NON-NLS-1$
            writer = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(htmlOutputFile)), "utf-8");
        } catch (Exception e) {
            throw new BuildException(MessageFormat.format(Messages.getString("MarkupToHtmlTask.16"), htmlOutputFile, e.getMessage()), // $NON-NLS-1$
            e);
        }
        try {
            HtmlDocumentBuilder builder = new HtmlDocumentBuilder(writer, formatOutput);
            for (Stylesheet stylesheet : stylesheets) {
                HtmlDocumentBuilder.Stylesheet builderStylesheet;
                if (stylesheet.url != null) {
                    builderStylesheet = new HtmlDocumentBuilder.Stylesheet(stylesheet.url);
                } else {
                    builderStylesheet = new HtmlDocumentBuilder.Stylesheet(stylesheet.file);
                }
                builder.addCssStylesheet(builderStylesheet);
                if (!stylesheet.attributes.isEmpty()) {
                    for (Map.Entry<String, String> attr : stylesheet.attributes.entrySet()) {
                        builderStylesheet.getAttributes().put(attr.getKey(), attr.getValue());
                    }
                }
            }
            builder.setTitle(title == null ? name : title);
            builder.setEmitDtd(emitDoctype);
            if (emitDoctype && htmlDoctype != null) {
                builder.setHtmlDtd(htmlDoctype);
            }
            builder.setUseInlineStyles(useInlineCssStyles);
            builder.setSuppressBuiltInStyles(suppressBuiltInCssStyles);
            builder.setLinkRel(linkRel);
            builder.setDefaultAbsoluteLinkTarget(defaultAbsoluteLinkTarget);
            builder.setPrependImagePrefix(prependImagePrefix);
            builder.setXhtmlStrict(xhtmlStrict);
            builder.setCopyrightNotice(copyrightNotice);
            builder.setHtmlFilenameFormat(htmlFilenameFormat);
            SplittingStrategy splittingStrategy = multipleOutputFiles ? new DefaultSplittingStrategy() : new NoSplittingStrategy();
            SplittingOutlineParser outlineParser = new SplittingOutlineParser();
            outlineParser.setMarkupLanguage(markupLanguage.clone());
            outlineParser.setSplittingStrategy(splittingStrategy);
            SplitOutlineItem item = outlineParser.parse(markupContent);
            item.setSplitTarget(htmlOutputFile.getName());
            SplittingHtmlDocumentBuilder splittingBuilder = new SplittingHtmlDocumentBuilder();
            splittingBuilder.setRootBuilder(builder);
            splittingBuilder.setOutline(item);
            splittingBuilder.setRootFile(htmlOutputFile);
            splittingBuilder.setNavigationImages(navigationImages);
            splittingBuilder.setFormatting(formatOutput);
            MarkupParser parser = new MarkupParser();
            parser.setMarkupLanguage(markupLanguage);
            parser.setBuilder(splittingBuilder);
            parser.parse(markupContent);
            processed(markupContent, item, baseDir, source);
        } finally {
            try {
                writer.close();
            } catch (Exception e) {
                throw new BuildException(MessageFormat.format(// $NON-NLS-1$
                Messages.getString("MarkupToHtmlTask.17"), // $NON-NLS-1$
                htmlOutputFile, e.getMessage()), e);
            }
        }
    }
    return markupContent;
}
Also used : SplittingStrategy(org.eclipse.mylyn.wikitext.splitter.SplittingStrategy) NoSplittingStrategy(org.eclipse.mylyn.wikitext.splitter.NoSplittingStrategy) DefaultSplittingStrategy(org.eclipse.mylyn.wikitext.splitter.DefaultSplittingStrategy) SplitOutlineItem(org.eclipse.mylyn.wikitext.splitter.SplitOutlineItem) HtmlDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder) SplittingHtmlDocumentBuilder(org.eclipse.mylyn.wikitext.splitter.SplittingHtmlDocumentBuilder) BuildException(org.apache.tools.ant.BuildException) FileOutputStream(java.io.FileOutputStream) SplittingOutlineParser(org.eclipse.mylyn.wikitext.splitter.SplittingOutlineParser) NoSplittingStrategy(org.eclipse.mylyn.wikitext.splitter.NoSplittingStrategy) OutputStreamWriter(java.io.OutputStreamWriter) BuildException(org.apache.tools.ant.BuildException) SplittingHtmlDocumentBuilder(org.eclipse.mylyn.wikitext.splitter.SplittingHtmlDocumentBuilder) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) HashMap(java.util.HashMap) Map(java.util.Map) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) DefaultSplittingStrategy(org.eclipse.mylyn.wikitext.splitter.DefaultSplittingStrategy) MarkupParser(org.eclipse.mylyn.wikitext.parser.MarkupParser)

Example 62 with BuildException

use of org.apache.tools.ant.BuildException in project mylyn.docs by eclipse.

the class WikiToDocTask method execute.

@Override
public void execute() throws ConfigurationException {
    if (dest == null) {
        // $NON-NLS-1$
        throw new ConfigurationException(Messages.getString("WikiToDocTask_specify_dest"));
    }
    if (wikiBaseUrl == null) {
        // $NON-NLS-1$
        throw new ConfigurationException(Messages.getString("WikiToDocTask_specify_wikiBaseUrl"));
    }
    if (paths.isEmpty()) {
        // $NON-NLS-1$
        throw new ConfigurationException(Messages.getString("WikiToDocTask_specify_paths"));
    }
    if (getInternalLinkPattern() == null) {
        setInternalLinkPattern(computeDefaultInternalLinkPattern());
    }
    Set<String> pathNames = new HashSet<String>();
    for (Path path : paths) {
        if (path.name == null) {
            // $NON-NLS-1$
            throw new ConfigurationException(Messages.getString("WikiToDocTask_path_must_have_name"));
        }
        if (path.name != null) {
            if (!pathNames.add(path.name)) {
                throw new ConfigurationException(MessageFormat.format(Messages.getString("WikiToDocTask_path_name_must_be_unique"), // $NON-NLS-1$
                path.name));
            }
        }
        if (!path.includeInUnifiedToc && path.getTocParentName() != null) {
            throw new ConfigurationException(MessageFormat.format(Messages.getString("WikiToDocTask_tocParentName_not_in_unified_toc"), // $NON-NLS-1$
            path.name));
        }
    }
    if (generateUnifiedToc) {
        for (Path path : paths) {
            if (path.getTocParentName() != null) {
                if (!pathNames.contains(path.getTocParentName())) {
                    throw new ConfigurationException(MessageFormat.format(Messages.getString("WikiToDocTask_unknown_tocParentName"), // $NON-NLS-1$
                    path.getTocParentName()));
                }
            }
        }
    }
    MediaWikiLanguage markupLanguage = (MediaWikiLanguage) createMarkupLanguage();
    WikiTemplateResolver templateResolver = new WikiTemplateResolver();
    templateResolver.setWikiBaseUrl(wikiBaseUrl);
    markupLanguage.getTemplateProviders().add(templateResolver);
    markupLanguage.setTemplateExcludes(templateExcludes);
    for (Stylesheet stylesheet : stylesheets) {
        if (stylesheet.url == null && stylesheet.file == null) {
            // $NON-NLS-1$
            throw new BuildException(Messages.getString("WikiToDocTask_stylesheet_file_or_url"));
        }
        if (stylesheet.url != null && stylesheet.file != null) {
            // $NON-NLS-1$
            throw new BuildException(Messages.getString("WikiToDocTask_stylesheet_not_both"));
        }
        if (stylesheet.file != null) {
            if (!stylesheet.file.exists()) {
                throw new BuildException(// $NON-NLS-1$
                MessageFormat.format(// $NON-NLS-1$
                Messages.getString("WikiToDocTask_stylesheet_file_not_exist"), stylesheet.file));
            }
            if (!stylesheet.file.isFile()) {
                throw new BuildException(// $NON-NLS-1$
                MessageFormat.format(// $NON-NLS-1$
                Messages.getString("WikiToDocTask_stylesheet_file_not_file"), stylesheet.file));
            }
            if (!stylesheet.file.canRead()) {
                throw new BuildException(MessageFormat.format(Messages.getString("WikiToDocTask_stylesheet_file_cannot_read"), // $NON-NLS-1$
                stylesheet.file));
            }
        }
    }
    if (!dest.exists()) {
        if (!dest.mkdirs()) {
            throw new BuildException(// $NON-NLS-1$
            MessageFormat.format("Cannot create dest folder: {0}", dest.getAbsolutePath()));
        }
    }
    if (tocFile == null) {
        // $NON-NLS-1$
        tocFile = new File(dest, "toc.xml");
    }
    Map<String, String> pathNameToContent = new HashMap<String, String>();
    Map<String, SplitOutlineItem> pathNameToOutline = new HashMap<String, SplitOutlineItem>();
    for (Path path : paths) {
        getProject().log(// $NON-NLS-1$
        MessageFormat.format(Messages.getString("WikiToDocTask_fetching_content_for_page"), path.name), Project.MSG_VERBOSE);
        URL pathUrl = computeRawUrl(path.name);
        try {
            Reader input = createInputReader(pathUrl);
            try {
                String content = readFully(input);
                content = preprocessMarkup(path, content);
                pathNameToContent.put(path.name, content);
                final File targetFile = computeHtmlOutputFile(path);
                SplitOutlineItem outline = computeOutline(path, markupLanguage, targetFile, content);
                outline.setResourcePath(targetFile.getAbsolutePath());
                pathNameToOutline.put(path.name, outline);
            } finally {
                input.close();
            }
        } catch (final IOException e) {
            // $NON-NLS-1$
            final String message = MessageFormat.format("Cannot read from {0}: {1}", pathUrl, e.getMessage());
            throw new BuildException(message, e);
        }
    }
    for (Path path : paths) {
        // $NON-NLS-1$
        getProject().log(// $NON-NLS-1$
        MessageFormat.format(Messages.getString("WikiToDocTask_processing_page"), path.name), Project.MSG_DEBUG);
        String markupContent = pathNameToContent.get(path.name);
        if (isValidate()) {
            performValidation(markupLanguage, path, markupContent);
        }
        Set<String> imageFilenames = null;
        if (!fetchImages) {
            // $NON-NLS-1$
            getProject().log(Messages.getString("WikiToDocTask_skipping_images"), Project.MSG_WARN);
        } else {
            imageFilenames = fetchImages(markupLanguage, path);
        }
        markupToDoc(markupLanguage, path, markupContent, pathNameToOutline, imageFilenames);
        if (path.isGenerateToc()) {
            createToc(path, pathNameToOutline.get(path.name));
        }
    }
    if (generateUnifiedToc) {
        createToc(paths, pathNameToOutline);
    }
}
Also used : HashMap(java.util.HashMap) WikiTemplateResolver(org.eclipse.mylyn.wikitext.mediawiki.WikiTemplateResolver) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) SplitOutlineItem(org.eclipse.mylyn.wikitext.splitter.SplitOutlineItem) IOException(java.io.IOException) URL(java.net.URL) MediaWikiLanguage(org.eclipse.mylyn.wikitext.mediawiki.MediaWikiLanguage) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) HashSet(java.util.HashSet)

Example 63 with BuildException

use of org.apache.tools.ant.BuildException in project mylyn.docs by eclipse.

the class WikiToDocTask method createToc.

private void createToc(final Path path, OutlineItem rootItem) {
    SplittingMarkupToEclipseToc markupToEclipseToc = new SplittingMarkupToEclipseToc() {

        @Override
        protected String computeFile(OutlineItem item) {
            if (item instanceof SplitOutlineItem) {
                return computeTocRelativeFile(item, path);
            }
            return super.computeFile(item);
        }
    };
    markupToEclipseToc.setBookTitle(path.getTitle());
    markupToEclipseToc.setHtmlFile(computeTocRelativeFile(rootItem, path));
    String tocContents = markupToEclipseToc.createToc(rootItem);
    // $NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
    File tocFile = new File(dest, path.name.replaceAll("[^a-zA-Z0-9]", "-") + "-toc.xml");
    try {
        // $NON-NLS-1$
        Writer writer = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(tocFile)), "UTF-8");
        try {
            writer.write(tocContents);
        } finally {
            writer.close();
        }
    } catch (IOException e) {
        // $NON-NLS-1$
        String message = MessageFormat.format("Cannot write {0}: {1}", tocFile, e.getMessage());
        throw new BuildException(message, e);
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) SplittingMarkupToEclipseToc(org.eclipse.mylyn.wikitext.splitter.SplittingMarkupToEclipseToc) SplitOutlineItem(org.eclipse.mylyn.wikitext.splitter.SplitOutlineItem) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer) SplitOutlineItem(org.eclipse.mylyn.wikitext.splitter.SplitOutlineItem) OutlineItem(org.eclipse.mylyn.wikitext.parser.outline.OutlineItem)

Example 64 with BuildException

use of org.apache.tools.ant.BuildException in project mylyn.docs by eclipse.

the class WikiToDocTask method computeRawUrl.

private URL computeRawUrl(String path) {
    try {
        String qualifiedUrl = wikiBaseUrl;
        if (!qualifiedUrl.endsWith("/")) {
            // $NON-NLS-1$
            // $NON-NLS-1$
            qualifiedUrl += "/";
        }
        // ignore titleParameter here, we always reference index.php when getting the raw page content
        // $NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$
        qualifiedUrl += "index.php?title=" + URLEncoder.encode(path, "UTF-8") + "&action=raw";
        return new URL(qualifiedUrl);
    } catch (IOException e) {
        throw new BuildException(// $NON-NLS-1$
        MessageFormat.format(// $NON-NLS-1$
        Messages.getString("WikiToDocTask_cannot_compute_raw_url"), // $NON-NLS-1$
        path, e.getMessage()), e);
    }
}
Also used : IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) URL(java.net.URL)

Example 65 with BuildException

use of org.apache.tools.ant.BuildException in project mylyn.docs by eclipse.

the class WikiToDocTask method fetchImages.

protected Set<String> fetchImages(MarkupLanguage markupLanguage, Path path) {
    File dest = computeDestDir(path);
    if (prependImagePrefix != null) {
        dest = new File(dest, prependImagePrefix);
        if (!dest.exists()) {
            if (!dest.mkdirs()) {
                throw new BuildException(// $NON-NLS-1$
                MessageFormat.format("Cannot create images folder: {0}", dest.getAbsolutePath()));
            }
        }
    }
    // $NON-NLS-1$
    getProject().log(// $NON-NLS-1$
    MessageFormat.format(Messages.getString("WikiToDocTask_fetching_images_for_page"), path.name), Project.MSG_VERBOSE);
    MediaWikiApiImageFetchingStrategy imageFetchingStrategy = createImageFetchingStrategy();
    imageFetchingStrategy.setTask(this);
    imageFetchingStrategy.setDest(dest);
    imageFetchingStrategy.setPageName(path.name);
    try {
        imageFetchingStrategy.setUrl(new URL(wikiBaseUrl));
    } catch (MalformedURLException e) {
        throw new BuildException(e);
    }
    return imageFetchingStrategy.fetchImages();
}
Also used : MalformedURLException(java.net.MalformedURLException) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) URL(java.net.URL)

Aggregations

BuildException (org.apache.tools.ant.BuildException)930 IOException (java.io.IOException)390 File (java.io.File)365 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)75 ArrayList (java.util.ArrayList)65 InputStream (java.io.InputStream)62 Project (org.apache.tools.ant.Project)61 Resource (org.apache.tools.ant.types.Resource)58 FileSet (org.apache.tools.ant.types.FileSet)52 Path (org.apache.tools.ant.types.Path)52 Commandline (org.apache.tools.ant.types.Commandline)51 Properties (java.util.Properties)50 OutputStream (java.io.OutputStream)44 FileOutputStream (java.io.FileOutputStream)42 FileResource (org.apache.tools.ant.types.resources.FileResource)42 FileInputStream (java.io.FileInputStream)41 URL (java.net.URL)40 BufferedReader (java.io.BufferedReader)37 Writer (java.io.Writer)37 MalformedURLException (java.net.MalformedURLException)37