All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.fife.ui.rsyntaxtextarea.folding.JsonFoldParser Maven / Gradle / Ivy

The newest version!
/*
 * 12/23/2012
 *
 * JsonFoldParser.java - Fold parser for JSON.
 * 
 * This library is distributed under a modified BSD license.  See the included
 * RSyntaxTextArea.License.txt file for details.
 */
package org.fife.ui.rsyntaxtextarea.folding;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import javax.swing.text.BadLocationException;

import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.Token;
import org.fife.ui.rsyntaxtextarea.TokenTypes;


/**
 * The fold parser for JSON.  Objects ("{ ... }") and arrays
 * ("[ ... ]") that span multiple lines are considered fold
 * regions.
 *
 * @author Robert Futrell
 * @version 1.0
 */
public class JsonFoldParser implements FoldParser {

	private static final Object OBJECT_BLOCK = new Object();
	private static final Object ARRAY_BLOCK = new Object();


	/**
	 * {@inheritDoc}
	 */
	public List getFolds(RSyntaxTextArea textArea) {

		Stack blocks = new Stack();
		List folds = new ArrayList();

		Fold currentFold = null;
		int lineCount = textArea.getLineCount();

		try {

			for (int line=0; linetrue.  Otherwise, return false.
	 *
	 * @param stack The stack.
	 * @param value The value to check for.
	 * @return Whether the value was found on top of the stack.
	 */
	private static final boolean popOffTop(Stack stack, Object value) {
		if (stack.size()>0 && stack.peek()==value) {
			stack.pop();
			return true;
		}
		return false;
	}


}