Source Code Cross Referenced for PathElementMask.java in  » Scripting » jruby » jregex » util » io » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Scripting » jruby » jregex.util.io 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.