// Path: src/main/java/de/tudarmstadt/ukp/dkpro/core/api/discourse/DiscourseDumpWriter.java
// public class DiscourseDumpWriter
//         extends JCasAnnotator_ImplBase
// {
//     /**
//      * Output file. If multiple CASes as processed, their contents are concatenated into this file.
//      * When this file is set to "-", the dump does to{@link System#out} (default).
//      */
//     public static final String PARAM_OUTPUT_FILE = "outputFile";
// 
//     @ConfigurationParameter(name = PARAM_OUTPUT_FILE, mandatory = true, defaultValue = "-")
//     private File outputFile;
// 
//     private PrintWriter out;
// 
//     @Override
//     public void initialize(UimaContext context)
//             throws ResourceInitializationException
//     {
//         super.initialize(context);
// 
//         try {
//             if (out == null) {
//                 if ("-".equals(outputFile.getName())) {
//                     out = new PrintWriter(new CloseShieldOutputStream(System.out));
//                 }
//                 else {
//                     if (outputFile.getParentFile() != null) {
//                         outputFile.getParentFile().mkdirs();
//                     }
//                     out = new PrintWriter(
//                             new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8"));
//                 }
//             }
//         }
//         catch (IOException e) {
//             throw new ResourceInitializationException(e);
//         }
// 
//     }
// 
//     @Override
//     public void process(JCas jCas)
//             throws AnalysisEngineProcessException
//     {
//         out.println("++ Document: " + DocumentMetaData.get(jCas).getDocumentId());
// 
//         for (DiscourseRelation relation : JCasUtil.select(jCas, DiscourseRelation.class)) {
//             out.println(debugRelation(relation));
//         }
// 
//         out.flush();
//     }
// 
//     /**
//      * Debugs discourse relation to String
//      *
//      * @param relation relation
//      */
//     public static String debugRelation(DiscourseRelation relation)
//     {
// 
//         //        StringWriter out = new StringWriter();
//         StringWriter stringWriter = new StringWriter();
//         PrintWriter out = new PrintWriter(stringWriter);
// 
//         out.println("---- discourse relation " + relation.getRelationId());
//         out.println(" relation class: " + relation.getClass().getSimpleName());
//         out.println(" coveredText: " + relation.getCoveredText());
//         for (DiscourseArgument argument : Arrays
//                 .asList(relation.getArg1(), relation.getArg2())) {
//             out.println("   -- arg" + argument.getArgumentNumber() + ": ");
//             out.println("      argumentType: " + (argument.getArgumentType() != null ?
//                     argument.getArgumentType() : "-"));
//             out.println("      coveredText: " + argument.getCoveredText());
// 
//         }
// 
//         if (relation instanceof ExplicitDiscourseRelation) {
//             ExplicitDiscourseRelation explicitRelation = (ExplicitDiscourseRelation) relation;
// 
//             for (DiscourseConnective connective : Arrays
//                     .asList(explicitRelation.getDiscourseConnective1(),
//                             explicitRelation.getDiscourseConnective2())) {
//                 if (connective != null) {
//                     out.println("   -- connectiveType: " + connective.getConnectiveType());
//                     out.println("      coveredText: " + connective.getCoveredText());
//                 }
//             }
//         }
// 
//         for (DiscourseAttribution attribution : JCasUtil
//                 .selectCovered(DiscourseAttribution.class, relation)) {
//             out.println("   -- attribution " + attribution.getAttributeId());
//             out.println("      coveredText: " + attribution.getCoveredText());
//         }
// 
//         out.println("---- end of relation " + relation.getRelationId());
//         out.flush();
// 
//         return stringWriter.toString();
//     }
// }


