<commit_before>package org.xtext.example.mydsl.ui.handlers;

import java.util.List;

import org.apache.poi.xwpf.usermodel.PositionInParagraph;
import org.apache.poi.xwpf.usermodel.TextSegement;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;

public class DocumentHelper {

	public static void replaceText(XWPFDocument doc, String tag, String value) {
		for (XWPFParagraph p : doc.getParagraphs()) {
			List<XWPFRun> runs = p.getRuns();

			if (runs != null) {
				for (XWPFRun r : runs) {
					String text = r.getText(0);
					if (text != null && text.contains(tag)) {
						text = text.replace(tag, value);
						r.setText(text, 0);
					}
				}
			}
		}

		for (XWPFTable tbl : doc.getTables()) {
			for (XWPFTableRow row : tbl.getRows()) {
				for (XWPFTableCell cell : row.getTableCells()) {
					for (XWPFParagraph p : cell.getParagraphs()) {
						replaceText(p, tag, value);
					}
				}
			}
		}
	}

	public static void replaceText(XWPFParagraph par, String tag, String value, boolean addLineBreak) {
		List<XWPFRun> runs = par.getRuns();

		if (runs != null) {
			TextSegement found = par.searchText(tag, new PositionInParagraph());

			if (found != null) {
				if (found.getBeginRun() == found.getEndRun()) {
					// whole search string is in one Run
					XWPFRun run = runs.get(found.getBeginRun());
					String runText = run.getText(run.getTextPosition());
					String replaced = runText.replace(tag, value);
					run.setText(replaced, 0);
					
					if (addLineBreak) {
						run.addCarriageReturn();
					}
				} else {
					// The search string spans over more than one Run
					// Put the Strings together
					StringBuilder b = new StringBuilder();

					for (int runPos = found.getBeginRun(); runPos <= found.getEndRun(); runPos++) {
						XWPFRun run = runs.get(runPos);
						b.append(run.getText(run.getTextPosition()));
					}

					String connectedRuns = b.toString();
					String replaced = connectedRuns.replace(tag, value);

					// The first Run receives the replaced String of all connected Runs
					XWPFRun partOne = runs.get(found.getBeginRun());
					partOne.setText(replaced, 0);

					if (addLineBreak) {
						partOne.addCarriageReturn();
					}
					
					// Remove the text in the other Runs.
					for (int runPos = found.getBeginRun() + 1; runPos <= found.getEndRun(); runPos++) {
						XWPFRun partNext = runs.get(runPos);
						partNext.setText("", 0);
					}
				}
			}
		}
	}

	public static void replaceText(XWPFParagraph par, String tag, String value) {
		replaceText(par, tag, value, false);
	}
	
	public static XWPFParagraph getParagraph(XWPFDocument doc, String tag) {
		XWPFParagraph paragraph = null;

		for (XWPFParagraph p : doc.getParagraphs()) {
			String text = p.getText();<commit_msg>addLineBreakToParagraph method added<commit_after>package org.xtext.example.mydsl.ui.handlers;

import java.util.List;

import org.apache.poi.xwpf.usermodel.PositionInParagraph;
import org.apache.poi.xwpf.usermodel.TextSegement;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;

public class DocumentHelper {

	public static void replaceText(XWPFDocument doc, String tag, String value) {
		for (XWPFParagraph p : doc.getParagraphs()) {
			List<XWPFRun> runs = p.getRuns();

			if (runs != null) {
				for (XWPFRun r : runs) {
					String text = r.getText(0);
					if (text != null && text.contains(tag)) {
						text = text.replace(tag, value);
						r.setText(text, 0);
					}
				}
			}
		}

		for (XWPFTable tbl : doc.getTables()) {
			for (XWPFTableRow row : tbl.getRows()) {
				for (XWPFTableCell cell : row.getTableCells()) {
					for (XWPFParagraph p : cell.getParagraphs()) {
						replaceText(p, tag, value);
					}
				}
			}
		}
	}

	public static void replaceText(XWPFParagraph par, String tag, String value, boolean addLineBreak) {
		List<XWPFRun> runs = par.getRuns();

		if (runs != null) {
			TextSegement found = par.searchText(tag, new PositionInParagraph());

			if (found != null) {
				if (found.getBeginRun() == found.getEndRun()) {
					// whole search string is in one Run
					XWPFRun run = runs.get(found.getBeginRun());
					String runText = run.getText(run.getTextPosition());
					String replaced = runText.replace(tag, value);
					run.setText(replaced, 0);
					
					if (addLineBreak) {
						run.addCarriageReturn();
					}
				} else {
					// The search string spans over more than one Run
					// Put the Strings together
					StringBuilder b = new StringBuilder();

					for (int runPos = found.getBeginRun(); runPos <= found.getEndRun(); runPos++) {
						XWPFRun run = runs.get(runPos);
						b.append(run.getText(run.getTextPosition()));
					}

					String connectedRuns = b.toString();
					String replaced = connectedRuns.replace(tag, value);

					// The first Run receives the replaced String of all connected Runs
					XWPFRun partOne = runs.get(found.getBeginRun());
					partOne.setText(replaced, 0);

					if (addLineBreak) {
						partOne.addCarriageReturn();
					}
					
					// Remove the text in the other Runs.
					for (int runPos = found.getBeginRun() + 1; runPos <= found.getEndRun(); runPos++) {
						XWPFRun partNext = runs.get(runPos);
						partNext.setText("", 0);
					}
				}
			}
		}
	}

	public static void replaceText(XWPFParagraph par, String tag, String value) {
		replaceText(par, tag, value, false);
	}
	
	public static void addLineBreakToParagraph(XWPFParagraph par) {
		List<XWPFRun> runs = par.getRuns();
		
		if (runs != null) {
			runs.get(runs.size() - 1).addCarriageReturn();
		}
	}
	
	public static XWPFParagraph getParagraph(XWPFDocument doc, String tag) {
		XWPFParagraph paragraph = null;

		for (XWPFParagraph p : doc.getParagraphs()) {
			String text = p.getText();
{'fraction_non_alphanumeric': 0.1257725587144623, 'fraction_numerical': 0.004480840543881335, 'mean_word_length': 3.3384718498659516, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5531815', 'n_tokens_mistral': 2732, 'n_tokens_neox': 2214, 'n_words': 486}