001: package org.objectweb.celtix.tools.generators.spring;
002:
003: import java.io.File;
004: import java.io.FileOutputStream;
005: import java.io.IOException;
006: import java.io.PrintWriter;
007: import java.util.ArrayList;
008: import java.util.Collection;
009: import java.util.List;
010: import java.util.logging.Logger;
011:
012: import javax.xml.XMLConstants;
013: import javax.xml.namespace.QName;
014:
015: import org.xml.sax.InputSource;
016:
017: import org.objectweb.celtix.common.i18n.Message;
018: import org.objectweb.celtix.common.logging.LogUtils;
019: import org.objectweb.celtix.configuration.ConfigurationException;
020: import org.objectweb.celtix.configuration.ConfigurationItemMetadata;
021: import org.objectweb.celtix.configuration.ConfigurationMetadata;
022: import org.objectweb.celtix.configuration.impl.ConfigurationMetadataBuilder;
023: import org.objectweb.celtix.configuration.impl.TypeSchema;
024: import org.objectweb.celtix.configuration.impl.TypeSchemaHelper;
025: import org.objectweb.celtix.jaxb.JAXBUtils;
026:
027: public class BeanGenerator {
028:
029: private static final Logger LOG = LogUtils
030: .getL7dLogger(BeanGenerator.class);
031:
032: File outputDir;
033:
034: public BeanGenerator() {
035: outputDir = new File(".");
036: }
037:
038: public static void main(String[] args) {
039:
040: BeanGenerator generator = new BeanGenerator();
041: List<String> schemaFiles = new ArrayList<String>();
042: int i = 0;
043: while (i < args.length) {
044: if ("-d".equals(args[i]) && i < (args.length - 1)) {
045: i++;
046: generator.setOutputDir(args[i]);
047: } else {
048: schemaFiles.addAll(splitArgument(args[i]));
049: }
050: i++;
051: }
052: for (String sf : schemaFiles) {
053: generator.generateBean(sf);
054: }
055: }
056:
057: void setOutputDir(String dir) {
058: outputDir = new File(dir);
059: }
060:
061: void generateBean(String path) {
062: System.out.println("Generating bean from resource : " + path);
063: InputSource src = null;
064: src = new InputSource(new File(path).toURI().toString());
065:
066: ConfigurationMetadataBuilder builder = new ConfigurationMetadataBuilder(
067: false);
068: builder.setValidation(true);
069: ConfigurationMetadata model = null;
070:
071: try {
072: model = builder.build(src);
073: } catch (IOException ex) {
074: throw new ConfigurationException(new Message(
075: "FAILED_TO_GENERATE_BEAN_EXC", LOG), ex);
076: }
077:
078: String className = SpringUtils.getBeanClassName(model
079: .getNamespaceURI());
080:
081: StringBuffer classFileName = new StringBuffer(className);
082: for (int i = 0; i < classFileName.length(); i++) {
083: if ('.' == classFileName.charAt(i)) {
084: classFileName.setCharAt(i, File.separatorChar);
085: }
086: }
087: classFileName.append(".java");
088:
089: File classFile = new File(outputDir, classFileName.toString());
090: File dir = classFile.getParentFile();
091: if (!dir.exists()) {
092: dir.mkdirs();
093: }
094:
095: LOG.info("Generating class: " + className + "\n"
096: + " file: " + classFile.getPath());
097:
098: PrintWriter pw = null;
099: try {
100: pw = new PrintWriter(new FileOutputStream(classFile));
101: // pw = new PrintWriter(System.out);
102: writeClass(pw, model, className);
103: } catch (IOException ex) {
104: throw new ConfigurationException(new Message(
105: "FAILED_TO_GENERATE_BEAN_EXC", LOG), ex);
106: } finally {
107: pw.close();
108: }
109: }
110:
111: void writeClass(PrintWriter pw, ConfigurationMetadata model,
112: String qualifiedClassName) {
113:
114: int index = qualifiedClassName.lastIndexOf('.');
115:
116: String packageName = qualifiedClassName.substring(0, index);
117: String className = qualifiedClassName.substring(index + 1);
118: pw.print("package ");
119: pw.print(packageName);
120: pw.println(";");
121: pw.println();
122:
123: writeImports(pw, model);
124:
125: pw.print("public class ");
126: pw.print(className);
127: pw.println(" {");
128: pw.println();
129:
130: writeDataMembers(pw, model);
131: writeAccessors(pw, model);
132: pw.println("}");
133: }
134:
135: private void writeImports(PrintWriter pw,
136: ConfigurationMetadata model) {
137: Collection<String> classNames = new ArrayList<String>();
138:
139: for (ConfigurationItemMetadata definition : model
140: .getDefinitions()) {
141: QName type = definition.getType();
142: String className = getClassName(type, true);
143: int index = className.lastIndexOf('.');
144: if (index < 0
145: || "java.lang"
146: .equals(className.substring(0, index))) {
147: continue;
148: }
149: if (!classNames.contains(className)) {
150: classNames.add(className);
151: }
152: }
153:
154: if (!classNames.contains("java.util.Collection")) {
155: classNames.add("java.util.Collection");
156: }
157:
158: if (!classNames.contains("java.util.ArrayList")) {
159: classNames.add("java.util.ArrayList");
160: }
161:
162: for (String className : classNames) {
163: pw.print("import ");
164: pw.print(className);
165: pw.println(";");
166: }
167: pw.println();
168: }
169:
170: private void writeDataMembers(PrintWriter pw,
171: ConfigurationMetadata model) {
172:
173: for (ConfigurationItemMetadata definition : model
174: .getDefinitions()) {
175: QName type = definition.getType();
176: String className = getClassName(type, false);
177: String memberName = JAXBUtils.nameToIdentifier(definition
178: .getName(), JAXBUtils.IdentifierType.VARIABLE);
179: pw.print(" private ");
180: pw.print(className);
181: pw.print(" ");
182: pw.print(memberName);
183: pw.println(";");
184: }
185: pw.println();
186: pw.print(" private Collection<String> _initialized = ");
187: pw.println("new ArrayList<String>();");
188:
189: pw.println();
190: }
191:
192: private void writeAccessors(PrintWriter pw,
193: ConfigurationMetadata model) {
194:
195: for (ConfigurationItemMetadata definition : model
196: .getDefinitions()) {
197: QName type = definition.getType();
198: String className = getClassName(type, false);
199: String memberName = JAXBUtils.nameToIdentifier(definition
200: .getName(), JAXBUtils.IdentifierType.VARIABLE);
201:
202: pw.print(" public ");
203: pw.print(className);
204: pw.print(" ");
205: pw.print(JAXBUtils.nameToIdentifier(definition.getName(),
206: JAXBUtils.IdentifierType.GETTER));
207: pw.println("() {");
208: pw.print(" return ");
209: pw.print(memberName);
210: pw.println(";");
211: pw.println(" }");
212: pw.println();
213:
214: pw.print(" public void ");
215: pw.print(JAXBUtils.nameToIdentifier(definition.getName(),
216: JAXBUtils.IdentifierType.SETTER));
217: pw.print("(");
218: pw.print(className);
219: pw.println(" obj) {");
220: pw.print(" ");
221: pw.print(memberName);
222: pw.println(" = obj;");
223: pw.print(" if (!_initialized.contains(\"");
224: pw.print(definition.getName());
225: pw.println("\")) {");
226: pw.print(" _initialized.add(\"");
227: pw.print(definition.getName());
228: pw.println("\");");
229: pw.println(" }");
230: pw.println(" }");
231: pw.println();
232: }
233:
234: pw.println(" public boolean isSet(String name) {");
235: pw.println(" return _initialized.contains(name);");
236: pw.println(" }");
237: }
238:
239: public static String getClassName(QName typeName, boolean qualified) {
240: String baseType = null;
241: TypeSchema ts = null;
242: if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(typeName
243: .getNamespaceURI())) {
244: baseType = typeName.getLocalPart();
245: } else {
246: ts = new TypeSchemaHelper(false).get(typeName
247: .getNamespaceURI());
248: baseType = ts.getXMLSchemaBaseType(typeName.getLocalPart());
249: }
250: String className = null;
251: if (null != baseType) {
252: className = JAXBUtils.builtInTypeToJavaType(baseType);
253: if (className != null && !qualified) {
254: int index = className.lastIndexOf('.');
255: if (index >= 0) {
256: className = className.substring(index + 1);
257: }
258: }
259: }
260: if (null == className) {
261: baseType = typeName.getLocalPart();
262: className = JAXBUtils.nameToIdentifier(baseType,
263: JAXBUtils.IdentifierType.CLASS);
264: if (qualified) {
265: className = ts.getPackageName() + "." + className;
266: }
267: }
268: return className;
269: }
270:
271: private static List<String> splitArgument(String arg) {
272: List<String> filenames = new ArrayList<String>();
273:
274: String filename = null;
275: int from = 0;
276: while (from < arg.length()) {
277: int to = 0;
278: if (arg.indexOf("\"", from) == from) {
279: to = arg.indexOf("\"", from + 1);
280: if (to >= 0) {
281: filename = arg.substring(from + 1, to);
282: to++;
283: } else {
284: throw new IllegalArgumentException(new Message(
285: "MISMATCHED_QUOTES_EXC", LOG).toString());
286: }
287: } else {
288: to = from;
289: while (to < arg.length()
290: && !Character.isWhitespace(arg.charAt(to))) {
291: to++;
292: }
293: filename = arg.substring(from, to);
294: }
295: while (to < arg.length()
296: && Character.isWhitespace(arg.charAt(to))) {
297: to++;
298: }
299: from = to;
300: filenames.add(filename);
301: }
302: return filenames;
303: }
304: }
|