KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > security > utils > resolver > implementations > ResolverLocalFilesystem


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17 package com.sun.org.apache.xml.internal.security.utils.resolver.implementations;
18
19
20
21 import java.io.FileInputStream JavaDoc;
22
23 import com.sun.org.apache.xml.internal.utils.URI;
24 import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput;
25 import com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException;
26 import com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverSpi;
27 import org.w3c.dom.Attr JavaDoc;
28
29
30 /**
31  * A simple ResourceResolver for requests into the local filesystem.
32  *
33  * @author $Author: raul $
34  */

35 public class ResolverLocalFilesystem extends ResourceResolverSpi {
36
37    /** {@link java.util.logging} logging facility */
38     static java.util.logging.Logger JavaDoc log =
39         java.util.logging.Logger.getLogger(
40                     ResolverLocalFilesystem.class.getName());
41
42    /**
43     * @inheritDoc
44     */

45    public XMLSignatureInput engineResolve(Attr JavaDoc uri, String JavaDoc BaseURI)
46            throws ResourceResolverException {
47
48      try {
49         URI uriNew = new URI(new URI(BaseURI), uri.getNodeValue());
50
51         // if the URI contains a fragment, ignore it
52
URI uriNewNoFrag = new URI(uriNew);
53
54         uriNewNoFrag.setFragment(null);
55
56         String JavaDoc fileName =
57            ResolverLocalFilesystem
58               .translateUriToFilename(uriNewNoFrag.toString());
59         FileInputStream JavaDoc inputStream = new FileInputStream JavaDoc(fileName);
60         XMLSignatureInput result = new XMLSignatureInput(inputStream);
61
62         result.setSourceURI(uriNew.toString());
63
64         return result;
65      } catch (Exception JavaDoc e) {
66         throw new ResourceResolverException("generic.EmptyMessage", e, uri,
67                                             BaseURI);
68       }
69    }
70
71    /**
72     * Method translateUriToFilename
73     *
74     * @param uri
75     * @return the string of the filename
76     */

77    private static String JavaDoc translateUriToFilename(String JavaDoc uri) {
78
79       String JavaDoc subStr = uri.substring("file:/".length());
80
81       if (subStr.indexOf("%20") > -1)
82       {
83         int offset = 0;
84         int index = 0;
85         StringBuffer JavaDoc temp = new StringBuffer JavaDoc(subStr.length());
86         do
87         {
88           index = subStr.indexOf("%20",offset);
89           if (index == -1) temp.append(subStr.substring(offset));
90           else
91           {
92             temp.append(subStr.substring(offset,index));
93             temp.append(' ');
94             offset = index+3;
95           }
96         }
97         while(index != -1);
98         subStr = temp.toString();
99       }
100
101       if (subStr.charAt(1) == ':') {
102          // we're running M$ Windows, so this works fine
103
return subStr;
104       }
105       // we're running some UNIX, so we have to prepend a slash
106
return "/" + subStr;
107    }
108
109    /**
110     * @inheritDoc
111     */

112    public boolean engineCanResolve(Attr JavaDoc uri, String JavaDoc BaseURI) {
113
114       if (uri == null) {
115          return false;
116       }
117
118       String JavaDoc uriNodeValue = uri.getNodeValue();
119
120       if (uriNodeValue.equals("") || (uriNodeValue.charAt(0)=='#')) {
121          return false;
122       }
123
124       try {
125              //URI uriNew = new URI(new URI(BaseURI), uri.getNodeValue());
126
if (true)
127                 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "I was asked whether I can resolve " + uriNodeValue/*uriNew.toString()*/);
128
129              if ( uriNodeValue.startsWith("file:") ||
130                      BaseURI.startsWith("file:")/*uriNew.getScheme().equals("file")*/) {
131                 if (true)
132                     if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "I state that I can resolve " + uriNodeValue/*uriNew.toString()*/);
133
134                 return true;
135              }
136       } catch (Exception JavaDoc e) {}
137
138       if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "But I can't");
139
140       return false;
141    }
142 }
143
Popular Tags