01: /*
02: * Created on Aug 11, 2004
03: *
04: * To change the template for this generated file go to
05: * Window - Preferences - Java - Code Generation - Code and Comments
06: */
07: package tools;
08:
09: import java.io.File;
10: import java.io.FileOutputStream;
11: import java.io.OutputStream;
12: import java.io.OutputStreamWriter;
13: import java.io.Writer;
14:
15: import myapp.Product;
16:
17: import org.apache.commons.logging.Log;
18: import org.apache.commons.logging.LogFactory;
19: import org.exolab.castor.tools.MappingTool;
20: import org.exolab.castor.xml.XMLContext;
21:
22: /**
23: * generate a Castor mapping file ready for editing
24: */
25:
26: public class CastorMapper {
27:
28: private MappingTool tool = null;
29: private static Log log = LogFactory.getFactory().getInstance(
30: CastorMapper.class);
31:
32: private static final String OUTPUT_FILE = "product-mapping.xml";
33:
34: public CastorMapper() {
35: try {
36: tool = new XMLContext().createMappingTool();
37: } catch (Exception e) {
38: log.error(e.getClass().getName(), e);
39: }
40: }
41:
42: public void createMapping(Class[] someClasses, String outputFile) {
43: try {
44: for (int i = 0; i < someClasses.length; i++) {
45: tool.addClass(someClasses[i]);
46: }
47: File file = new File(outputFile);
48: log.debug("Output will has been created at "
49: + file.getAbsolutePath());
50: OutputStream stream = new FileOutputStream(file);
51:
52: Writer writer = new OutputStreamWriter(stream);
53: tool.write(writer);
54: writer.close();
55: } catch (Exception e) {
56: log.error(e.getClass().getName(), e);
57: }
58: }
59:
60: public void createMapping(Class aClass, String outputFile) {
61: try {
62: tool.addClass(aClass);
63: File file = new File(outputFile);
64: log.debug("Output will has been created at "
65: + file.getAbsolutePath());
66: OutputStream stream = new FileOutputStream(file);
67:
68: Writer writer = new OutputStreamWriter(stream);
69: tool.write(writer);
70: writer.close();
71: } catch (Exception e) {
72: log.error(e.getClass().getName(), e);
73: }
74: }
75:
76: public static void main(String[] args) {
77: CastorMapper mapper = new CastorMapper();
78: mapper.createMapping(Product.class, OUTPUT_FILE);
79: }
80:
81: }
|