package net.aeten.core.parsing.test;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.Queue;

import net.aeten.core.event.Handler;
import net.aeten.core.parsing.MarkupNode;
import net.aeten.core.parsing.Parser;
import net.aeten.core.parsing.ParsingData;
import net.aeten.core.parsing.ParsingException;
import net.aeten.core.parsing.yaml.YamlParser;

public class ParsingTest {
	public static class Yaml {
		public static void main(String[] args)
				throws Exception {
			ParsingTest.test (new YamlParser (), "test.yaml");
		}
	}

	public static <T extends Enum<?>> void test(Parser<MarkupNode> parser,
			String resource)
			throws FileNotFoundException,
				ParsingException {
		resource = ParsingTest.class.getPackage ().getName ().replace ('.', '/') + "/" + resource;
		String file = ParsingTest.class.getClassLoader ().getResource (resource).getFile ();
		final Queue<String> currentTag = Collections.asLifoQueue (new ArrayDeque<String> ());
		parser.parse (new BufferedReader (new FileReader (file)), new Handler<ParsingData<MarkupNode>> () {
			private int level = 0;
			boolean tagKeyAppend = false;
			boolean parentIsTag = false;
			boolean parentIsMapOrList = false;

			@Override
			public void handleEvent(ParsingData<MarkupNode> data) {
				switch (data.getEvent ()) {
				case START_NODE:
					switch (data.getNodeType ()) {
					case TEXT:
						System.out.print (" \"" + data.getValue ());
						if (parentIsTag && !tagKeyAppend) {
							tagKeyAppend = true;
							currentTag.add (data.getValue ());
							level++;
						}
						break;
					case ANCHOR:
						break;
					case REFERENCE:
						break;
					case TYPE:
						if (parentIsTag && !tagKeyAppend) {
							System.out.print ("!" + data.getValue ());
						} else {
							this.print ("!" + data.getValue ());
							if (parentIsMapOrList && !tagKeyAppend) {
								System.out.println ();
							}
						}
						break;
					case MAP:
						parentIsMapOrList = true;
						System.out.println ();
						this.println ("{");
						level++;
						break;
					case LIST:
						parentIsMapOrList = true;
						System.out.println ();
						this.println ("[");
						level++;
						break;
					case TAG:
						tagKeyAppend = false;
						parentIsTag = true;
						this.print ("« ");
						break;
					case DOCUMENT:
						this.println ("---");
						break;
					default:
						break;
					}
					break;
				case END_NODE:
					switch (data.getNodeType ()) {
					case TEXT:
						System.out.println ("\"");
						break;
					case ANCHOR:
						break;
					case REFERENCE:
						break;
					case TYPE:
						break;
					case MAP:
						level--;
						this.println ("}");
						parentIsMapOrList = false;
						if (parentIsTag && !tagKeyAppend) {
							tagKeyAppend = true;
							System.out.print (": ");
						}
						break;
					case LIST:
						level--;
						this.println ("]");
						parentIsMapOrList = false;
						if (parentIsTag && !tagKeyAppend) {
							tagKeyAppend = true;
							System.out.print (": ");
						}
						break;
					case TAG:
						level--;
						this.println (currentTag.poll () + " »");
						parentIsTag = false;
						break;
					case DOCUMENT:
						this.println ("...");
						break;
					default:
						break;
					}
					break;
				}
			}

			private void print(String text) {
				for (int i = 0; i < level; i++) {
					System.out.print ('\t');
				}
				System.out.print (text);
			}

			private void println(String text) {
				this.print (text + '\n');
			}
		});
	}

}

@@ -115,20 +115,55 @@
 java.io.
-File
+InputStream;%0Aimport java.io.InputStream
 Reader;%0A
@@ -926,22 +926,27 @@
 urce;%0A%09%09
-String
+InputStream
  file = 
@@ -992,16 +992,24 @@
 Resource
+AsStream
  (resour
@@ -1015,19 +1015,8 @@
 rce)
-.getFile ()
 ;%0A%09%09
@@ -1141,20 +1141,27 @@
 er (new 
-File
+InputStream
 Reader (
@@ -1235,24 +1235,32 @@
 vel = 0;%0A%09%09%09
+private 
 boolean tagK
@@ -1272,32 +1272,40 @@
 end = false;%0A%09%09%09
+private 
 boolean parentIs
@@ -1320,16 +1320,24 @@
 lse;%0A%09%09%09
+private 
 boolean 
@@ -1362,16 +1362,44 @@
 = false;
+%0A%09%09%09private long start, end;
 %0A%0A%09%09%09@Ov
@@ -2523,24 +2523,66 @@
 ln (%22---%22);%0A
+%09%09%09%09%09%09start = System.currentTimeMillis();%0A
 %09%09%09%09%09%09break;
@@ -3412,26 +3412,108 @@
 %09%09%09%09
-this.println (%22...
+end = System.currentTimeMillis();%0A%09%09%09%09%09%09this.println (%22...%22);%0A%09%09%09%09%09%09this.println ((end-start) + %22 ms
 %22);%0A
@@ -3812,11 +3812,10 @@
 %09%7D);%0A%09%7D%0A
-%0A
 %7D%0A

Fix resource loading and add process time computation