1 9 package org.jboss.portal.common.net.jar; 10 11 import org.jboss.portal.common.net.URLNavigationProvider; 12 import org.jboss.portal.common.net.URLVisitor; 13 import org.jboss.portal.common.util.Jar; 14 15 import java.net.URL ; 16 import java.net.JarURLConnection ; 17 import java.net.MalformedURLException ; 18 import java.util.Iterator ; 19 import java.util.jar.JarEntry ; 20 import java.util.jar.JarFile ; 21 import java.io.IOException ; 22 23 27 public class JarURLNavigationProvider implements URLNavigationProvider 28 { 29 30 public boolean isDir(URL url) throws IllegalArgumentException , IOException 31 { 32 throw new UnsupportedOperationException (); 33 } 34 35 public Iterator getChildren(URL url) throws IllegalArgumentException 36 { 37 throw new UnsupportedOperationException (); 38 } 39 40 public void visit(URL url, URLVisitor visitor) throws IllegalArgumentException , IOException 41 { 42 if (url == null) 43 { 44 throw new IllegalArgumentException ("Null URL not accepted"); 45 } 46 if (!"jar".equals(url.getProtocol())) 47 { 48 throw new IllegalArgumentException ("Only jar URL are accepted not " + url.getProtocol()); 49 } 50 JarURLConnection conn = (JarURLConnection )url.openConnection(); 51 JarFile jarFile = conn.getJarFile(); 52 URL jarURL = conn.getJarFileURL(); 53 JarEntry rootEntry = conn.getJarEntry(); 54 55 Iterator iterator = Jar.iterator(jarFile); 56 visit(jarURL, iterator, new Jar.EntryInfo(rootEntry), visitor); 57 } 58 59 private void visit(URL jarURL, Iterator iterator, Jar.EntryInfo root, URLVisitor visitor) throws IOException 60 { 61 Jar.EntryInfo previous = new Jar.EntryInfo(new JarEntry ("/")); 62 while (iterator.hasNext()) 63 { 64 Jar.EntryInfo current = (Jar.EntryInfo)iterator.next(); 65 if (current.isDescendantOf(root)) 66 { 67 handle(previous, current, jarURL, visitor); 68 previous = current; 69 } 70 } 71 handle(previous, new Jar.EntryInfo(new JarEntry ("/")), jarURL, visitor); 72 } 73 74 private void handle(Jar.EntryInfo previous, Jar.EntryInfo current, URL jarURL, URLVisitor visitor) 75 { 76 int index = 0; 78 while (index < previous.size() && index < current.size() && previous.get(index).equals(current.get(index))) 79 { 80 index++; 81 } 82 83 if (previous.isDirectory()) 85 { 86 for (int i = previous.size() - 1;i >= index;i--) 87 { 88 String dir = previous.get(i); 89 visitor.endDir(dir); 90 } 91 } 92 else 93 { 94 for (int i = previous.size() - 2;i >= index;i--) 95 { 96 String dir = previous.get(i); 97 visitor.endDir(dir); 98 } 99 } 100 101 if (current.isDirectory()) 103 { 104 for (int i = index;i < current.size();i++) 105 { 106 String dir = current.get(i); 107 visitor.startDir(dir); 108 } 109 } 110 else 111 { 112 for (int i = index;i < current.size() - 1;i++) 113 { 114 String dir = current.get(i); 115 visitor.startDir(dir); 116 } 117 try 118 { 119 URL file = current.toURL(jarURL); 120 String name = current.get(current.size() - 1); 121 visitor.file(name, file); 122 } 123 catch (MalformedURLException e) 124 { 125 e.printStackTrace(); 126 } 127 } 128 } 129 } 130 | Popular Tags |