01: /**
02: * Copyright (c) 2001, Sergey A. Samokhodkin
03: * All rights reserved.
04: *
05: * Redistribution and use in source and binary forms, with or without modification,
06: * are permitted provided that the following conditions are met:
07: *
08: * - Redistributions of source code must retain the above copyright notice,
09: * this list of conditions and the following disclaimer.
10: * - Redistributions in binary form
11: * must reproduce the above copyright notice, this list of conditions and the following
12: * disclaimer in the documentation and/or other materials provided with the distribution.
13: * - Neither the name of jregex nor the names of its contributors may be used
14: * to endorse or promote products derived from this software without specific prior
15: * written permission.
16: *
17: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
18: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20: * IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
21: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
25: * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26: *
27: * @version 1.2_01
28: */package jregex.util.io;
29:
30: import jregex.*;
31: import java.io.FilenameFilter;
32: import java.io.File;
33: import java.util.Enumeration;
34: import java.util.NoSuchElementException;
35:
36: class PathElementEnumerator implements Enumeration {
37: private PathElementEnumerator nextPEE;
38: private PathElementMask mask;
39:
40: protected Enumeration entries;
41:
42: private Object nextObj;
43:
44: PathElementEnumerator() {
45: }
46:
47: PathElementEnumerator(PathElementMask mask) {
48: this .mask = mask;
49: PathElementMask nextMask = mask.next;
50: if (nextMask != null)
51: nextPEE = nextMask.newEnumerator();
52: }
53:
54: protected void setDir(File f) {
55: entries = mask.elements(f);
56: }
57:
58: public boolean hasMoreElements() {
59: return (nextObj != null || getNext());
60: }
61:
62: public Object nextElement() {
63: if (nextObj == null && !getNext())
64: throw new NoSuchElementException();
65: Object tmp = nextObj;
66: nextObj = null;
67: return tmp;
68: }
69:
70: private boolean getNext() {
71: if (nextPEE == null) {
72: return (nextObj = nextPathEntry()) != null;
73: } else {
74: while (!nextPEE.hasMoreElements()) {
75: File f = nextPathEntry();
76: if (f == null)
77: return false;
78: nextPEE.setDir(f);
79: }
80: nextObj = nextPEE.nextElement();
81: return true;
82: }
83: }
84:
85: private File nextPathEntry() {
86: if (entries == null || !entries.hasMoreElements())
87: return null;
88: return (File) entries.nextElement();
89: }
90: }
|