[ Back | Previous | Next ]

How to extract Descriptor/Tabledefinition from a Project/Table file?

Package:
TOPLink.Public.Expressions.*
Product:
TopLink
Release:
2.1
Related Links:
General
General
General
General
General
Comment:
import java.text.*;
import java.io.*;
import java.util.*;
public class StreamtokenTip {
/**
 * StreamtokenTip constructor comment.
 */
public StreamtokenTip() {
	super();
}
/**
 * Insert the method's description here.
 * Creation date: (12/31/99 6:20:33 PM)
 * @param str java.lang.String
 */
public static void dbg(String str) {
	System.out.println(str);
}
public static void main(String args[]) {
	// Insert code to start the application here.
	int msgnum = -1;
	int optind;
	String usage = "Usage: StreamtokenTop [-v] [-f ] [..files..] ";
	String url = null;
	String filename = null;
	String intrvl = null;
	boolean verbosity = false;
	for (optind = 0; optind < args.length; optind++) {
		System.out.println("" + args[optind]);
		if (args[optind].equals("-f")) {
			filename = args[++optind];
		} else
			if (args[optind].equals("-v")) {
				verbosity = true;
			} else
				if (args[optind].equals("--")) {
					optind++;
					break;
				} else
					if (args[optind].startsWith("-")) {
						System.out.println("" + usage);
						System.out.println("-- Invalid option " + args[optind]);
						System.exit(1);
					} else {
						break;
					}
	}
	if (filename == null) {
		System.err.println("missing output filename");
		System.exit(1);
	}
	dbg("-- end option = " + optind);
	PropertyResourceBundle tokens = null, translations = null, descriptions = null;
	try {
		descriptions = new PropertyResourceBundle(new FileInputStream("descriptor.properties"));
	} catch (Exception e) {
		System.err.println("-- missing descriptor.properties. --");
		System.exit(1);
	}
	try {
		tokens = new PropertyResourceBundle(new FileInputStream("token.properties"));
	} catch (Exception e) {
		System.err.println("-- missing token.properties. --");
		System.exit(1);
	}
	try {
		translations = new PropertyResourceBundle(new FileInputStream("translate.properties"));
	} catch (Exception e) {
		System.err.println("-- missing translate.properties. --");
		System.exit(1);
	}
	PrintWriter out = null;
	try {
		out = new PrintWriter(new BufferedWriter(new FileWriter(filename)));
	} catch (Throwable t) {
		System.err.println("-- unable to write file." + filename);
		System.exit(1);
	}
	//
	/*	if (true) {
	test("addForeignKey='AME_V_MEETREEKSEN_ALL.PAL_PAR_CODE','AME_PARAMETERS.CODE'");
	return;
	}*/

	//
	int soort = 0;
	for (int k = optind; k < args.length; k++) {
		Vector wordlist = new Vector();
		try {
			System.out.println("Processing file... " + args[k]);
			FileReader fr = new FileReader(args[k]);
			BufferedReader br = new BufferedReader(fr);
			StreamTokenizer st = new StreamTokenizer(br);
			//StreamTokenizer st =
			//    new StreamTokenizer(new StringReader(
			//    "this is a test"));
			st.resetSyntax();
			st.commentChar('#');
			st.quoteChar('[');
			st.quoteChar(']');
			//st.ordinaryChar(' ');
			st.wordChars('A', 'z');
			st.wordChars(' ', ' ');
			int type = -1;
			Object dummy = new Object();
			while ((type = st.nextToken()) != StreamTokenizer.TT_EOF) {
				if (type == StreamTokenizer.TT_WORD) {
					if (st.sval.indexOf("[") >= 0 && tokens.handleGetObject(st.sval) != null) {
						System.out.println(">- " + st.sval);
						wordlist.addElement(st.sval);
						if (st.sval.equals("[DESCRIPTOR]")) {
							soort = 100;
						} else
							if (st.sval.equals("[TABLE]")) {
								soort = 200;
							}
					}
				}
			}
			br.close();
		} catch (IOException e) {
			System.err.println(e);
			//System.exit(0);
		}
		//for (int i = 0; i < wordlist.size(); i++) {
		//	System.out.println("--" + wordlist.elementAt(i).toString());
		//}
		dbg("-- wordlist: " + wordlist);
		//
		Vector headers = new Vector();
		try {
			FileReader fr = new FileReader(args[k]);
			BufferedReader br = new BufferedReader(fr);
			String line = null;
			int count = 0;
			Vector v = null;
			while ((line = br.readLine()) != null) {
				if (count < wordlist.size() && line.equals(wordlist.elementAt(count))) {
					System.out.println(">> " + line);
					if (v != null) {
						headers.addElement(v);
					}
					v = new Vector();
					count++;
				}
				if (v != null)
					v.addElement(line);
			}
			if (v != null) {
				headers.addElement(v);
			}
		} catch (Exception e) {
			System.err.println(e);
			System.exit(0);
		}
		//for (int i = 0; i < descriptions.size(); i++) {
		//System.out.println("--" + descriptions.elementAt(i).toString() + );
		//dbg(" " + printVector((Vector)descriptions.elementAt(i));
		System.out.println(printVector(headers));
		//dbg("== Loading properties");
		//for (Enumeration e = descriptions.getKeys(); e.hasMoreElements();) {
		//	dbg("   " + e.nextElement());
		//}

		//
		//
		//boolean printEnd = false;
		try {
			if (soort == 100) {
				out.println("public static Descriptor buildDescriptor" + k + "() {");
			}
			if (soort == 200) {
				out.println("public static TableDefinition buildTableDefinition" + k + "() {");
			}
			for (int i = 0; i < headers.size(); i++) {
				Vector l = (Vector) headers.elementAt(i);
				System.out.println("Header: " + headers.elementAt(i));
				Object[] testArgs = {new Integer(i)};
				MessageFormat formMap = new MessageFormat((soort == 100 ? (i == 0 ? "d{0}" : "aMap{0}") : (i == 0 ? "def{0}" : "aDef{0}")));
				for (int j = 0; j < l.size(); j++) {
					String elem = (String) l.elementAt(j);
					//dbg("--#" + i + ", |" + elem + "|");
					Object o = tokens.handleGetObject(elem);
					MessageFormat form = new MessageFormat("" + o);
					if (o != null) {
						// print constructor
						out.println("  " + form.format(testArgs));
						//printEnd = true;
						continue;
					} else {
						// print methods
						//dbg("-- elem: " + elem);
						StringTokenizer st = new StringTokenizer(elem, "=");
						try {
							String anElement = st.nextToken();
							//dbg(" anElement: |" + anElement + "|");
							Object oo = descriptions.handleGetObject(anElement);
							//dbg(" anElement: |" + anElement + "|" + oo);
							if (oo == null) {
								continue;
							}
							Vector par = new Vector();
							Object[] pars = null;
							while (st.hasMoreTokens()) {
								pars = (streamString(st.nextToken(), translations));
							};
							//Object[] pars = toArray(par);
							dbg("-- Vector:" + par + " for " + oo);
							MessageFormat form1 = new MessageFormat("" + oo);
							out.println("  " + formMap.format(testArgs) + "." + form1.format(pars));
						} catch (NoSuchElementException n) {
						}
					}
				}
				if (i != 0) {
					if (soort == 100)
						out.println("  d0.addMapping(" + formMap.format(testArgs) + ");");
					else
						if (soort == 200) {
							out.println("  def0.addField(" + formMap.format(testArgs) + ");");
						}
				}
				out.println("// ---");
			}
			if (soort == 100)
				out.println("   return d0;");
			if (soort == 200)
				out.println("   return def0;");
			out.println("}");
		} catch (Throwable t) {
			t.printStackTrace();
		}
	}
	out.flush();
	out.close();
	//}
}
/**
 * This method was created in VisualAge.
 * @param v java.util.Vector
 */
public static String printVector(Vector v) {
	if (v == null || v.size() == 0) {
		return "[]";
	}
	StringBuffer s = new StringBuffer();
	s.append("\n[");
	for (int i = 0; i < v.size(); i++) {
		Object o = v.elementAt(i);
		if (o instanceof Vector) {
			s.append(printVector((Vector) o));
		} else
			s.append(o);
		s.append(",");
	}
	s.append("]");
	return s.toString();
}
/**
 * Insert the method's description here.
 * Creation date: (12/31/99 8:51:19 PM)
 * @return java.lang.String
 * @param str java.lang.String
 */
public static String[] streamString(String str,PropertyResourceBundle prb) {
	String s = new String(str);
	Vector elements = new Vector();
	try {
		BufferedReader br = new BufferedReader(new StringReader(str));
		StreamTokenizer st = new StreamTokenizer(br);
		//StreamTokenizer st =
		//    new StreamTokenizer(new StringReader(
		//    "this is a test"));
		st.resetSyntax();
		st.commentChar('#');
		//st.quoteChar('\'');
		st.ordinaryChar('.');
		st.wordChars('A', 'z');
		st.wordChars('0','9');
		st.wordChars('.', '.');
		int type;
		Object dummy = new Object();
		while ((type = st.nextToken()) != StreamTokenizer.TT_EOF) {
			if (type == StreamTokenizer.TT_WORD) {
				System.out.println(">- " + st.sval);
				if ( prb.handleGetObject(st.sval) != null) {
				elements.addElement(prb.handleGetObject(st.sval));
				} else
				elements.addElement(st.sval);
			}
		}
		br.close();
	} catch (Exception e) {
	}
	dbg(" in streamSTring: " + printVector(elements));
	return toArray(elements);
}
/**
 * Insert the method's description here.
 * Creation date: (12/31/99 10:07:40 PM)
 * @return java.lang.Object[]
 * @param a java.util.Vector
 */
public static String[] toArray(Vector a) {
	String [] aa = new String[a.size()];
	for (int i=0; i < aa.length;i++) {
		aa[i] = a.elementAt(i).toString();
	}
	return aa;
}
}
1