001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2014 ForgeRock AS
015 */
016
017package org.forgerock.doc.maven.pre;
018
019import org.apache.maven.plugin.MojoExecutionException;
020import org.codehaus.plexus.util.DirectoryScanner;
021import org.codehaus.plexus.util.FileUtils;
022import org.codehaus.plexus.util.StringUtils;
023import org.forgerock.doc.maven.AbstractDocbkxMojo;
024
025import java.io.File;
026import java.io.IOException;
027import java.util.Set;
028
029/**
030 * Replace {@code CURRENT.DOCID#} with the current document ID + #.
031 * The current document ID is used to resolve olinks.
032 */
033public class CurrentDocId {
034
035    /**
036     * The Mojo that holds configuration and related methods.
037     */
038    private AbstractDocbkxMojo m;
039
040    /**
041     * Constructor setting the Mojo that holds the configuration.
042     *
043     * @param mojo The Mojo that holds the configuration.
044     */
045    public CurrentDocId(final AbstractDocbkxMojo mojo) {
046        m = mojo;
047    }
048
049    private static final String CURRENT_DOCID = "CURRENT.DOCID#";
050
051    /**
052     * Replace {@code CURRENT.DOCID#} with the current document ID + #.
053     *
054     * @throws MojoExecutionException Failed to handle an XML source file.
055     */
056    public void execute() throws MojoExecutionException {
057
058        final Set<String> docNames = m.getDocNames();
059        final String sourceEncoding = m.getProject().getProperties()
060                .getProperty("project.build.sourceEncoding", "UTF-8");
061
062        for (String docName : docNames) {
063
064            File documentDirectory = new File(m.getDocbkxModifiableSourcesDirectory(), docName);
065            DirectoryScanner scanner = new DirectoryScanner();
066            scanner.setBasedir(documentDirectory);
067            scanner.setIncludes(new String[] { "**/*.xml" });
068            scanner.addDefaultExcludes();
069            scanner.scan();
070
071            for (String docFile : scanner.getIncludedFiles()) {
072                try {
073                    File documentFile = new File(documentDirectory, docFile);
074                    String content = FileUtils.fileRead(documentFile, sourceEncoding);
075                    String newContent = StringUtils.replace(content, CURRENT_DOCID, docName + "#");
076                    FileUtils.fileWrite(documentFile, sourceEncoding, newContent);
077                } catch (IOException e) {
078                    throw new MojoExecutionException(e.getMessage(), e);
079                }
080            }
081        }
082    }
083}