Java Bean Builder

Source: Internet
Author: User
Tags definition readline split tostring trim stringbuffer
This is a Java Bean builder that generates Java beans using a text file, which may be called a bean definition file.

We may use a lot of data objects (value objects) in the development process, most of which are encapsulated into one bean. Although the major

The IDE provides the tools to facilitate the generation of this data bean, but it's all step-by-Step dialog boxes, unlike this,

This allows you to define multiple beans in one file and generate all beans at a time. Of course it has its limitations, and it is only

Simply generate the private data domain and public setter, getter, and will not import the necessary packages for you. But still,

Also can greatly facilitate the data bean more, or bean fields more applications.

The bean definition file (. bd) format is as follows:

This is the comment, preceded by "//".
Use "#" to represent the bean name.
Note cannot be written on the same line as the Bean name or data field.
#MyBean1
int I
Long L
String s

#MyBean2
String User
String pwd

#MyBean3
Object o
...

...



Import java.io.*;
Import java.util.*;


public class Createbean {

Private BufferedReader in;
Private String Inputfile;
Private HashMap beans = new HashMap ();
private static final Char Beanmark = ' # ';

Public Createbean (String inputfilename) {
Inputfile = InputFileName;
try {
in = new BufferedReader (new FileReader (InputFileName));
catch (FileNotFoundException e) {
System.out.println ("file" + InputFileName + "not found!");
E.printstacktrace ();
}
}

public void Generate () {
String ln = null;
String beanname = null;
ArrayList properties = new ArrayList ();

try {
ln = In.readline (). Trim (); Throw IO excption

while (ln!= null) {
If
(Ln.length () < 1) | |
(Ln.startswith ("//")) | | Comments
(Ln.charat (0)!=beanmark && ln.split ("\\s+"). length!=2)
){}
else if (ln.charat (0) = = Beanmark) {
if (beanname!= null) {
Beans.put (Beanname, properties); Save Last bean to HashMap
}

Properties = new ArrayList ();

Gen New Bean Name
Beanname = Upperfirst (ln.substring (1));
System.out.println ("Bean:" + beanname);
}
else {
System.out.println ("T" + ln);
Properties.add (LN);
}

ln = In.readline ();
if (ln==null) {//Last Bean
if (beanname!= null) {
Beans.put (Beanname, properties);
}
}
else{
ln = Ln.trim ();
}
}//while

System.out.println (beans.size () + "beans tested ...");
Dumpbeans ();

catch (IOException e) {
SYSTEM.OUT.PRINTLN ("Read file failed!");
finally {
try {
if (in!= null)
In.close ();
catch (IOException E2) {
SYSTEM.OUT.PRINTLN ("Close input file failed!");
E2.printstacktrace ();
}
}

}

public static string Upperfirst (string s) {
StringBuffer buf = new StringBuffer (s);
if (Buf.charat (0) >= ' a ' && buf.charat (0) <= ' z ') {
Buf.setcharat (0, (char) (Buf.charat (0)-32));
}
return buf.tostring ();
}

private void Dumpbeans () {
SYSTEM.OUT.PRINTLN ("Creating ...");

StringBuffer buf = null;
String key = null;
PrintWriter pw = null;
iterator ITR = null;
Set keyset = Beans.keyset ();

if (keyset!=null)
ITR = Keyset.iterator ();

while (Itr.hasnext ()) {
BUF = new StringBuffer ();
Key = (String) itr.next ();
ArrayList L = (ArrayList) beans.get (key);
Buf.append ("public class");
Buf.append (key);
Buf.append ("{\r\n\n");

Add properties
for (int i = 0; i < l.size (); i++) {
Buf.append ("\tprivate");
Buf.append ((String) l.get (i));
Buf.append ("; \ r \ n");
}
Buf.append ("\ r \ n");

Add getter and Setter
for (int i = 0; i < l.size (); i++) {
Getter
String item = (string) l.get (i); Like "int a"
String type = Item.split ("\\s+", 2) [0]; TOK needed!!!
String var = item.split ("\\s+", 2) [1];
Buf.append ("\tpublic" + type + "get");
Buf.append (Upperfirst (Var));
Buf.append ("() {\r\n\t\treturn this.");
Buf.append (VAR);
Buf.append ("; \r\n\t}\n");

Setter
Buf.append ("\tpublic void Set");
Buf.append (Upperfirst (Var));
Buf.append ("(" + Type + "" + var + ") {\r\n\t\tthis.");
Buf.append (VAR);
Buf.append ("=");
Buf.append (VAR);
Buf.append ("; \r\n\t}\n");
}
Buf.append ("}\r\n");

Dump to a named file
try{
PW = new PrintWriter (
New BufferedWriter (
New FileWriter (key+ ". Java"));
Pw.write (Buf.tostring ());
Pw.close ();

}catch (IOException e) {
SYSTEM.OUT.PRINTLN ("Create Java file failed!");
E.printstacktrace ();
}

}

}

/********************************/

public static void Main (string[] args) throws Exception {

if (args.length<1) {
System.out.println ("Usage:");
System.out.println ("\tjava createbean yourinputfilename");
System.out.println ("Where your input file should like this:");
System.out.println ("#myBean1");
SYSTEM.OUT.PRINTLN ("int field1");
System.out.println ("Long Field2");
System.out.println ("String filed3");
System.out.println ("... \ n");
System.out.println ("#myBean2");
System.out.println ("char field1");
System.out.println ("Object field2");
System.out.println ("...");
System.out.println ("\ n...");
Return
}
SYSTEM.OUT.PRINTLN ("Begin ...");
Createbean cb = new Createbean (args[0]);
Cb.generate ();
System.out.println ("complete.");

}

}





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.