public Enumeration<URL> findEntries( final String path, final String glob, final boolean recurse ) { return new ResourceEnumeration( path, glob, recurse, getClassPath() ); }
public URL nextElement() { if ( hasMoreElements() ) { // initialized by hasMoreElements() final String name = nextEntryName; nextEntryName = null; try { return findResource( name ); } catch ( final MalformedURLException e ) { // this shouldn't happen, hence illegal state throw new IllegalStateException( e.toString() ); } } throw new NoSuchElementException(); }
public boolean hasMoreElements() { while ( null == nextEntryName ) { if ( entryNames.hasNext() ) { final String name = entryNames.next(); if ( matchesRequest( name ) ) { nextEntryName = name; } } else if ( index < urls.length ) { currentURL = urls[index++]; entryNames = scan( currentURL ); } else { return false; // no more URLs } } return true; }
/** * Creates an {@link Enumeration} that scans the given URLs for resources matching the globbed pattern. * * @param subPath An optional path to begin the search from * @param glob The globbed basename pattern * @param recurse When {@code true} search paths below the initial search point; otherwise don't * @param urls The URLs containing resources */ ResourceEnumeration( final String subPath, final String glob, final boolean recurse, final URL[] urls ) { this.subPath = normalizeSearchPath( subPath ); globber = GlobberStrategy.selectFor( glob ); globPattern = globber.compile( glob ); this.recurse = recurse; this.urls = urls; }
/** * Returns the appropriate {@link Iterator} to iterate over the contents of the given URL. * * @param url The containing URL * @return Iterator that iterates over resources contained inside the given URL */ private Iterator<String> scan( final URL url ) { isFolder = url.getPath().endsWith( "/" ); if ( globber == GlobberStrategy.EXACT && !recurse ) { try { // short-cut the nextElement() process nextEntryName = subPath + globPattern; // but still need to check resource actually exists! Streams.open( findResource( nextEntryName ) ).close(); } catch ( final Exception e ) // IOException + SecurityException + etc... { nextEntryName = null; } return NO_ENTRIES; } return isFolder ? new FileEntryIterator( url, subPath, recurse ) : new ZipEntryIterator( url ); }
@SuppressWarnings( "unchecked" ) public synchronized Enumeration<URL> findEntries( final String path, final String glob, final boolean recurse ) { if ( null == classPath ) { classPath = getBundleClassPath( bundle ); } final Enumeration<URL> entries = bundle.findEntries( null != path ? path : "/", glob, recurse ); if ( classPath.length > 0 ) { return new ChainedEnumeration<URL>( entries, new ResourceEnumeration( path, glob, recurse, classPath ) ); } return null != entries ? entries : NO_ENTRIES; }