001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.source.impl;
018:
019: import java.io.ByteArrayInputStream;
020: import java.io.IOException;
021: import java.io.InputStream;
022:
023: import org.apache.cocoon.xml.XMLUtils;
024: import org.apache.excalibur.xml.sax.XMLizable;
025: import org.apache.excalibur.source.Source;
026: import org.apache.excalibur.source.SourceNotFoundException;
027: import org.apache.excalibur.source.SourceValidity;
028: import org.apache.excalibur.source.impl.validity.NOPValidity;
029:
030: import org.xml.sax.ContentHandler;
031: import org.xml.sax.SAXException;
032:
033: /**
034: * A <code>Source</code> that generates completely empty XML document or an
035: * XML document that contains just a root node.
036: *
037: * <p>
038: * The URI syntax is <code>empty:</code> for completely empty XML document
039: * or <code>create-document:root-element</code> for document with root element,
040: * where <code>root-element</code> is the name of the root element to create.
041: *
042: * @version $Id: EmptySource.java 433543 2006-08-22 06:22:54Z crossley $
043: * @since 2.1.8
044: */
045: public class EmptySource implements XMLizable, Source {
046:
047: protected String rootElementName;
048: protected String scheme;
049: protected String uri;
050: protected String xmlDocument;
051:
052: public EmptySource(String location) {
053: this .uri = location;
054: final int pos = location.indexOf(':');
055: this .scheme = location.substring(0, pos);
056:
057: final String rootName = location.substring(pos + 1).trim();
058: if (rootName.length() > 0) {
059: this .rootElementName = rootName;
060: this .xmlDocument = '<' + this .rootElementName + "/>";
061: } else {
062: this .xmlDocument = "";
063: }
064: }
065:
066: /**
067: * @see org.apache.excalibur.xml.sax.XMLizable#toSAX(org.xml.sax.ContentHandler)
068: */
069: public void toSAX(ContentHandler handler) throws SAXException {
070: handler.startDocument();
071: if (rootElementName != null) {
072: XMLUtils.createElement(handler, this .rootElementName);
073: }
074: handler.endDocument();
075: }
076:
077: /**
078: * @see org.apache.excalibur.source.Source#exists()
079: */
080: public boolean exists() {
081: return true;
082: }
083:
084: /**
085: * @see org.apache.excalibur.source.Source#getContentLength()
086: */
087: public long getContentLength() {
088: return this .xmlDocument.length();
089: }
090:
091: /**
092: * @see org.apache.excalibur.source.Source#getInputStream()
093: */
094: public InputStream getInputStream() throws IOException,
095: SourceNotFoundException {
096: return new ByteArrayInputStream(this .xmlDocument
097: .getBytes("utf-8"));
098: }
099:
100: /**
101: * @see org.apache.excalibur.source.Source#getLastModified()
102: */
103: public long getLastModified() {
104: // this document *never* changes
105: return 1;
106: }
107:
108: /**
109: * @see org.apache.excalibur.source.Source#getMimeType()
110: */
111: public String getMimeType() {
112: return "text/xml";
113: }
114:
115: /**
116: * @see org.apache.excalibur.source.Source#getScheme()
117: */
118: public String getScheme() {
119: return this .scheme;
120: }
121:
122: /**
123: * @see org.apache.excalibur.source.Source#getURI()
124: */
125: public String getURI() {
126: return this .uri;
127: }
128:
129: /**
130: * @see org.apache.excalibur.source.Source#getValidity()
131: */
132: public SourceValidity getValidity() {
133: return NOPValidity.SHARED_INSTANCE;
134: }
135:
136: /**
137: * @see org.apache.excalibur.source.Source#refresh()
138: */
139: public void refresh() {
140: // nothing to do here
141: }
142: }
|