001: /**
002: * Copyright (c) 2001, Sergey A. Samokhodkin
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without modification,
006: * are permitted provided that the following conditions are met:
007: *
008: * - Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * - Redistributions in binary form
011: * must reproduce the above copyright notice, this list of conditions and the following
012: * disclaimer in the documentation and/or other materials provided with the distribution.
013: * - Neither the name of jregex nor the names of its contributors may be used
014: * to endorse or promote products derived from this software without specific prior
015: * written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
018: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
019: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
020: * IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
021: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
022: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
023: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
024: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
025: * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
026: *
027: * @version 1.2_01
028: */package jregex.util.io;
029:
030: import jregex.*;
031: import java.io.FilenameFilter;
032: import java.io.File;
033: import java.util.Enumeration;
034: import java.util.Stack;
035: import java.util.NoSuchElementException;
036:
037: abstract class PathElementMask {
038: protected PathElementMask next;
039: protected boolean dirsOnly;
040:
041: protected PathElementMask(boolean dirsOnly) {
042: this .dirsOnly = dirsOnly;
043: }
044:
045: public abstract Enumeration elements(final File dir);
046:
047: PathElementEnumerator newEnumerator() {
048: return new PathElementEnumerator(this );
049: }
050:
051: static PathElementMask regularMask(String s, int flags,
052: boolean dirsOnly) {
053: return new RegularMask(s, flags, dirsOnly);
054: }
055:
056: static PathElementMask fixedMask(String s, boolean dirsOnly) {
057: return new FixedPathElement(s, dirsOnly);
058: }
059:
060: static PathElementMask anyFile(boolean dirsOnly) {
061: return new AnyFile(dirsOnly);
062: }
063:
064: static PathElementMask anyPath(boolean dirsOnly) {
065: return new AnyPath(dirsOnly);
066: }
067: }
068:
069: class RegularMask extends PathElementMask {
070: Pattern pattern;
071:
072: RegularMask(String s, int flags, boolean dirsOnly) {
073: super (dirsOnly);
074: //System.out.println("PathElementMask("+s+","+dirsOnly+"):");
075: pattern = new WildcardPattern(s, flags);
076: }
077:
078: class MatchingElementEnumerator extends PathElementEnumerator {
079: private RegularMask rmask;
080: private Matcher matcher;
081:
082: MatchingElementEnumerator(RegularMask rm) {
083: super (rm);
084: rmask = rm;
085: matcher = rm.pattern.matcher();
086: }
087:
088: protected void setDir(File f) {
089: entries = rmask.elements(f, matcher);
090: }
091: }
092:
093: PathElementEnumerator newEnumerator() {
094: return new MatchingElementEnumerator(this );
095: }
096:
097: public Enumeration elements(File dir) {
098: throw new Error();
099: }
100:
101: public Enumeration elements(File dir, final Matcher matcher) {
102: if (dir == null)
103: throw new IllegalArgumentException();
104: //System.out.println("PathElementMask.elements("+dir+"{"+dir.getName()+","+dir.getAbsolutePath()+"}, mask=\""+matcher.pattern()+")\"");
105: return new ListEnumerator(dir,
106: new ListEnumerator.Instantiator() {
107: public File instantiate(File dir, String name) {
108: //System.out.println(" next name:"+name);
109: //if(matcher!=null && !matcher.matches(name)) return null;
110: if (!matcher.matches(name))
111: return null;
112: //System.out.println(" mask ok");
113: File f = new File(dir, name);
114: if (dirsOnly && !f.isDirectory())
115: return null;
116: return f;
117: }
118: });
119: }
120: }
121:
122: class FixedPathElement extends PathElementMask {
123: private String[] list;
124:
125: FixedPathElement(String s, boolean dirsOnly) {
126: super (dirsOnly);
127: //System.out.println("FixedPathElement("+s+","+dirsOnly+"):");
128: //windows: paths like "d:" don't work as expected, "d:/" is ok
129: list = new String[] { dirsOnly ? s + File.separator : s };
130: }
131:
132: public Enumeration elements(File dir) {
133: //System.out.println("FixedPathElement.elements("+dir+"), mask=\""+name+"\"");
134: if (dir == null)
135: throw new IllegalArgumentException();
136: return new ListEnumerator(dir, list,
137: new ListEnumerator.Instantiator() {
138: public File instantiate(File dir, String name) {
139: File f = dir.getName().equals(".") ? new File(
140: name) : new File(dir, name);
141: if (!f.exists()
142: || (dirsOnly && !f.isDirectory()))
143: return null;
144: return f;
145: }
146: });
147: }
148: }
149:
150: class AnyFile extends PathElementMask {
151: AnyFile(boolean dirsOnly) {
152: super (dirsOnly);
153: //System.out.println("AnyFile("+dirsOnly+"):");
154: }
155:
156: public Enumeration elements(File dir) {
157: //System.out.println("AnyFile.elements("+dir+")");
158: if (dir == null)
159: throw new IllegalArgumentException();
160: return new ListEnumerator(dir,
161: new ListEnumerator.Instantiator() {
162: public File instantiate(File dir, String name) {
163: File f = new File(dir, name);
164: if (dirsOnly && !f.isDirectory())
165: return null;
166: return f;
167: }
168: });
169: }
170: }
171:
172: class AnyPath extends PathElementMask {
173: private ListEnumerator.Instantiator inst = new ListEnumerator.Instantiator() {
174: public File instantiate(File dir, String name) {
175: if (dir == null || name == null)
176: throw new IllegalArgumentException();
177: File f = new File(dir, name);
178: if (dirsOnly && !f.isDirectory())
179: return null;
180: return f;
181: }
182: };
183:
184: AnyPath(boolean dirsOnly) {
185: super (dirsOnly);
186: //System.out.println("AnyFile("+dirsOnly+"):");
187: }
188:
189: public Enumeration elements(final File dir) {
190: //System.out.println("AnyFile.elements("+dir+")");
191: if (dir == null)
192: throw new IllegalArgumentException();
193: final Stack stack = new Stack();
194: stack.push(dir);
195: return new Enumerator() {
196: {
197: currObj = dir;
198: }
199: private Enumeration currEn;
200:
201: protected boolean find() {
202: while (currEn == null || !currEn.hasMoreElements()) {
203: if (stack.size() == 0) {
204: return false;
205: }
206: currEn = new ListEnumerator((File) stack.pop(),
207: inst);
208: }
209: currObj = currEn.nextElement();
210: if (((File) currObj).isDirectory())
211: stack.push(currObj);
212: return true;
213: }
214: };
215: }
216: }
|