1 package org.objectweb.celtix.tools.generators.spring; 2 3 import java.io.File ; 4 import java.io.FileOutputStream ; 5 import java.io.IOException ; 6 import java.io.PrintWriter ; 7 import java.util.ArrayList ; 8 import java.util.Collection ; 9 import java.util.List ; 10 import java.util.logging.Logger ; 11 12 import javax.xml.XMLConstants ; 13 import javax.xml.namespace.QName ; 14 15 import org.xml.sax.InputSource ; 16 17 import org.objectweb.celtix.common.i18n.Message; 18 import org.objectweb.celtix.common.logging.LogUtils; 19 import org.objectweb.celtix.configuration.ConfigurationException; 20 import org.objectweb.celtix.configuration.ConfigurationItemMetadata; 21 import org.objectweb.celtix.configuration.ConfigurationMetadata; 22 import org.objectweb.celtix.configuration.impl.ConfigurationMetadataBuilder; 23 import org.objectweb.celtix.configuration.impl.TypeSchema; 24 import org.objectweb.celtix.configuration.impl.TypeSchemaHelper; 25 import org.objectweb.celtix.jaxb.JAXBUtils; 26 27 public class BeanGenerator { 28 29 private static final Logger LOG = LogUtils.getL7dLogger(BeanGenerator.class); 30 31 File outputDir; 32 33 public BeanGenerator() { 34 outputDir = new File ("."); 35 } 36 37 public static void main(String [] args) { 38 39 BeanGenerator generator = new BeanGenerator(); 40 List <String > schemaFiles = new ArrayList <String >(); 41 int i = 0; 42 while (i < args.length) { 43 if ("-d".equals(args[i]) && i < (args.length - 1)) { 44 i++; 45 generator.setOutputDir(args[i]); 46 } else { 47 schemaFiles.addAll(splitArgument(args[i])); 48 } 49 i++; 50 } 51 for (String sf : schemaFiles) { 52 generator.generateBean(sf); 53 } 54 } 55 56 void setOutputDir(String dir) { 57 outputDir = new File (dir); 58 } 59 60 void generateBean(String path) { 61 System.out.println("Generating bean from resource : " + path); 62 InputSource src = null; 63 src = new InputSource (new File (path).toURI().toString()); 64 65 ConfigurationMetadataBuilder builder = new ConfigurationMetadataBuilder(false); 66 builder.setValidation(true); 67 ConfigurationMetadata model = null; 68 69 try { 70 model = builder.build(src); 71 } catch (IOException ex) { 72 throw new ConfigurationException(new Message("FAILED_TO_GENERATE_BEAN_EXC", LOG), ex); 73 } 74 75 String className = SpringUtils.getBeanClassName(model.getNamespaceURI()); 76 77 StringBuffer classFileName = new StringBuffer (className); 78 for (int i = 0; i < classFileName.length(); i++) { 79 if ('.' == classFileName.charAt(i)) { 80 classFileName.setCharAt(i, File.separatorChar); 81 } 82 } 83 classFileName.append(".java"); 84 85 File classFile = new File (outputDir, classFileName.toString()); 86 File dir = classFile.getParentFile(); 87 if (!dir.exists()) { 88 dir.mkdirs(); 89 } 90 91 LOG.info("Generating class: " + className + "\n" 92 + " file: " + classFile.getPath()); 93 94 PrintWriter pw = null; 95 try { 96 pw = new PrintWriter (new FileOutputStream (classFile)); 97 writeClass(pw, model, className); 99 } catch (IOException ex) { 100 throw new ConfigurationException(new Message("FAILED_TO_GENERATE_BEAN_EXC", LOG), ex); 101 } finally { 102 pw.close(); 103 } 104 } 105 106 void writeClass(PrintWriter pw, ConfigurationMetadata model, String qualifiedClassName) { 107 108 int index = qualifiedClassName.lastIndexOf('.'); 109 110 String packageName = qualifiedClassName.substring(0, index); 111 String className = qualifiedClassName.substring(index + 1); 112 pw.print("package "); 113 pw.print(packageName); 114 pw.println(";"); 115 pw.println(); 116 117 writeImports(pw, model); 118 119 pw.print("public class "); 120 pw.print(className); 121 pw.println(" {"); 122 pw.println(); 123 124 writeDataMembers(pw, model); 125 writeAccessors(pw, model); 126 pw.println("}"); 127 } 128 129 private void writeImports(PrintWriter pw, ConfigurationMetadata model) { 130 Collection <String > classNames = new ArrayList <String >(); 131 132 for (ConfigurationItemMetadata definition : model.getDefinitions()) { 133 QName type = definition.getType(); 134 String className = getClassName(type, true); 135 int index = className.lastIndexOf('.'); 136 if (index < 0 || "java.lang".equals(className.substring(0, index))) { 137 continue; 138 } 139 if (!classNames.contains(className)) { 140 classNames.add(className); 141 } 142 } 143 144 if (!classNames.contains("java.util.Collection")) { 145 classNames.add("java.util.Collection"); 146 } 147 148 if (!classNames.contains("java.util.ArrayList")) { 149 classNames.add("java.util.ArrayList"); 150 } 151 152 for (String className : classNames) { 153 pw.print("import "); 154 pw.print(className); 155 pw.println(";"); 156 } 157 pw.println(); 158 } 159 160 private void writeDataMembers(PrintWriter pw, ConfigurationMetadata model) { 161 162 for (ConfigurationItemMetadata definition : model.getDefinitions()) { 163 QName type = definition.getType(); 164 String className = getClassName(type, false); 165 String memberName = JAXBUtils.nameToIdentifier(definition.getName(), 166 JAXBUtils.IdentifierType.VARIABLE); 167 pw.print(" private "); 168 pw.print(className); 169 pw.print(" "); 170 pw.print(memberName); 171 pw.println(";"); 172 } 173 pw.println(); 174 pw.print(" private Collection<String> _initialized = "); 175 pw.println("new ArrayList<String>();"); 176 177 pw.println(); 178 } 179 180 private void writeAccessors(PrintWriter pw, ConfigurationMetadata model) { 181 182 for (ConfigurationItemMetadata definition : model.getDefinitions()) { 183 QName type = definition.getType(); 184 String className = getClassName(type, false); 185 String memberName = JAXBUtils.nameToIdentifier(definition.getName(), 186 JAXBUtils.IdentifierType.VARIABLE); 187 188 pw.print(" public "); 189 pw.print(className); 190 pw.print(" "); 191 pw.print(JAXBUtils.nameToIdentifier(definition.getName(), JAXBUtils.IdentifierType.GETTER)); 192 pw.println("() {"); 193 pw.print(" return "); 194 pw.print(memberName); 195 pw.println(";"); 196 pw.println(" }"); 197 pw.println(); 198 199 pw.print(" public void "); 200 pw.print(JAXBUtils.nameToIdentifier(definition.getName(), JAXBUtils.IdentifierType.SETTER)); 201 pw.print("("); 202 pw.print(className); 203 pw.println(" obj) {"); 204 pw.print(" "); 205 pw.print(memberName); 206 pw.println(" = obj;"); 207 pw.print(" if (!_initialized.contains(\""); 208 pw.print(definition.getName()); 209 pw.println("\")) {"); 210 pw.print(" _initialized.add(\""); 211 pw.print(definition.getName()); 212 pw.println("\");"); 213 pw.println(" }"); 214 pw.println(" }"); 215 pw.println(); 216 } 217 218 pw.println(" public boolean isSet(String name) {"); 219 pw.println(" return _initialized.contains(name);"); 220 pw.println(" }"); 221 } 222 223 public static String getClassName(QName typeName, boolean qualified) { 224 String baseType = null; 225 TypeSchema ts = null; 226 if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(typeName.getNamespaceURI())) { 227 baseType = typeName.getLocalPart(); 228 } else { 229 ts = new TypeSchemaHelper(false).get(typeName.getNamespaceURI()); 230 baseType = ts.getXMLSchemaBaseType(typeName.getLocalPart()); 231 } 232 String className = null; 233 if (null != baseType) { 234 className = JAXBUtils.builtInTypeToJavaType(baseType); 235 if (className != null && !qualified) { 236 int index = className.lastIndexOf('.'); 237 if (index >= 0) { 238 className = className.substring(index + 1); 239 } 240 } 241 } 242 if (null == className) { 243 baseType = typeName.getLocalPart(); 244 className = JAXBUtils.nameToIdentifier(baseType, 245 JAXBUtils.IdentifierType.CLASS); 246 if (qualified) { 247 className = ts.getPackageName() + "." + className; 248 } 249 } 250 return className; 251 } 252 253 private static List <String > splitArgument(String arg) { 254 List <String > filenames = new ArrayList <String >(); 255 256 String filename = null; 257 int from = 0; 258 while (from < arg.length()) { 259 int to = 0; 260 if (arg.indexOf("\"", from) == from) { 261 to = arg.indexOf("\"", from + 1); 262 if (to >= 0) { 263 filename = arg.substring(from + 1, to); 264 to++; 265 } else { 266 throw new IllegalArgumentException (new Message("MISMATCHED_QUOTES_EXC", LOG).toString()); 267 } 268 } else { 269 to = from; 270 while (to < arg.length() && !Character.isWhitespace(arg.charAt(to))) { 271 to++; 272 } 273 filename = arg.substring(from, to); 274 } 275 while (to < arg.length() && Character.isWhitespace(arg.charAt(to))) { 276 to++; 277 } 278 from = to; 279 filenames.add(filename); 280 } 281 return filenames; 282 } 283 } 284 | Popular Tags |