org.hibernate.tool.hbm2x.HibernateConfigurationExporter Maven / Gradle / Ivy
/*
* Created on 2004-12-03
*
*/
package org.hibernate.tool.hbm2x;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import java.util.TreeMap;
import org.hibernate.cfg.Environment;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.RootClass;
/**
* @author max
*
*/
public class HibernateConfigurationExporter extends AbstractExporter {
private Writer output;
private Properties customProperties = new Properties();
public Properties getCustomProperties() {
return customProperties;
}
public void setCustomProperties(Properties customProperties) {
this.customProperties = customProperties;
}
public Writer getOutput() {
return output;
}
public void setOutput(Writer output) {
this.output = output;
}
/* (non-Javadoc)
* @see org.hibernate.tool.hbm2x.Exporter#finish()
*/
public void doStart() throws ExporterException {
PrintWriter pw = null;
File file = null;
try {
if(output==null) {
file = new File(getOutputDirectory(), "hibernate.cfg.xml");
getTemplateHelper().ensureExistence(file);
pw = new PrintWriter(new FileWriter(file) );
getArtifactCollector().addFile(file, "cfg.xml");
}
else {
pw = new PrintWriter(output);
}
pw.println("\n" +
"\r\n" +
"");
boolean ejb3 = Boolean.valueOf((String)getProperties().get("ejb3")).booleanValue();
Map ");
}
catch (IOException e) {
throw new ExporterException("Problems while creating hibernate.cfg.xml", e);
}
finally {
if(pw!=null) {
pw.flush();
pw.close();
}
}
}
/**
* @param pw
* @param element
*/
private void dump(PrintWriter pw, boolean useClass, PersistentClass element) {
if(useClass) {
pw.println(" ");
} else {
pw.println(" ");
}
Iterator> directSubclasses = element.getDirectSubclasses();
while (directSubclasses.hasNext() ) {
PersistentClass subclass = (PersistentClass)directSubclasses.next();
dump(pw, useClass, subclass);
}
}
/**
* @param element
* @return
*/
private String getMappingFileResource(PersistentClass element) {
return element.getClassName().replace('.', '/') + ".hbm.xml";
}
public String getName() {
return "cfg2cfgxml";
}
/**
*
* @param text
* @return String with escaped [<,>] special characters.
*/
public static String forXML(String text) {
if (text == null) return null;
final StringBuilder result = new StringBuilder();
char[] chars = text.toCharArray();
for (int i = 0; i < chars.length; i++){
char character = chars[i];
if (character == '<') {
result.append("<");
} else if (character == '>'){
result.append(">");
} else {
result.append(character);
}
}
return result.toString();
}
}