KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > source > impl > EmptySource


1 /*
2  * Copyright 1999-2005 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.source.impl;
17
18 import java.io.ByteArrayInputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21
22 import org.apache.cocoon.xml.XMLUtils;
23 import org.apache.excalibur.xml.sax.XMLizable;
24 import org.apache.excalibur.source.Source;
25 import org.apache.excalibur.source.SourceNotFoundException;
26 import org.apache.excalibur.source.SourceValidity;
27 import org.apache.excalibur.source.impl.validity.NOPValidity;
28
29 import org.xml.sax.ContentHandler JavaDoc;
30 import org.xml.sax.SAXException JavaDoc;
31
32 /**
33  * A <code>Source</code> that generates completely empty XML document or an
34  * XML document that contains just a root node.
35  *
36  * <p>
37  * The URI syntax is <code>empty:</code> for completely empty XML document
38  * or <code>create-document:root-element</code> for document with root element,
39  * where <code>root-element</code> is the name of the root element to create.
40  *
41  * @version $Id: EmptySource.java 292330 2005-09-28 22:07:18Z vgritsenko $
42  * @since 2.1.8
43  */

44 public class EmptySource implements XMLizable, Source {
45
46     protected String JavaDoc rootElementName;
47     protected String JavaDoc scheme;
48     protected String JavaDoc uri;
49     protected String JavaDoc xmlDocument;
50
51     public EmptySource(String JavaDoc location) {
52         this.uri = location;
53         final int pos = location.indexOf(':');
54         this.scheme = location.substring(0, pos);
55
56         final String JavaDoc rootName = location.substring(pos + 1);
57         if (rootName != null && rootName.trim().length() > 0) {
58             this.rootElementName = rootName.trim();
59             this.xmlDocument = '<' + this.rootElementName + "/>";
60         } else {
61             this.xmlDocument = "";
62         }
63     }
64
65     /**
66      * @see org.apache.excalibur.xml.sax.XMLizable#toSAX(org.xml.sax.ContentHandler)
67      */

68     public void toSAX(ContentHandler JavaDoc handler) throws SAXException JavaDoc {
69         handler.startDocument();
70         if (rootElementName != null) {
71             XMLUtils.createElement(handler, this.rootElementName);
72         }
73         handler.endDocument();
74     }
75
76     /**
77      * @see org.apache.excalibur.source.Source#exists()
78      */

79     public boolean exists() {
80         return true;
81     }
82
83     /**
84      * @see org.apache.excalibur.source.Source#getContentLength()
85      */

86     public long getContentLength() {
87         return this.xmlDocument.length();
88     }
89
90     /**
91      * @see org.apache.excalibur.source.Source#getInputStream()
92      */

93     public InputStream JavaDoc getInputStream() throws IOException JavaDoc, SourceNotFoundException {
94         return new ByteArrayInputStream JavaDoc(this.xmlDocument.getBytes("utf-8"));
95     }
96
97     /**
98      * @see org.apache.excalibur.source.Source#getLastModified()
99      */

100     public long getLastModified() {
101         // this document *never* changes
102
return 1;
103     }
104
105     /**
106      * @see org.apache.excalibur.source.Source#getMimeType()
107      */

108     public String JavaDoc getMimeType() {
109         return "text/xml";
110     }
111
112     /**
113      * @see org.apache.excalibur.source.Source#getScheme()
114      */

115     public String JavaDoc getScheme() {
116         return this.scheme;
117     }
118
119     /**
120      * @see org.apache.excalibur.source.Source#getURI()
121      */

122     public String JavaDoc getURI() {
123         return this.uri;
124     }
125
126     /**
127      * @see org.apache.excalibur.source.Source#getValidity()
128      */

129     public SourceValidity getValidity() {
130         return NOPValidity.SHARED_INSTANCE;
131     }
132
133     /**
134      * @see org.apache.excalibur.source.Source#refresh()
135      */

136     public void refresh() {
137         // nothing to do here
138
}
139 }
140
Popular Tags