// Path: src/name/admitriev/jhelper/exceptions/JHelperException.java
// public class JHelperException extends RuntimeException {
// 
// 	public JHelperException(String message, Throwable cause) {
// 		super(message, cause);
// 	}
// 
// 	public JHelperException(String message) {
// 		super(message);
// 	}
// 
// 	@Override
// 	public String getMessage() {
// 		return "Please, report that bug to issue tracker on https://github.com/AlexeyDmitriev/JHelper . Attach your code and this stack trace. "
// 		       + super.getMessage();
// 	}
// }
// 
// Path: src/name/admitriev/jhelper/exceptions/NotificationException.java
// public class NotificationException extends RuntimeException {
// 	private final String title;
// 	private final String content;
// 
// 	public NotificationException(String content) {
// 		this(content, (Throwable) null);
// 	}
// 
// 	public NotificationException(String content, Throwable cause) {
// 		super(content, cause);
// 		title = "";
// 		this.content = content;
// 	}
// 
// 	public NotificationException(String title, String content) {
// 		this(title, content, null);
// 	}
// 
// 	public NotificationException(String title, String content, Throwable cause) {
// 		super(title + ": " + content, cause);
// 		this.title = title;
// 		this.content = content;
// 	}
// 
// 	public String getTitle() {
// 		return title;
// 	}
// 
// 	public String getContent() {
// 		return content;
// 	}
// }


import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import name.admitriev.jhelper.exceptions.JHelperException;
import name.admitriev.jhelper.exceptions.NotificationException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
package name.admitriev.jhelper.generation;



/**
 * Utility class for customizing templates of code
 */
public class TemplatesUtils {

	public static final Pattern CLASS_NAME = Pattern.compile("%ClassName%", Pattern.LITERAL);
	public static final Pattern TASK_FILE = Pattern.compile("%TaskFile%", Pattern.LITERAL);
	public static final Pattern TESTS = Pattern.compile("%Tests%", Pattern.LITERAL);
	public static final Pattern SOLVER_CALL = Pattern.compile("%SolverCall%", Pattern.LITERAL);
	public static final Pattern INPUT = Pattern.compile("%Input%", Pattern.LITERAL);
	public static final Pattern OUTPUT = Pattern.compile("%Output%", Pattern.LITERAL);
	public static final Pattern CODE = Pattern.compile("%Code%", Pattern.LITERAL);

	private TemplatesUtils() {
	}

	public static String replaceAll(String text, Pattern pattern, String replacement) {
		return pattern.matcher(text).replaceAll(Matcher.quoteReplacement(replacement));
	}

	public static String getTemplate(Project project, String name) {
		String filename = name + ".template";
		VirtualFile file = project.getBaseDir().findFileByRelativePath(filename);
		if (file == null) {
			createTemplateFromDefault(project, name);
			file = project.getBaseDir().findFileByRelativePath(filename);
			if (file == null) {
				throw new JHelperException("Can't open template file(" + filename + ") after its creation");
			}

		}
		Document document = FileDocumentManager.getInstance().getDocument(file);
		if (document == null) {
// Path: src/name/admitriev/jhelper/exceptions/JHelperException.java
// public class JHelperException extends RuntimeException {
// 
// 	public JHelperException(String message, Throwable cause) {
// 		super(message, cause);
// 	}
// 
// 	public JHelperException(String message) {
// 		super(message);
// 	}
// 
// 	@Override
// 	public String getMessage() {
// 		return "Please, report that bug to issue tracker on https://github.com/AlexeyDmitriev/JHelper . Attach your code and this stack trace. "
// 		       + super.getMessage();
// 	}
// }
// 
// Path: src/name/admitriev/jhelper/exceptions/NotificationException.java
// public class NotificationException extends RuntimeException {
// 	private final String title;
// 	private final String content;
// 
// 	public NotificationException(String content) {
// 		this(content, (Throwable) null);
// 	}
// 
// 	public NotificationException(String content, Throwable cause) {
// 		super(content, cause);
// 		title = "";
// 		this.content = content;
// 	}
// 
// 	public NotificationException(String title, String content) {
// 		this(title, content, null);
// 	}
// 
// 	public NotificationException(String title, String content, Throwable cause) {
// 		super(title + ": " + content, cause);
// 		this.title = title;
// 		this.content = content;
// 	}
// 
// 	public String getTitle() {
// 		return title;
// 	}
// 
// 	public String getContent() {
// 		return content;
// 	}
// }


// Path: src/name/admitriev/jhelper/generation/TemplatesUtils.java
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import name.admitriev.jhelper.exceptions.JHelperException;
import name.admitriev.jhelper.exceptions.NotificationException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

package name.admitriev.jhelper.generation;



/**
 * Utility class for customizing templates of code
 */
public class TemplatesUtils {

	public static final Pattern CLASS_NAME = Pattern.compile("%ClassName%", Pattern.LITERAL);
	public static final Pattern TASK_FILE = Pattern.compile("%TaskFile%", Pattern.LITERAL);
	public static final Pattern TESTS = Pattern.compile("%Tests%", Pattern.LITERAL);
	public static final Pattern SOLVER_CALL = Pattern.compile("%SolverCall%", Pattern.LITERAL);
	public static final Pattern INPUT = Pattern.compile("%Input%", Pattern.LITERAL);
	public static final Pattern OUTPUT = Pattern.compile("%Output%", Pattern.LITERAL);
	public static final Pattern CODE = Pattern.compile("%Code%", Pattern.LITERAL);

	private TemplatesUtils() {
	}

	public static String replaceAll(String text, Pattern pattern, String replacement) {
		return pattern.matcher(text).replaceAll(Matcher.quoteReplacement(replacement));
	}

	public static String getTemplate(Project project, String name) {
		String filename = name + ".template";
		VirtualFile file = project.getBaseDir().findFileByRelativePath(filename);
		if (file == null) {
			createTemplateFromDefault(project, name);
			file = project.getBaseDir().findFileByRelativePath(filename);
			if (file == null) {
				throw new JHelperException("Can't open template file(" + filename + ") after its creation");
			}

		}
		Document document = FileDocumentManager.getInstance().getDocument(file);
		if (document == null) {
			throw new NotificationException("Couldn't find template \"" + name + '"');