KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > xscript > XScriptObjectFromURL


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 package org.apache.cocoon.components.xscript;
17
18 import org.apache.excalibur.source.Source;
19 import org.apache.excalibur.source.SourceException;
20 import org.apache.excalibur.source.SourceResolver;
21 import org.apache.excalibur.source.SourceNotFoundException;
22
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25
26 /**
27  * An <code>XScriptObject</code> created from the contents of a URL.
28  *
29  * @author <a HREF="mailto:ovidiu@cup.hp.com">Ovidiu Predescu</a>
30  * @version CVS $Id: XScriptObjectFromURL.java 30932 2004-07-29 17:35:38Z vgritsenko $
31  * @since August 30, 2001
32  */

33 public class XScriptObjectFromURL extends XScriptObject {
34
35     /**
36      * The content obtained from this URL becomes the content of this
37      * instance.
38      */

39     String JavaDoc systemId;
40
41     /**
42      * When was the content of the URL last modified.
43      */

44     long lastModified;
45
46
47     public XScriptObjectFromURL(XScriptManager manager, String JavaDoc systemId) {
48         super(manager);
49         this.systemId = systemId;
50     }
51
52     public InputStream JavaDoc getInputStream() throws IOException JavaDoc, SourceNotFoundException {
53         SourceResolver resolver = null;
54         Source source = null;
55         try {
56             resolver = (SourceResolver) serviceManager.lookup(SourceResolver.ROLE);
57             source = resolver.resolveURI(this.systemId);
58             return source.getInputStream();
59         } catch (Exception JavaDoc e) {
60             throw new SourceException("Exception during processing of " + this.systemId, e);
61         } finally {
62             if (resolver != null) {
63                 resolver.release(source);
64                 serviceManager.release(resolver);
65             }
66         }
67     }
68
69     public long getContentLength() {
70         return -1;
71     }
72
73     public long getLastModified() {
74         return 0;
75     }
76
77     public String JavaDoc toString() {
78         return new StringBuffer JavaDoc("XScriptObjectFromURL(systemId = ").append(systemId).append(")").toString();
79     }
80
81     public String JavaDoc getURI() {
82         // FIXME: generate a real system id to represent this object
83
return "xscript:url:" + systemId;
84     }
85 }
86
Popular Tags