import de.tudarmstadt.ukp.dkpro.core.api.discourse.DiscourseDumpWriter;
import de.tudarmstadt.ukp.dkpro.core.api.metadata.type.DocumentMetaData;
import de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Paragraph;
import de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token;
import de.tudarmstadt.ukp.dkpro.core.discourse.pdtb.*;
import de.tudarmstadt.ukp.dkpro.core.io.xmi.XmiReader;
import de.tudarmstadt.ukp.dkpro.core.io.xmi.XmiWriter;
import nu.xom.*;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.uima.UimaContext;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
import org.apache.uima.fit.descriptor.ConfigurationParameter;
import org.apache.uima.fit.factory.AnalysisEngineFactory;
import org.apache.uima.fit.factory.CollectionReaderFactory;
import org.apache.uima.fit.pipeline.SimplePipeline;
import org.apache.uima.fit.util.JCasUtil;
import org.apache.uima.jcas.JCas;
import org.apache.uima.resource.ResourceInitializationException;
import java.io.File;
import java.io.IOException;
import java.util.*;
                    int begin =
                            arg1.getBegin() < arg2.getBegin() ? arg1.getBegin() : arg2.getBegin();
                    int end = arg1.getEnd() > arg2.getEnd() ? arg1.getEnd() : arg2.getEnd();

                    discourseRelation.setBegin(begin);
                    discourseRelation.setEnd(end);

                    arg1.addToIndexes();
                    arg2.addToIndexes();
                    discourseRelation.addToIndexes();
                }
                else {
                    System.err.println("Both arguments must be properly closed");
                }
            }
            else {
                System.err.println("Cannot find both arguments to relation " + relationId);
            }
        }

        // annotate all non-null Attributions
        for (DiscourseAttribution attribution : discourseAttributions) {
            if (attribution.getEnd() > 0) {
                attribution.addToIndexes();
            }
        }

        if (verbose) {
            for (DiscourseRelation discourseRelation : JCasUtil
                    .select(jCas, DiscourseRelation.class)) {
// Path: src/main/java/de/tudarmstadt/ukp/dkpro/core/api/discourse/DiscourseDumpWriter.java
// public class DiscourseDumpWriter
//         extends JCasAnnotator_ImplBase
// {
//     /**
//      * Output file. If multiple CASes as processed, their contents are concatenated into this file.
//      * When this file is set to "-", the dump does to{@link System#out} (default).
//      */
//     public static final String PARAM_OUTPUT_FILE = "outputFile";
// 
//     @ConfigurationParameter(name = PARAM_OUTPUT_FILE, mandatory = true, defaultValue = "-")
//     private File outputFile;
// 
//     private PrintWriter out;
// 
//     @Override
//     public void initialize(UimaContext context)
//             throws ResourceInitializationException
//     {
//         super.initialize(context);
// 
//         try {
//             if (out == null) {
//                 if ("-".equals(outputFile.getName())) {
//                     out = new PrintWriter(new CloseShieldOutputStream(System.out));
//                 }
//                 else {
//                     if (outputFile.getParentFile() != null) {
//                         outputFile.getParentFile().mkdirs();
//                     }
//                     out = new PrintWriter(
//                             new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8"));
//                 }
//             }
//         }
//         catch (IOException e) {
//             throw new ResourceInitializationException(e);
//         }
// 
//     }
// 
//     @Override
//     public void process(JCas jCas)
//             throws AnalysisEngineProcessException
//     {
//         out.println("++ Document: " + DocumentMetaData.get(jCas).getDocumentId());
// 
//         for (DiscourseRelation relation : JCasUtil.select(jCas, DiscourseRelation.class)) {
//             out.println(debugRelation(relation));
//         }
// 
//         out.flush();
//     }
// 
//     /**
//      * Debugs discourse relation to String
//      *
//      * @param relation relation
//      */
//     public static String debugRelation(DiscourseRelation relation)
//     {
// 
//         //        StringWriter out = new StringWriter();
//         StringWriter stringWriter = new StringWriter();
//         PrintWriter out = new PrintWriter(stringWriter);
// 
//         out.println("---- discourse relation " + relation.getRelationId());
//         out.println(" relation class: " + relation.getClass().getSimpleName());
//         out.println(" coveredText: " + relation.getCoveredText());
//         for (DiscourseArgument argument : Arrays
//                 .asList(relation.getArg1(), relation.getArg2())) {
//             out.println("   -- arg" + argument.getArgumentNumber() + ": ");
//             out.println("      argumentType: " + (argument.getArgumentType() != null ?
//                     argument.getArgumentType() : "-"));
//             out.println("      coveredText: " + argument.getCoveredText());
// 
//         }
// 
//         if (relation instanceof ExplicitDiscourseRelation) {
//             ExplicitDiscourseRelation explicitRelation = (ExplicitDiscourseRelation) relation;
// 
//             for (DiscourseConnective connective : Arrays
//                     .asList(explicitRelation.getDiscourseConnective1(),
//                             explicitRelation.getDiscourseConnective2())) {
//                 if (connective != null) {
//                     out.println("   -- connectiveType: " + connective.getConnectiveType());
//                     out.println("      coveredText: " + connective.getCoveredText());
//                 }
//             }
//         }
// 
//         for (DiscourseAttribution attribution : JCasUtil
//                 .selectCovered(DiscourseAttribution.class, relation)) {
//             out.println("   -- attribution " + attribution.getAttributeId());
//             out.println("      coveredText: " + attribution.getCoveredText());
//         }
// 
//         out.println("---- end of relation " + relation.getRelationId());
//         out.flush();
// 
//         return stringWriter.toString();
//     }
// }


// Path: src/main/java/de/tudarmstadt/ukp/dkpro/discourse/pdtbparser/PDTBDiscourseAnnotator.java
import de.tudarmstadt.ukp.dkpro.core.api.discourse.DiscourseDumpWriter;
import de.tudarmstadt.ukp.dkpro.core.api.metadata.type.DocumentMetaData;
import de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Paragraph;
import de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token;
import de.tudarmstadt.ukp.dkpro.core.discourse.pdtb.*;
import de.tudarmstadt.ukp.dkpro.core.io.xmi.XmiReader;
import de.tudarmstadt.ukp.dkpro.core.io.xmi.XmiWriter;
import nu.xom.*;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.uima.UimaContext;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
import org.apache.uima.fit.descriptor.ConfigurationParameter;
import org.apache.uima.fit.factory.AnalysisEngineFactory;
import org.apache.uima.fit.factory.CollectionReaderFactory;
import org.apache.uima.fit.pipeline.SimplePipeline;
import org.apache.uima.fit.util.JCasUtil;
import org.apache.uima.jcas.JCas;
import org.apache.uima.resource.ResourceInitializationException;
import java.io.File;
import java.io.IOException;
import java.util.*;

                    int begin =
                            arg1.getBegin() < arg2.getBegin() ? arg1.getBegin() : arg2.getBegin();
                    int end = arg1.getEnd() > arg2.getEnd() ? arg1.getEnd() : arg2.getEnd();

                    discourseRelation.setBegin(begin);
                    discourseRelation.setEnd(end);

                    arg1.addToIndexes();
                    arg2.addToIndexes();
                    discourseRelation.addToIndexes();
                }
                else {
                    System.err.println("Both arguments must be properly closed");
                }
            }
            else {
                System.err.println("Cannot find both arguments to relation " + relationId);
            }
        }

        // annotate all non-null Attributions
        for (DiscourseAttribution attribution : discourseAttributions) {
            if (attribution.getEnd() > 0) {
                attribution.addToIndexes();
            }
        }

        if (verbose) {
            for (DiscourseRelation discourseRelation : JCasUtil
                    .select(jCas, DiscourseRelation.class)) {
                DiscourseDumpWriter.debugRelation(discourseRelation);