Package org.codehaus.plexus.components.io.resources

Source Code of org.codehaus.plexus.components.io.resources.PlexusIoFileResourceCollection

package org.codehaus.plexus.components.io.resources;

/*
* Copyright 2007 The Codehaus Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.codehaus.plexus.components.io.attributes.FileAttributes;
import org.codehaus.plexus.components.io.attributes.PlexusIoResourceAttributeUtils;
import org.codehaus.plexus.components.io.attributes.PlexusIoResourceAttributes;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.util.DirectoryScanner;


/**
* Implementation of {@link PlexusIoResourceCollection} for the set
* of files in a common directory.
*/
public class PlexusIoFileResourceCollection
    extends AbstractPlexusIoResourceCollection
    implements PlexusIOResourceCollectionWithAttributes
{
    /**
     * Role hint of this component
     */
    public static final String ROLE_HINT = "files";
   
    private File baseDir;

    private boolean isFollowingSymLinks = true;
   
    private FileAttributes defaultFileAttributes;
   
    private FileAttributes defaultDirAttributes;
   
    private FileAttributes overrideFileAttributes;
   
    private FileAttributes overrideDirAttributes;

    public PlexusIoFileResourceCollection()
    {
    }
   
    public PlexusIoFileResourceCollection( Logger logger )
    {
        super( logger );
    }
   
    public void setDefaultAttributes( int uid, String userName, int gid, String groupName, int fileMode, int dirMode )
    {
        defaultFileAttributes = new FileAttributes( uid, userName, gid, groupName, new char[]{} );
        defaultFileAttributes.setOctalMode( fileMode );
       
        defaultDirAttributes = new FileAttributes( uid, userName, gid, groupName, new char[]{} );
        defaultDirAttributes.setOctalMode( dirMode );
    }
   
    public void setOverrideAttributes( int uid, String userName, int gid, String groupName, int fileMode, int dirMode )
    {
        overrideFileAttributes = new FileAttributes( uid, userName, gid, groupName, new char[]{} );
        overrideFileAttributes.setOctalMode( fileMode );
       
        overrideDirAttributes = new FileAttributes( uid, userName, gid, groupName, new char[]{} );
        overrideDirAttributes.setOctalMode( dirMode );
    }
   
    /**
     * Sets the file collections base directory.
     */
    public void setBaseDir( File baseDir )
    {
        this.baseDir = baseDir;
    }

    /**
     * Returns the file collections base directory.
     */
    public File getBaseDir()
    {
        return baseDir;
    }

    /**
     * Returns, whether symbolic links should be followed.
     * Defaults to true.
     */
    public boolean isFollowingSymLinks()
    {
        return isFollowingSymLinks;
    }

    /**
     * Returns, whether symbolic links should be followed.
     * Defaults to true.
     */
    public void setFollowingSymLinks( boolean pIsFollowingSymLinks )
    {
        isFollowingSymLinks = pIsFollowingSymLinks;
    }

    private void addResources( List list, String[] resources, Map attributesByPath ) throws IOException
    {
        final File dir = getBaseDir();
        for ( int i = 0; i < resources.length; i++ )
        {
            String name = resources[i];
            String sourceDir = name.replace( '\\', '/' );
           
            File f = new File( dir, sourceDir );
           
            PlexusIoResourceAttributes attrs = (PlexusIoResourceAttributes) attributesByPath.get( name.length() > 0 ? name : "." );
            if ( attrs == null )
            {
                attrs = (PlexusIoResourceAttributes) attributesByPath.get( f.getAbsolutePath() );
            }
           
            if ( f.isDirectory() )
            {
                attrs = PlexusIoResourceAttributeUtils.mergeAttributes( overrideDirAttributes, attrs, defaultDirAttributes );
            }
            else
            {
                attrs = PlexusIoResourceAttributeUtils.mergeAttributes( overrideFileAttributes, attrs, defaultFileAttributes );
            }
           
            PlexusIoFileResource resource = new PlexusIoFileResource( f, name, attrs );
            if ( isSelected( resource ) )
            {
                list.add( resource );
            }
        }
    }

    public Iterator getResources() throws IOException
    {
        final DirectoryScanner ds = new DirectoryScanner();
        final File dir = getBaseDir();
        ds.setBasedir( dir );
        final String[] inc = getIncludes();
        if ( inc != null  &&  inc.length > 0 )
        {
            ds.setIncludes( inc );
        }
        final String[] exc = getExcludes();
        if ( exc != null  &&  exc.length > 0 )
        {
            ds.setExcludes( exc );
        }
        if ( isUsingDefaultExcludes() )
        {
            ds.addDefaultExcludes();
        }
        ds.setCaseSensitive( isCaseSensitive() );
        ds.setFollowSymlinks( isFollowingSymLinks() );
        ds.scan();

        Map attributesByPath = PlexusIoResourceAttributeUtils.getFileAttributesByPath( getBaseDir() );
       
        final List result = new ArrayList();
        if ( isIncludingEmptyDirectories() )
        {
            String[] dirs = ds.getIncludedDirectories();
            addResources( result, dirs, attributesByPath );
        }

        String[] files = ds.getIncludedFiles();
        addResources( result, files, attributesByPath );
        return result.iterator();
    }
}
TOP

Related Classes of org.codehaus.plexus.components.io.resources.PlexusIoFileResourceCollection

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.