commit 920327eadf276a933f0fd55bf6ea61f24e58ee5a
Author: Michal Horký <horkmi@students.zcu.cz>
Date:   Tue Mar 31 09:48:38 2020 +0200

    #7767
    
    Základní projekt, rozpracovaná gramatika.

diff --git a/demo_mh/.gitignore b/demo_mh/.gitignore
new file mode 100644
index 0000000..e10e727
--- /dev/null
+++ b/demo_mh/.gitignore
@@ -0,0 +1 @@
+/.metadata/
diff --git a/demo_mh/Deserializer/.classpath b/demo_mh/Deserializer/.classpath
new file mode 100644
index 0000000..8a18d82
--- /dev/null
+++ b/demo_mh/Deserializer/.classpath
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+	<classpathentry kind="src" path="src"/>
+	<classpathentry kind="lib" path="libs/json-simple-1.1.1.jar"/>
+	<classpathentry kind="output" path="bin"/>
+</classpath>
diff --git a/demo_mh/Deserializer/.gitignore b/demo_mh/Deserializer/.gitignore
new file mode 100644
index 0000000..ae3c172
--- /dev/null
+++ b/demo_mh/Deserializer/.gitignore
@@ -0,0 +1 @@
+/bin/
diff --git a/demo_mh/Deserializer/.project b/demo_mh/Deserializer/.project
new file mode 100644
index 0000000..3e0ac87
--- /dev/null
+++ b/demo_mh/Deserializer/.project
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>Deserializer</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+	</natures>
+</projectDescription>
diff --git a/demo_mh/Deserializer/data.out b/demo_mh/Deserializer/data.out
new file mode 100644
index 0000000..a1fa0f8
Binary files /dev/null and b/demo_mh/Deserializer/data.out differ
diff --git a/demo_mh/Deserializer/libs/Licenses b/demo_mh/Deserializer/libs/Licenses
new file mode 100644
index 0000000..3ab7778
--- /dev/null
+++ b/demo_mh/Deserializer/libs/Licenses
@@ -0,0 +1,3 @@
+JSON Simple
+	https://code.google.com/archive/p/json-simple/
+	Apache License 2.0
\ No newline at end of file
diff --git a/demo_mh/Deserializer/libs/json-simple-1.1.1.jar b/demo_mh/Deserializer/libs/json-simple-1.1.1.jar
new file mode 100644
index 0000000..66347a6
Binary files /dev/null and b/demo_mh/Deserializer/libs/json-simple-1.1.1.jar differ
diff --git a/demo_mh/Deserializer/results.json b/demo_mh/Deserializer/results.json
new file mode 100644
index 0000000..0c8bdce
--- /dev/null
+++ b/demo_mh/Deserializer/results.json
@@ -0,0 +1 @@
+{"States":["Madhya Pradesh","Maharastra","Rajasthan"],"Population":1000000,"Name":"India"}
\ No newline at end of file
diff --git a/demo_mh/Deserializer/src/deserialize/Deserializer.java b/demo_mh/Deserializer/src/deserialize/Deserializer.java
new file mode 100644
index 0000000..5955ba5
--- /dev/null
+++ b/demo_mh/Deserializer/src/deserialize/Deserializer.java
@@ -0,0 +1,61 @@
+package deserialize;
+
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+
+import general.*;
+
+public class Deserializer {
+	
+	public static void main(String[] args) {
+		try {
+			
+			// Try to deserialize classically.
+			FileInputStream fis = new FileInputStream(Constants.SERIALIZED_FILE);
+			ObjectInputStream ois = new ObjectInputStream(fis);
+			Deserializer d = (Deserializer) ois.readObject();
+			System.out.println(d.toString());
+			ois.close();
+			
+		} catch (IOException e) {
+			
+			// Stream error.
+			System.out.println("Chyba pri otevirani (uzavirani) streamu.");
+			e.printStackTrace();
+			
+		} catch (Exception e) {
+			
+			// Others: ClassNotFoundException, ClassCastException, ...
+			// Usually due to problems with classical deserialization.
+			byte buffer[];
+			try {
+				File f = new File(Constants.SERIALIZED_FILE);
+				buffer = new byte[(int) f.length()];
+				
+				BufferedInputStream fis = new BufferedInputStream(new FileInputStream(f));
+		        fis.read(buffer);
+		        fis.close();
+		        
+		        System.out.println("Doslo k chybe pri deserializaci.");
+				System.out.println("Spoustim univerzalni deserializer.");
+				
+				ToJSON thread = new ToJSON(buffer);
+				thread.start();
+				try {
+					thread.join();
+					System.out.println("JSON byl vytvoren.");
+				} catch (InterruptedException e1) {
+					e1.printStackTrace();
+				}
+				
+		    } catch (IOException e1) {
+		        e1.printStackTrace();
+		    }
+			
+		}
+	}
+	
+}
diff --git a/demo_mh/Deserializer/src/deserialize/Formatter.java b/demo_mh/Deserializer/src/deserialize/Formatter.java
new file mode 100644
index 0000000..df910f2
--- /dev/null
+++ b/demo_mh/Deserializer/src/deserialize/Formatter.java
@@ -0,0 +1,9 @@
+package deserialize;
+
+public class Formatter {
+
+	public static int createInt(byte first, byte second) {
+		return ((first & 0xFF) << 8) | (second & 0xFF);
+	}
+	
+}
diff --git a/demo_mh/Deserializer/src/deserialize/Grammar.java b/demo_mh/Deserializer/src/deserialize/Grammar.java
new file mode 100644
index 0000000..6480d77
--- /dev/null
+++ b/demo_mh/Deserializer/src/deserialize/Grammar.java
@@ -0,0 +1,187 @@
+package deserialize;
+
+import java.util.HashMap;
+
+/**
+ * https://docs.oracle.com/javase/7/docs/platform/serialization/spec/protocol.html
+ */
+public class Grammar {
+	
+	private final static short STREAM_MAGIC = (short) 0xACED;
+	private final static short STREAM_VERSION = 5;
+	private final static byte TC_NULL = 0x70;
+	private final static byte TC_REFERENCE = 0x71;
+	private final static byte TC_CLASSDESC = 0x72;
+	private final static byte TC_OBJECT = 0x73;
+	private final static byte TC_STRING = 0x74;
+	private final static byte TC_ARRAY = 0x75;
+	private final static byte TC_CLASS = 0x76;
+	private final static byte TC_BLOCKDATA = 0x77;
+	private final static byte TC_ENDBLOCKDATA = 0x78;
+	private final static byte TC_RESET = 0x79;
+	private final static byte TC_BLOCKDATALONG = 0x7A;
+	private final static byte TC_EXCEPTION = 0x7B;
+	private final static byte TC_LONGSTRING = 0x7C;
+	private final static byte TC_PROXYCLASSDESC = 0x7D;
+	private final static byte TC_ENUM = 0x7E;
+	private final static int baseWireHandle = 0x7E0000;
+	
+	final static byte SC_WRITE_METHOD = 0x01; // if SC_SERIALIZABLE
+	final static byte SC_BLOCK_DATA = 0x08; // if SC_EXTERNALIZABLE
+	final static byte SC_SERIALIZABLE = 0x02;
+	final static byte SC_EXTERNALIZABLE = 0x04;
+	final static byte SC_ENUM = 0x10;
+
+	HashMap<Character, String> primTypeCodes;
+	HashMap<Character, String> objTypeCodes;
+	
+	
+	// Note. (XXX) This token has the XXX type specified
+	
+	
+	public Grammar() {
+		primTypeCodes = new HashMap<Character, String>();
+		primTypeCodes.put('B', "byte");
+		primTypeCodes.put('C', "char");
+		primTypeCodes.put('D', "double");
+		primTypeCodes.put('F', "float");
+		primTypeCodes.put('I', "integer");
+		primTypeCodes.put('J', "long");
+		primTypeCodes.put('S', "short");
+		primTypeCodes.put('Z', "boolean");
+		objTypeCodes = new HashMap<Character, String>();
+		objTypeCodes.put('[', "array");
+		objTypeCodes.put('L', "object");
+	}
+	
+	
+	
+	
+	public void stream() {
+		short x = STREAM_MAGIC; x = STREAM_VERSION; contents();
+	}
+	
+	public void contents() {
+		content();
+		// OR
+		contents(); content();
+	}
+	
+	public void content() {
+		object();
+		// OR
+		byte x = TC_BLOCKDATA; // TODO (unsigned byte)<size> (byte)[size];
+		// OR
+		x = TC_BLOCKDATALONG; // TODO (int)<size> (byte)[size];
+	}
+	
+	public void object() {
+		byte x = TC_OBJECT; classDesc(); newHandle(); classdata()/* TODO []*/;  // data for each class
+		// OR
+		x = TC_CLASS; classDesc(); newHandle();
+		// OR
+		x = TC_ARRAY; classDesc(); newHandle(); /* TODO (int)<size>*/ values()/*[size]*/;
+		// OR
+		x = TC_STRING; newHandle(); // TODO (utf) read 16-bit integer (string length)
+		// OR
+		x = TC_LONGSTRING; newHandle(); // TODO (long-utf) read 64-bit integer (string length)
+		// OR
+		x = TC_ENUM; classDesc(); newHandle(); /* TODO (String)*/ object();
+		// OR
+		newClassDesc();
+		// OR
+		x = TC_REFERENCE; // TODO (int) handle ?!?;
+		// OR
+		x = TC_NULL;
+		// OR
+		x = TC_EXCEPTION; reset(); /* TODO (Throwable)*/object(); reset();
+		// OR
+		x = TC_RESET;
+	}
+	
+	public void classDesc() {
+		newClassDesc();
+		// OR
+		byte x = TC_NULL;
+		// OR
+		/* TODO (ClassDesc)*/ x = TC_REFERENCE; // TODO (int) handle ?!?;
+	}
+	
+	public void newClassDesc() {
+		byte x = TC_CLASSDESC; /* TODO (utf) read 16-bit integer (string length); (long)*/; newHandle(); classDescFlags(); fields(); classAnnotation(); classDesc();
+		// OR
+		x = TC_PROXYCLASSDESC; newHandle(); /* TODO (int)<count> => read int as count /(utf) read 16-bit integer (string length)/ [count]*/; classAnnotation(); classDesc();
+	}
+	
+	public void classDescFlags() {
+		// TODO (byte)
+	}
+	
+	public void fields() {
+		/* TODO (short)<count> =>  read short as count */ fieldDesc() /*[count]*/;
+	}
+	
+	public void fieldDesc() {
+		// TODO primTypeCodes.get(key); (utf) read 16-bit integer (string length);
+		// OR
+		/* TODO objTypeCodes.get(key) - array or object; (utf) read 16-bit integer (string length);*/ /*(String)*/object();		// String containing the field's type,
+																															// in field descriptor format
+	}
+	
+	public void classAnnotation() {
+		byte x = TC_ENDBLOCKDATA;
+		// OR
+		contents(); x = TC_ENDBLOCKDATA;      // contents written by annotateClass
+	}
+	
+	public void classdata() {
+		values(); // fields in order of class descriptor    // SC_SERIALIZABLE & classDescFlag &&
+															// !(SC_WRITE_METHOD & classDescFlags)
+		// OR
+		values() /* fields in order of class descriptor*/; objectAnnotation();  // SC_SERIALIZABLE & classDescFlag &&
+																				// SC_WRITE_METHOD & classDescFlags
+		// OR
+		externalContents();          // SC_EXTERNALIZABLE & classDescFlag &&
+								// !(SC_BLOCKDATA  & classDescFlags
+		// OR
+		objectAnnotation();          // SC_EXTERNALIZABLE & classDescFlag&&
+								// SC_BLOCKDATA & classDescFlags
+	}
+	
+	public void objectAnnotation() {
+		byte x = TC_ENDBLOCKDATA;
+		// OR
+		contents(); x = TC_ENDBLOCKDATA;     // contents written by writeObject
+		                            // or writeExternal PROTOCOL_VERSION_2.
+	}
+	
+	public void externalContent() { // Only parseable by readExternal
+		// TODO  ( bytes)                // primitive data
+		// OR 
+		object();
+	}
+	
+	public void externalContents() {// externalContent written by 
+		externalContent();        // writeExternal in PROTOCOL_VERSION_1.
+		// OR
+		externalContents(); externalContent();
+	}
+	
+	public void values() {
+		// The size and types are described by the
+        // classDesc for the current object
+	}
+	
+	public void newHandle() {
+		// The next number in sequence is assigned
+        // to the object being serialized or deserialized
+	}
+	
+	public void reset() {
+		// The set of known objects is discarded
+        // so the objects of the exception do not
+        // overlap with the previously sent objects 
+        // or with objects that may be sent after 
+        // the exception
+	}
+}
diff --git a/demo_mh/Deserializer/src/deserialize/ToJSON.java b/demo_mh/Deserializer/src/deserialize/ToJSON.java
new file mode 100644
index 0000000..820aca6
--- /dev/null
+++ b/demo_mh/Deserializer/src/deserialize/ToJSON.java
@@ -0,0 +1,110 @@
+package deserialize;
+
+import java.io.BufferedWriter;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.HashMap;
+
+import org.json.simple.JSONArray;
+import org.json.simple.JSONObject;
+
+import general.Constants;
+
+public class ToJSON extends Thread {
+	
+	private final static byte TC_CLASSDESC = 0x72;
+	private final static byte TC_ENDBLOCKDATA = 0x78;
+	private final static byte SC_SERIALIZABLE = 0x02;
+
+	HashMap<Character, String> typeCodes;
+	
+	private byte buffer[];
+	
+	public ToJSON(byte buffer[]) {
+		this.buffer = buffer;
+		
+		typeCodes = new HashMap<Character, String>();
+		typeCodes.put('B', "byte");
+		typeCodes.put('C', "char");
+		typeCodes.put('D', "double");
+		typeCodes.put('F', "float");
+		typeCodes.put('I', "integer");
+		typeCodes.put('J', "long");
+		typeCodes.put('S', "short");
+		typeCodes.put('Z', "boolean");
+		// ---
+		typeCodes.put('[', "array");
+		typeCodes.put('L', "object");
+	}
+	
+	@Override
+	public void run() {
+		super.run();
+		
+		for (int i = 0; i < buffer.length; i++) {
+			if (buffer[i] == TC_CLASSDESC) {
+				// Class name
+				int nameLength = Formatter.createInt(buffer[++i], buffer[++i]);
+				String name = new String(buffer, ++i, nameLength);
+				i += nameLength - 1;
+				System.out.println(name);
+				
+				// UID
+				ByteBuffer toLong = ByteBuffer.allocate(Long.BYTES);
+				toLong.put(buffer, i + 1, 8);
+				toLong.flip();
+		        System.out.println(toLong.getLong());
+		        
+		        // implements ...
+				i += 9;
+				System.out.println(buffer[i] == SC_SERIALIZABLE);
+				
+				// Variables
+				int vCount = Formatter.createInt(buffer[++i], buffer[++i]);
+				for (int j = 0; j < vCount; j++) {
+					// Type
+					char vType = (char) (buffer[++i]);
+					// Name
+					int vNameLength = Formatter.createInt(buffer[++i], buffer[++i]);
+					String vName = new String(buffer, ++i, vNameLength);
+					i += vNameLength - 1;
+					// toString()
+					System.out.println(typeCodes.get(vType) + " " + new String(vName));
+				}
+				
+				// TODO
+				System.out.println(buffer[++i] == TC_ENDBLOCKDATA);
+			}
+		}
+	}
+	
+	@SuppressWarnings("unchecked")
+	private void save() {
+		/*
+		JSONObject countryObj = new JSONObject();
+        countryObj.put("Name", "India");
+        countryObj.put("Population", new Integer(1000000));
+ 
+        JSONArray listOfStates = new JSONArray();
+        listOfStates.add("Madhya Pradesh");
+        listOfStates.add("Maharastra");
+        listOfStates.add("Rajasthan");
+ 
+        countryObj.put("States", listOfStates);
+ 
+        try {
+            // Writing to a file
+            FileWriter fw = new FileWriter(Constants.JSON_FILE);
+            BufferedWriter bw = new BufferedWriter(fw);
+            System.out.println("Zapisuji JSON objekt do souboru.");
+            bw.write(countryObj.toJSONString());
+            bw.flush();
+            bw.close();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        */
+	}
+	
+}
diff --git a/demo_mh/Deserializer/src/general/Constants.java b/demo_mh/Deserializer/src/general/Constants.java
new file mode 100644
index 0000000..cb96d0f
--- /dev/null
+++ b/demo_mh/Deserializer/src/general/Constants.java
@@ -0,0 +1,8 @@
+package general;
+
+public class Constants {
+
+	public static String SERIALIZED_FILE = "data.out";
+	public static String JSON_FILE = "results.json";
+	
+}
diff --git a/demo_mh/Deserializer/src/serialize/Contain.java b/demo_mh/Deserializer/src/serialize/Contain.java
new file mode 100644
index 0000000..72c6a31
--- /dev/null
+++ b/demo_mh/Deserializer/src/serialize/Contain.java
@@ -0,0 +1,11 @@
+package serialize;
+
+import java.io.Serializable;
+
+public class Contain implements Serializable {
+
+	private static final long serialVersionUID = 1L;
+	
+	char array[] = new char[] {'W', 'X', 'Y', 'Z'};
+	
+}
diff --git a/demo_mh/Deserializer/src/serialize/Example.java b/demo_mh/Deserializer/src/serialize/Example.java
new file mode 100644
index 0000000..bc75cec
--- /dev/null
+++ b/demo_mh/Deserializer/src/serialize/Example.java
@@ -0,0 +1,42 @@
+package serialize;
+
+import java.io.FileOutputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
+
+import general.*;
+
+public class Example extends Parent implements Serializable {
+	
+	private static final long serialVersionUID = 1L;
+	
+	private int number = 2;
+	Contain variable = new Contain();
+	
+	public long getNumber() {
+		return number;
+	}
+
+	public static void main(String[] args) {
+		try {
+			FileOutputStream fos = new FileOutputStream(Constants.SERIALIZED_FILE);
+			ObjectOutputStream oos = new ObjectOutputStream(fos);
+			Example s = new Example();
+			oos.writeObject(s);
+			oos.flush();
+			oos.close();
+			System.out.println("File " + Constants.SERIALIZED_FILE + " created.");
+			
+			/*
+			FileInputStream fis = new FileInputStream(Constants.FILE_NAME);
+			ObjectInputStream ois = new ObjectInputStream(fis);
+			s = (Example) ois.readObject();
+			System.out.println("DATA: " + s.string + " " + s.number + " " + Arrays.toString(s.variable.array));
+			ois.close();
+			*/
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+	}
+
+}
diff --git a/demo_mh/Deserializer/src/serialize/Parent.java b/demo_mh/Deserializer/src/serialize/Parent.java
new file mode 100644
index 0000000..e0cbd0f
--- /dev/null
+++ b/demo_mh/Deserializer/src/serialize/Parent.java
@@ -0,0 +1,11 @@
+package serialize;
+
+import java.io.Serializable;
+
+public class Parent implements Serializable {
+
+	private static final long serialVersionUID = 1L;
+	
+	String string = "ABCD";
+	
+}
