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.serialization;
18:
19: import javax.xml.transform.OutputKeys;
20: import javax.xml.transform.sax.TransformerHandler;
21: import javax.xml.transform.stream.StreamResult;
22:
23: import org.apache.avalon.framework.configuration.Configuration;
24: import org.apache.avalon.framework.configuration.ConfigurationException;
25: import org.apache.cocoon.CascadingIOException;
26:
27: import java.io.IOException;
28: import java.io.OutputStream;
29:
30: /**
31: * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
32: * @version CVS $Id: XMLSerializer.java 433543 2006-08-22 06:22:54Z crossley $
33: */
34:
35: public class XMLSerializer extends AbstractTextSerializer {
36:
37: /**
38: * Set the configurations for this serializer.
39: */
40: public void configure(Configuration conf)
41: throws ConfigurationException {
42: super .configure(conf);
43: this .format.put(OutputKeys.METHOD, "xml");
44: }
45:
46: /**
47: * Set the {@link OutputStream} where the requested resource should
48: * be serialized.
49: */
50: public void setOutputStream(OutputStream out) throws IOException {
51: super .setOutputStream(out);
52: try {
53: TransformerHandler handler = this .getTransformerHandler();
54: handler.getTransformer().setOutputProperties(this .format);
55: handler.setResult(new StreamResult(this .output));
56: this .setContentHandler(handler);
57: this .setLexicalHandler(handler);
58: } catch (Exception e) {
59: final String message = "Cannot set XMLSerializer outputstream";
60: throw new CascadingIOException(message, e);
61: }
62: }
63:
64: }
|