/**
 *  Copyright (C) 2004 Orbeon, Inc.
 *
 *  This program is free software; you can redistribute it and/or modify it under the terms of the
 *  GNU Lesser General Public License as published by the Free Software Foundation; either version
 *  2.1 of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *  See the GNU Lesser General Public License for more details.
 *
 *  The full text of the license is available at http://www.gnu.org/copyleft/lesser.html
 */
package org.orbeon.oxf.processor;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.Element;
import org.orbeon.oxf.common.OXFException;
import org.orbeon.oxf.pipeline.api.PipelineContext;
import org.orbeon.oxf.util.LoggerFactory;
import org.orbeon.oxf.xml.SAXStore;
import org.orbeon.oxf.xml.dom4j.Dom4jUtils;
import org.orbeon.oxf.xml.dom4j.LocationData;
import org.orbeon.oxf.xml.dom4j.LocationSAXContentHandler;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;

public class DebugProcessor extends ProcessorImpl {

    static public Logger logger = LoggerFactory.createLogger(DebugProcessor.class);

    public DebugProcessor() {
        addInputInfo(new ProcessorInputOutputInfo(INPUT_DATA));
        addInputInfo(new ProcessorInputOutputInfo(INPUT_CONFIG));
        addInputInfo(new ProcessorInputOutputInfo(OUTPUT_DATA));
    }

    public ProcessorOutput createOutput(final String name) {
        ProcessorOutput output = new ProcessorImpl.ProcessorOutputImpl(getClass(), name) {
            public void readImpl(PipelineContext pipelineContext, ContentHandler contentHandler) {
                try {
                    if (logger.isInfoEnabled()) {

                        // Extract information to be logged
                        final String debugMessage;
                        final LocationData debugLocationData;
                        {
                            final Element configElement = readInputAsDOM4J(pipelineContext, INPUT_CONFIG).getRootElement();
                            debugMessage = configElement.element("message").getText();
                            if (configElement.element("system-id") != null) {
                                debugLocationData = new LocationData(configElement.element("system-id").getText(),
                                        Integer.parseInt(configElement.element("line").getText()),
                                        Integer.parseInt(configElement.element("column").getText()));
                            } else {
                                debugLocationData = null;
                            }
                        }
                        final Document loggedDocument;
                        final SAXStore saxStore = new SAXStore();   
                        {
                            readInputAsSAX(pipelineContext, name, saxStore);
                            final LocationSAXContentHandler saxContentHandler = new LocationSAXContentHandler();
                            saxStore.replay(saxContentHandler);
                            loggedDocument = saxContentHandler.getDocument();
                            if (loggedDocument.getRootElement() == null)
                                throw new OXFException("Null document for debug '" + debugMessage + "'");
                        }

                        // Send to log4j
                        logger.info(debugMessage + ":\n"
                                + (debugLocationData != null ? debugLocationData.toString() + "\n" : "")
                                + Dom4jUtils.domToString(loggedDocument));

                        // Set to output
                        saxStore.replay(contentHandler);

                    } else {
                        // No debugging, just pass-through
                        readInputAsSAX(pipelineContext, name, contentHandler);
                    }
                } catch (SAXException e) {
                    throw new OXFException(e);
                }
            }
        };
        addOutput(name, output);
        return output;
    }
}

@@ -3770,16 +3770,29 @@
 Document
+, true, false
 ));%0A%0A   

Debug processor output trims text again.