aspose file tools*
The moose likes Java in General and the fly likes Getters/Setters for the key pair in .ini file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Getters/Setters for the key pair in .ini file" Watch "Getters/Setters for the key pair in .ini file" New topic
Author

Getters/Setters for the key pair in .ini file

Praveen Ramachandran
Greenhorn

Joined: Jul 18, 2006
Posts: 16
reading from a .ini file


I have an ini file and i have to make the setters/getters in a configuration(configuration.java) file.There should be one setter for tablenames (setTablenames) that has a Map type input and stores all key-value pairs


the ini file is as follows:

[bwiseserver]
server = localhost
port = 80

[database]
type = sqlserver
server = localhost
port = 1433
database =
user = sa
password = *****************

[tables]
activity = true
activitykind = false
activitytype = false

[tablenames]
Assertion ControlObjective
AssertionRelation ControlObjectiveRelation
Group Division
Resource Person
BusinessProcess Process
BusinessProcessRelation ProcessRelation
ControlMeasure Control
ControlMeasureRelation ControlRelation
ControlMeasureCategory ControlCategory
BWCntrlMonitorAnswer CntrlMonitorAnswer
BWCntrlMonitorAssm CntrlMonitorAssm
BWCntrlMonitorSession CntrlMonitorSession
BWCntrlReviewAnswer CntrlReviewAnswer
BWCntrlReviewAssm CntrlReviewAssm
BWCntrlReviewSession CntrlReviewSession

[columnnames]
BusinessProcessRelation.subject process
BusinessProcessRelation.relation account
BusinessProcessRelation.context division
RiskRelation.subject risk
RiskRelation.relation proces
RiskRelation.context division
SignificantAccountRelation.relation division
SignificantAccountRelation.subject account
ControlMeasureRelation.context division
ControlMeasureRelation.relation risk
ControlMeasureRelation.subject control
BWCntrlMonitorAssm.group division


The configuration.java is as follows




public class Configuration
{
private static final String SECTION_BWISE_SERVER = "bwiseserver";
private static final String SECTION_DATABASE = "database";
private static final String SECTION_TABLES = "tables";
private static final String SECTION_VIEWS = "views";
// private static final String SECTION_TABLENAMES = "tablenames";
// private static final String SECTION_COLUMNNAMES = "columnnames";

private static Configuration config = new Configuration();

private IniFileEditor iniEditor = new IniFileEditor();
private File iniFile = new File("datawarehouse.ini");

private Configuration()
{
load();
}

public static Configuration instance()
{
return config;
}

public void load()
{
if (iniFile.exists())
{
try
{
iniEditor.load(iniFile);
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}

public void save()
{
try
{
iniEditor.save(iniFile);
}
catch (IOException ex)
{
ex.printStackTrace();
}
}

private String getValue(String section, String option, String defaultValue)
{
String value = iniEditor.get(section, option);
return value == null?defaultValue:value;
}

private void setValue(String section, String option, String value)
{
if (!iniEditor.hasSection(section))
{
iniEditor.addSection(section);
}

iniEditor.set(section, option, value);
}

public String getBWiseServer()
{
return getValue(SECTION_BWISE_SERVER, "server", "localhost");
}

public void setBWiseServer(String bwiseServer)
{
setValue(SECTION_BWISE_SERVER, "server", bwiseServer);
}

public String getBWiseServerPort()
{
return getValue(SECTION_BWISE_SERVER, "port", "81");
}

public void setBWiseServerPort(String bwiseServerPort)
{
setValue(SECTION_BWISE_SERVER, "port", bwiseServerPort);
}

public String getDatabaseType()
{
return getValue(SECTION_DATABASE, "type", DatabaseType.SQL_SERVER.getId());
}

public void setDatabaseType(String databaseType)
{
setValue(SECTION_DATABASE, "type", databaseType);
}

public String getDatabaseServer()
{
return getValue(SECTION_DATABASE, "server", "localhost");
}

public void setDatabaseServer(String databaseServer)
{
setValue(SECTION_DATABASE, "server", databaseServer);
}

public String getDatabaseServerPort()
{
return getValue(SECTION_DATABASE, "port", DatabaseType.fromId(getDatabaseType()).getDefaultPort());
}

public void setDatabaseServerPort(String databaseServer)
{
setValue(SECTION_DATABASE, "port", databaseServer);
}

public void setDatabaseName(String databaseName)
{
setValue(SECTION_DATABASE, "database", databaseName);
}

public void setInsertViews(boolean insertViews)
{
setValue(SECTION_VIEWS, "insertViews", String.valueOf(insertViews));
}

public String getDatabaseName()
{
return getValue(SECTION_DATABASE, "database", "");
}

public void setDatabaseUser(String databaseUser)
{
setValue(SECTION_DATABASE, "user", databaseUser);
}

public String getDatabaseUser()
{
return getValue(SECTION_DATABASE, "user", "sa");
}

public void setDatabasePassword(String databasePassword)
{
setValue(SECTION_DATABASE, "password", databasePassword);
}

public String getDatabasePassword()
{
return getValue(SECTION_DATABASE, "password", "*****************");
}

public boolean getInsertViews()
{
return stringToBoolean(getValue(SECTION_VIEWS, "insertViews", "false"));
}

public String[] getSelectedTables()
{
List selected = new ArrayList();
if (iniEditor.hasSection(SECTION_TABLES))
{
List tables = iniEditor.optionNames(SECTION_TABLES);
Iterator iter = tables.iterator();
while (iter.hasNext())
{
String option = (String) iter.next();
String value = (String) iniEditor.get(SECTION_TABLES, option);
if (stringToBoolean(value))
{
selected.add(option);
}
}
}
return (String[])selected.toArray(new String[0]);
}

public void setSelectedTables(String[] tables)
{
if (!iniEditor.hasSection(SECTION_TABLES))
{
iniEditor.addSection(SECTION_TABLES);
}

// Put all tables on false first
Iterator options = iniEditor.optionNames(SECTION_TABLES).iterator();
while(options.hasNext())
{
iniEditor.set(SECTION_TABLES, (String)options.next(), "false");
}

// Now put the selected on true
for (int i = 0; i < tables.length; i++)
{
iniEditor.set(SECTION_TABLES, tables[i], "true");
}
}

private boolean stringToBoolean(String str)
{
return (str != null && (str.equals("true") || str.equals("1")));
}
}


Could you please help me on this and suggest me the methods..Thank you

Praveen
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getters/Setters for the key pair in .ini file