/**
 *  Copyright (C) 2005 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.test;

import org.orbeon.oxf.pipeline.api.PipelineContext;
import org.orbeon.oxf.processor.ProcessorImpl;
import org.orbeon.oxf.processor.ProcessorInputOutputInfo;
import org.orbeon.oxf.processor.ProcessorOutput;
import org.orbeon.oxf.processor.generator.ExceptionGenerator;
import org.orbeon.oxf.xml.ContentHandlerHelper;
import org.orbeon.oxf.xml.SAXStore;
import org.xml.sax.ContentHandler;

/**
 * This processor has a data input and data output and behaves like the identity processor except if an exception is
 * thrown while reading its input, in which case it outputs the exception like the ExceptionGenerator does.
 */
public class ExceptionCatcher extends ProcessorImpl {

    public ExceptionCatcher() {
        addInputInfo(new ProcessorInputOutputInfo(INPUT_DATA));
        addOutputInfo(new ProcessorInputOutputInfo(OUTPUT_DATA));
    }

    public ProcessorOutput createOutput(String name) {
        ProcessorOutput output = new ProcessorOutputImpl(getClass(), name) {
            public void readImpl(PipelineContext context, ContentHandler contentHandler) {
                try {
                    // Try to read input in SAX store
                    SAXStore dataInput = new SAXStore();
                    readInputAsSAX(context, getInputByName(INPUT_DATA), dataInput);
                    // No exception: output what was read
                    dataInput.replay(contentHandler);
                } catch (Exception e) {
                    // Exception was thrown while reading input: generate a document with that exception
                    ContentHandlerHelper helper = new ContentHandlerHelper(contentHandler);
                    helper.startDocument();
                    String rootElementName = "exceptions";
                    helper.startElement(rootElementName);
                    ExceptionGenerator.addThrowable(helper, e);
                    helper.endElement();
                    helper.endDocument();
                }
            }
        };
        addOutput(name, output);
        return output;
    }
}

@@ -1040,16 +1040,59 @@
 XStore;%0A
+import org.orbeon.oxf.common.OXFException;%0A
 import o
@@ -2522,24 +2522,442 @@
 ementName);%0A
+%0A                    // Find the root throwable%0A                    Throwable innerMostThrowable = e; %7B%0A                        while (true) %7B%0A                            Throwable candidate = OXFException.getNestedException(innerMostThrowable);%0A                            if (candidate == null) break;%0A                            else innerMostThrowable = candidate;%0A                        %7D%0A                    %7D%0A%0A
             
@@ -3004,16 +3004,33 @@
 helper, 
+innerMostThrowabl
 e);%0A    

Fix [ #306449 ] ExceptionCatcher return the outermost exception