use of org.codehaus.plexus.util.DirectoryScanner in project ph-schematron by phax.
the class Schematron2XSLTMojo method execute.
public void execute() throws MojoExecutionException, MojoFailureException {
StaticLoggerBinder.getSingleton().setMavenLog(getLog());
if (m_aSchematronDirectory == null)
throw new MojoExecutionException("No Schematron directory specified!");
if (m_aSchematronDirectory.exists() && !m_aSchematronDirectory.isDirectory())
throw new MojoExecutionException("The specified Schematron directory " + m_aSchematronDirectory + " is not a directory!");
if (StringHelper.hasNoText(m_sSchematronPattern))
throw new MojoExecutionException("No Schematron pattern specified!");
if (m_aXsltDirectory == null)
throw new MojoExecutionException("No XSLT directory specified!");
if (m_aXsltDirectory.exists() && !m_aXsltDirectory.isDirectory())
throw new MojoExecutionException("The specified XSLT directory " + m_aXsltDirectory + " is not a directory!");
if (StringHelper.hasNoText(m_sXsltExtension) || !m_sXsltExtension.startsWith("."))
throw new MojoExecutionException("The XSLT extension '" + m_sXsltExtension + "' is invalid!");
if (!m_aXsltDirectory.exists() && !m_aXsltDirectory.mkdirs())
throw new MojoExecutionException("Failed to create the XSLT directory " + m_aXsltDirectory);
// for all Schematron files that match the pattern
final DirectoryScanner aScanner = new DirectoryScanner();
aScanner.setBasedir(m_aSchematronDirectory);
aScanner.setIncludes(new String[] { m_sSchematronPattern });
aScanner.setCaseSensitive(true);
aScanner.scan();
final String[] aFilenames = aScanner.getIncludedFiles();
if (aFilenames != null) {
for (final String sFilename : aFilenames) {
final File aFile = new File(m_aSchematronDirectory, sFilename);
// 1. build XSLT file name (outputdir + localpath with new extension)
final File aXSLTFile = new File(m_aXsltDirectory, FilenameHelper.getWithoutExtension(sFilename) + m_sXsltExtension);
getLog().info("Converting Schematron file '" + aFile.getPath() + "' to XSLT file '" + aXSLTFile.getPath() + "'");
// 2. The Schematron resource
final IReadableResource aSchematronResource = new FileSystemResource(aFile);
// 3. Check if the XSLT file already exists
if (aXSLTFile.exists() && !m_bOverwriteWithoutNotice) {
// 3.1 Not overwriting the existing file
getLog().debug("Skipping XSLT file '" + aXSLTFile.getPath() + "' because it already exists!");
} else {
// 3.2 Create the directory, if necessary
final File aXsltFileDirectory = aXSLTFile.getParentFile();
if (aXsltFileDirectory != null && !aXsltFileDirectory.exists()) {
getLog().debug("Creating directory '" + aXsltFileDirectory.getPath() + "'");
if (!aXsltFileDirectory.mkdirs()) {
final String message = "Failed to convert '" + aFile.getPath() + "' because directory '" + aXsltFileDirectory.getPath() + "' could not be created";
getLog().error(message);
throw new MojoFailureException(message);
}
}
// 3.3 Okay, write the XSLT file
try {
buildContext.removeMessages(aFile);
// Custom error listener to log to the Mojo logger
final ErrorListener aMojoErrorListener = new PluginErrorListener(aFile);
// Custom error listener
// No custom URI resolver
// Specified phase - default = null
// Specified language code - default = null
final SCHTransformerCustomizer aCustomizer = new SCHTransformerCustomizer().setErrorListener(aMojoErrorListener).setPhase(m_sPhaseName).setLanguageCode(m_sLanguageCode).setParameters(m_aCustomParameters);
final ISchematronXSLTBasedProvider aXsltProvider = SchematronResourceSCHCache.createSchematronXSLTProvider(aSchematronResource, aCustomizer);
if (aXsltProvider != null) {
// Write the resulting XSLT file to disk
final MapBasedNamespaceContext aNSContext = new MapBasedNamespaceContext().addMapping("svrl", CSVRL.SVRL_NAMESPACE_URI);
// Add all namespaces from XSLT document root
final String sNSPrefix = XMLConstants.XMLNS_ATTRIBUTE + ":";
XMLHelper.forAllAttributes(aXsltProvider.getXSLTDocument().getDocumentElement(), (sAttrName, sAttrValue) -> {
if (sAttrName.startsWith(sNSPrefix))
aNSContext.addMapping(sAttrName.substring(sNSPrefix.length()), sAttrValue);
});
final XMLWriterSettings aXWS = new XMLWriterSettings();
aXWS.setNamespaceContext(aNSContext).setPutNamespaceContextPrefixesInRoot(true);
final OutputStream aOS = FileHelper.getOutputStream(aXSLTFile);
if (aOS == null)
throw new IllegalStateException("Failed to open output stream for file " + aXSLTFile.getAbsolutePath());
XMLWriter.writeToStream(aXsltProvider.getXSLTDocument(), aOS, aXWS);
getLog().debug("Finished creating XSLT file '" + aXSLTFile.getPath() + "'");
buildContext.refresh(aXsltFileDirectory);
} else {
final String message = "Failed to convert '" + aFile.getPath() + "': the Schematron resource is invalid";
getLog().error(message);
throw new MojoFailureException(message);
}
} catch (final MojoFailureException up) {
throw up;
} catch (final Exception ex) {
final String sMessage = "Failed to convert '" + aFile.getPath() + "' to XSLT file '" + aXSLTFile.getPath() + "'";
getLog().error(sMessage, ex);
throw new MojoExecutionException(sMessage, ex);
}
}
}
}
}
use of org.codehaus.plexus.util.DirectoryScanner in project maven-plugins by apache.
the class DefaultAssemblyReader method readAssemblies.
@Override
public List<Assembly> readAssemblies(final AssemblerConfigurationSource configSource) throws AssemblyReadException, InvalidAssemblerConfigurationException {
final Locator locator = new Locator();
final List<LocatorStrategy> strategies = new ArrayList<LocatorStrategy>();
strategies.add(new RelativeFileLocatorStrategy(configSource.getBasedir()));
strategies.add(new FileLocatorStrategy());
final List<LocatorStrategy> refStrategies = new ArrayList<LocatorStrategy>();
refStrategies.add(new PrefixedClasspathLocatorStrategy("/assemblies/"));
final List<Assembly> assemblies = new ArrayList<Assembly>();
final String[] descriptors = configSource.getDescriptors();
final String[] descriptorRefs = configSource.getDescriptorReferences();
final File descriptorSourceDirectory = configSource.getDescriptorSourceDirectory();
if ((descriptors != null) && (descriptors.length > 0)) {
locator.setStrategies(strategies);
for (String descriptor1 : descriptors) {
getLogger().info("Reading assembly descriptor: " + descriptor1);
addAssemblyFromDescriptor(descriptor1, locator, configSource, assemblies);
}
}
if ((descriptorRefs != null) && (descriptorRefs.length > 0)) {
locator.setStrategies(refStrategies);
for (String descriptorRef : descriptorRefs) {
addAssemblyForDescriptorReference(descriptorRef, configSource, assemblies);
}
}
if ((descriptorSourceDirectory != null) && descriptorSourceDirectory.isDirectory()) {
// CHECKSTYLE_OFF: LineLength
locator.setStrategies(Collections.<LocatorStrategy>singletonList(new RelativeFileLocatorStrategy(descriptorSourceDirectory)));
// CHECKSTYLE_ON: LineLength
final DirectoryScanner scanner = new DirectoryScanner();
scanner.setBasedir(descriptorSourceDirectory);
scanner.setIncludes(new String[] { "**/*.xml" });
scanner.addDefaultExcludes();
scanner.scan();
final String[] paths = scanner.getIncludedFiles();
for (String path : paths) {
addAssemblyFromDescriptor(path, locator, configSource, assemblies);
}
}
if (assemblies.isEmpty()) {
if (configSource.isIgnoreMissingDescriptor()) {
getLogger().debug("Ignoring missing assembly descriptors per configuration. " + "See messages above for specifics.");
} else {
throw new AssemblyReadException("No assembly descriptors found.");
}
}
// check unique IDs
final Set<String> ids = new HashSet<String>();
for (final Assembly assembly : assemblies) {
if (!ids.add(assembly.getId())) {
getLogger().warn("The assembly id " + assembly.getId() + " is used more than once.");
}
}
return assemblies;
}
use of org.codehaus.plexus.util.DirectoryScanner in project maven-plugins by apache.
the class PathSet method addAllFilesInDirectory.
/**
* Adds to the set all files in the given directory
*
* @param directory that will be searched for file's paths to add
* @param prefix to be added to all found files
*/
public void addAllFilesInDirectory(File directory, String prefix) {
DirectoryScanner scanner = new DirectoryScanner();
scanner.setBasedir(directory);
scanner.scan();
addAll(scanner.getIncludedFiles(), prefix);
}
use of org.codehaus.plexus.util.DirectoryScanner in project maven-plugins by apache.
the class WarProjectPackagingTask method getFilesToCopy.
/**
* Returns a list of filenames that should be copied over to the destination directory.
*
* @param resource the resource to be scanned
* @return the array of filenames, relative to the sourceDir
*/
private String[] getFilesToCopy(Resource resource) {
// CHECKSTYLE_OFF: LineLength
DirectoryScanner scanner = new DirectoryScanner();
scanner.setBasedir(resource.getDirectory());
if (resource.getIncludes() != null && !resource.getIncludes().isEmpty()) {
scanner.setIncludes((String[]) resource.getIncludes().toArray(new String[resource.getIncludes().size()]));
} else {
scanner.setIncludes(DEFAULT_INCLUDES);
}
if (resource.getExcludes() != null && !resource.getExcludes().isEmpty()) {
scanner.setExcludes((String[]) resource.getExcludes().toArray(new String[resource.getExcludes().size()]));
}
scanner.addDefaultExcludes();
scanner.scan();
return scanner.getIncludedFiles();
// CHECKSTYLE_ON: LineLength
}
use of org.codehaus.plexus.util.DirectoryScanner in project maven-plugins by apache.
the class JavadocUtil method addFilesFromSource.
/**
* Convenience method that gets the files to be included in the javadoc.
*
* @param sourceDirectory the directory where the source files are located
* @param files the variable that contains the appended filenames of the files to be included in the javadoc
* @param excludePackages the packages to be excluded in the javadocs
* @param sourceFileIncludes files to include.
* @param sourceFileExcludes files to exclude.
*/
protected static void addFilesFromSource(List<String> files, File sourceDirectory, List<String> sourceFileIncludes, List<String> sourceFileExcludes, String[] excludePackages) {
DirectoryScanner ds = new DirectoryScanner();
if (sourceFileIncludes == null) {
sourceFileIncludes = Collections.singletonList("**/*.java");
}
ds.setIncludes(sourceFileIncludes.toArray(new String[sourceFileIncludes.size()]));
if (sourceFileExcludes != null && sourceFileExcludes.size() > 0) {
ds.setExcludes(sourceFileExcludes.toArray(new String[sourceFileExcludes.size()]));
}
ds.setBasedir(sourceDirectory);
ds.scan();
String[] fileList = ds.getIncludedFiles();
String[] pathList = new String[fileList.length];
for (int x = 0; x < fileList.length; x++) {
pathList[x] = new File(sourceDirectory, fileList[x]).getAbsolutePath();
}
if (pathList.length != 0) {
List<String> tmpFiles = getIncludedFiles(sourceDirectory, pathList, excludePackages);
files.addAll(tmpFiles);
}
}
Aggregations