<commit_before>/**
 * Copyright (c) 2010-2012, <NAME> and Elexis
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    <NAME> - initial implementation
 */

package ch.medelexis.templator.ui;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.swt.widgets.Composite;
import org.jdom.Element;
import org.jdom.JDOMException;

import ch.elexis.core.data.interfaces.text.ReplaceCallback;
import ch.elexis.core.ui.text.ITextPlugin;
import ch.elexis.core.ui.text.MimeTypeUtil;
import ch.elexis.core.ui.util.SWTHelper;
import ch.elexis.core.ui.views.textsystem.TextTemplatePrintSettings;
import ch.medelexis.templator.model.ProcessingSchema;
import ch.rgw.tools.ExHandler;

public class TextPluginImpl implements ITextPlugin {
	ProcessingSchemaDisplay schemaDisplay;
	ProcessingSchema schema;
	ICallback saveHandler;
	
	private TextTemplatePrintSettings printSettings;
	
	@Override
	public boolean clear(){
		return createEmptyDocument();
	}
	
	@Override
	public boolean createEmptyDocument(){
		schema = new ProcessingSchema();
		schemaDisplay.set(schema);
		return true;
	}
	
	@Override
	public void dispose(){
		if (schemaDisplay != null && !schemaDisplay.isDisposed()) {
			schemaDisplay.dispose();
		}
		schema = null;
	}
	
	@Override
	public boolean findOrReplace(String pattern, ReplaceCallback cb){
		boolean bHit = false;
		Pattern pat = Pattern.compile(pattern);
		for (Element field : schema.getFields()) {
			StringBuffer sb = new StringBuffer();
			Matcher matcher = pat.matcher(field.getText());
			while (matcher.find()) {
				String found = matcher.group();
				bHit = true;
				matcher.appendReplacement(sb, (String) cb.replace(found));
			}
			matcher.appendTail(sb);
			field.setText(sb.toString());
		}
		schemaDisplay.set(schema);
		return bHit;
	}
	
	@Override
	public PageFormat getFormat(){
		return PageFormat.A4;
	}
	
	@Override
	public String getMimeType(){
		return MimeTypeUtil.MIME_TYPE_TEMPLATOR;
	}
	
	@Override
	public boolean insertTable(String place, int properties, String[][] contents, int[] columnSizes){
		StringBuffer sbu = new StringBuffer();
		for (int z = 0; z < contents.length; z++) {
			for (int s = 0; s < contents[z].length; s++) {
				sbu.append(contents[z][s]).append("\t");
			}
			sbu.append("\n");
		}
		String repl = sbu.toString();<commit_msg>[11639] escape replacement string on templator text findOrReplace<commit_after>/**
 * Copyright (c) 2010-2012, <NAME> and Elexis
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    <NAME> - initial implementation
 */

package ch.medelexis.templator.ui;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.swt.widgets.Composite;
import org.jdom.Element;
import org.jdom.JDOMException;

import ch.elexis.core.data.interfaces.text.ReplaceCallback;
import ch.elexis.core.ui.text.ITextPlugin;
import ch.elexis.core.ui.text.MimeTypeUtil;
import ch.elexis.core.ui.util.SWTHelper;
import ch.elexis.core.ui.views.textsystem.TextTemplatePrintSettings;
import ch.medelexis.templator.model.ProcessingSchema;
import ch.rgw.tools.ExHandler;

public class TextPluginImpl implements ITextPlugin {
	ProcessingSchemaDisplay schemaDisplay;
	ProcessingSchema schema;
	ICallback saveHandler;
	
	private TextTemplatePrintSettings printSettings;
	
	@Override
	public boolean clear(){
		return createEmptyDocument();
	}
	
	@Override
	public boolean createEmptyDocument(){
		schema = new ProcessingSchema();
		schemaDisplay.set(schema);
		return true;
	}
	
	@Override
	public void dispose(){
		if (schemaDisplay != null && !schemaDisplay.isDisposed()) {
			schemaDisplay.dispose();
		}
		schema = null;
	}
	
	@Override
	public boolean findOrReplace(String pattern, ReplaceCallback cb){
		boolean bHit = false;
		Pattern pat = Pattern.compile(pattern);
		for (Element field : schema.getFields()) {
			StringBuffer sb = new StringBuffer();
			Matcher matcher = pat.matcher(field.getText());
			while (matcher.find()) {
				String found = matcher.group();
				bHit = true;
				matcher.appendReplacement(sb, Matcher.quoteReplacement((String) cb.replace(found)));
			}
			matcher.appendTail(sb);
			field.setText(sb.toString());
		}
		schemaDisplay.set(schema);
		return bHit;
	}
	
	@Override
	public PageFormat getFormat(){
		return PageFormat.A4;
	}
	
	@Override
	public String getMimeType(){
		return MimeTypeUtil.MIME_TYPE_TEMPLATOR;
	}
	
	@Override
	public boolean insertTable(String place, int properties, String[][] contents, int[] columnSizes){
		StringBuffer sbu = new StringBuffer();
		for (int z = 0; z < contents.length; z++) {
			for (int s = 0; s < contents[z].length; s++) {
				sbu.append(contents[z][s]).append("\t");
			}
			sbu.append("\n");
		}
		String repl = sbu.toString();
{'fraction_non_alphanumeric': 0.11719858156028369, 'fraction_numerical': 0.0062056737588652485, 'mean_word_length': 4.646646646646647, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, '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': '5110875', 'n_tokens_mistral': 1895, 'n_tokens_neox': 1743, 'n_words': 416}