Skip to content

Commit 27660a4

Browse files
committed
HBX-3210: Improve the implementation of various 'exporter' classes and their helpers
Signed-off-by: Koen Aers <[email protected]>
1 parent 1c21a60 commit 27660a4

19 files changed

+4419
-4437
lines changed

orm/src/main/java/org/hibernate/tool/internal/export/cfg/CfgExporter.java

Lines changed: 137 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.io.PrintWriter;
2424
import java.io.Writer;
2525
import java.util.HashMap;
26-
import java.util.Iterator;
2726
import java.util.Map;
2827
import java.util.Map.Entry;
2928
import java.util.Properties;
@@ -42,162 +41,147 @@
4241
*/
4342
public class CfgExporter extends AbstractExporter {
4443

45-
private Writer output;
44+
private Writer output;
4645
private Properties customProperties = new Properties();
47-
48-
public Properties getCustomProperties() {
49-
return customProperties;
50-
}
51-
52-
public void setCustomProperties(Properties customProperties) {
53-
this.customProperties = customProperties;
54-
}
55-
56-
public Writer getOutput() {
57-
return output;
58-
}
59-
60-
public void setOutput(Writer output) {
61-
this.output = output;
62-
}
63-
64-
/* (non-Javadoc)
65-
* @see org.hibernate.tool.hbm2x.Exporter#finish()
66-
*/
67-
public void doStart() {
68-
PrintWriter pw = null;
69-
File file = null;
70-
try {
71-
if(output==null) {
72-
file = new File(getOutputDirectory(), "hibernate.cfg.xml");
73-
getTemplateHelper().ensureExistence(file);
74-
pw = new PrintWriter(new FileWriter(file) );
75-
getArtifactCollector().addFile(file, "cfg.xml");
76-
}
77-
else {
78-
pw = new PrintWriter(output);
46+
47+
public Properties getCustomProperties() {
48+
return customProperties;
49+
}
50+
51+
public void setCustomProperties(Properties customProperties) {
52+
this.customProperties = customProperties;
53+
}
54+
55+
public Writer getOutput() {
56+
return output;
57+
}
58+
59+
public void setOutput(Writer output) {
60+
this.output = output;
61+
}
62+
63+
/* (non-Javadoc)
64+
* @see org.hibernate.tool.hbm2x.Exporter#finish()
65+
*/
66+
public void doStart() {
67+
PrintWriter pw = null;
68+
File file;
69+
try {
70+
if(output==null) {
71+
file = new File(getOutputDirectory(), "hibernate.cfg.xml");
72+
getTemplateHelper().ensureExistence(file);
73+
pw = new PrintWriter(new FileWriter(file) );
74+
getArtifactCollector().addFile(file, "cfg.xml");
75+
}
76+
else {
77+
pw = new PrintWriter(output);
78+
}
79+
80+
81+
pw.println( """
82+
<?xml version="1.0" encoding="UTF-8"?>
83+
<!DOCTYPE hibernate-configuration PUBLIC\r
84+
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"\r
85+
"https://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">\r
86+
<hibernate-configuration>""" );
87+
88+
boolean ejb3 = Boolean.parseBoolean( (String) getProperties().get( "ejb3" ) );
89+
90+
Map<Object, Object> props = new TreeMap<>();
91+
if (getProperties() != null) {
92+
props.putAll(getProperties());
93+
}
94+
if(customProperties!=null) {
95+
props.putAll(customProperties);
96+
}
97+
98+
String sfname = (String) props.get(Environment.SESSION_FACTORY_NAME);
99+
pw.println(" <session-factory" + (sfname==null?"":" name=\"" + sfname + "\"") + ">");
100+
101+
Map<Object, Object> ignoredProperties = new HashMap<>();
102+
ignoredProperties.put(Environment.SESSION_FACTORY_NAME, null);
103+
ignoredProperties.put(Environment.HBM2DDL_AUTO, "false" );
104+
ignoredProperties.put("hibernate.temp.use_jdbc_metadata_defaults", null );
105+
ignoredProperties.put(Environment.TRANSACTION_COORDINATOR_STRATEGY, "org.hibernate.console.FakeTransactionManagerLookup");
106+
107+
Set<Entry<Object, Object>> set = props.entrySet();
108+
for ( Entry<Object, Object> element : set ) {
109+
String key = (String) element.getKey();
110+
if ( ignoredProperties.containsKey( key ) ) {
111+
Object ignoredValue = ignoredProperties.get( key );
112+
if ( ignoredValue == null || element.getValue().equals( ignoredValue ) ) {
113+
continue;
114+
}
115+
}
116+
if ( key.startsWith( "hibernate." ) ) { // if not starting with hibernate. not relevant for cfg.xml
117+
pw.println( " <property name=\"" + key + "\">" + forXML(
118+
element.getValue().toString() ) + "</property>" );
119+
}
120+
}
121+
122+
if(getMetadata()!=null) {
123+
for ( PersistentClass element : getMetadata().getEntityBindings() ) {
124+
if ( element instanceof RootClass ) {
125+
dump( pw, ejb3, element );
126+
}
127+
}
128+
}
129+
pw.println(" </session-factory>\r\n" +
130+
"</hibernate-configuration>");
131+
132+
}
133+
134+
catch (IOException e) {
135+
throw new RuntimeException("Problems while creating hibernate.cfg.xml", e);
136+
}
137+
finally {
138+
if(pw!=null) {
139+
pw.flush();
140+
pw.close();
141+
}
79142
}
80-
81-
82-
pw.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
83-
"<!DOCTYPE hibernate-configuration PUBLIC\r\n" +
84-
" \"-//Hibernate/Hibernate Configuration DTD 3.0//EN\"\r\n" +
85-
" \"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd\">\r\n" +
86-
"<hibernate-configuration>");
87-
88-
boolean ejb3 = Boolean.valueOf((String)getProperties().get("ejb3")).booleanValue();
89-
90-
Map<Object, Object> props = new TreeMap<Object, Object>();
91-
if (getProperties() != null) {
92-
props.putAll(getProperties());
143+
144+
}
145+
146+
private void dump(PrintWriter pw, boolean useClass, PersistentClass element) {
147+
if(useClass) {
148+
pw.println("<mapping class=\"" + element.getClassName() + "\"/>");
93149
}
94-
if(customProperties!=null) {
95-
props.putAll(customProperties);
150+
else {
151+
pw.println("<mapping resource=\"" + getMappingFileResource(element) + "\"/>");
96152
}
97-
98-
String sfname = (String) props.get(Environment.SESSION_FACTORY_NAME);
99-
pw.println(" <session-factory" + (sfname==null?"":" name=\"" + sfname + "\"") + ">");
100-
101-
Map<Object, Object> ignoredProperties = new HashMap<Object, Object>();
102-
ignoredProperties.put(Environment.SESSION_FACTORY_NAME, null);
103-
ignoredProperties.put(Environment.HBM2DDL_AUTO, "false" );
104-
ignoredProperties.put("hibernate.temp.use_jdbc_metadata_defaults", null );
105-
ignoredProperties.put(Environment.TRANSACTION_COORDINATOR_STRATEGY, "org.hibernate.console.FakeTransactionManagerLookup");
106-
107-
Set<Entry<Object, Object>> set = props.entrySet();
108-
Iterator<Entry<Object, Object>> iterator = set.iterator();
109-
while (iterator.hasNext() ) {
110-
Entry<Object, Object> element = iterator.next();
111-
String key = (String) element.getKey();
112-
if(ignoredProperties.containsKey( key )) {
113-
Object ignoredValue = ignoredProperties.get( key );
114-
if(ignoredValue == null || element.getValue().equals(ignoredValue)) {
115-
continue;
116-
}
117-
}
118-
if(key.startsWith("hibernate.") ) { // if not starting with hibernate. not relevant for cfg.xml
119-
pw.println(" <property name=\"" + key + "\">" + forXML(element.getValue().toString()) + "</property>");
153+
154+
for ( Subclass value : element.getDirectSubclasses() ) {
155+
dump( pw, useClass, value );
156+
}
157+
158+
}
159+
160+
private String getMappingFileResource(PersistentClass element) {
161+
162+
return element.getClassName().replace('.', '/') + ".hbm.xml";
163+
}
164+
165+
public String getName() {
166+
return "cfg2cfgxml";
167+
}
168+
169+
public static String forXML(String text) {
170+
if (text == null) return null;
171+
final StringBuilder result = new StringBuilder();
172+
char[] chars = text.toCharArray();
173+
for ( char character : chars ) {
174+
if ( character == '<' ) {
175+
result.append( "&lt;" );
176+
}
177+
else if ( character == '>' ) {
178+
result.append( "&gt;" );
179+
}
180+
else {
181+
result.append( character );
120182
}
121183
}
122-
123-
if(getMetadata()!=null) {
124-
Iterator<PersistentClass> classMappings = getMetadata().getEntityBindings().iterator();
125-
while (classMappings.hasNext() ) {
126-
PersistentClass element = classMappings.next();
127-
if(element instanceof RootClass) {
128-
dump(pw, ejb3, element);
129-
}
130-
}
131-
}
132-
pw.println(" </session-factory>\r\n" +
133-
"</hibernate-configuration>");
134-
135-
}
136-
137-
catch (IOException e) {
138-
throw new RuntimeException("Problems while creating hibernate.cfg.xml", e);
139-
}
140-
finally {
141-
if(pw!=null) {
142-
pw.flush();
143-
pw.close();
144-
}
145-
}
146-
147-
}
148-
149-
/**
150-
* @param pw
151-
* @param element
152-
*/
153-
private void dump(PrintWriter pw, boolean useClass, PersistentClass element) {
154-
if(useClass) {
155-
pw.println("<mapping class=\"" + element.getClassName() + "\"/>");
156-
} else {
157-
pw.println("<mapping resource=\"" + getMappingFileResource(element) + "\"/>");
158-
}
159-
160-
Iterator<Subclass> directSubclasses = element.getDirectSubclasses().iterator();
161-
while (directSubclasses.hasNext() ) {
162-
PersistentClass subclass = (PersistentClass)directSubclasses.next();
163-
dump(pw, useClass, subclass);
164-
}
165-
166-
}
167-
168-
/**
169-
* @param element
170-
* @return
171-
*/
172-
private String getMappingFileResource(PersistentClass element) {
173-
174-
return element.getClassName().replace('.', '/') + ".hbm.xml";
175-
}
176-
177-
public String getName() {
178-
return "cfg2cfgxml";
179-
}
180-
181-
/**
182-
*
183-
* @param text
184-
* @return String with escaped [<,>] special characters.
185-
*/
186-
public static String forXML(String text) {
187-
if (text == null) return null;
188-
final StringBuilder result = new StringBuilder();
189-
char[] chars = text.toCharArray();
190-
for (int i = 0; i < chars.length; i++){
191-
char character = chars[i];
192-
if (character == '<') {
193-
result.append("&lt;");
194-
} else if (character == '>'){
195-
result.append("&gt;");
196-
} else {
197-
result.append(character);
198-
}
199-
}
200-
return result.toString();
201-
}
202-
184+
return result.toString();
185+
}
186+
203187
}

0 commit comments

Comments
 (0)