code complete for yaml transformation
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright (c) DuxTechnica
|
||||
*
|
||||
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||
* contacting:
|
||||
*
|
||||
* Dux Technica, [email protected]
|
||||
*
|
||||
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package dxt.moe.codec.yaml;
|
||||
|
||||
import dxt.moe.codec.MoeCodec;
|
||||
import dxt.moe.codec.MoeDecoder;
|
||||
import dxt.moe.codec.MoeEncoder;
|
||||
|
||||
/**
|
||||
* YamlCodec is an implementation of the MoeCodec interface for YAML format.
|
||||
*
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
public class YamlCodec implements MoeCodec {
|
||||
|
||||
@Override
|
||||
public String getFormat() {
|
||||
return "yaml";
|
||||
}
|
||||
|
||||
@Override
|
||||
public MoeDecoder decoder() {
|
||||
return new YamlDecoder();
|
||||
}
|
||||
|
||||
@Override
|
||||
public MoeEncoder encoder() {
|
||||
return new YamlEncoder();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
/*
|
||||
* Copyright (c) DuxTechnica
|
||||
*
|
||||
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||
* contacting:
|
||||
*
|
||||
* Dux Technica, [email protected]
|
||||
*
|
||||
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package dxt.moe.codec.yaml;
|
||||
|
||||
import dxt.moe.app.CodecException;
|
||||
import dxt.moe.app.Moe;
|
||||
import dxt.moe.codec.MoeDecoder;
|
||||
import dxt.moe.model.AnyData;
|
||||
import dxt.moe.model.LinearData;
|
||||
import dxt.moe.model.MappedData;
|
||||
import dxt.moe.model.SimpleData;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
import org.yaml.snakeyaml.nodes.MappingNode;
|
||||
import org.yaml.snakeyaml.nodes.Node;
|
||||
import org.yaml.snakeyaml.nodes.NodeTuple;
|
||||
import org.yaml.snakeyaml.nodes.ScalarNode;
|
||||
import org.yaml.snakeyaml.nodes.SequenceNode;
|
||||
import org.yaml.snakeyaml.nodes.Tag;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
public class YamlDecoder implements MoeDecoder {
|
||||
|
||||
private static final Logger L = Logger.getLogger(YamlDecoder.class.getName());
|
||||
|
||||
private Reader reader;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Methods of interface MoeDecoder
|
||||
|
||||
@Override
|
||||
public void setSource(byte[] data) throws CodecException {
|
||||
reader = new InputStreamReader(new ByteArrayInputStream(data));
|
||||
L.fine(() -> "byte array set as source");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSource(InputStream input) throws CodecException {
|
||||
reader = new InputStreamReader(input);
|
||||
L.fine(() -> "input stream set as source");
|
||||
}
|
||||
|
||||
@Override
|
||||
public AnyData decode(List<Moe.Options> options) throws CodecException {
|
||||
L.fine(() -> "converting from yaml");
|
||||
Yaml yaml = new Yaml();
|
||||
Node node = yaml.compose(reader);
|
||||
if(node == null) {
|
||||
return null;
|
||||
}
|
||||
if(node instanceof MappingNode) {
|
||||
return new AnyData(convertMappedData((MappingNode) node, options));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Helper methods
|
||||
|
||||
private MappedData convertMappedData(MappingNode mapNode,
|
||||
List<Moe.Options> options) throws CodecException {
|
||||
MappedData mapData = new MappedData();
|
||||
for(NodeTuple tuple : mapNode.getValue()) {
|
||||
ScalarNode snode = (ScalarNode) tuple.getKeyNode();
|
||||
String keyStr = options.contains(Moe.Options.ANYCASE)
|
||||
? snode.getValue().toLowerCase(Locale.getDefault())
|
||||
: snode.getValue();
|
||||
|
||||
Node valueNode = tuple.getValueNode();
|
||||
if(valueNode instanceof ScalarNode svnode) {
|
||||
String valueStr = svnode.getValue();
|
||||
|
||||
if(svnode.getTag().equals(Tag.BOOL)) {
|
||||
SimpleData itemData = new SimpleData(Boolean.valueOf(valueStr));
|
||||
mapData.put(keyStr, new AnyData(itemData));
|
||||
}
|
||||
else if(svnode.getTag().equals(Tag.FLOAT)) {
|
||||
try {
|
||||
SimpleData itemData = new SimpleData(Double.valueOf(valueStr));
|
||||
mapData.put(keyStr, new AnyData(itemData));
|
||||
}
|
||||
catch(Exception exep) {
|
||||
throw new CodecException("yaml",
|
||||
"Error converting float value: " + valueStr, exep);
|
||||
}
|
||||
}
|
||||
else if(svnode.getTag().equals(Tag.INT)) {
|
||||
try {
|
||||
SimpleData itemData = new SimpleData(Long.valueOf(valueStr));
|
||||
mapData.put(keyStr, new AnyData(itemData));
|
||||
}
|
||||
catch(Exception exep) {
|
||||
throw new CodecException("yaml",
|
||||
"Error converting int value: " + valueStr, exep);
|
||||
}
|
||||
}
|
||||
else if(svnode.getTag().equals(Tag.STR)) {
|
||||
SimpleData itemData = new SimpleData(valueStr);
|
||||
mapData.put(keyStr, new AnyData(itemData));
|
||||
}
|
||||
else {
|
||||
L.info(() -> "unhandled tag in scalar node: " + svnode.getTag().getValue());
|
||||
}
|
||||
}
|
||||
else if(valueNode instanceof SequenceNode) {
|
||||
LinearData itemData = convertLinearData((SequenceNode) valueNode, options);
|
||||
mapData.put(keyStr, new AnyData(itemData));
|
||||
}
|
||||
else if(valueNode instanceof MappingNode) {
|
||||
MappedData itemData = convertMappedData((MappingNode) valueNode, options);
|
||||
mapData.put(keyStr, new AnyData(itemData));
|
||||
}
|
||||
else {
|
||||
L.warning(() -> String.format("no suitable conversion found for node: %s", keyStr));
|
||||
}
|
||||
}
|
||||
return mapData;
|
||||
}
|
||||
|
||||
private LinearData convertLinearData(SequenceNode seqNode,
|
||||
List<Moe.Options> options) throws CodecException {
|
||||
LinearData linData = new LinearData();
|
||||
for (Node valueNode : seqNode.getValue()) {
|
||||
if (valueNode instanceof ScalarNode) {
|
||||
ScalarNode svnode = (ScalarNode) valueNode;
|
||||
String valueStr = svnode.getValue();
|
||||
|
||||
if(svnode.getTag().equals(Tag.BOOL)) {
|
||||
SimpleData itemData = new SimpleData(Boolean.valueOf(valueStr));
|
||||
linData.add(new AnyData(itemData));
|
||||
}
|
||||
else if(svnode.getTag().equals(Tag.FLOAT)) {
|
||||
try {
|
||||
SimpleData itemData = new SimpleData(Double.valueOf(valueStr));
|
||||
linData.add(new AnyData(itemData));
|
||||
}
|
||||
catch(Exception exep) {
|
||||
throw new CodecException("yaml",
|
||||
"Error converting float value: " + valueStr, exep);
|
||||
}
|
||||
}
|
||||
else if(svnode.getTag().equals(Tag.INT)) {
|
||||
try {
|
||||
SimpleData itemData = new SimpleData(Long.valueOf(valueStr));
|
||||
linData.add(new AnyData(itemData));
|
||||
}
|
||||
catch(Exception exep) {
|
||||
throw new CodecException("yaml",
|
||||
"Error converting int value: " + valueStr, exep);
|
||||
}
|
||||
}
|
||||
else if(svnode.getTag().equals(Tag.STR)) {
|
||||
SimpleData itemData = new SimpleData(valueStr);
|
||||
linData.add(new AnyData(itemData));
|
||||
}
|
||||
else {
|
||||
L.info(() -> "unhandled tag in scalar node: " + svnode.getTag().getValue());
|
||||
}
|
||||
}
|
||||
else if (valueNode instanceof SequenceNode) {
|
||||
LinearData itemData = convertLinearData((SequenceNode) valueNode, options);
|
||||
linData.add(new AnyData(itemData));
|
||||
}
|
||||
else if (valueNode instanceof MappingNode) {
|
||||
MappedData itemData = convertMappedData((MappingNode) valueNode, options);
|
||||
linData.add(new AnyData(itemData));
|
||||
}
|
||||
else {
|
||||
L.warning(() -> "no suitable conversion found for node: " + valueNode.toString());
|
||||
}
|
||||
}
|
||||
return linData;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
/*
|
||||
* Copyright (c) DuxTechnica
|
||||
*
|
||||
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||
* contacting:
|
||||
*
|
||||
* Dux Technica, [email protected]
|
||||
*
|
||||
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package dxt.moe.codec.yaml;
|
||||
|
||||
import dxt.moe.app.CodecException;
|
||||
import dxt.moe.codec.MoeEncoder;
|
||||
import dxt.moe.model.AnyData;
|
||||
import dxt.moe.model.LinearData;
|
||||
import dxt.moe.model.MappedData;
|
||||
import dxt.moe.model.SimpleData;
|
||||
import org.yaml.snakeyaml.DumperOptions;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
import org.yaml.snakeyaml.nodes.MappingNode;
|
||||
import org.yaml.snakeyaml.nodes.Node;
|
||||
import org.yaml.snakeyaml.nodes.NodeTuple;
|
||||
import org.yaml.snakeyaml.nodes.ScalarNode;
|
||||
import org.yaml.snakeyaml.nodes.SequenceNode;
|
||||
import org.yaml.snakeyaml.nodes.Tag;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
public class YamlEncoder implements MoeEncoder {
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Methods of interface MoeEncoder
|
||||
|
||||
@Override
|
||||
public byte[] convert(AnyData data) throws CodecException {
|
||||
return convert(data, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] convert(AnyData data, boolean pretty) throws CodecException {
|
||||
if(data.asMapped() == null) {
|
||||
return new byte[0];
|
||||
}
|
||||
|
||||
Node node = convertMappedData(data.asMapped());
|
||||
Yaml yaml = new Yaml();
|
||||
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
||||
yaml.serialize(node, new OutputStreamWriter(buffer));
|
||||
if(pretty) {
|
||||
|
||||
}
|
||||
else {
|
||||
|
||||
}
|
||||
return buffer.toByteArray();
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Helper methods
|
||||
|
||||
private MappingNode convertMappedData(MappedData mapData) {
|
||||
List<NodeTuple> tuples = new LinkedList<>();
|
||||
|
||||
for(String key: mapData.getKeys()) {
|
||||
//create the key node
|
||||
ScalarNode knode = new ScalarNode(Tag.STR, key, null, null,
|
||||
DumperOptions.ScalarStyle.LITERAL);
|
||||
|
||||
AnyData itemData = mapData.get(key);
|
||||
|
||||
if(itemData.asSimple() != null) {
|
||||
SimpleData sdata = itemData.asSimple();
|
||||
if(sdata.getBoolean() != null) {
|
||||
ScalarNode vnode = new ScalarNode(Tag.BOOL, sdata.getBoolean().toString(),
|
||||
null, null, DumperOptions.ScalarStyle.PLAIN);
|
||||
tuples.add(new NodeTuple(knode, vnode));
|
||||
}
|
||||
else if(sdata.getNumber() != null) {
|
||||
Number nval = sdata.getNumber();
|
||||
if(nval instanceof Byte || nval instanceof Short || nval instanceof Integer
|
||||
|| nval instanceof Long || nval instanceof BigInteger) {
|
||||
ScalarNode vnode = new ScalarNode(Tag.INT, nval.toString(), null, null,
|
||||
DumperOptions.ScalarStyle.PLAIN);
|
||||
tuples.add(new NodeTuple(knode, vnode));
|
||||
}
|
||||
else if(nval instanceof Float || nval instanceof Double
|
||||
|| nval instanceof BigDecimal) {
|
||||
ScalarNode vnode = new ScalarNode(Tag.FLOAT, nval.toString(), null, null,
|
||||
DumperOptions.ScalarStyle.PLAIN);
|
||||
tuples.add(new NodeTuple(knode, vnode));
|
||||
}
|
||||
}
|
||||
else if(sdata.getString() != null) {
|
||||
ScalarNode vnode = new ScalarNode(Tag.STR, sdata.getString(),
|
||||
null, null, DumperOptions.ScalarStyle.DOUBLE_QUOTED);
|
||||
tuples.add(new NodeTuple(knode, vnode));
|
||||
}
|
||||
}
|
||||
else if(itemData.asLinear() != null) {
|
||||
Node vnode = convertLinearData(itemData.asLinear());
|
||||
tuples.add(new NodeTuple(knode, vnode));
|
||||
}
|
||||
else if(itemData.asMapped() != null) {
|
||||
Node vnode = convertMappedData(itemData.asMapped());
|
||||
tuples.add(new NodeTuple(knode, vnode));
|
||||
}
|
||||
}
|
||||
return new MappingNode(Tag.MAP, tuples, DumperOptions.FlowStyle.AUTO);
|
||||
}
|
||||
|
||||
private SequenceNode convertLinearData(LinearData linData) {
|
||||
List<Node> nodeList = new LinkedList<>();
|
||||
for(int i=0; i < linData.size(); i++) {
|
||||
AnyData itemData = linData.get(i);
|
||||
|
||||
if(itemData.asSimple() != null) {
|
||||
SimpleData sdata = itemData.asSimple();
|
||||
if(sdata.getBoolean() != null) {
|
||||
ScalarNode vnode = new ScalarNode(Tag.BOOL, sdata.getBoolean().toString(),
|
||||
null, null, DumperOptions.ScalarStyle.PLAIN);
|
||||
nodeList.add(vnode);
|
||||
}
|
||||
else if(sdata.getNumber() != null) {
|
||||
Number nval = sdata.getNumber();
|
||||
if(nval instanceof Byte || nval instanceof Short || nval instanceof Integer
|
||||
|| nval instanceof Long || nval instanceof BigInteger) {
|
||||
ScalarNode vnode = new ScalarNode(Tag.INT, nval.toString(), null, null,
|
||||
DumperOptions.ScalarStyle.PLAIN);
|
||||
nodeList.add(vnode);
|
||||
}
|
||||
else if(nval instanceof Float || nval instanceof Double
|
||||
|| nval instanceof BigDecimal) {
|
||||
ScalarNode vnode = new ScalarNode(Tag.FLOAT, nval.toString(), null, null,
|
||||
DumperOptions.ScalarStyle.PLAIN);
|
||||
nodeList.add(vnode);
|
||||
}
|
||||
}
|
||||
else if(sdata.getString() != null) {
|
||||
ScalarNode vnode = new ScalarNode(Tag.STR, sdata.getString(),
|
||||
null, null, DumperOptions.ScalarStyle.DOUBLE_QUOTED);
|
||||
nodeList.add(vnode);
|
||||
}
|
||||
}
|
||||
else if(itemData.asLinear() != null) {
|
||||
Node vnode = convertLinearData(itemData.asLinear());
|
||||
nodeList.add(vnode);
|
||||
}
|
||||
else if(itemData.asMapped() != null) {
|
||||
Node vnode = convertMappedData(itemData.asMapped());
|
||||
nodeList.add(vnode);
|
||||
}
|
||||
}
|
||||
return new SequenceNode(Tag.SEQ, nodeList, DumperOptions.FlowStyle.AUTO);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#
|
||||
# Copyright (c) DuxTechnica
|
||||
#
|
||||
# Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||
# use this file except in compliance with the License. You may obtain a copy of the License by
|
||||
# contacting:
|
||||
#
|
||||
# Dux Technica, [email protected]
|
||||
#
|
||||
# This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
# either express or implied. See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
dxt.moe.codec.yaml.YamlCodec
|
||||
Reference in New Issue
Block a user