View Javadoc

1   /*
2    * Copyright (c) 2010 Ralph Jones
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20   * THE SOFTWARE.
21   */
22  package com.totalchange.osopendata.generator;
23  
24  import java.io.File;
25  
26  import org.apache.maven.model.Resource;
27  import org.apache.maven.plugin.AbstractMojo;
28  import org.apache.maven.plugin.MojoExecutionException;
29  import org.apache.maven.plugin.MojoFailureException;
30  import org.apache.maven.project.MavenProject;
31  
32  /**
33   * Populates an HSQLDB database with OS OpenData.
34   * 
35   * @goal populate
36   */
37  public class PopulateMojo extends AbstractMojo {
38      /**
39       * @parameter default-value="${basedir}/src/main/osopendata/Code-Point Open"
40       * @required
41       */
42      private String codePointDirectory;
43  
44      /**
45       * @parameter default-value="${basedir}/src/main/osopendata/OS Locator"
46       * @required
47       */
48      private String locatorDirectory;
49  
50      /**
51       * @parameter default-value="com/totalchange/osopendata/osopendata"
52       * @required
53       */
54      private String outputDatabase;
55  
56      /**
57       * @parameter 
58       *            default-value="${basedir}/target/generated-resources/osopendata"
59       * @required
60       */
61      private String generateInto;
62  
63      /**
64       * The maven project
65       * 
66       * @parameter expression="${project}"
67       * @required
68       * @readonly
69       */
70      private MavenProject project;
71  
72      @Override
73      public void execute() throws MojoExecutionException, MojoFailureException {
74          File dbFile = new File(generateInto + File.separator + outputDatabase);
75  
76          getLog().info(
77                  "Creating OS OpenData database " + dbFile.getAbsolutePath()
78                          + " based upon code point directory "
79                          + codePointDirectory + " and locator directory "
80                          + locatorDirectory);
81  
82          File dbDataFile = new File(dbFile.getAbsolutePath() + ".data");
83          if (!dbDataFile.exists()) {
84              try {
85                  dbFile.getParentFile().mkdirs();
86  
87                  Populate.populate(dbFile.getAbsolutePath(), codePointDirectory,
88                          locatorDirectory);
89  
90                  // Get rid of the backup file (it's not necessary)
91                  File dbBackupFile = new File(dbFile.getAbsolutePath()
92                          + ".backup");
93                  dbBackupFile.delete();
94              } catch (Throwable th) {
95                  throw new MojoExecutionException(
96                          "Failed to create OS OpenData "
97                                  + "database with error: " + th.getMessage(), th);
98              }
99          } else {
100             getLog().info("Skipped database population as already exists");
101         }
102 
103         Resource resource = new Resource();
104         resource.setDirectory(generateInto);
105         project.addResource(resource);
106     }
107 }