From 51818b3666e9aacb90a307d8aa46b0eb624654aa Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Tue, 21 Jun 2016 13:36:45 +0200 Subject: [PATCH] WIP --- pom.xml | 2 +- .../java/de/juplo/jackson/SimpleMapper.java | 282 ++++++++++++++++++ .../ImportVariablesAttrProcessor.java | 145 --------- 3 files changed, 283 insertions(+), 146 deletions(-) create mode 100644 src/main/java/de/juplo/jackson/SimpleMapper.java delete mode 100644 src/main/java/de/juplo/thymeleaf/ImportVariablesAttrProcessor.java diff --git a/pom.xml b/pom.xml index e4725e1..c687ece 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 de.juplo.jackson - simple-mapping + simple-mapper 1.0-SNAPSHOT diff --git a/src/main/java/de/juplo/jackson/SimpleMapper.java b/src/main/java/de/juplo/jackson/SimpleMapper.java new file mode 100644 index 0000000..941c4b1 --- /dev/null +++ b/src/main/java/de/juplo/jackson/SimpleMapper.java @@ -0,0 +1,282 @@ +package de.juplo.jackson; + + +import com.fasterxml.jackson.core.JsonFactory; +import com.fasterxml.jackson.core.JsonLocation; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonToken; +import java.io.IOException; +import java.io.InputStream; +import java.util.Collections; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.NoSuchElementException; +import java.util.Spliterator; +import static java.util.Spliterator.IMMUTABLE; +import java.util.Spliterators; +import java.util.function.Consumer; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + + +/** + * + * @author kai + */ +public class SimpleMapper +{ + private static final Logger LOG = + LoggerFactory.getLogger(SimpleMapper.class); + + + private JsonFactory factory = new JsonFactory(); + + + public static Stream getArrayStream(final JsonParser parser) + throws + IOException + { + return StreamSupport.stream(getArraySpliterator(parser), false); + } + + public static Spliterator getArraySpliterator(final JsonParser parser) + throws + IOException + { + JsonToken token = parser.nextToken(); + + if (token == null) + { + LOG.warn("empty input"); + return Spliterators.emptySpliterator(); + } + + if (!JsonToken.START_ARRAY.equals(token)) + { + throw new IllegalArgumentException("The root-element must be an array!"); + } + + return new Spliterator() + { + @Override + public boolean tryAdvance(Consumer action) + { + try + { + JsonToken token = parser.nextToken(); + if (token == null) + throw new IllegalArgumentException("Unexpected end of data!"); + if (JsonToken.END_ARRAY.equals(token)) + return false; + action.accept(convert(parser)); + return true; + } + catch (IOException e) + { + throw new IllegalArgumentException(e); + } + } + + @Override + public Spliterator trySplit() + { + return null; + } + + @Override + public long estimateSize() + { + return Long.MAX_VALUE; + } + + @Override + public int characteristics() + { + return IMMUTABLE; + } + }; + } + + public static Iterator getArrayIterator(final JsonParser parser) + throws + IOException + { + JsonToken token = parser.nextToken(); + + if (token == null) + { + LOG.warn("empty input"); + return Collections.emptyIterator(); + } + + if (!JsonToken.START_ARRAY.equals(token)) + { + throw new IllegalArgumentException("The root-element must be an array!"); + } + + return new Iterator() + { + + @Override + public boolean tryAdvance(Consumer action) + { + try + { + JsonToken token = parser.nextToken(); + if (token == null) + throw new IllegalArgumentException("Unexpected end of data!"); + if (JsonToken.END_ARRAY.equals(token)) + throw new NoSuchElementException(); + action.accept(convert(parser)); + return true; + } + catch (IOException e) + { + throw new IllegalArgumentException(e); + } + } + @Override + public boolean hasNext() + { + } + + @Override + public Object next() + { + try + { + JsonToken token = parser.nextToken(); + if (token == null) + throw new IllegalArgumentException("Unexpected end of data!"); + if (JsonToken.END_ARRAY.equals(token)) + return false; + action.accept(convert(parser)); + return true; + } + catch (IOException e) + { + throw new IllegalArgumentException(e); + } + } + }; + } + + public Object processAttribute(InputStream is) + { + try + { + /** + * Read the JSON and create the variables + */ + JsonParser parser = factory.createParser(is); + + JsonToken token = parser.nextToken(); + + if (token == null) + { + LOG.warn("empty input-stream"); + return null; + } + + Object result = convert(parser); + + if (parser.nextToken() != null) + fail(parser, "unexpected data after parsed variables"); + + return result; + } + catch (IOException e) + { + LOG.error("cannot parse input-stream as JSON: {}", e.getMessage()); + throw new RuntimeException(e); + } + } + + + static Object convert(JsonParser parser) throws IOException + { + JsonToken token = parser.getCurrentToken(); + if (token == null) + fail(parser, "unexpected EOF"); + + switch (token) + { + case VALUE_STRING: return parser.getText(); + case VALUE_NUMBER_INT: return parser.getIntValue(); + case VALUE_NUMBER_FLOAT: return parser.getDoubleValue(); + case START_OBJECT: return convertObject(parser); + case START_ARRAY: return convertArray(parser); + case VALUE_TRUE: return Boolean.TRUE; + case VALUE_FALSE: return Boolean.FALSE; + case VALUE_NULL: return null; + } + + fail(parser, "unexpected token " + token.toString()); + return null; // << Will never be reached, because fail always throws an exception + } + + static Map convertObject(JsonParser parser) throws IOException + { + JsonToken token = parser.nextToken(); + if (token == null) + fail(parser, "unexpected EOF"); + + Map map = new LinkedHashMap<>(); + + while (!JsonToken.END_OBJECT.equals(token)) + { + if (!JsonToken.FIELD_NAME.equals(token)) + fail(parser, "expected a field-name"); + + String name = parser.getText(); + parser.nextToken(); + Object value = convert(parser); + map.put(name, value); + + token = parser.nextToken(); + if (token == null) + fail(parser, "unexpected EOF"); + } + + return map; + } + + static List convertArray(JsonParser parser) throws IOException + { + JsonToken token = parser.nextToken(); + if (token == null) + fail(parser, "unexpected EOF"); + + List list = new LinkedList<>(); + + while (!JsonToken.END_ARRAY.equals(token)) + { + list.add(convert(parser)); + + token = parser.nextToken(); + if (token == null) + fail(parser, "unexpected EOF"); + } + + return list; + } + + static void fail(JsonParser parser, String message) + { + JsonLocation location = parser.getCurrentLocation(); + LOG.error( + "{} at char-offset {} (line {}, column {})", + message, + location.getCharOffset(), + location.getLineNr(), + location.getColumnNr() + ); + throw new RuntimeException("Cannot parse JSON: " + message); + } +} diff --git a/src/main/java/de/juplo/thymeleaf/ImportVariablesAttrProcessor.java b/src/main/java/de/juplo/thymeleaf/ImportVariablesAttrProcessor.java deleted file mode 100644 index fe2725e..0000000 --- a/src/main/java/de/juplo/thymeleaf/ImportVariablesAttrProcessor.java +++ /dev/null @@ -1,145 +0,0 @@ -package de.juplo.thymeleaf; - - -import com.fasterxml.jackson.core.JsonFactory; -import com.fasterxml.jackson.core.JsonLocation; -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.core.JsonToken; -import com.fasterxml.jackson.databind.ObjectMapper; -import java.io.IOException; -import java.io.InputStream; -import java.time.format.DateTimeFormatter; -import java.util.LinkedHashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - - - -/** - * - * @author kai - */ -public class ImportVariablesAttrProcessor -{ - private static final Logger LOG = - LoggerFactory.getLogger(ImportVariablesAttrProcessor.class); - private static final DateTimeFormatter FORMATTER = - DateTimeFormatter.ofPattern("dd.MM"); - private static final ObjectMapper MAPPER = new ObjectMapper(); - private static final JsonFactory FACTORY = new JsonFactory(); - - - public Object processAttribute(InputStream is) - { - try - { - /** Read the JSON and create the variables */ - JsonParser parser = FACTORY.createParser(is); - - JsonToken token = parser.nextToken(); - - if (token == null) - { - LOG.warn("empty input-stream"); - return null; - } - - Object result = convert(parser); - - if (parser.nextToken() != null) - fail(parser, "unexpected data after parsed variables"); - - return result; - } - catch (IOException e) - { - LOG.error("cannot parse input-stream as JSON: {}", e.getMessage()); - throw new RuntimeException(e); - } - } - - - static Object convert(JsonParser parser) throws IOException - { - JsonToken token = parser.getCurrentToken(); - if (token == null) - fail(parser, "unexpected EOF"); - - switch (token) - { - case VALUE_STRING: return parser.getText(); - case VALUE_NUMBER_INT: return parser.getIntValue(); - case VALUE_NUMBER_FLOAT: return parser.getDoubleValue(); - case START_OBJECT: return convertObject(parser); - case START_ARRAY: return convertArray(parser); - case VALUE_TRUE: return Boolean.TRUE; - case VALUE_FALSE: return Boolean.FALSE; - case VALUE_NULL: return null; - } - - fail(parser, "unexpected token " + token.toString()); - return null; // << Will never be reached, because fail always throws an exception - } - - static Map convertObject(JsonParser parser) throws IOException - { - JsonToken token = parser.nextToken(); - if (token == null) - fail(parser, "unexpected EOF"); - - Map map = new LinkedHashMap<>(); - - while (!JsonToken.END_OBJECT.equals(token)) - { - if (!JsonToken.FIELD_NAME.equals(token)) - fail(parser, "expected a field-name"); - - String name = parser.getText(); - parser.nextToken(); - Object value = convert(parser); - map.put(name, value); - - token = parser.nextToken(); - if (token == null) - fail(parser, "unexpected EOF"); - } - - return map; - } - - static List convertArray(JsonParser parser) throws IOException - { - JsonToken token = parser.nextToken(); - if (token == null) - fail(parser, "unexpected EOF"); - - List list = new LinkedList<>(); - - while (!JsonToken.END_ARRAY.equals(token)) - { - list.add(convert(parser)); - - token = parser.nextToken(); - if (token == null) - fail(parser, "unexpected EOF"); - } - - return list; - } - - static void fail(JsonParser parser, String message) - { - JsonLocation location = parser.getCurrentLocation(); - LOG.error( - "{} at char-offset {} (line {}, column {})", - message, - location.getCharOffset(), - location.getLineNr(), - location.getColumnNr() - ); - throw new RuntimeException("Cannot parse JSON: " + message); - } -} -- 2.20.1