[jboss-cvs] JBossAS SVN: r101912 - in projects/jboss-osgi/projects/vfs/trunk/vfs30: src/main/java/org/jboss/osgi/vfs30 and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Mar 4 17:38:17 EST 2010


Author: thomas.diesler at jboss.com
Date: 2010-03-04 17:38:17 -0500 (Thu, 04 Mar 2010)
New Revision: 101912

Added:
   projects/jboss-osgi/projects/vfs/trunk/vfs30/src/main/java/org/jboss/osgi/vfs30/VFSEntryPathsEnumeration.java
   projects/jboss-osgi/projects/vfs/trunk/vfs30/src/main/java/org/jboss/osgi/vfs30/VFSFindEntriesEnumeration.java
Modified:
   projects/jboss-osgi/projects/vfs/trunk/vfs30/pom.xml
   projects/jboss-osgi/projects/vfs/trunk/vfs30/src/main/java/org/jboss/osgi/vfs30/VirtualFileAdaptor30.java
Log:
Implement getEntryPaths, findEntries

Modified: projects/jboss-osgi/projects/vfs/trunk/vfs30/pom.xml
===================================================================
--- projects/jboss-osgi/projects/vfs/trunk/vfs30/pom.xml	2010-03-04 22:19:11 UTC (rev 101911)
+++ projects/jboss-osgi/projects/vfs/trunk/vfs30/pom.xml	2010-03-04 22:38:17 UTC (rev 101912)
@@ -30,7 +30,7 @@
 
   <!-- Properties -->
   <properties>
-    <version.jboss.vfs>3.0.0.CR2</version.jboss.vfs>
+    <version.jboss.vfs>3.0.0.CR3</version.jboss.vfs>
   </properties>
   
   <!-- Dependencies -->

Added: projects/jboss-osgi/projects/vfs/trunk/vfs30/src/main/java/org/jboss/osgi/vfs30/VFSEntryPathsEnumeration.java
===================================================================
--- projects/jboss-osgi/projects/vfs/trunk/vfs30/src/main/java/org/jboss/osgi/vfs30/VFSEntryPathsEnumeration.java	                        (rev 0)
+++ projects/jboss-osgi/projects/vfs/trunk/vfs30/src/main/java/org/jboss/osgi/vfs30/VFSEntryPathsEnumeration.java	2010-03-04 22:38:17 UTC (rev 101912)
@@ -0,0 +1,92 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file in the
+* distribution for a full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.osgi.vfs30;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.List;
+
+import org.jboss.vfs.VirtualFile;
+
+/**
+ * An enumeration of VFS entry paths.
+ * 
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @author thomas.diesler at jboss.com
+ * @version $Revision: 1.1 $
+ */
+class VFSEntryPathsEnumeration implements Enumeration<String>
+{
+   /** The paths */
+   private Iterator<String> paths;
+
+   /**
+    * Create a new VFSEntryPathsEnumeration.
+    * 
+    * @param root the root file
+    * @param file the file to enumerate
+    * @throws IOException for any error
+    */
+   public VFSEntryPathsEnumeration(VirtualFile root, VirtualFile file) throws IOException
+   {
+      if (root == null)
+         throw new IllegalArgumentException("Null root");
+      if (file == null)
+         throw new IllegalArgumentException("Null file");
+
+      String rootPath = root.getPathName();
+      ArrayList<String> paths = new ArrayList<String>();
+      paths.add(fixPath(rootPath, file));
+
+      List<VirtualFile> children = file.getChildrenRecursively();
+      for (VirtualFile child : children)
+         paths.add(fixPath(rootPath, child));
+
+      this.paths = paths.iterator();
+   }
+
+   public boolean hasMoreElements()
+   {
+      return paths.hasNext();
+   }
+
+   public String nextElement()
+   {
+      return paths.next();
+   }
+
+   private String fixPath(String rootPath, VirtualFile file)
+   {
+      String result = file.getPathName();
+
+      int length = rootPath.length();
+      if (length != 0)
+         result = result.substring(length);
+
+      if (file.isDirectory() && result.endsWith("/") == false)
+         result += "/";
+
+      return result;
+   }
+}

