| Method from org.hibernate.tool.hbm2x.TemplateHelper Detail: |
public void ensureExistence(File destination) {
// if the directory exists, make sure it is a directory
File dir = destination.getAbsoluteFile().getParentFile();
if ( dir.exists() && !dir.isDirectory() ) {
throw new ExporterException("The path: " + dir.getAbsolutePath() + " exists, but is not a directory");
} // else make the directory and any non-existent parent directories
else if ( !dir.exists() ) {
if ( !dir.mkdirs() ) {
if(dir.getName().equals(".")) { // Workaround that Linux/JVM apparently can't handle mkdirs of File's with current dir references.
if(dir.getParentFile().mkdirs()) {
return;
}
}
throw new ExporterException( "unable to create directory: " + dir.getAbsolutePath() );
}
}
}
|
protected SimpleHash getContext() {
return context;
}
|
public File getOutputDirectory() {
return outputDirectory;
}
|
protected String getTemplatePrefix() {
return templatePrefix;
}
|
public void init(File outputDirectory,
String[] templatePaths) {
this.outputDirectory = outputDirectory;
context = new SimpleHash(ObjectWrapper.BEANS_WRAPPER);
freeMarkerEngine = new Configuration();
List loaders = new ArrayList();
for (int i = 0; i < templatePaths.length; i++) {
File file = new File(templatePaths[i]);
if(file.exists() && file.isDirectory()) {
try {
loaders.add(new FileTemplateLoader(file));
}
catch (IOException e) {
throw new ExporterException("Problems with templatepath " + file, e);
}
} else {
log.warn("template path" + file + " either does not exist or is not a directory");
}
}
loaders.add(new ClassTemplateLoader(this.getClass(),"/")); // the template names are like pojo/Somewhere so have to be a rooted classpathloader
freeMarkerEngine.setTemplateLoader(new MultiTemplateLoader((TemplateLoader[]) loaders.toArray(new TemplateLoader[loaders.size()])));
}
|
protected Object internalPutInContext(String key,
Object value) {
TemplateModel model = null;
try {
model = getContext().get(key);
}
catch (TemplateModelException e) {
throw new ExporterException("Could not get key " + key, e);
}
getContext().put(key, value);
return model;
}
|
protected Object internalRemoveFromContext(String key) {
TemplateModel model = null;
try {
model = getContext().get(key);
}
catch (TemplateModelException e) {
throw new ExporterException("Could not get key " + key, e);
}
getContext().remove(key);
return model;
}
|
public void processString(String template,
Writer output) {
try {
Reader r = new StringReader(template);
Template t = new Template("unknown", r, freeMarkerEngine);
t.process(getContext(), output);
}
catch (IOException e) {
throw new ExporterException("Error while processing template string", e);
}
catch (TemplateException te) {
throw new ExporterException("Error while processing template string", te);
}
catch (Exception e) {
throw new ExporterException("Error while processing template string", e);
}
}
|
public void processTemplate(String templateName,
Writer output) {
try {
Template template = freeMarkerEngine.getTemplate(templateName);
template.process(getContext(), output);
}
catch (IOException e) {
throw new ExporterException("Error while processing template " + templateName, e);
}
catch (TemplateException te) {
throw new ExporterException("Error while processing template " + templateName, te);
}
catch (Exception e) {
throw new ExporterException("Error while processing template " + templateName, e);
}
}
look up the template named templateName via the paths and print the content to the output |
public void putInContext(String key,
Object value) {
log.trace("putInContext " + key + "=" + value);
if(value == null) throw new IllegalStateException("value must not be null for " + key);
Object replaced = internalPutInContext(key,value);
if(replaced!=null) {
log.warn( "Overwriting " + replaced + " when setting " + key + " to " + value + ".");
}
}
|
public void removeFromContext(String key,
Object expected) {
log.trace("removeFromContext " + key + "=" + expected);
Object replaced = internalRemoveFromContext(key);
if(replaced==null) throw new IllegalStateException(key + " did not exist in template context.");
/*if(replaced!=expected) { //FREEMARKER-TODO: how can i validate this ? or maybe not needed to validate since mutation is considered bad ?
throw new IllegalStateException("expected " + key + " to be bound to " + expected + " but was to " + replaced);
}*/
}
|
public void setupContext() {
getContext().put("version", Version.getDefault());
getContext().put("ctx", getContext() ); //TODO: I would like to remove this, but don't know another way to actually get the list possible "root" keys for debugging.
getContext().put("templates", new Templates());
getContext().put("date", new SimpleDate(new Date(), TemplateDateModel.DATETIME));
}
|
public boolean templateExists(String templateName) {
TemplateLoader templateLoader = freeMarkerEngine.getTemplateLoader();
try {
return templateLoader.findTemplateSource(templateName)!=null;
}
catch (IOException e) {
throw new ExporterException("templateExists for " + templateName + " failed", e);
}
}
Check if the template exists. Tries to search with the templatePrefix first and then secondly without the template prefix. |