<commit_before>package com.temporaryteam.noticeditor.model;

import com.temporaryteam.noticeditor.io.IOUtil;
import java.util.ArrayList;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.scene.control.TreeItem;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.pegdown.PegDownProcessor;

/**
 * Model representation of notice. Contains notice data or branch data
 *
 * @author naik, setser, annimon, kalter
 */
public class NoticeTreeItem extends TreeItem<String> {

	public static final String KEY_TITLE = "title";
	public static final String KEY_CONTENT = "content";
	public static final String KEY_CHILDREN = "childs";

	public static final int STATUS_NORMAL = 1;
	public static final int STATUS_IMPORTANT = 2;

	private String title;
	private ObservableList<TreeItem<String>> childs;
	private String content;
	private int status = STATUS_NORMAL;

	/**
	 * Create branch node on tree.
	 *
	 * @param title
	 */
	public NoticeTreeItem(String title) {
		super(title);
		this.title = title;
		childs = getChildren();
	}

	/**
	 * Create leaf node on tree.
	 *
	 * @param title
	 * @param content
	 */
	public NoticeTreeItem(String title, String content) {
		this(title);
		this.content = content;
	}

	public NoticeTreeItem(JSONObject json) throws JSONException {
		title = json.getString(KEY_TITLE);
		if (json.has(KEY_CONTENT)) {
			content = json.getString(KEY_CONTENT);
		}
		JSONArray arr = json.getJSONArray(KEY_CHILDREN);
		childs = getChildren();
		for (int i = 0; i < arr.length(); i++) {
			childs.add(new NoticeTreeItem(arr.getJSONObject(i)));
		}
		setValue(title);
	}
	
	public void addChild(NoticeTreeItem item) {
		childs.add(item);
	}

	@Override
	public boolean isLeaf() {
		return content != null;
	}

	/**
	 * @return true if content == null
	 */
	public boolean isBranch() {
		return content == null;
	}

	/**
	 * @return notice content or null if its a branch
	 */
	public String getContent() {
		return content;
	}

	/**
	 * Content will be changed only when is a leaf node.
	 *
	 * @param content
	 */
	public void changeContent(String content) {
		if (isLeaf()) {
			this.content = content;
		}
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		setValue(title);
		this.title = title;
	}

	public void setStatus(int status) {
		this.status = status;
		Event.fireEvent(this, new TreeModificationEvent(childrenModificationEvent(), this));
	}

	public int getStatus() {
		return status;
	}

	public void toHTML(PegDownProcessor processor, Document doc, String filename) {
		doc.title(title);
		doc.select("#notice_title").first().text(title);
		Element data = doc.select("#content").first();
		if (isBranch()) {
			Element list = doc.createElement("div").addClass("list-group");
			for (TreeItem<String> treeItem : childs) {
				NoticeTreeItem child = (NoticeTreeItem) treeItem;
				Element item = doc.createElement("div").addClass("list-group-item");
				if (child.isBranch()) {
					item.appendElement("span").addClass("glyphicon glyphicon-folder-open");
				} else {
					switch (child.getStatus()) {
						case STATUS_IMPORTANT:
							item.appendElement("span").addClass("glyphicon glyphicon-pushpin important");
							break;
						default:
							item.appendElement("span").addClass("glyphicon glyphicon-pushpin normal");
					}
				}
				item.appendElement("a").attr("href", filename)
						.text(child.getTitle())
						.appendElement("br");
				list.appendChild(item);
			}
			data.appendChild(list);
		} else {
			data.html(processor.markdownToHtml(content));
		}
	}

	public JSONObject toJson() throws JSONException {
		JSONObject json = new JSONObject();
		json.put(KEY_TITLE, title);
		if (content != null) {
			json.put(KEY_CONTENT, content);
		}
		ArrayList list = new ArrayList();
		for (TreeItem<String> treeItem : childs) {
			NoticeTreeItem child = (NoticeTreeItem) treeItem;
			list.add(child.toJson());
		}
		json.put(KEY_CHILDREN, new JSONArray(list));
		return json;
	}

}<commit_msg>Clean up code in tree item<commit_after>package com.temporaryteam.noticeditor.model;