Added: projects/jboss-osgi/projects/vfs/trunk/vfs30/src/main/java/org/jboss/osgi/vfs30/VFSFindEntriesEnumeration.java
===================================================================
--- projects/jboss-osgi/projects/vfs/trunk/vfs30/src/main/java/org/jboss/osgi/vfs30/VFSFindEntriesEnumeration.java	                        (rev 0)
+++ projects/jboss-osgi/projects/vfs/trunk/vfs30/src/main/java/org/jboss/osgi/vfs30/VFSFindEntriesEnumeration.java	2010-03-04 22:38:17 UTC (rev 101912)
@@ -0,0 +1,134 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file in the
+* distribution for a full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.osgi.vfs30;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.jboss.vfs.VirtualFile;
+import org.jboss.vfs.VirtualFileVisitor;
+import org.jboss.vfs.VisitorAttributes;
+import org.jboss.vfs.util.MatchAllVirtualFileFilter;
+
+
+/**
+ * An enumeration of VFS entries.
+ * 
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @author thomas.diesler at jboss.com
+ * @version $Revision: 1.1 $
+ */
+class VFSFindEntriesEnumeration implements Enumeration<URL>
+{
+   /** The paths */
+   private Iterator<URL> paths;
+
+   /**
+    * Create a new VFSFindEntriesEnumeration.
+    * 
+    * @param root the root file
+    * @param file the file to enumerate
+    * @param filePattern the file pattern
+    * @param recurse whether to recurse
+    * @throws IOException for any error
+    */
+   public VFSFindEntriesEnumeration(VirtualFile root, VirtualFile file, String filePattern, boolean recurse) throws IOException
+   {
+      if (root == null)
+         throw new IllegalArgumentException("Null root");
+      if (file == null)
+         throw new IllegalArgumentException("Null file");
+
+      String rootPath = root.getPathName();
+      VisitorAttributes attributes = new VisitorAttributes();
+      attributes.setIncludeRoot(false);
+      attributes.setLeavesOnly(true);
+      if (recurse)
+         attributes.setRecurseFilter(MatchAllVirtualFileFilter.INSTANCE);
+      
+      VisitorImpl visitor = new VisitorImpl(rootPath, filePattern, attributes);
+      file.visit(visitor);
+      
+      this.paths = visitor.paths.iterator();
+   }
+
+   public boolean hasMoreElements()
+   {
+      return paths.hasNext();
+   }
+
+   public URL nextElement()
+   {
+      return paths.next();
+   }
+   
+   static class VisitorImpl implements VirtualFileVisitor
+   {
+      ArrayList<URL> paths = new ArrayList<URL>();
+
+      Pattern filter;
+      String rootPath;
+      VisitorAttributes attributes;
+      
+      VisitorImpl(String rootPath, String filter, VisitorAttributes attributes)
+      {
+         this.rootPath = rootPath;
+         this.filter = convertToPattern(filter);
+         this.attributes = attributes;
+      }
+
+      public VisitorAttributes getAttributes()
+      {
+         return attributes;
+      }
+
+      public void visit(VirtualFile virtualFile)
+      {
+         // See if the filter matches
+         Matcher matcher = filter.matcher(virtualFile.getName());
+         if (matcher.find() == false)
+            return;
+         
+         try
+         {
+            paths.add(virtualFile.toURL());
+         }
+         catch (Exception e)
+         {
+            throw new RuntimeException("Error visiting " + virtualFile, e);
+         }
+      }
+      
+      // Convert file pattern (RFC 1960-based Filter) into a RegEx pattern
+      private static Pattern convertToPattern(String filePattern)
+      {
+         filePattern = filePattern.replace("*", ".*");
+         return Pattern.compile("^" + filePattern + "$");
+      }
+
+   }
+}

Modified: projects/jboss-osgi/projects/vfs/trunk/vfs30/src/main/java/org/jboss/osgi/vfs30/VirtualFileAdaptor30.java
===================================================================
--- projects/jboss-osgi/projects/vfs/trunk/vfs30/src/main/java/org/jboss/osgi/vfs30/VirtualFileAdaptor30.java	2010-03-04 22:19:11 UTC (rev 101911)
+++ projects/jboss-osgi/projects/vfs/trunk/vfs30/src/main/java/org/jboss/osgi/vfs30/VirtualFileAdaptor30.java	2010-03-04 22:38:17 UTC (rev 101912)
@@ -77,7 +77,7 @@
    public VirtualFile getChild(String path) throws IOException
    {
       org.jboss.vfs.VirtualFile child = delegate.getChild(path);
-      if (child == null)
+      if (child.exists() == false)
          return null;
 
       return new VirtualFileAdaptor30(child);
@@ -94,12 +94,35 @@
 
    public Enumeration<URL> findEntries(String path, String pattern, boolean recurse) throws IOException
    {
-      throw new IllegalArgumentException("not implemented");
+      if (path == null)
+         throw new IllegalArgumentException("Null path");
+      
+      if (pattern == null)
+         pattern = "*";
+
+      if (path.startsWith("/"))
+         path = path.substring(1);
+
+      org.jboss.vfs.VirtualFile child = delegate.getChild(path);
+      if (child.exists() == false)
+         return null;
+      
+      return new VFSFindEntriesEnumeration(delegate, child, pattern, recurse);
    }
 
-   public Enumeration<String> getEntryPaths(String path)
+   public Enumeration<String> getEntryPaths(String path) throws IOException
    {
-      throw new IllegalArgumentException("not implemented");
+      if (path == null)
+         throw new IllegalArgumentException("Null path");
+      
+      if (path.startsWith("/"))
+         path = path.substring(1);
+      
+      org.jboss.vfs.VirtualFile child = delegate.getChild(path);
+      if (child.exists() == false)
+         return null;
+      
+      return new VFSEntryPathsEnumeration(delegate, child);
    }
 
    public InputStream openStream() throws IOException




More information about the jboss-cvs-commits mailing list