KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > retriever > FileResourceRetriever


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * FileResourceRetriever.java
22  *
23  * Created on January 9, 2006, 10:47 PM
24  *
25  * To change this template, choose Tools | Template Manager
26  * and open the template in the editor.
27  */

28
29 package org.netbeans.modules.xml.retriever;
30
31 import java.io.File JavaDoc;
32 import java.io.FileInputStream JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.InputStream JavaDoc;
35 import java.net.URI JavaDoc;
36 import java.net.URISyntaxException JavaDoc;
37 import java.util.HashMap JavaDoc;
38
39 /**
40  *
41  * @author girix
42  */

43 public class FileResourceRetriever implements ResourceRetriever{
44     
45     /** Creates a new instance of FileResourceRetriever */
46     public FileResourceRetriever() {
47     }
48     
49     public boolean accept(String JavaDoc baseAddr, String JavaDoc currentAddr) throws URISyntaxException JavaDoc {
50         
51         URI JavaDoc currURI = new URI JavaDoc(currentAddr);
52         if( (currURI.isAbsolute()) && (currURI.getScheme().equalsIgnoreCase("file"))) //NOI18N
53
return true;
54         if(!currURI.isAbsolute() && (baseAddr == null))
55             return true;
56         if(baseAddr != null){
57             if(!currURI.isAbsolute()){
58                 URI JavaDoc baseURI = new URI JavaDoc(baseAddr);
59                 if(baseURI.getScheme().equalsIgnoreCase("file")) //NOI18N
60
return true;
61             }
62         }
63         
64         
65         return false;
66         
67     }
68     
69     long streamLength = 0;
70     public HashMap JavaDoc<String JavaDoc, InputStream JavaDoc> retrieveDocument(String JavaDoc baseAddress, String JavaDoc documentAddress) throws IOException JavaDoc,URISyntaxException JavaDoc{
71         URI JavaDoc currURI = new URI JavaDoc(getEffectiveAddress(baseAddress, documentAddress));
72         HashMap JavaDoc<String JavaDoc, InputStream JavaDoc> result = null;
73         File JavaDoc curFile = new File JavaDoc(currURI);
74         if(curFile.isFile()){
75             InputStream JavaDoc is = new FileInputStream JavaDoc(curFile);
76             result = new HashMap JavaDoc<String JavaDoc, InputStream JavaDoc>();
77             result.put(curFile.toURI().toString(), is);
78             streamLength = curFile.length();
79             return result;
80         }else{
81             //file not found in the system
82
throw new IOException JavaDoc("File not found: "+curFile.toString()); //NOI18N
83
}
84     }
85     
86     public long getStreamLength() {
87         return streamLength;
88     }
89     
90     public String JavaDoc getEffectiveAddress(String JavaDoc baseAddress, String JavaDoc documentAddress) throws IOException JavaDoc, URISyntaxException JavaDoc {
91         URI JavaDoc currURI = new URI JavaDoc(documentAddress);
92         if(currURI.isAbsolute()){
93             //abs file URI
94
return currURI.toString();
95         }else{
96             //relative URI
97
if(baseAddress != null){
98                 URI JavaDoc baseURI = new URI JavaDoc(baseAddress);
99                 return (baseURI.resolve(currURI)).toString();
100             }else{
101                 //neither the current URI nor the base URI are absoulte. So, can not resolve this
102
//path
103
return null;
104             }
105         }
106     }
107 }
108
Popular Tags