View Javadoc
1   /*
2    * The contents of this file are subject to the terms of the Common Development and
3    * Distribution License (the License). You may not use this file except in compliance with the
4    * License.
5    *
6    * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7    * specific language governing permission and limitations under the License.
8    *
9    * When distributing Covered Software, include this CDDL Header Notice in each file and include
10   * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11   * Header, with the fields enclosed by brackets [] replaced by your own identifying
12   * information: "Portions copyright [year] [name of copyright owner]".
13   *
14   * Copyright 2014 ForgeRock AS
15   */
16  
17  package org.forgerock.doc.maven.pre;
18  
19  import org.apache.maven.plugin.MojoExecutionException;
20  import org.codehaus.plexus.util.DirectoryScanner;
21  import org.codehaus.plexus.util.FileUtils;
22  import org.codehaus.plexus.util.StringUtils;
23  import org.forgerock.doc.maven.AbstractDocbkxMojo;
24  
25  import java.io.File;
26  import java.io.IOException;
27  import java.util.Set;
28  
29  /**
30   * Replace {@code CURRENT.DOCID#} with the current document ID + #.
31   * The current document ID is used to resolve olinks.
32   */
33  public class CurrentDocId {
34  
35      /**
36       * The Mojo that holds configuration and related methods.
37       */
38      private AbstractDocbkxMojo m;
39  
40      /**
41       * Constructor setting the Mojo that holds the configuration.
42       *
43       * @param mojo The Mojo that holds the configuration.
44       */
45      public CurrentDocId(final AbstractDocbkxMojo mojo) {
46          m = mojo;
47      }
48  
49      private static final String CURRENT_DOCID = "CURRENT.DOCID#";
50  
51      /**
52       * Replace {@code CURRENT.DOCID#} with the current document ID + #.
53       *
54       * @throws MojoExecutionException Failed to handle an XML source file.
55       */
56      public void execute() throws MojoExecutionException {
57  
58          final Set<String> docNames = m.getDocNames();
59          final String sourceEncoding = m.getProject().getProperties()
60                  .getProperty("project.build.sourceEncoding", "UTF-8");
61  
62          for (String docName : docNames) {
63  
64              File documentDirectory = new File(m.getDocbkxModifiableSourcesDirectory(), docName);
65              DirectoryScanner scanner = new DirectoryScanner();
66              scanner.setBasedir(documentDirectory);
67              scanner.setIncludes(new String[] { "**/*.xml" });
68              scanner.addDefaultExcludes();
69              scanner.scan();
70  
71              for (String docFile : scanner.getIncludedFiles()) {
72                  try {
73                      File documentFile = new File(documentDirectory, docFile);
74                      String content = FileUtils.fileRead(documentFile, sourceEncoding);
75                      String newContent = StringUtils.replace(content, CURRENT_DOCID, docName + "#");
76                      FileUtils.fileWrite(documentFile, sourceEncoding, newContent);
77                  } catch (IOException e) {
78                      throw new MojoExecutionException(e.getMessage(), e);
79                  }
80              }
81          }
82      }
83  }