01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.components.xscript;
18:
19: import java.io.ByteArrayInputStream;
20: import java.io.IOException;
21: import java.io.InputStream;
22: import java.io.StringReader;
23:
24: import org.xml.sax.ContentHandler;
25: import org.xml.sax.InputSource;
26:
27: /**
28: * An <code>XScriptObject</code> created from an inline XML fragment.
29: *
30: * @author <a href="mailto:ovidiu@cup.hp.com">Ovidiu Predescu</a>
31: * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
32: * @version CVS $Id: XScriptObjectInlineXML.java 454482 2006-10-09 20:08:23Z joerg $
33: * @since July 7, 2001
34: */
35: public class XScriptObjectInlineXML extends XScriptObject {
36:
37: StringBuffer stringBuffer;
38: StringBufferContentHandler streamHandler;
39:
40: public XScriptObjectInlineXML(XScriptManager manager) {
41: this (manager, new StringBuffer("<?xml version=\"1.0\"?>\n\n"));
42: }
43:
44: public XScriptObjectInlineXML(XScriptManager manager, String string) {
45: this (manager, new StringBuffer(string));
46: }
47:
48: public XScriptObjectInlineXML(XScriptManager manager,
49: StringBuffer stringBuffer) {
50: super (manager);
51: this .stringBuffer = stringBuffer;
52: this .streamHandler = new StringBufferContentHandler(
53: this .stringBuffer);
54: }
55:
56: public InputStream getInputStream() throws IOException {
57: // FIXME(VG): This method should never be used because it
58: // always converts content into system encoding. This will
59: // ruin i18n documents. Use getInputSource() instead.
60: return new ByteArrayInputStream(this .stringBuffer.toString()
61: .getBytes());
62: }
63:
64: public InputSource getInputSource() throws IOException {
65: InputSource is = new InputSource(new StringReader(
66: this .stringBuffer.toString()));
67: is.setSystemId(getURI());
68: return is;
69: }
70:
71: public ContentHandler getContentHandler() {
72: return this .streamHandler;
73: }
74:
75: public String toString() {
76: return this .stringBuffer.toString();
77: }
78:
79: public long getContentLength() {
80: return this .stringBuffer.length();
81: }
82:
83: public String getContent() {
84: return this .stringBuffer.toString();
85: }
86:
87: public String getURI() {
88: // FIXME: Implement a URI scheme to be able to refer to XScript
89: // variables by URI
90: return "xscript:inline:" + System.identityHashCode(this);
91: }
92:
93: }
|