import java.util.ArrayList;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.scene.control.TreeItem;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.pegdown.PegDownProcessor;

/**
 * Model representation of notice. Contains notice data or branch data
 *
 * @author naik, setser, annimon, kalter
 */
public class NoticeTreeItem extends TreeItem<String> {

	public static final String KEY_TITLE = "title";
	public static final String KEY_CONTENT = "content";
	public static final String KEY_CHILDREN = "childs";

	public static final int STATUS_NORMAL = 1;
	public static final int STATUS_IMPORTANT = 2;

	private String title;
	private ObservableList<TreeItem<String>> childs;
	private String content;
	private int status;

	/**
	 * Create branch node on tree.
	 *
	 * @param title
	 */
	public NoticeTreeItem(String title) {
		this(title, null);
	}

	/**
	 * Create leaf node on tree.
	 *
	 * @param title
	 * @param content
	 */
	public NoticeTreeItem(String title, String content) {
		super(title);
		this.title = title;
		this.content = content;
		childs = getChildren();
		status = STATUS_NORMAL;
	}

	public NoticeTreeItem(JSONObject json) throws JSONException {
		this(json.getString(KEY_TITLE), json.optString(KEY_CONTENT, null));
		JSONArray arr = json.getJSONArray(KEY_CHILDREN);
		for (int i = 0; i < arr.length(); i++) {
			childs.add(new NoticeTreeItem(arr.getJSONObject(i)));
		}
	}
	
	public void addChild(NoticeTreeItem item) {
		childs.add(item);
	}

	@Override
	public boolean isLeaf() {
		return content != null;
	}

	/**
	 * @return true if content == null
	 */
	public boolean isBranch() {
		return content == null;
	}

	/**
	 * @return notice content or null if its a branch
	 */
	public String getContent() {
		return content;
	}

	/**
	 * Content will be changed only when is a leaf node.
	 *
	 * @param content
	 */
	public void changeContent(String content) {
		if (isLeaf()) {
			this.content = content;
		}
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		setValue(title);
		this.title = title;
	}

	public void setStatus(int status) {
		this.status = status;
		Event.fireEvent(this, new TreeModificationEvent(childrenModificationEvent(), this));
	}

	public int getStatus() {
		return status;
	}

	public void toHTML(PegDownProcessor processor, Document doc, String filename) {
		doc.title(title);
		doc.select("#notice_title").first().text(title);
		Element data = doc.select("#content").first();
		if (isBranch()) {
			Element list = doc.createElement("div").addClass("list-group");
			for (TreeItem<String> treeItem : childs) {
				NoticeTreeItem child = (NoticeTreeItem) treeItem;
				Element item = doc.createElement("div").addClass("list-group-item");
				if (child.isBranch()) {
					item.appendElement("span").addClass("glyphicon glyphicon-folder-open");
				} else {
					switch (child.getStatus()) {
						case STATUS_IMPORTANT:
							item.appendElement("span").addClass("glyphicon glyphicon-pushpin important");
							break;
						default:
							item.appendElement("span").addClass("glyphicon glyphicon-pushpin normal");
					}
				}
				item.appendElement("a").attr("href", filename)
						.text(child.getTitle())
						.appendElement("br");
				list.appendChild(item);
			}
			data.appendChild(list);
		} else {
			data.html(processor.markdownToHtml(content));
		}
	}

	public JSONObject toJson() throws JSONException {
		JSONObject json = new JSONObject();
		json.put(KEY_TITLE, title);
		json.putOpt(KEY_CONTENT, content);
		ArrayList list = new ArrayList();
		for (TreeItem<String> treeItem : childs) {
			NoticeTreeItem child = (NoticeTreeItem) treeItem;
			list.add(child.toJson());
		}
		json.put(KEY_CHILDREN, new JSONArray(list));
		return json;
	}

}
{'fraction_non_alphanumeric': 0.1260037059913527, 'fraction_numerical': 0.0007411982705373688, 'mean_word_length': 3.979089790897909, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '978846', 'n_tokens_mistral': 2882, 'n_tokens_neox': 2529, 'n_words': 630}