Giter Site home page Giter Site logo

coffeepot-bean-wr's Introduction

coffeepot-bean-wr (Writer and Reader)

Coffeepot-bean-wr is a simple Java framework for marshalling Java beans to flat file and unmarshal flat file to Java Beans.

Features:

  • Support for delimited and fixed length formats.
  • Uses Java annotations for mapping.
  • Maps unannotated classes dynamically.
  • Override annotations dynamically.
  • Custom type handlers are supported.

Real examples of use you will find them in coffeepot-br-sped-fiscal and coffeepot-br-sintegra projects. The coffeepot-br-sped-fiscal project uses delimited format, and the sintegra project uses FixedLength format.

Version 2.0.0

In version 2, the type handlers are enhanced with the command feature.

Example:

	// DefaultStringHandler has the built-in replace command
	@Field(name="descr", commands={@Cmd(name=DefaultStringHandler.CMD_REPLACE, args={"foo", "bar"} )})

Download from Maven central

        <dependency>
            <groupId>com.github.jean-merelis</groupId>
            <artifactId>coffeepot-bean-wr</artifactId>
            <version>2.0.0-rc.1</version>            
        </dependency> 

Version 1.3.0

Download from Maven central

        <dependency>
            <groupId>com.github.jean-merelis</groupId>
            <artifactId>coffeepot-bean-wr</artifactId>
            <version>1.3.0</version>            
        </dependency> 

Examples

Mapping

		@Record(fields = {
				@Field(name = "", id = true, constantValue = "ORDER"),
				@Field(name = "id"),
				@Field(name = "date"),
				@Field(name = "customer", params = {DefaultStringHandler.CMD_REPLACE, "Sr.", "", "CharCase.UPPER"}),
				@Field(name = "items")
			})
		public class Order {

			private Integer id;
			private Date date;
			private String customer;
			private List<Item> items;
		
			// getters and setters...
		}

		@Record(fields = {
				@Field(name = "", id = true, constantValue = "ITEM"),
				@Field(name = "number"),
				@Field(name = "product"),
				@Field(name = "quantity")
			})
		public class Item {

			private int number;
			private String product;
			private double quantity;
		
			// getters ... setters...
		}		

Using...

	public void test() throws Exception{
		Order order = new Order();
		order.setCustomer("Sr. John B.");
		//add some data

        File file = new File("ORDER.tmp");
        Writer w = new FileWriter(file);

        DelimitedWriter instance = new DelimitedWriter(w);
        instance.setDelimiter('|');
        instance.setEscape('\\');
        instance.setRecordTerminator("|\r\n");
        
        instance.write(order);

        w.flush();
        w.close();

Output:

	ORDER|123|2015-03-10T00:04:15|JOHN B.|
	ITEM|1|product 1|10|
	ITEM|2|product 2|5|
	ITEM|3|product 3|2|

Reading

For simple file structure, i.e. where the file has only one type of record, simply map the fields of the class that represents this record and perform the reading.

If file contains a list of these records you can read with the parseAsListOf.

	//Example for a file with a list of the records.

    public void read(){
		File file = new File("file-with-a-list-of-records.txt");

		try (FileReader fr = new FileReader(file)) {
			FixedLengthReader reader = new FixedLengthReader(fr);
			List<MyRecord> records = reader.parseAsListOf(MyRecord.class);
			for (MyRecord r: records){ 
			  ...
			}
		}

	}

For more complex structures that have various types of records, it is necessary that the records have an identifier field so it can be possible to instantiate the corresponding class of the record. The identifier field must have the value that identifies the record in the constantValue property and the id property must be true.

@Record(fields = {
    @Field(name = "", constantValue = "01", id = true, length = 2), // identifier field
    @Field(name = "value", length = 18)
	// ... more fields
})

public class Rec01 {

    private String value;
		
	// more fields getters and setters...
	...
}


@Record(fields = {
    @Field(name = "", constantValue = "AA", id = true, length = 2), // identifier field
    @Field(name = "value", length = 18)
	// ... more fields
})

public class RecAA {

    private String value;
		
	// more fields getters and setters...
	...
}

coffeepot-bean-wr's People

Contributors

djselzlein avatar jean-merelis avatar maumorais avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

coffeepot-bean-wr's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.