org.milyn.ejc.WriteMethod Maven / Gradle / Ivy
/*
* Milyn - Copyright (C) 2006 - 2010
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License (version 2.1) as published by the Free Software
* Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See the GNU Lesser General Public License for more details:
* http://www.gnu.org/licenses/lgpl.txt
*/
package org.milyn.ejc;
import org.milyn.config.Configurable;
import org.milyn.edisax.model.internal.Component;
import org.milyn.edisax.model.internal.ContainerNode;
import org.milyn.edisax.model.internal.DelimiterType;
import org.milyn.edisax.model.internal.Delimiters;
import org.milyn.edisax.model.internal.Field;
import org.milyn.edisax.model.internal.MappingNode;
import org.milyn.edisax.model.internal.Segment;
import org.milyn.edisax.model.internal.SegmentGroup;
import org.milyn.edisax.model.internal.ValueNode;
import org.milyn.edisax.util.EDIUtils;
import org.milyn.javabean.DataDecoder;
import org.milyn.javabean.DataEncoder;
import org.milyn.javabean.decoders.DABigDecimalDecoder;
import org.milyn.javabean.pojogen.JClass;
import org.milyn.javabean.pojogen.JMethod;
import org.milyn.javabean.pojogen.JNamedType;
import org.milyn.javabean.pojogen.JType;
import org.milyn.smooks.edi.EDIWritable;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.*;
/**
* EDIWritable bean serialization class.
*
* @author [email protected]
*/
class WriteMethod extends JMethod {
private JClass jClass;
private MappingNode mappingNode;
private boolean appendFlush = false;
private boolean trunacate;
private DelimiterType terminatingDelimiter;
WriteMethod(JClass jClass, MappingNode mappingNode) {
super("write");
addParameter(new JType(Writer.class), "writer");
addParameter(new JType(Delimiters.class), "delimiters");
getExceptions().add(new JType(IOException.class));
jClass.getImplementTypes().add(new JType(EDIWritable.class));
jClass.getMethods().add(this);
this.jClass = jClass;
this.mappingNode = mappingNode;
this.trunacate = (mappingNode instanceof ContainerNode && ((ContainerNode)mappingNode).isTruncatable());
if(trunacate) {
jClass.getRawImports().add(new JType(StringWriter.class));
jClass.getRawImports().add(new JType(List.class));
jClass.getRawImports().add(new JType(ArrayList.class));
jClass.getRawImports().add(new JType(EDIUtils.class));
jClass.getRawImports().add(new JType(DelimiterType.class));
}
}
public void writeObject(JNamedType property, DelimiterType delimiterType, BindingConfig bindingConfig, MappingNode mappingNode) {
writeDelimiter(delimiterType);
writeObject(property, bindingConfig, mappingNode);
}
public void writeObject(JNamedType property, BindingConfig bindingConfig, MappingNode mappingNode) {
appendToBody("\n if(" + property.getName() + " != null) {");
if(mappingNode instanceof Segment) {
if(!((Segment) mappingNode).getFields().isEmpty() && (bindingConfig.getParent() == null || bodyLength() > 0)) {
appendToBody("\n nodeWriter.write(\"" + ((Segment)mappingNode).getSegcode() + "\");");
appendToBody("\n nodeWriter.write(delimiters.getField());");
}
}
appendToBody("\n " + property.getName() + ".write(nodeWriter, delimiters);");
if(trunacate) {
appendToBody("\n nodeTokens.add(nodeWriter.toString());");
appendToBody("\n ((StringWriter)nodeWriter).getBuffer().setLength(0);");
}
appendToBody("\n }");
}
public void writeValue(JNamedType property, ValueNode modelNode, DelimiterType delimiterType) {
writeDelimiter(delimiterType);
writeValue(property, modelNode);
}
public void writeValue(JNamedType property, ValueNode modelNode) {
appendToBody("\n if(" + property.getName() + " != null) {");
DataDecoder dataDecoder = modelNode.getDecoder();
if(dataDecoder instanceof DataEncoder) {
String encoderName = property.getName() + "Encoder";
Class extends DataDecoder> decoderClass = dataDecoder.getClass();
// Add the property for the encoder instance...
jClass.getProperties().add(new JNamedType(new JType(decoderClass), encoderName));
// Create the encoder in the constructor...
JMethod defaultConstructor = jClass.getDefaultConstructor();
defaultConstructor.appendToBody("\n " + encoderName + " = new " + decoderClass.getSimpleName() + "();");
// Configure the encoder in the constructor (if needed)....
if(dataDecoder instanceof Configurable) {
Properties configuration = ((Configurable) dataDecoder).getConfiguration();
if(configuration != null) {
Set> encoderConfig = configuration.entrySet();
String encoderPropertiesName = encoderName + "Properties";
jClass.getRawImports().add(new JType(Properties.class));
defaultConstructor.appendToBody("\n Properties " + encoderPropertiesName + " = new Properties();");
for(Map.Entry
© 2015 - 2025 Weber Informatics LLC | Privacy Policy