Skip to content

Commit 9614cb8

Browse files
Now is possible to load XML propertes.
TO specify holder type is no more required unless you need to subclass it
1 parent 0fa6024 commit 9614cb8

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

fj-core/src/main/java/org/fugerit/java/core/cfg/xml/PropertyCatalog.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99

1010
public class PropertyCatalog extends ListMapCatalogConfig<PropertyHolder> {
1111

12+
public PropertyCatalog() {
13+
super();
14+
this.getGeneralProps().setProperty( ATT_TYPE , PropertyHolder.class.getName() );
15+
}
16+
1217
public static final String PROP_DEFAULT_CATALOG = "default-catalog";
1318

1419
/**

fj-core/src/main/java/org/fugerit/java/core/cfg/xml/PropertyHolder.java

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package org.fugerit.java.core.cfg.xml;
22

3+
import java.io.File;
4+
import java.io.FileInputStream;
35
import java.io.IOException;
6+
import java.io.InputStream;
47
import java.util.Enumeration;
58
import java.util.Map.Entry;
69
import java.util.Properties;
710
import java.util.Set;
811

9-
import org.fugerit.java.core.util.PropsIO;
12+
import org.fugerit.java.core.lang.helpers.BooleanUtils;
13+
import org.fugerit.java.core.lang.helpers.ClassHelper;
1014

1115
public class PropertyHolder extends BasicIdConfigType {
1216

@@ -27,6 +31,8 @@ public class PropertyHolder extends BasicIdConfigType {
2731

2832
private String mode;
2933

34+
private String xml;
35+
3036
private Properties props;
3137

3238
public String getDescription() {
@@ -52,19 +58,41 @@ public String getMode() {
5258
public void setMode(String mode) {
5359
this.mode = mode;
5460
}
61+
62+
public String getXml() {
63+
return xml;
64+
}
65+
66+
public void setXml(String xml) {
67+
this.xml = xml;
68+
}
69+
70+
private static void loadWorker( InputStream is, Properties props, boolean xml ) throws IOException {
71+
if ( xml ) {
72+
props.loadFromXML( is );
73+
} else {
74+
props.load( is );
75+
}
76+
}
5577

56-
public static Properties load( String mode, String path ) throws IOException {
57-
Properties props = null;
58-
if ( MODE_FILE.equalsIgnoreCase( mode ) ) {
59-
props = PropsIO.loadFromFile( path );
60-
} else if ( MODE_CLASS_LOADER.equalsIgnoreCase( mode ) ) {
61-
props = PropsIO.loadFromClassLoader( path );
78+
public static Properties load( String mode, String path, String xml ) throws IOException {
79+
Properties props = new Properties();
80+
if ( MODE_CLASS_LOADER.equalsIgnoreCase( mode ) ) {
81+
try ( InputStream is = ClassHelper.loadFromDefaultClassLoader( path ) ) {
82+
loadWorker( is , props, BooleanUtils.isTrue( xml ) );
83+
} catch (Exception e) {
84+
throw new IOException( e );
85+
}
86+
} else {
87+
try ( InputStream is = new FileInputStream( new File( path ) ) ) {
88+
loadWorker( is , props, BooleanUtils.isTrue( xml ) );
89+
}
6290
}
6391
return props;
6492
}
6593

6694
public void init() throws IOException {
67-
this.props = load( this.getMode() , this.getPath() );
95+
this.props = load( this.getMode() , this.getPath(), this.getXml() );
6896
}
6997

7098
public boolean isEmpty() {

0 commit comments

Comments
 (0)