Author: tchemit Date: 2009-12-21 14:02:20 +0100 (Mon, 21 Dec 2009) New Revision: 1687 Added: trunk/maven-jaxx-plugin/src/main/java/org/nuiton/jaxx/plugin/AbstractGenerateHelpMojo.java trunk/maven-jaxx-plugin/src/main/java/org/nuiton/jaxx/plugin/GenerateHelpSearchMojo.java Modified: trunk/maven-jaxx-plugin/src/main/java/org/nuiton/jaxx/plugin/AbstractJaxxMojo.java trunk/maven-jaxx-plugin/src/main/java/org/nuiton/jaxx/plugin/GenerateHelpMojo.java trunk/maven-jaxx-plugin/src/main/java/org/nuiton/jaxx/plugin/GenerateMojo.java trunk/maven-jaxx-plugin/src/site/rst/index.rst trunk/maven-jaxx-plugin/src/site/site.xml Log: Evolution #172: G?\195?\169n?\195?\169rer les index de l'aide dans un goal sp?\195?\169cific Copied: trunk/maven-jaxx-plugin/src/main/java/org/nuiton/jaxx/plugin/AbstractGenerateHelpMojo.java (from rev 1686, trunk/maven-jaxx-plugin/src/main/java/org/nuiton/jaxx/plugin/GenerateHelpMojo.java) =================================================================== --- trunk/maven-jaxx-plugin/src/main/java/org/nuiton/jaxx/plugin/AbstractGenerateHelpMojo.java (rev 0) +++ trunk/maven-jaxx-plugin/src/main/java/org/nuiton/jaxx/plugin/AbstractGenerateHelpMojo.java 2009-12-21 13:02:20 UTC (rev 1687) @@ -0,0 +1,211 @@ +/* *##% + * JAXX Maven plugin + * Copyright (C) 2008 - 2009 CodeLutin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * ##%*/ +package org.nuiton.jaxx.plugin; + +import org.apache.maven.plugin.MojoFailureException; +import org.nuiton.i18n.I18n; +import org.nuiton.plugin.VelocityTemplateGenerator; +import org.nuiton.util.SortedProperties; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStreamReader; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; +import java.util.Properties; + +/** + * Abstract Mojo to generate help stuff. + * + * @author chemit + * @since 2.0.0 + */ +public abstract class AbstractGenerateHelpMojo extends AbstractJaxxMojo { + + protected static final String AUTOREMOVE_LINE = "REMOVE THS LINE TO DISABLE AUTO-REGENERATE THE FILE"; + /** + * The directory where to create or update help files. + * + * @parameter expression="${jaxx.outHelp}" default-value="${project.basedir}/src/main/help" + * @required + * @since 1.3 + */ + protected File outHelp; + /** + * The locales to generate for help, separeted by comma. + * <p/> + * The first locale given is the default locale. + * + * @parameter expression="${jaxx.locales}" + * @required + * @since 2.0.0 + */ + protected String locales; + /** + * The name of the helpset to generate. + * + * @parameter expression="${jaxx.helpsetName}" default-value="${project.artifactId}" + * @required + * @since 1.3 + */ + protected String helpsetName; + /** + * Flag to generate the search index. + * + * @parameter expression="${jaxx.generateSearch}" default-value="true" + * @required + * @since 1.3 + */ + protected boolean generateSearch; + /** + * The help ids discovered in {@link #helpIdsStore} files. + */ + protected static Properties helpIds; + /** + * Default locale (the first locale in {@link #localesToTreate}. + */ + protected Locale defaultLocale; + /** + * Locales to treate + */ + protected Locale[] localesToTreate; + + /** + * Do the action for the given locale. + * + * @param locale the locale to treate + * @param isDefaultLocale {@code true} if given locale is de the default locale + * @param localizedTarget where are stored help files for the given locale + * @param localePath the locale path to use (is {@code default} if given locale is default). + * @throws Exception if any pb + */ + protected abstract void doActionForLocale(Locale locale, boolean isDefaultLocale, File localizedTarget, String localePath) throws Exception; + + /** + * Call back after doing all stuff for all locales declared + */ + protected abstract void postDoAction(); + + @Override + public boolean init() throws Exception { + + File idsStore = getHelpIdsStore(); + + if (!idsStore.exists()) { + getLog().info("no helpIdStore to react at " + idsStore); + return false; + } + + if (locales == null || locales.trim().isEmpty()) { + throw new MojoFailureException("You must set the 'locales' property properly (was " + locales + ")."); + } + + // check there is a outHelp + if (outHelp == null) { + throw new MojoFailureException("You must set the 'outHelp' property."); + } + + List<Locale> tmp = new ArrayList<Locale>(); + for (String loc : locales.split(",")) { + Locale l = I18n.newLocale(loc); + tmp.add(l); + } + + if (tmp.isEmpty()) { + throw new MojoFailureException("you must set the 'locales' property."); + } + + localesToTreate = tmp.toArray(new Locale[tmp.size()]); + defaultLocale = localesToTreate[0]; + + createDirectoryIfNecessary(getTargetDirectory()); + + // load ids from idsStore file (only once by session) + + if (helpIds == null) { + helpIds = new SortedProperties(); + + BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(idsStore), getEncoding())); + String id; + while ((id = reader.readLine()) != null) { + id = id.trim(); + String path = id.replaceAll("\\.", File.separator) + ".html"; + helpIds.put(id, path); + } + + if (helpIds.isEmpty()) { + + // no ids detected + getLog().warn("No helpIds detected, will skip."); + return false; + } + } + return true; + } + + @Override + public final void doAction() throws Exception { + + for (Locale locale : localesToTreate) { + + boolean isDefaultLocale = locale == defaultLocale; + + String language = locale.getLanguage(); + + String localePath = (isDefaultLocale ? "default" : language); + + File localizedTarget = new File(outHelp, localePath); + + createDirectoryIfNecessary(localizedTarget); + + doActionForLocale(locale, isDefaultLocale, localizedTarget, localePath); + + } + + postDoAction(); + } + + @Override + public File getTargetDirectory() { + return outHelp; + } + + @Override + public void setTargetDirectory(File targetDirectory) { + this.outHelp = targetDirectory; + } + + protected void doGen(File template, File f, Properties env) throws Exception { + VelocityTemplateGenerator gen = prepareGenerator(template); + gen.generate(env, f); + } + + protected VelocityTemplateGenerator prepareGenerator(File template) throws Exception { + URL templateURL = getTemplate(template); + + if (verbose) { + getLog().info("using template " + templateURL); + } + VelocityTemplateGenerator gen = new VelocityTemplateGenerator(project, templateURL); + return gen; + } +} \ No newline at end of file Property changes on: trunk/maven-jaxx-plugin/src/main/java/org/nuiton/jaxx/plugin/AbstractGenerateHelpMojo.java ___________________________________________________________________ Added: svn:mergeinfo + Modified: trunk/maven-jaxx-plugin/src/main/java/org/nuiton/jaxx/plugin/AbstractJaxxMojo.java =================================================================== --- trunk/maven-jaxx-plugin/src/main/java/org/nuiton/jaxx/plugin/AbstractJaxxMojo.java 2009-12-20 19:57:10 UTC (rev 1686) +++ trunk/maven-jaxx-plugin/src/main/java/org/nuiton/jaxx/plugin/AbstractJaxxMojo.java 2009-12-21 13:02:20 UTC (rev 1687) @@ -18,9 +18,13 @@ * ##%*/ package org.nuiton.jaxx.plugin; +import org.apache.maven.model.Resource; import org.apache.maven.project.MavenProject; import java.io.File; +import java.io.IOException; +import java.util.List; + import org.nuiton.plugin.AbstractPlugin; /** @@ -109,4 +113,68 @@ public void setEncoding(String encoding) { this.encoding = encoding; } + + /** + * TODO-TC20091221 Should move this to AbstractPlugin + * Create the directory if necessary. + * + * @param dir the directory to create if not already existing + * @throws IOException if could not create the directory + */ + protected void createDirectoryIfNecessary(File dir) throws IOException { + if (!dir.exists()) { + if (verbose) { + getLog().info("mkdir " + dir); + } + boolean b = dir.mkdirs(); + if (!b) { + throw new IOException("could not create directory "+dir); + } + } + } + + //TODO-TC20091221 move this api to PluginHelper + public static boolean addResourceDir(File dir, MavenProject project, String... includes) { + List<?> resources = project.getResources(); + boolean added = addResourceDir(dir, project, resources, includes); + return added; + } + + //TODO-TC20091221 move this api to PluginHelper + public static boolean addTestResourceDir(File newresourceDir, MavenProject project, String... includes) { + List<?> resources = project.getTestResources(); + boolean added = addResourceDir(newresourceDir, project, resources, includes); + return added; + } + + //TODO-TC20091221 move this api to PluginHelper + public static boolean addResourceDir(File dir, MavenProject project, List<?> resources, String... includes) { + String newresourceDir = dir.getAbsolutePath(); + boolean shouldAdd = true; + for (Object o : resources) { + Resource r = (Resource) o; + if (!r.getDirectory().equals(newresourceDir)) { + continue; + } + + for (String i : includes) { + if (!r.getIncludes().contains(i)) { + r.addInclude(i); + } + } + shouldAdd = false; + break; + } + if (shouldAdd) { + Resource r = new Resource(); + r.setDirectory(newresourceDir); + for (String i : includes) { + if (!r.getIncludes().contains(i)) { + r.addInclude(i); + } + } + project.addResource(r); + } + return shouldAdd; + } } Modified: trunk/maven-jaxx-plugin/src/main/java/org/nuiton/jaxx/plugin/GenerateHelpMojo.java =================================================================== --- trunk/maven-jaxx-plugin/src/main/java/org/nuiton/jaxx/plugin/GenerateHelpMojo.java 2009-12-20 19:57:10 UTC (rev 1686) +++ trunk/maven-jaxx-plugin/src/main/java/org/nuiton/jaxx/plugin/GenerateHelpMojo.java 2009-12-21 13:02:20 UTC (rev 1687) @@ -18,185 +18,89 @@ * ##%*/ package org.nuiton.jaxx.plugin; -import com.sun.java.help.search.Indexer; -import java.io.BufferedReader; -import java.io.IOException; -import java.lang.reflect.InvocationTargetException; -import org.apache.maven.plugin.MojoFailureException; +import org.nuiton.plugin.VelocityTemplateGenerator; +import org.nuiton.util.FileUtil; +import org.nuiton.util.SortedProperties; import java.io.File; -import java.io.FileInputStream; -import java.io.InputStreamReader; -import java.io.PrintStream; -import java.lang.reflect.Method; -import java.net.URL; -import java.util.ArrayList; import java.util.Enumeration; -import java.util.List; import java.util.Locale; import java.util.Properties; -import org.codehaus.plexus.util.DirectoryScanner; -import org.codehaus.plexus.util.FileUtils; -import org.codehaus.plexus.util.StringUtils; -import org.nuiton.i18n.I18n; -import org.nuiton.plugin.VelocityTemplateGenerator; -import org.nuiton.util.FileUtil; -import org.nuiton.util.SortedProperties; /** * Mojo to generate javax help stuff for your project. - * + * <p/> * HelpIds should have been discovered by the JaxxMojo. * * @author chemit * @goal generate-help * @phase process-sources - * * @requiresProject * @requiresDependencyResolution compile * @since 1.3 */ -public class GenerateHelpMojo extends AbstractJaxxMojo { +public class GenerateHelpMojo extends AbstractGenerateHelpMojo { - private static final String AUTOREMOVE_LINE = "REMOVE THS LINE TO DISABLE AUTO-REGENERATE THE FILE"; + /** - * The directory where to generate help files. - * - * @parameter expression="${jaxx.outHelp}" default-value="${project.basedir}/src/main/help" - * @required - * - * @since 1.3 - */ - protected File outHelp; - /** - * The locales to generate for help, seprated by comma. - * - * The first locale given is the default locale. - * - * @parameter expression="${jaxx.locales}" - * @required - * - * @since 2.0.0 - */ - protected String locales; - /** - * The name of the helpset to generate. - * - * @parameter expression="${jaxx.helpsetName}" default-value="${project.artifactId}" - * @required - * - * @since 1.3 - */ - protected String helpsetName; - /** * The template used to generate helpset file. - * + * <p/> * Must be an existing file or a ressource in class-path - * + * * @parameter expression="${jaxx.helpsetTemplate}" default-value="/defaultHelpSet.hs.vm" * @required - * * @since 1.3 */ protected File helpsetTemplate; /** * The template used to generate helpset map file. - * + * <p/> * Must be an existing file or a ressource in class-path - * + * * @parameter expression="${jaxx.mapTemplate}" default-value="/defaultMap.jhm.vm" * @required - * * @since 1.3 */ protected File mapTemplate; /** * The template used to generate helpset index file. - * + * <p/> * Must be an existing file or a ressource in class-path - * + * * @parameter expression="${jaxx.indexTemplate}" default-value="/defaultIndex.xml.vm" * @required - * * @since 1.3 */ protected File indexTemplate; /** * The template used to generate helpset toc file. - * + * <p/> * Must be an existing file or a ressource in class-path - * + * * @parameter expression="${jaxx.tocTemplate}" default-value="/defaultToc.xml.vm" * @required - * * @since 1.3 */ protected File tocTemplate; /** * The template used to generate helpset content file. - * + * <p/> * Must be an existing file or a ressource in class-path * * @parameter expression="${jaxx.contentTemplate}" default-value="/defaultContent.html.vm" * @required - * * @since 1.3 */ protected File contentTemplate; - /** - * Flag to generate the search index. - * - * @parameter expression="${jaxx.generateSearch}" default-value="true" - * @required - * - * @since 1.3 - */ - protected boolean generateSearch; - /** - * The help ids discovered in {@link #helpIdsStore} files. - */ - private Properties helpIds; - /** - * Default locale (the first locale in {@link #localesToTreate}. - */ - private Locale defaultLocale; - /** - * Locales to treate - */ - private Locale[] localesToTreate; + protected String mapFileName; + protected String indexFileName; + protected String tocFileName; + protected int touchedFiles; + @Override public boolean init() throws Exception { - File idsStore = getHelpIdsStore(); - - if (!idsStore.exists()) { - getLog().info("no helpIdStore to react at " + idsStore); - return false; - } - - if (locales == null || locales.trim().isEmpty()) { - throw new MojoFailureException("You must set the 'locales' property properly (was " + locales + ")."); - } - - // check there is a outHelp - if (outHelp == null) { - throw new MojoFailureException("You must set the 'target' property."); - } - - List<Locale> tmp = new ArrayList<Locale>(); - for (String loc : locales.split(",")) { - Locale l = I18n.newLocale(loc); - tmp.add(l); - } - - if (tmp.isEmpty()) { - throw new MojoFailureException("you must set the 'locales' property."); - } - - localesToTreate = tmp.toArray(new Locale[tmp.size()]); - defaultLocale = localesToTreate[0]; - // check ressources checkResource(helpsetTemplate); checkResource(mapTemplate); @@ -204,147 +108,90 @@ checkResource(tocTemplate); checkResource(contentTemplate); - if (!getTargetDirectory().exists()) { - getLog().info("mkdir " + getTargetDirectory()); - getTargetDirectory().mkdirs(); - } + mapFileName = helpsetName + "Map.jhm"; + indexFileName = helpsetName + "Index.xml"; + tocFileName = helpsetName + "TOC.xml"; + touchedFiles=0; + return super.init(); + } - // load ids from idsStore file + protected void postDoAction() { + getLog().info(touchedFiles + " file(s) treated."); + } - helpIds = new SortedProperties(); + protected void doActionForLocale(Locale locale, boolean isDefaultLocale, File localizedTarget, String localePath) throws Exception { - BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(idsStore), getEncoding())); - String id = null; - while ((id = reader.readLine()) != null) { - id = id.trim(); - String path = id.replaceAll("\\.", File.separator) + ".html"; - helpIds.put(id, path); - } + String language = locale.getLanguage(); - if (helpIds.isEmpty()) { - - // no ids detected - getLog().warn("No helpIds detected, will skip."); - return false; + getLog().info("Generate help for language " + language); + if (isVerbose()) { + getLog().info(" Localized target : " + localizedTarget); } - return true; - } - @Override - public void doAction() throws Exception { + Properties env = new Properties(); - int touchedFiles = 0; + env.put("helpSetName", helpsetName); + env.put("locale", language); + env.put("localePath", localePath); + env.put("separator", " "); + env.put("autoremoveLine", AUTOREMOVE_LINE); - String mapFileName = helpsetName + "Map.jhm"; - String indexFileName = helpsetName + "Index.xml"; - String tocFileName = helpsetName + "TOC.xml"; + String localeSuffix = isDefaultLocale ? "" : "_" + language; + String helpsetFileName = helpsetName + localeSuffix + ".hs"; - for (Locale locale : localesToTreate) { + env.put("helpSetFileName", helpsetFileName); - boolean isDefaultLocale = locale == defaultLocale; + env.put("mapFileName", mapFileName); + env.put("indexFileName", indexFileName); + env.put("tocFileName", tocFileName); + env.put("generateSearch", generateSearch); - String language = locale.getLanguage(); + // --------------------------------------------------------------- + // --- main helpset file ----------------------------------------- + // --------------------------------------------------------------- - String localePath = (isDefaultLocale ? "default" : language); + File file = new File(outHelp, helpsetFileName); - File localizedTarget = new File(outHelp, localePath); + boolean doCreate = generateHelpsetFile(file, env); - if (!localizedTarget.exists()) { - localizedTarget.mkdirs(); - } - getLog().info("Generate help for language " + language); - if (isVerbose()) { - getLog().info(" Localized target : " + localizedTarget); - } - - Properties env = new Properties(); - - env.put("helpSetName", helpsetName); - env.put("locale", language); - env.put("localePath", localePath); - env.put("separator", " "); - env.put("autoremoveLine", AUTOREMOVE_LINE); - - String localeSuffix = isDefaultLocale ? "" : "_" + language; - String helpsetFileName = helpsetName + localeSuffix + ".hs"; - - env.put("helpSetFileName", helpsetFileName); - - env.put("mapFileName", mapFileName); - env.put("indexFileName", indexFileName); - env.put("tocFileName", tocFileName); - env.put("generateSearch", generateSearch); - - // --------------------------------------------------------------- - // --- main helpset file ----------------------------------------- - // --------------------------------------------------------------- - - File file = new File(outHelp, helpsetFileName); - - boolean doCreate = generateHelpsetFile(file, env); - - if (doCreate) { - touchedFiles++; - } - - // --------------------------------------------------------------- - // --- helpset map file ------------------------------------------ - // --------------------------------------------------------------- - - file = new File(localizedTarget, mapFileName); - - generateMapFile(file, env); + if (doCreate) { touchedFiles++; + } - // --------------------------------------------------------------- - // --- helpset index file ---------------------------------------- - // --------------------------------------------------------------- + // --------------------------------------------------------------- + // --- helpset map file ------------------------------------------ + // --------------------------------------------------------------- - file = new File(localizedTarget, indexFileName); + file = new File(localizedTarget, mapFileName); - generateIndexFile(file, env); - touchedFiles++; + generateMapFile(file, env); + touchedFiles++; - // --------------------------------------------------------------- - // --- helpset toc file ------------------------------------------ - // --------------------------------------------------------------- + // --------------------------------------------------------------- + // --- helpset index file ---------------------------------------- + // --------------------------------------------------------------- - file = new File(localizedTarget, tocFileName); + file = new File(localizedTarget, indexFileName); - generateTocFile(file, env); - touchedFiles++; + generateIndexFile(file, env); + touchedFiles++; - // --------------------------------------------------------------- - // --- helpset content files ------------------------------------- - // --------------------------------------------------------------- + // --------------------------------------------------------------- + // --- helpset toc file ------------------------------------------ + // --------------------------------------------------------------- - touchedFiles += generateContentFiles(localizedTarget, env, localePath); + file = new File(localizedTarget, tocFileName); - // --------------------------------------------------------------- - // --- generate search index ------------------------------------- - // --------------------------------------------------------------- - - if (generateSearch) { + generateTocFile(file, env); + touchedFiles++; - generateSearchIndex(localizedTarget, locale); - } + // --------------------------------------------------------------- + // --- helpset content files ------------------------------------- + // --------------------------------------------------------------- - - } - - getLog().info(touchedFiles + " file(s) treated."); + touchedFiles += generateContentFiles(localizedTarget, env, localePath); } - @Override - public File getTargetDirectory() { - return outHelp; - } - - @Override - public void setTargetDirectory(File targetDirectory) { - this.outHelp = targetDirectory; - } - protected int generateContentFiles(File localizedTarget, Properties env, String localePath) throws Exception { int touchedFiles = 0; @@ -367,7 +214,7 @@ continue; } } - f.getParentFile().mkdirs(); + createDirectoryIfNecessary(f.getParentFile()); if (verbose) { if (exist) { getLog().info("regenerate content file " + f); @@ -412,7 +259,7 @@ boolean create; - Properties mergedHelpIds = null; + Properties mergedHelpIds; if (file.exists()) { // get back the exisiting data and merge it with incoming ones @@ -503,35 +350,6 @@ return rootItem; } - protected void generateSearchIndex(File localizedTarget, Locale locale) throws IllegalArgumentException, IOException, InvocationTargetException, SecurityException, IllegalAccessException, NoSuchMethodException { - - Method m = Indexer.class.getDeclaredMethod("main", String[].class); - File indexDir = new File(localizedTarget, "JavaHelpSearch"); - // remove old index - FileUtils.deleteDirectory(indexDir); - //copy resources to a tmp dir (without any VCS infos) - File tmpDir = new File(project.getBasedir(), "target" + File.separator + "jaxx-tmp" + File.separator + "indexer-" + locale + "-" + System.nanoTime()); - getLog().info("copy files to " + tmpDir + " for indexing them"); - FileUtils.copyDirectory(localizedTarget, tmpDir, "**/*", StringUtils.join(DirectoryScanner.DEFAULTEXCLUDES, ",")); - List<String> params = new ArrayList<String>(); - params.add("-verbose"); - params.add("-db"); - params.add(indexDir.getAbsolutePath()); - params.add("-logfile"); - File logFile = new File(project.getBasedir(), "target" + File.separator + "generated-sources" + File.separator + "jaxx" + File.separator + "indexer-" + locale + ".log"); - params.add(logFile.getAbsolutePath()); - params.add(tmpDir.getAbsolutePath()); - PrintStream out = System.out; - PrintStream err = System.err; - try { - m.invoke(null, (Object) params.toArray(new String[params.size()])); - } finally { - System.setOut(out); - System.setErr(err); - } - getLog().info("Search Index generated"); - } - protected NodeItem generateTocFile(File file, Properties env) throws Exception { NodeItem rootItem = null; @@ -572,18 +390,4 @@ return rootItem; } - protected void doGen(File template, File f, Properties env) throws Exception { - VelocityTemplateGenerator gen = prepareGenerator(template); - gen.generate(env, f); - } - - protected VelocityTemplateGenerator prepareGenerator(File template) throws Exception { - URL templateURL = getTemplate(template); - - if (verbose) { - getLog().info("using template " + templateURL); - } - VelocityTemplateGenerator gen = new VelocityTemplateGenerator(project, templateURL); - return gen; - } } Copied: trunk/maven-jaxx-plugin/src/main/java/org/nuiton/jaxx/plugin/GenerateHelpSearchMojo.java (from rev 1686, trunk/maven-jaxx-plugin/src/main/java/org/nuiton/jaxx/plugin/GenerateHelpMojo.java) =================================================================== --- trunk/maven-jaxx-plugin/src/main/java/org/nuiton/jaxx/plugin/GenerateHelpSearchMojo.java (rev 0) +++ trunk/maven-jaxx-plugin/src/main/java/org/nuiton/jaxx/plugin/GenerateHelpSearchMojo.java 2009-12-21 13:02:20 UTC (rev 1687) @@ -0,0 +1,128 @@ +/* *##% + * JAXX Maven plugin + * Copyright (C) 2008 - 2009 CodeLutin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * ##%*/ +package org.nuiton.jaxx.plugin; + +import com.sun.java.help.search.Indexer; +import org.codehaus.plexus.util.DirectoryScanner; +import org.codehaus.plexus.util.FileUtils; +import org.codehaus.plexus.util.StringUtils; + +import java.io.File; +import java.io.PrintStream; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; + +/** + * Mojo to generate javax search index help stuff for your project. + * <p/> + * The current files should be generated always in a generated directory and not in + * your src directories (this is mainly binary files not to be stored in scm system)... + * + * @author chemit + * @goal generate-help-search + * @phase process-sources + * @requiresProject + * @requiresDependencyResolution compile + * @since 2.0.0 + */ +public class GenerateHelpSearchMojo extends AbstractGenerateHelpMojo { + + protected File targetRoot; + protected String timestamp; + + @Override + public boolean init() throws Exception { + if (!generateSearch) { + getLog().info("Do not generate search."); + return false; + } + targetRoot = getFileFromBasedir("target", "generated-sources", "help"); + createDirectoryIfNecessary(targetRoot); + + timestamp = "-" + System.currentTimeMillis(); + return super.init(); + } + + @Override + protected void doActionForLocale(Locale locale, boolean isDefaultLocale, File localizedTarget, String localePath) throws Exception { + + String language = locale.getLanguage(); + + File target = new File(targetRoot, localePath + File.separator + "JavaHelpSearch"); + + createDirectoryIfNecessary(localizedTarget); + createDirectoryIfNecessary(target); + + getLog().info("Generate help search index for language " + language); + if (isVerbose()) { + getLog().info(" Localized source : " + localizedTarget); + getLog().info(" Localized target : " + target); + } + + // --------------------------------------------------------------- + // --- generate search index ------------------------------------- + // --------------------------------------------------------------- + + generateSearchIndex(localizedTarget, target, locale); + } + + @Override + protected void postDoAction() { + + // add resources to the project + addResourceDir(targetRoot, getProject(), "**/*"); + } + + protected void generateSearchIndex(File source, File target, Locale locale) throws Exception { + + Method m = Indexer.class.getDeclaredMethod("main", String[].class); + + // remove old index + FileUtils.deleteDirectory(target); + + //copy resources to a tmp dir (without any VCS infos) + File tmpDir = getFileFromBasedir("target", "jaxx-tmp", "indexer-" + locale + timestamp); + getLog().info("copy files to " + tmpDir + " for indexing them"); + FileUtils.copyDirectory(source, tmpDir, "**/*", StringUtils.join(DirectoryScanner.DEFAULTEXCLUDES, ",")); + + // prepare arguments of Indexer.main calling + List<String> params = new ArrayList<String>(); + params.add("-verbose"); + params.add("-db"); + params.add(target.getAbsolutePath()); + params.add("-logfile"); + File logFile = getFileFromBasedir("target", "generated-sources", "jaxx", "indexer-" + locale + ".log"); + params.add(logFile.getAbsolutePath()); + params.add(tmpDir.getAbsolutePath()); + PrintStream out = System.out; + PrintStream err = System.err; + + // invoke Indexer.main + try { + m.invoke(null, (Object) params.toArray(new String[params.size()])); + } finally { + System.setOut(out); + System.setErr(err); + } + getLog().info("Search Index generated for " + locale); + } + +} \ No newline at end of file Modified: trunk/maven-jaxx-plugin/src/main/java/org/nuiton/jaxx/plugin/GenerateMojo.java =================================================================== --- trunk/maven-jaxx-plugin/src/main/java/org/nuiton/jaxx/plugin/GenerateMojo.java 2009-12-20 19:57:10 UTC (rev 1686) +++ trunk/maven-jaxx-plugin/src/main/java/org/nuiton/jaxx/plugin/GenerateMojo.java 2009-12-21 13:02:20 UTC (rev 1687) @@ -367,9 +367,7 @@ BeanInfoUtil.addJaxxBeanInfoPath(beanInfoSearchPath); } - if (!getTargetDirectory().exists()) { - getTargetDirectory().mkdirs(); - } + createDirectoryIfNecessary(getTargetDirectory()); // compute extra imports (can not use java classes since some of // imports can not be still compiled) @@ -567,9 +565,7 @@ } File idsStore = getHelpIdsStore(); - if (!idsStore.getParentFile().exists()) { - idsStore.getParentFile().mkdirs(); - } + createDirectoryIfNecessary(idsStore.getParentFile()); StringBuilder buffer = new StringBuilder(); Modified: trunk/maven-jaxx-plugin/src/site/rst/index.rst =================================================================== --- trunk/maven-jaxx-plugin/src/site/rst/index.rst 2009-12-20 19:57:10 UTC (rev 1686) +++ trunk/maven-jaxx-plugin/src/site/rst/index.rst 2009-12-21 13:02:20 UTC (rev 1687) @@ -9,8 +9,10 @@ The plugin permits to generate java files from jaxx files via the `generate`_ goal. -You can also generate the java help system via the `generate-help`_ goal. +You can also generate the java help system via the `generate-help`_ goal and generate search index via +the `generate-help-search`_ goal. + Default layout -------------- @@ -20,7 +22,8 @@ * the jaxx files should be under *src/main/java* * the generated java files should be under *target/generated-sources/java* * the java help files should be under *src/main/help* - + * the generated help search index should be under *target/generated-sources/help* + .. _generate: generate-mojo.html - .. _generate-help: generate-help-mojo.html +.. _generate-help-search: generate-help-search-mojo.html Modified: trunk/maven-jaxx-plugin/src/site/site.xml =================================================================== --- trunk/maven-jaxx-plugin/src/site/site.xml 2009-12-20 19:57:10 UTC (rev 1686) +++ trunk/maven-jaxx-plugin/src/site/site.xml 2009-12-21 13:02:20 UTC (rev 1687) @@ -20,6 +20,7 @@ <item name="Détail goals" href="plugin-info.html"> <item name="generate" href="generate-mojo.html"/> <item name="generate-help" href="generate-help-mojo.html"/> + <item name="generate-help-search" href="generate-help-search-mojo.html"/> <item name="help" href="help-mojo.html"/> </item> </menu>
participants (1)
-
tchemit@users.nuiton.org