diff --git a/moe-main/src/main/java/daxi/moe/app/DecodeException.java b/moe-main/src/main/java/daxi/moe/app/DecodeException.java new file mode 100644 index 0000000..6123dcd --- /dev/null +++ b/moe-main/src/main/java/daxi/moe/app/DecodeException.java @@ -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, contact@duxtechnica.com + * + * 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 daxi.moe.app; + +public class DecodeException extends Exception { + + private final String type; + private final String method; + + public DecodeException(String typeName, String methodName, String message, Throwable cause) { + super(message, cause); + this.type = typeName; + this.method = methodName; + } + + public DecodeException(String message, Throwable cause) { + this(null, null, message, cause); + } + + public DecodeException(Throwable cause) { + this(null, null, null, cause); + } + + public String getType() { + return type; + } + + public String getMethod() { + return method; + } +} diff --git a/moe-main/src/main/java/daxi/moe/codec/mem/MemCodec.java b/moe-main/src/main/java/daxi/moe/codec/mem/MemCodec.java new file mode 100644 index 0000000..ad41fc7 --- /dev/null +++ b/moe-main/src/main/java/daxi/moe/codec/mem/MemCodec.java @@ -0,0 +1,41 @@ +/* + * 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, contact@duxtechnica.com + * + * 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 daxi.moe.codec.mem; + +import daxi.moe.codec.MoeCodec; +import daxi.moe.codec.MoeDecoder; +import daxi.moe.codec.MoeEncoder; + +/** + * @author indroneel + */ + +public class MemCodec implements MoeCodec { + + @Override + public String getFormat() { + return "mem"; + } + + @Override + public MoeDecoder decoder() { + return new MemDecoder(); + } + + @Override + public MoeEncoder encoder() { + return new MemEncoder(); + } +} diff --git a/moe-main/src/main/java/daxi/moe/codec/mem/MemDecoder.java b/moe-main/src/main/java/daxi/moe/codec/mem/MemDecoder.java new file mode 100644 index 0000000..c36c9fa --- /dev/null +++ b/moe-main/src/main/java/daxi/moe/codec/mem/MemDecoder.java @@ -0,0 +1,40 @@ +/* + * 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, contact@duxtechnica.com + * + * 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 daxi.moe.codec.mem; + +import daxi.moe.app.Moe; +import daxi.moe.codec.MoeDecoder; +import daxi.moe.model.AnyData; + +import java.io.InputStream; +import java.util.List; + +public class MemDecoder implements MoeDecoder { + + @Override + public void setSource(byte[] data) { + + } + + @Override + public void setSource(InputStream input) { + + } + + @Override + public AnyData decode(List options) { + return null; + } +} diff --git a/moe-main/src/main/java/daxi/moe/codec/mem/MemEncoder.java b/moe-main/src/main/java/daxi/moe/codec/mem/MemEncoder.java new file mode 100644 index 0000000..afcc854 --- /dev/null +++ b/moe-main/src/main/java/daxi/moe/codec/mem/MemEncoder.java @@ -0,0 +1,35 @@ +/* + * 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, contact@duxtechnica.com + * + * 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 daxi.moe.codec.mem; + +import daxi.moe.codec.MoeEncoder; +import daxi.moe.model.AnyData; + +/** + * @author indroneel + */ + +public class MemEncoder implements MoeEncoder { + + @Override + public byte[] convert(AnyData data) { + return new byte[0]; + } + + @Override + public byte[] convert(AnyData data, boolean pretty) { + return new byte[0]; + } +} diff --git a/moe-main/src/main/java/daxi/moe/lib/ExternalImpl.java b/moe-main/src/main/java/daxi/moe/lib/ExternalImpl.java index 15d6e06..6c78cb6 100644 --- a/moe-main/src/main/java/daxi/moe/lib/ExternalImpl.java +++ b/moe-main/src/main/java/daxi/moe/lib/ExternalImpl.java @@ -18,6 +18,7 @@ package daxi.moe.lib; import daxi.moe.app.External; import daxi.moe.app.Moe; import daxi.moe.codec.MoeCodec; +import daxi.moe.codec.MoeDecoder; import daxi.moe.lib.m2j.M2JLinearImpl; import daxi.moe.lib.m2j.M2JMappedImpl; import daxi.moe.lib.m2j.M2JObjectImpl; @@ -28,9 +29,12 @@ import daxi.moe.lib.transform.M2JMapped; import daxi.moe.lib.transform.M2JObject; import daxi.moe.lib.transform.M2JSimple; import daxi.moe.model.AnyData; +import daxi.moe.model.MappedData; import java.io.InputStream; import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; /** * @author indroneel @@ -38,26 +42,29 @@ import java.util.List; public class ExternalImpl implements External { - private final MoeCodec mprov; + private static final Logger L = Logger.getLogger(ExternalImpl.class.getName()); + + private final MoeDecoder decoder; private final List options; private final M2JGlobals globals; + private AnyData model; + + ExternalImpl(MoeCodec codec, List options) { + decoder = codec.decoder(); - ExternalImpl(MoeCodec provider, List options) { - mprov = provider; this.options = options; - globals = new M2JGlobals(options); + globals = new M2JGlobals(options); M2JSimple m2jSim = new M2JSimpleImpl(globals); globals.setM2J(m2jSim); - M2JLinear m2jLin = new M2JLinearImpl(globals); globals.setM2J(m2jLin); - M2JMapped m2jMap = new M2JMappedImpl(globals); globals.setM2J(m2jMap); - M2JObject m2jObj = new M2JObjectImpl(globals); globals.setM2J(m2jObj); + + model = new AnyData(new MappedData()); } //////////////////////////////////////////////////////////////////////////////////////////////// @@ -65,12 +72,22 @@ public class ExternalImpl implements External { @Override public External from(InputStream in) { - return null; + decoder.setSource(in); + model = decoder.decode(options); + if(model == null) { + model = new AnyData(new MappedData()); + } + return this; } @Override public External from(byte[] data) { - return null; + decoder.setSource(data); + model = decoder.decode(options); + if(model == null) { + model = new AnyData(new MappedData()); + } + return this; } @Override @@ -80,12 +97,19 @@ public class ExternalImpl implements External { @Override public AnyData getModel() { - return null; + return model; } @Override public T convert(Class type) { - return null; + MappedData data = model.asMapped(); + if(data == null) { + if(L.isLoggable(Level.WARNING)) { + L.warning("input data model does not contain MappedData as root element"); + } + return null; + } + return globals.getM2JObject().convert(data, type); } @Override diff --git a/moe-main/src/main/java/daxi/moe/lib/InternalImpl.java b/moe-main/src/main/java/daxi/moe/lib/InternalImpl.java index 8e7b877..cfada6d 100644 --- a/moe-main/src/main/java/daxi/moe/lib/InternalImpl.java +++ b/moe-main/src/main/java/daxi/moe/lib/InternalImpl.java @@ -18,10 +18,13 @@ package daxi.moe.lib; import daxi.moe.app.Internal; import daxi.moe.app.Moe; import daxi.moe.codec.MoeCodec; +import daxi.moe.codec.MoeEncoder; import daxi.moe.model.AnyData; +import daxi.moe.model.MappedData; import java.io.IOException; import java.util.List; +import java.util.logging.Logger; /** * @@ -30,18 +33,32 @@ import java.util.List; public class InternalImpl implements Internal { + private static final Logger L = Logger.getLogger(InternalImpl.class.getName()); + + private final MoeEncoder encoder; + private final List options; + + //private final M2JGlobals globals; + + private AnyData model; + InternalImpl(MoeCodec provider, List options) { - //TODO + encoder = provider.encoder(); + this.options = options; + + //globals = new M2JGlobals(options); + + model = new AnyData(new MappedData()); } @Override public Internal from(Object obj) { - return null; + return this; } @Override public AnyData getModel() { - return null; + return model; } @Override diff --git a/moe-main/src/main/java/daxi/moe/lib/MoeImpl.java b/moe-main/src/main/java/daxi/moe/lib/MoeImpl.java index 4ec64d3..340c99f 100644 --- a/moe-main/src/main/java/daxi/moe/lib/MoeImpl.java +++ b/moe-main/src/main/java/daxi/moe/lib/MoeImpl.java @@ -31,32 +31,32 @@ public class MoeImpl extends Moe { private static final Logger L = Logger.getLogger(MoeImpl.class.getName()); - private final static Map providers; + private final static Map codecs; static { if (L.isLoggable(Level.FINE)) { - L.fine("loading mapping providers"); + L.fine("loading MOE codecs"); } - providers = new HashMap<>(); - ServiceLoader mpl = ServiceLoader.load(MoeCodec.class); - for (MoeCodec mp : mpl) { - String format = mp.getFormat(); - providers.put(format.toLowerCase(), mp); + codecs = new HashMap<>(); + ServiceLoader codecs = ServiceLoader.load(MoeCodec.class); + for (MoeCodec codec : codecs) { + String format = codec.getFormat(); + MoeImpl.codecs.put(format.toLowerCase(), codec); if (L.isLoggable(Level.FINE)) { - L.fine(String.format("%s -> %s", format, mp.getClass().getName())); + L.fine(String.format("%s -> %s", format, codec.getClass().getName())); } } } private List options; - protected MoeImpl() { + public MoeImpl() { options = new ArrayList<>(); } @Override public External external(String format) { - MoeCodec mp = providers.get(format.toLowerCase()); + MoeCodec mp = codecs.get(format.toLowerCase()); if (mp != null) { return new ExternalImpl(mp, options); } @@ -65,6 +65,10 @@ public class MoeImpl extends Moe { @Override public Internal internal (String format) { + MoeCodec mp = codecs.get(format.toLowerCase()); + if (mp != null) { + return new InternalImpl(mp, options); + } return null; } diff --git a/moe-main/src/main/java/daxi/moe/lib/m2j/M2JObjectImpl.java b/moe-main/src/main/java/daxi/moe/lib/m2j/M2JObjectImpl.java index 9d34820..bf237b2 100644 --- a/moe-main/src/main/java/daxi/moe/lib/m2j/M2JObjectImpl.java +++ b/moe-main/src/main/java/daxi/moe/lib/m2j/M2JObjectImpl.java @@ -58,11 +58,17 @@ public class M2JObjectImpl implements M2JObject { @Override public T convert(MappedData data, Class targetType) { if(data == null || targetType == null) { + if(L.isLoggable(Level.WARNING)) { + L.warning("Invalid arguments: data=" + data + ", targetType=" + targetType); + } return null; } List methods = new LinkedList<>(); collateAllMethods(targetType, methods); if(methods.isEmpty()) { + if(L.isLoggable(Level.WARNING)) { + L.warning("No suitable methods found in targetType: " + targetType); + } return null; } diff --git a/moe-main/src/main/java/daxi/moe/lib/m2j/M2JSimpleImpl.java b/moe-main/src/main/java/daxi/moe/lib/m2j/M2JSimpleImpl.java index 0239646..536a878 100644 --- a/moe-main/src/main/java/daxi/moe/lib/m2j/M2JSimpleImpl.java +++ b/moe-main/src/main/java/daxi/moe/lib/m2j/M2JSimpleImpl.java @@ -17,11 +17,16 @@ package daxi.moe.lib.m2j; import daxi.moe.lib.transform.M2JGlobals; import daxi.moe.lib.transform.M2JSimple; import daxi.moe.lib.transform.M2JStatus; +import daxi.moe.model.AnyData; import daxi.moe.model.MappedData; +import daxi.moe.model.SimpleData; import java.lang.reflect.Method; import java.math.BigDecimal; import java.math.BigInteger; +import java.util.Locale; +import java.util.logging.Level; +import java.util.logging.Logger; /** * The only implementation of the {@link daxi.moe.lib.transform.M2JSimple} interface. @@ -31,6 +36,8 @@ import java.math.BigInteger; public class M2JSimpleImpl implements M2JSimple { + private static final Logger L = Logger.getLogger(M2JSimpleImpl.class.getName()); + private final M2JGlobals globals; public M2JSimpleImpl(M2JGlobals globals) { @@ -53,9 +60,71 @@ public class M2JSimpleImpl implements M2JSimple { return M2JStatus.NOT_VALID; } + String key = m2jMthd.getMapFromKey(); + if(!data.contains(key)) { + return M2JStatus.NOT_VALID; + } + + AnyData adata = data.get(key); + SimpleData sdata = adata.asSimple(); + if(sdata == null) { + return M2JStatus.NOT_VALID; + } + + Object paramVal = null; + + if(paramType == String.class) { + paramVal = sdata.getString(); + } + else if(paramType == BigInteger.class) { + paramVal = getBigIntegerValue(sdata); + } + else if(paramType == BigDecimal.class) { + paramVal = getBigDecimalValue(sdata); + } + else if(paramType == byte.class || paramType == Byte.class) { + paramVal = getByteValue(sdata); + } + else if(paramType == short.class || paramType == Short.class) { + paramVal = getShortValue(sdata); + } + else if(paramType == int.class || paramType == Integer.class) { + paramVal = getIntegerValue(sdata); + } + else if(paramType == long.class || paramType == Long.class) { + paramVal = getLongValue(sdata); + } + else if(paramType == float.class || paramType == Float.class) { + paramVal = getFloatValue(sdata); + } + else if(paramType == double.class || paramType == Double.class) { + paramVal = getDoubleValue(sdata); + } + else if(paramType == boolean.class || paramType == Boolean.class) { + paramVal = getBooleanValue(sdata); + } + + if(paramVal == null) { + return M2JStatus.NOT_VALID; + } + + try { + method.setAccessible(true); + method.invoke(target, paramVal); + method.setAccessible(false); + } + catch (ReflectiveOperationException exep) { + if(L.isLoggable(Level.WARNING)) { + L.log(Level.WARNING, exep.getMessage(), exep); + } + return M2JStatus.ERROR; + } return M2JStatus.SUCCESS; } + //////////////////////////////////////////////////////////////////////////////////////////////// + // Helper methods + private boolean isSupportedParamType(Class paramType) { return paramType == String.class || paramType == BigInteger.class @@ -70,4 +139,199 @@ public class M2JSimpleImpl implements M2JSimple { || paramType == boolean.class || paramType == Boolean.class; } + + private BigInteger getBigIntegerValue(SimpleData data) { + BigInteger paramVal = null; + Number nval = data.getNumber(); + if(nval != null) { + paramVal = new BigInteger(Long.toString(nval.longValue())); + } + else { + String sval = data.getString(); + if(sval != null) { + try { + paramVal = new BigInteger(sval); + } + catch(Exception exep) { + if(L.isLoggable(Level.WARNING)) { + L.warning("Unable to convert string [" + sval + "] to BigInteger"); + } + } + } + } + return paramVal; + } + + private BigDecimal getBigDecimalValue(SimpleData data) { + BigDecimal paramVal = null; + Number nval = data.getNumber(); + if(nval != null) { + paramVal = new BigDecimal(nval.toString()); + } + else { + String sval = data.getString(); + if(sval != null) { + try { + paramVal = new BigDecimal(sval); + } + catch(Exception exep) { + if(L.isLoggable(Level.WARNING)) { + L.warning("Unable to convert string [" + sval + "] to BigDecimal"); + } + } + } + } + return paramVal; + } + + private Byte getByteValue(SimpleData data) { + Byte paramVal = null; + Number nval = data.getNumber(); + if(nval != null) { + paramVal = nval.byteValue(); + } + else { + String sval = data.getString(); + if(sval != null) { + try { + paramVal = Byte.parseByte(sval); + } + catch(NumberFormatException exep) { + if(L.isLoggable(Level.WARNING)) { + L.warning("Unable to convert string [" + sval + "] to byte"); + } + } + } + } + return paramVal; + } + + private Short getShortValue(SimpleData data) { + Short paramVal = null; + Number nval = data.getNumber(); + if(nval != null) { + paramVal = nval.shortValue(); + } + else { + String sval = data.getString(); + if(sval != null) { + try { + paramVal = Short.parseShort(sval); + } + catch(NumberFormatException exep) { + if(L.isLoggable(Level.WARNING)) { + L.warning("Unable to convert string [" + sval + "] to short"); + } + } + } + } + return paramVal; + } + + private Integer getIntegerValue(SimpleData data) { + Integer paramVal = null; + Number nval = data.getNumber(); + if(nval != null) { + paramVal = nval.intValue(); + } + else { + String sval = data.getString(); + if(sval != null) { + try { + paramVal = Integer.parseInt(sval); + } + catch(NumberFormatException exep) { + if(L.isLoggable(Level.WARNING)) { + L.warning("Unable to convert string [" + sval + "] to integer"); + } + } + } + } + return paramVal; + } + + private Long getLongValue(SimpleData data) { + Long paramVal = null; + Number nval = data.getNumber(); + if(nval != null) { + paramVal = nval.longValue(); + } + else { + String sval = data.getString(); + if(sval != null) { + try { + paramVal = Long.parseLong(sval); + } + catch(NumberFormatException exep) { + if(L.isLoggable(Level.WARNING)) { + L.warning("Unable to convert string [" + sval + "] to long"); + } + } + } + } + return paramVal; + } + + private Float getFloatValue(SimpleData data) { + Float paramVal = null; + Number nval = data.getNumber(); + if(nval != null) { + paramVal = nval.floatValue(); + } + else { + String sval = data.getString(); + if(sval != null) { + try { + paramVal = Float.parseFloat(sval); + } + catch(NumberFormatException exep) { + if(L.isLoggable(Level.WARNING)) { + L.warning("Unable to convert string [" + sval + "] to float"); + } + } + } + } + return paramVal; + } + + private Double getDoubleValue(SimpleData data) { + Double paramVal = null; + Number nval = data.getNumber(); + if(nval != null) { + paramVal = nval.doubleValue(); + } + else { + String sval = data.getString(); + if(sval != null) { + try { + paramVal = Double.parseDouble(sval); + } + catch(NumberFormatException exep) { + if(L.isLoggable(Level.WARNING)) { + L.warning("Unable to convert string [" + sval + "] to double"); + } + } + } + } + return paramVal; + } + + private Boolean getBooleanValue(SimpleData data) { + Boolean paramVal = data.getBoolean(); + if(paramVal != null) { + return paramVal; + } + + Number nval = data.getNumber(); + if(nval != null) { + paramVal = nval.intValue() != 0; + } + else { + String sval = data.getString(); + if(sval != null) { + paramVal = Boolean.parseBoolean(sval.toLowerCase(Locale.getDefault())); + } + } + return paramVal; + } } diff --git a/moe-main/src/main/resources/META-INF/services/daxi.moe.codec.MoeCodec b/moe-main/src/main/resources/META-INF/services/daxi.moe.codec.MoeCodec new file mode 100644 index 0000000..05113e4 --- /dev/null +++ b/moe-main/src/main/resources/META-INF/services/daxi.moe.codec.MoeCodec @@ -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, contact@duxtechnica.com +# +# 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. +# + +daxi.moe.codec.mem.MemCodec \ No newline at end of file diff --git a/moe-toml/pom.xml b/moe-toml/pom.xml index 405b098..4ed2d50 100644 --- a/moe-toml/pom.xml +++ b/moe-toml/pom.xml @@ -19,7 +19,7 @@ daxi.moe - daxi.moe.app + daxi.moe.main ${project.parent.version} diff --git a/moe-yaml/pom.xml b/moe-yaml/pom.xml index 695f3e5..482e98c 100644 --- a/moe-yaml/pom.xml +++ b/moe-yaml/pom.xml @@ -19,7 +19,7 @@ daxi.moe - daxi.moe.app + daxi.moe.main ${project.parent.version}