
org.nuiton.i18n.plugin.parser.impl.ParserJspMojo Maven / Gradle / Ivy
/*
* #%L
* I18n :: Maven Plugin
*
* $Id: ParserJspMojo.java 1993 2012-11-11 10:36:04Z tchemit $
* $HeadURL: http://svn.nuiton.org/svn/i18n/tags/i18n-2.5.2/i18n-maven-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ParserJspMojo.java $
* %%
* Copyright (C) 2007 - 2010 CodeLutin, Tony Chemit
* %%
* 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
* .
* #L%
*/
package org.nuiton.i18n.plugin.parser.impl;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.nuiton.i18n.plugin.parser.I18nSourceEntry;
import org.nuiton.i18n.plugin.parser.SourceEntry;
import org.nuiton.io.FileUpdater;
import org.nuiton.io.MirroredFileUpdater;
import org.nuiton.plugin.PluginHelper;
import org.nuiton.processor.ProcessorUtil;
import org.nuiton.processor.filters.DefaultFilter;
import org.nuiton.processor.filters.Filter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.xpath.XPath;
import java.io.File;
import java.io.IOException;
/**
* Find i18n keys from jsp files.
*
* Note: this goal must always be invoked before the {@code process-resources}
* phase, otherwise all files will be considered as uptodate.
*
* @author tchemit
* @since 2.0
* @deprecated since 2.5, hard to maintain jsp as valid xml docs. Will be removed soon
*/
@Mojo(name = "parserJsp", defaultPhase = LifecyclePhase.GENERATE_RESOURCES)
public class ParserJspMojo extends AbstractParserXmlMojo {
public static final String DEFAULT_INCLUDES = "**/*.jsp";
/**
* Root directory of the default entry.
*
* @since 2.0
*/
@Parameter(property = "i18n.defaultBasedir", defaultValue = "${basedir}/src/main/webapp", required = true)
protected File defaultBasedir;
/**
* Default included files to process (ant-like expression).
*
* Note: default value is **\/*.jsp
*
* @since 2.0
*/
@Parameter(property = "i18n.defaultIncludes", defaultValue = DEFAULT_INCLUDES, required = true)
protected String defaultIncludes;
/**
* Defines the core rules file used to detect i18n keys in jsp files.
*
* Note : If you do not want to use it, set it to empty and fill the
* {@link #userRulesFiles} parameter.
*
* @since 2.0
*/
@Parameter(property = "i18n.coreRuleFile")
protected String coreRuleFile;
/**
* Defines the file name of the getter where to put detected i18n keys
* while getter phase.
*
* @since 2.0
*/
@Parameter(property = "i18n.outputGetter", defaultValue = "jsp.getter", required = true)
protected String outputGetter;
/**
* Where to generated temporary processed files.
*
* @since 2.0
*/
@Parameter(property = "i18n.workdir", defaultValue = "${basedir}/target/i18n-workdir", required = true)
protected File workdir;
@Override
public String[] getDefaultIncludes() {
return new String[]{defaultIncludes};
}
@Override
public String[] getDefaultExcludes() {
return I18nSourceEntry.EMPTY_STRING_ARRAY;
}
@Override
public File getDefaultBasedir() {
return defaultBasedir;
}
MirroredFileUpdater entryUpdater;
@Override
protected boolean onEnterEntry(I18nSourceEntry entry) {
boolean b = super.onEnterEntry(entry);
if (!b) {
// no skipped entry
// keep the file updater
entryUpdater = (MirroredFileUpdater) entry.getUpdater();
}
return b;
}
@Override
public FileUpdater newFileUpdater(SourceEntry entry) {
return new MirroredFileUpdater("", "", entry.getBasedir(), workdir) {
@Override
public File getMirrorFile(File f) {
String file =
f.getAbsolutePath().substring(prefixSourceDirecotory);
return new File(destinationDirectory + File.separator + file + "~");
}
};
}
@Override
protected String getOutGetter() {
return outputGetter;
}
@Override
protected String getCoreRuleFile() {
return coreRuleFile;
}
protected XmlFileParser newXmlFileParser(final XPath xpath,
final DocumentBuilder builder) {
return new XmlFileParser(getLog(),
encoding,
oldParser,
acceptPattern,
showTouchedFiles,
rules,
xpath,
builder,
namespaces,
isVerbose()) {
@Override
public File prepareFile(File file) throws IOException {
// clean the jsp to make it xml
File result = entryUpdater.getMirrorFile(file);
createDirectoryIfNecessary(result.getParentFile());
JspFileProcessor processor = new JspFileProcessor();
processor.process(file, result, getEncoding());
String resultFileContent =
PluginHelper.readAsString(result, getEncoding());
resultFileContent = resultFileContent.trim();
if (StringUtils.isBlank(resultFileContent)) {
// nothing to scan inside this file
result = null;
} else {
if (resultFileContent.startsWith("";
PluginHelper.writeString(result, resultFileContent, getEncoding());
}
}
return result;
}
@Override
public String extract(String i18nString) {
String s = null;
if (!StringUtils.isEmpty(i18nString.trim())) {
s = i18nString.trim();
}
if (getLog().isDebugEnabled()) {
getLog().debug(i18nString + " = " + s);
}
return s;
}
};
}
/**
* To transform jsp files to valid xml files.
*
* Says :
*
* - Remove jsp directive
* - Remove jsp comment
* - Remove any tags in attributes (used for example in struts)
* - ? Other thing
*
*
* @author tchemit
* @since 2.0
*/
public static class JspFileProcessor extends ProcessorHelper.AbstractParserProcessor {
protected ProcessorHelper.FragmentRemover remover1 =
new ProcessorHelper.FragmentRemover(
"<" + "%" + "-" + "-",
"-" + "-" + "%" + ">"
);
protected ProcessorHelper.FragmentRemover remover2 =
new ProcessorHelper.FragmentRemover(
"<" + "%",
"%" + ">"
);
protected ProcessorHelper.FragmentRemover remover3 =
new ProcessorHelper.FragmentRemover(
"<" + "!" + "-" + "-",
"-" + "-" + ">"
);
public JspFileProcessor() {
setInputFilter(
new Filter[]{
remover1,
remover2,
remover3,
new JspAttributeWithTagFilter()
}
);
}
/**
* @param filein the source file to process
* @param fileout the output file to generate
* @param encoding encoding used to read and write files
* @throws IOException if any io problems while processing
* @since 1.0.4
*/
@Override
public void process(File filein,
File fileout,
String encoding) throws IOException {
ProcessorUtil.doProcess(this, filein, fileout, encoding);
}
/**
* To remove in attributes any sub tags inside it (used for example in
* struts) from jsp files.
*
* @author tchemit
* @since 2.0
*/
public static class JspAttributeWithTagFilter extends DefaultFilter {
private String header = "=" + "\"" + "<";
private String footer = "\"" + "/" + ">" + "\"";
@Override
protected String performInFilter(String ch) {
return "=\"\"";
}
@Override
protected String performOutFilter(String ch) {
return ch;
}
protected String getHeader() {
return header;
}
protected String getFooter() {
return footer;
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy