Commit b2e1667b authored by doc. Ing. Jaroslav Porubän PhD.'s avatar doc. Ing. Jaroslav Porubän PhD.
Browse files

Verzia3

parent cbff7cca
......@@ -11,5 +11,8 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.9.7" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.9.0" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.9.7" level="project" />
</component>
</module>
\ No newline at end of file
<component name="libraryTable">
<library name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.9.0">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/com/fasterxml/jackson/core/jackson-annotations/2.9.0/jackson-annotations-2.9.0.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/com/fasterxml/jackson/core/jackson-annotations/2.9.0/jackson-annotations-2.9.0-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/com/fasterxml/jackson/core/jackson-annotations/2.9.0/jackson-annotations-2.9.0-sources.jar!/" />
</SOURCES>
</library>
</component>
\ No newline at end of file
<component name="libraryTable">
<library name="Maven: com.fasterxml.jackson.core:jackson-core:2.9.7">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/com/fasterxml/jackson/core/jackson-core/2.9.7/jackson-core-2.9.7.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/com/fasterxml/jackson/core/jackson-core/2.9.7/jackson-core-2.9.7-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/com/fasterxml/jackson/core/jackson-core/2.9.7/jackson-core-2.9.7-sources.jar!/" />
</SOURCES>
</library>
</component>
\ No newline at end of file
<component name="libraryTable">
<library name="Maven: com.fasterxml.jackson.core:jackson-databind:2.9.7">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/com/fasterxml/jackson/core/jackson-databind/2.9.7/jackson-databind-2.9.7.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/com/fasterxml/jackson/core/jackson-databind/2.9.7/jackson-databind-2.9.7-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/com/fasterxml/jackson/core/jackson-databind/2.9.7/jackson-databind-2.9.7-sources.jar!/" />
</SOURCES>
</library>
</component>
\ No newline at end of file
......@@ -17,6 +17,14 @@
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
......
package sk.tuke.dsl.testlang.main;
import sk.tuke.dsl.testlang.builder.MethodChainingBuilder;
import sk.tuke.dsl.testlang.model.Test;
import sk.tuke.dsl.testlang.semantics.PrintInterpreter;
import sk.tuke.dsl.testlang.serialization.json.JSONSerializer;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import static sk.tuke.dsl.testlang.builder.MethodChainingBuilder.test;
public class Main05JSONSerializer {
private static final String FILE = "test.json";
public static void main(String[] args) throws IOException {
test("Test z fyziky").
question("Aky symbol sa pouziva na rychlost").
answer("m").
answer("a").
correct_answer("v").
answer("F").
question("Aky symbol sa pouziva na hmotnost").
correct_answer("m").
answer("a").
answer("v").
answer("F")
;
Test test = MethodChainingBuilder.getTest();
JSONSerializer jsonSerializer = new JSONSerializer();
//save
jsonSerializer.save(new FileWriter(FILE), test);
//load
test = jsonSerializer.load(new FileReader(FILE));
test.validate();
PrintInterpreter printer = new PrintInterpreter();
printer.print(test);
}
}
package sk.tuke.dsl.testlang.main;
import sk.tuke.dsl.testlang.builder.MethodChainingBuilder;
import sk.tuke.dsl.testlang.model.Test;
import sk.tuke.dsl.testlang.semantics.PrintInterpreter;
import sk.tuke.dsl.testlang.serialization.xml.XMLSerializer;
import java.io.FileReader;
import java.io.FileWriter;
public class Main06XMLSerializer {
private static final String FILE = "test.xml";
public static void main(String[] args) throws Exception {
MethodChainingBuilder.test("Test z fyziky").
question("Aky symbol sa pouziva na rychlost").
answer("m").
answer("a").
correct_answer("v").
answer("F").
question("Aky symbol sa pouziva na hmotnost").
correct_answer("m").
answer("a").
answer("v").
answer("F")
;
Test test = MethodChainingBuilder.getTest();
XMLSerializer xmlSerializer = new XMLSerializer();
//save
xmlSerializer.save(new FileWriter(FILE), test);
//load
test = xmlSerializer.load(new FileReader(FILE));
test.validate();
PrintInterpreter printer = new PrintInterpreter();
printer.print(test);
}
}
package sk.tuke.dsl.testlang.main;
import sk.tuke.dsl.testlang.builder.MethodChainingBuilder;
import sk.tuke.dsl.testlang.model.Test;
import sk.tuke.dsl.testlang.semantics.PrintInterpreter;
import sk.tuke.dsl.testlang.serialization.java.JavaSerializer;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class Main07JavaSerializer {
private static final String FILE = "test.bin";
public static void main(String[] args) throws Exception {
MethodChainingBuilder.test("Test z fyziky").
question("Aky symbol sa pouziva na rychlost").
answer("m").
answer("a").
correct_answer("v").
answer("F").
question("Aky symbol sa pouziva na hmotnost").
correct_answer("m").
answer("a").
answer("v").
answer("F")
;
Test test = MethodChainingBuilder.getTest();
JavaSerializer javaSerializer = new JavaSerializer();
//save
javaSerializer.save(new FileOutputStream(FILE), test);
//load
test = javaSerializer.load(new FileInputStream(FILE));
test.validate();
PrintInterpreter printer = new PrintInterpreter();
printer.print(test);
}
}
package sk.tuke.dsl.testlang.model;
public class Answer {
import javax.xml.bind.annotation.XmlAttribute;
import java.io.Serializable;
public class Answer implements Serializable {
private String text;
private boolean correct;
......@@ -16,10 +19,19 @@ public class Answer {
return text;
}
public void setText(String text) {
this.text = text;
}
@XmlAttribute
public boolean isCorrect() {
return correct;
}
public void setCorrect(boolean correct) {
this.correct = correct;
}
@Override
public String toString() {
return "Answer{" +
......
package sk.tuke.dsl.testlang.model;
import javax.xml.bind.annotation.XmlElement;
import java.io.Serializable;
import java.util.Arrays;
public class Question {
public class Question implements Serializable {
private String text;
private Answer[] answers;
......@@ -18,10 +20,19 @@ public class Question {
return text;
}
public void setText(String text) {
this.text = text;
}
@XmlElement(name = "answer")
public Answer[] getAnswers() {
return answers;
}
public void setAnswers(Answer[] answers) {
this.answers = answers;
}
@Override
public String toString() {
return "Question{" +
......
package sk.tuke.dsl.testlang.model;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;
import java.util.Arrays;
public class Test {
@XmlRootElement(namespace = "http://kpi.fei.tuke.sk/testlang")
public class Test implements Serializable {
private String title;
private Question[] questions;
......@@ -19,10 +22,18 @@ public class Test {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Question[] getQuestions() {
return questions;
}
public void setQuestions(Question[] questions) {
this.questions = questions;
}
@Override
public String toString() {
return "Test{" +
......
package sk.tuke.dsl.testlang.serialization.java;
import sk.tuke.dsl.testlang.model.Test;
import java.io.*;
public class JavaSerializer {
public Test load(InputStream stream) throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(stream);
return (Test) ois.readObject();
}
public void save(OutputStream stream, Test test) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(stream);
oos.writeObject(test);
}
}
package sk.tuke.dsl.testlang.serialization.json;
import com.fasterxml.jackson.databind.ObjectMapper;
import sk.tuke.dsl.testlang.model.Test;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
public class JSONSerializer {
public Test load(Reader reader) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readValue(reader, Test.class);
}
public void save(Writer writer, Test test) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.writeValue(writer, test);
}
}
package sk.tuke.dsl.testlang.serialization.xml;
import sk.tuke.dsl.testlang.model.Test;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.Reader;
import java.io.Writer;
public class XMLSerializer {
public Test load(Reader reader) throws JAXBException {
JAXBContext jc = JAXBContext.newInstance("sk.tuke.dsl.testlang.model");
Unmarshaller unmarshaller = jc.createUnmarshaller();
return (Test) unmarshaller.unmarshal(reader);
}
public void save(Writer writer, Test test) throws JAXBException {
JAXBContext jc = JAXBContext.newInstance("sk.tuke.dsl.testlang.model");
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(test, writer);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment