* The output directory.
* @throws Exception
*/
public void generate(File outputDir) throws Exception {
Service service = new Service(serviceRef);
Metadata metadata = (Metadata) service.getMetadata();
if (metadata == null) {
throw new Exception("Can't get the metadata for this service: "
+ serviceRef);
}
Configuration fmc = new Configuration();
fmc.setDefaultEncoding(CharacterSet.UTF_8.getName());
// Generate classes
String rootTemplates = "clap://class/org/restlet/ext/odata/internal/templates";
Representation complexTmpl = new StringRepresentation(
new ClientResource(rootTemplates + "/complexType.ftl").get()
.getText());
Representation entityTmpl = new StringRepresentation(
new ClientResource(rootTemplates + "/entityType.ftl").get()
.getText());
Representation serviceTmpl = new StringRepresentation(
new ClientResource(rootTemplates + "/service.ftl").get()
.getText());
for (Schema schema : metadata.getSchemas()) {
if ((schema.getEntityTypes() != null && !schema.getEntityTypes()
.isEmpty())
|| (schema.getComplexTypes() != null && !schema
.getComplexTypes().isEmpty())) {
String packageName = TypeUtils.getPackageName(schema);
File packageDir = new File(outputDir, packageName.replace(".",
System.getProperty("file.separator")));
packageDir.mkdirs();
// For each entity type
for (EntityType type : schema.getEntityTypes()) {
String className = type.getClassName();
Map<String, Object> dataModel = new HashMap<String, Object>();
dataModel.put("type", type);
dataModel.put("schema", schema);
dataModel.put("metadata", metadata);
dataModel.put("className", className);
dataModel.put("packageName", packageName);
TemplateRepresentation templateRepresentation = new TemplateRepresentation(
entityTmpl, fmc, dataModel, MediaType.TEXT_PLAIN);
templateRepresentation.setCharacterSet(CharacterSet.UTF_8);
// Write the template representation as a Java class
templateRepresentation.write(new FileOutputStream(new File(
packageDir, type.getClassName() + ".java")));
}
for (ComplexType type : schema.getComplexTypes()) {
String className = type.getClassName();
Map<String, Object> dataModel = new HashMap<String, Object>();
dataModel.put("type", type);
dataModel.put("schema", schema);
dataModel.put("metadata", metadata);
dataModel.put("className", className);
dataModel.put("packageName", packageName);
TemplateRepresentation templateRepresentation = new TemplateRepresentation(
complexTmpl, fmc, dataModel, MediaType.TEXT_PLAIN);
templateRepresentation.setCharacterSet(CharacterSet.UTF_8);
// Write the template representation as a Java class
templateRepresentation.write(new FileOutputStream(new File(
packageDir, type.getClassName() + ".java")));
}
}
}
if (metadata.getContainers() != null
&& !metadata.getContainers().isEmpty()) {
for (EntityContainer entityContainer : metadata.getContainers()) {
Schema schema = entityContainer.getSchema();
// Generate Service subclass
StringBuffer className = new StringBuffer();
if (serviceClassName != null) {
// Try to use the Client preference
if (entityContainer.isDefaultEntityContainer()) {
className.append(serviceClassName);
} else if (metadata.getContainers().size() == 1) {
className.append(serviceClassName);
} else {
className.append(schema.getNamespace()
.getNormalizedName().substring(0, 1)
.toUpperCase());