001/*
002 * Copyright (c) 2004-2010, Kohsuke Kawaguchi
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification, are permitted provided
006 * that the following conditions are met:
007 *
008 *     * Redistributions of source code must retain the above copyright notice, this list of
009 *       conditions and the following disclaimer.
010 *     * Redistributions in binary form must reproduce the above copyright notice, this list of
011 *       conditions and the following disclaimer in the documentation and/or other materials
012 *       provided with the distribution.
013 *
014 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
015 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
016 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
017 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
018 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
019 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
020 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
021 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
022 */
023
024package org.kohsuke.stapler.jelly;
025
026import org.apache.commons.jelly.XMLOutput;
027import org.dom4j.io.HTMLWriter;
028import org.dom4j.io.OutputFormat;
029import java.io.IOException;
030import java.io.OutputStream;
031import java.io.UnsupportedEncodingException;
032import java.io.Writer;
033import java.util.logging.Level;
034import java.util.logging.Logger;
035
036/**
037 * Wrapper for XMLOutput using HTMLWriter that can turn off its HTML handling
038 * (if the Content-Type gets set to something other than text/html).
039 * 
040 * @author Alan.Harder@Sun.Com
041 */
042public class HTMLWriterOutput extends XMLOutput {
043    private HTMLWriter htmlWriter;
044    private OutputFormat format;
045
046    private static final Logger LOGGER = Logger.getLogger(HTMLWriterOutput.class.getName());
047
048    public static HTMLWriterOutput create(OutputStream out) throws UnsupportedEncodingException {
049        OutputFormat format = createFormat();
050        return new HTMLWriterOutput(new HTMLWriter(out, format), format, false);
051    }
052
053    public static HTMLWriterOutput create(Writer out, boolean escapeText) {
054        OutputFormat format = createFormat();
055        return new HTMLWriterOutput(new HTMLWriter(out, format), format, escapeText);
056    }
057
058    private static OutputFormat createFormat() {
059        OutputFormat format = new OutputFormat();
060        format.setXHTML(true);
061        // Only use short close for tags identified by HTMLWriter:
062        format.setExpandEmptyElements(true);
063        return format;
064    }
065
066    private HTMLWriterOutput(HTMLWriter hw, OutputFormat fmt, boolean escapeText) {
067        super(hw);
068        hw.setEscapeText(escapeText);
069        this.htmlWriter = hw;
070        this.format = fmt;
071        format.setExpandEmptyElements(true);
072    }
073
074    @Override public void close() throws IOException {
075        htmlWriter.close();
076    }
077
078    @Deprecated
079    public void useHTML(boolean enabled) {
080        LOGGER.log(Level. WARNING, null, new UnsupportedOperationException("Deprecated: HTMLWriterOutput.useHtml() has been deprecated and its functionality removed."));
081    }
082}