1 19 20 28 29 package org.netbeans.modules.xml.retriever; 30 31 import java.io.File ; 32 import java.io.FileInputStream ; 33 import java.io.IOException ; 34 import java.io.InputStream ; 35 import java.net.URI ; 36 import java.net.URISyntaxException ; 37 import java.util.HashMap ; 38 39 43 public class FileResourceRetriever implements ResourceRetriever{ 44 45 46 public FileResourceRetriever() { 47 } 48 49 public boolean accept(String baseAddr, String currentAddr) throws URISyntaxException { 50 51 URI currURI = new URI (currentAddr); 52 if( (currURI.isAbsolute()) && (currURI.getScheme().equalsIgnoreCase("file"))) return true; 54 if(!currURI.isAbsolute() && (baseAddr == null)) 55 return true; 56 if(baseAddr != null){ 57 if(!currURI.isAbsolute()){ 58 URI baseURI = new URI (baseAddr); 59 if(baseURI.getScheme().equalsIgnoreCase("file")) return true; 61 } 62 } 63 64 65 return false; 66 67 } 68 69 long streamLength = 0; 70 public HashMap <String , InputStream > retrieveDocument(String baseAddress, String documentAddress) throws IOException ,URISyntaxException { 71 URI currURI = new URI (getEffectiveAddress(baseAddress, documentAddress)); 72 HashMap <String , InputStream > result = null; 73 File curFile = new File (currURI); 74 if(curFile.isFile()){ 75 InputStream is = new FileInputStream (curFile); 76 result = new HashMap <String , InputStream >(); 77 result.put(curFile.toURI().toString(), is); 78 streamLength = curFile.length(); 79 return result; 80 }else{ 81 throw new IOException ("File not found: "+curFile.toString()); } 84 } 85 86 public long getStreamLength() { 87 return streamLength; 88 } 89 90 public String getEffectiveAddress(String baseAddress, String documentAddress) throws IOException , URISyntaxException { 91 URI currURI = new URI (documentAddress); 92 if(currURI.isAbsolute()){ 93 return currURI.toString(); 95 }else{ 96 if(baseAddress != null){ 98 URI baseURI = new URI (baseAddress); 99 return (baseURI.resolve(currURI)).toString(); 100 }else{ 101 return null; 104 } 105 } 106 } 107 } 108 | Popular Tags |