All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.cocoon.pipeline.component.sax.XMLSerializer Maven / Gradle / Ivy

There is a newer version: 3.0.0-alpha-3
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.cocoon.pipeline.component.sax;

import java.io.OutputStream;
import java.util.Map;
import java.util.Properties;

import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;

import org.apache.cocoon.pipeline.ProcessingException;
import org.apache.cocoon.pipeline.caching.CacheKey;
import org.apache.cocoon.pipeline.caching.SimpleCacheKey;
import org.apache.cocoon.pipeline.component.CachingPipelineComponent;

public class XMLSerializer extends AbstractSerializer implements CachingPipelineComponent {

    private static final SAXTransformerFactory SAX_TRANSFORMER_FACTORY = (SAXTransformerFactory) TransformerFactory
            .newInstance();

    private Properties format = new Properties();

    private TransformerHandler transformerHandler;

    public XMLSerializer() {
    }

    public XMLSerializer(Properties format) {
        super();
        this.format = format;
    }

    @Override
    public void setup(Map inputParameters) {
        try {
            this.transformerHandler = SAX_TRANSFORMER_FACTORY.newTransformerHandler();
        } catch (TransformerConfigurationException e) {
            throw new ProcessingException("Can't setup transformer handler for the serializer.", e);
        }
        this.transformerHandler.getTransformer().setOutputProperties(this.format);
        this.setContentHandler(this.transformerHandler);
        this.setLexicalHandler(this.transformerHandler);

        // set a default format because some transformer implementations run
        // into NPEs if it is missing
        if (this.format == null) {
            this.format = new Properties();
            this.format.put("method", "xml");
        }
    }

    public CacheKey constructCacheKey() {
        return new SimpleCacheKey();
    }

    public void setFormat(Properties format) {
        this.format = format;
    }

    @Override
    public void setOutputStream(OutputStream outputStream) {
        this.transformerHandler.setResult(new StreamResult(outputStream));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy