KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.ByteArrayInputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.StringReader JavaDoc;
22
23 import org.xml.sax.ContentHandler JavaDoc;
24 import org.xml.sax.InputSource JavaDoc;
25
26 /**
27  * An <code>XScriptObject</code> created from an inline XML fragment.
28  *
29  * @author <a HREF="mailto:ovidiu@cup.hp.com">Ovidiu Predescu</a>
30  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
31  * @version CVS $Id: XScriptObjectInlineXML.java 30932 2004-07-29 17:35:38Z vgritsenko $
32  * @since July 7, 2001
33  */

34 public class XScriptObjectInlineXML extends XScriptObject {
35
36     StringBuffer JavaDoc stringBuffer;
37     StringBufferContentHandler streamHandler;
38
39     public XScriptObjectInlineXML(XScriptManager manager) {
40         super(manager);
41         stringBuffer = new StringBuffer JavaDoc();
42         stringBuffer.append("<?xml version=\"1.0\"?>\n\n");
43         streamHandler = new StringBufferContentHandler(stringBuffer);
44     }
45
46     public XScriptObjectInlineXML(XScriptManager manager, StringBuffer JavaDoc stringBuffer) {
47         super(manager);
48         this.stringBuffer = stringBuffer;
49         streamHandler = new StringBufferContentHandler(stringBuffer);
50     }
51
52     public XScriptObjectInlineXML(XScriptManager manager, String JavaDoc string) {
53         super(manager);
54         this.stringBuffer = new StringBuffer JavaDoc(string);
55         streamHandler = new StringBufferContentHandler(stringBuffer);
56     }
57
58     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
59         // FIXME(VG): This method should never be used because it
60
// always converts content into system encoding. This will
61
// ruin i18n documents. Use getInputSource() instead.
62
return new ByteArrayInputStream JavaDoc(stringBuffer.toString().getBytes());
63     }
64
65     public InputSource JavaDoc getInputSource() throws IOException JavaDoc {
66         InputSource JavaDoc is = new InputSource JavaDoc(new StringReader JavaDoc(stringBuffer.toString()));
67         is.setSystemId(getURI());
68         return is;
69     }
70
71     public ContentHandler JavaDoc getContentHandler() {
72         return streamHandler;
73     }
74
75     public String JavaDoc toString() {
76         return stringBuffer.toString();
77     }
78
79     public long getContentLength() {
80         return stringBuffer.length();
81     }
82
83     public String JavaDoc getContent() {
84         return stringBuffer.toString();
85     }
86
87     public String JavaDoc 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
Popular Tags