/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2008, 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.virtual.plugins.registry;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;

import org.jboss.virtual.VirtualFile;
import org.jboss.virtual.VFSUtils;
import org.jboss.virtual.spi.VFSContext;
import org.jboss.virtual.spi.VirtualFileHandler;
import org.jboss.virtual.spi.TempInfo;
import org.jboss.virtual.spi.cache.VFSCache;
import org.jboss.virtual.spi.cache.VFSCacheFactory;
import org.jboss.virtual.spi.registry.VFSRegistry;

/**
 * Default vfs registry.
 *
 * @author <a href="mailto:ales.justin@jboss.org">Ales Justin</a>
 */
public class DefaultVFSRegistry extends VFSRegistry
{
   /**
    * Get vfs cache.
    *
    * @return the vfs cache
    */
   protected VFSCache getCache()
   {
      return VFSCacheFactory.getInstance();
   }

   public void addContext(VFSContext context)
   {
      getCache().putContext(context);
   }

   public void removeContext(VFSContext context)
   {
      getCache().removeContext(context);
   }

   public VirtualFile getFile(URI uri) throws IOException
   {
      if (uri == null)
         throw new IllegalArgumentException("Null uri");

      VFSContext context = getCache().findContext(uri);
      if (context != null)
      {
         String relativePath = VFSUtils.getRelativePath(context, uri);
         for (TempInfo ti : context.getTempInfos())
         {
            String path = ti.getPath();
            if (relativePath.startsWith(path) && ti.getHandler() != null)
            {
               VirtualFileHandler handler = ti.getHandler();
               String subpath = relativePath.substring(path.length());
               VirtualFileHandler child = handler.getChild(subpath);
               if (child == null)
               {
                  List<VirtualFileHandler> children = handler.getChildren(true);
                  throw new IOException("Child not found " + subpath + " for " + handler + ", available children: " + children);
               }

               return child.getVirtualFile();
            }
         }

         VirtualFileHandler root = context.getRoot();
         VirtualFileHandler child = root.getChild(relativePath);
         if (child == null)
         {
            List<VirtualFileHandler> children = root.getChildren(true);
            throw new IOException("Child not found " + relativePath + " for " + root + ", available children: " + children);
         }

         return child.getVirtualFile();
      }
      return null;
   }

   public VirtualFile getFile(URL url) throws IOException
   {
      if (url == null)
         throw new IllegalArgumentException("Null url");

      try
      {
         return getFile(VFSUtils.toURI(url));
      }
      catch (URISyntaxException e)
      {
         IOException ioe = new IOException();
         ioe.initCause(e);
         throw ioe;
      }
   }
}

@@ -2640,70 +2640,8 @@
  %7B%0D%0A
-               VirtualFileHandler handler = ti.getHandler();%0D%0A
     
@@ -2754,25 +2754,37 @@
 d = 
-h
+findH
 andler
+(ti
 .get
-Child(
+Handler(), 
 subp
@@ -2805,39 +2805,48 @@
         
-if (child == null)%0D%0A   
+return child.getVirtualFile();%0D%0A
         
@@ -2841,33 +2841,33 @@
 );%0D%0A            
-%7B
+%7D
 %0D%0A              
@@ -2853,32 +2853,37 @@
     %7D%0D%0A         
+%7D%0D%0A%0D%0A
          List%3CVi
@@ -2867,37 +2867,32 @@
   %7D%0D%0A%0D%0A         
-List%3C
 VirtualFileHandl
@@ -2897,175 +2897,143 @@
 dler
-%3E children = handler.getChildren(true);%0D%0A                  throw new IOException(%22Child not found %22 + subpath + %22 for %22 + handler + %22, available children: %22 + children
+ root = context.getRoot();%0D%0A         VirtualFileHandler child = findHandler(root, relativePath);%0D%0A         return child.getVirtualFile(
 );%0D%0A
@@ -3042,156 +3042,321 @@
     
+%7D%0D%0A
       
-   %7D%0D%0A%0D%0A               return child.getVirtualFile();%0D%0A            %7D%0D%0A         %7D%0D%0A%0D%0A         VirtualFileHandler root = context.getRoot();
+return null;%0D%0A   %7D%0D%0A%0D%0A   /**%0D%0A    * Find the handler.%0D%0A    *%0D%0A    * @param root the root%0D%0A    * @param path the path%0D%0A    * @return child handler%0D%0A    * @throws IOException for any error%0D%0A    */%0D%0A   protected VirtualFileHandler findHandler(VirtualFileHandler root, String path) throws IOException
 %0D%0A   
+%7B%0D%0A
     
@@ -3398,35 +3398,24 @@
 etChild(
-relativeP
+p
 ath);%0D%0A
-   
       if
@@ -3430,35 +3430,32 @@
 == null)%0D%0A      
-   
 %7B%0D%0A            L
@@ -3450,19 +3450,16 @@
         
- 
-  
 List%3CVir
@@ -3516,27 +3516,24 @@
 ;%0D%0A         
-   
 throw new IO
@@ -3563,25 +3563,17 @@
 und %22 + 
-relativeP
+p
 ath + %22 
@@ -3628,35 +3628,27 @@
 en);%0D%0A      
-   %7D%0D%0A%0D%0A   
+%7D%0D%0A
       return
@@ -3657,54 +3657,8 @@
 hild
-.getVirtualFile();%0D%0A      %7D%0D%0A      return null
 ;%0D%0A 

Refactor code.