0
|
1 |
/*
|
1
|
2 |
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
0
|
17 |
package com.nokia.epdt.plugins.s60.pan;
|
|
18 |
|
1
|
19 |
import java.io.FileReader;
|
0
|
20 |
import java.io.FileWriter;
|
1
|
21 |
import java.io.IOException;
|
0
|
22 |
import java.io.LineNumberReader;
|
1
|
23 |
import java.util.Enumeration;
|
0
|
24 |
import java.util.LinkedList;
|
1
|
25 |
import java.util.Properties;
|
0
|
26 |
|
|
27 |
/**
|
|
28 |
*
|
1
|
29 |
* Class PropSets Contains a number of property sets - Sections, each Section is
|
|
30 |
* titled: [title1] property11= value11 property12= value12
|
|
31 |
*
|
|
32 |
* [titleN] propertyNK= valueNK
|
|
33 |
*
|
|
34 |
*/
|
|
35 |
public class PropSets {
|
0
|
36 |
LinkedList sections = new LinkedList();
|
1
|
37 |
|
|
38 |
private class Section {
|
0
|
39 |
private String title = new String();
|
|
40 |
private Properties props = new Properties();
|
1
|
41 |
|
|
42 |
public Properties getProps() {
|
0
|
43 |
return props;
|
|
44 |
}
|
1
|
45 |
|
|
46 |
public void setTitle(String title) {
|
|
47 |
this.title = title;
|
|
48 |
}
|
|
49 |
|
|
50 |
public String getTitle() {
|
|
51 |
return title;
|
|
52 |
}
|
0
|
53 |
|
1
|
54 |
public String getValue(String key) {
|
|
55 |
return (String) props.get(key);
|
|
56 |
}
|
|
57 |
|
|
58 |
public void setValue(String key, String value) {
|
|
59 |
props.put(key, value);
|
|
60 |
}
|
|
61 |
|
0
|
62 |
/**
|
|
63 |
* Function for saving a section into FileOutputStream
|
1
|
64 |
*
|
0
|
65 |
* @param writer
|
|
66 |
* @throws IOException
|
|
67 |
*/
|
1
|
68 |
public void save(FileWriter writer) throws IOException {
|
0
|
69 |
String newLine = System.getProperty("line.separator");
|
|
70 |
writer.write(title + newLine);
|
1
|
71 |
// props.store(writer, null);
|
0
|
72 |
Enumeration keys = props.keys();
|
1
|
73 |
while (keys.hasMoreElements()) {
|
|
74 |
String key = (String) keys.nextElement();
|
|
75 |
writer.write(key + "= " + props.getProperty(key) + newLine);
|
0
|
76 |
}
|
|
77 |
writer.write(newLine);
|
|
78 |
}
|
|
79 |
}
|
|
80 |
|
|
81 |
/**
|
|
82 |
* Loading sections from file
|
|
83 |
*/
|
1
|
84 |
public void load(FileReader reader) throws IOException {
|
0
|
85 |
LineNumberReader lineReader = new LineNumberReader(reader);
|
|
86 |
Section currentSection = null;
|
|
87 |
String currentLine;
|
|
88 |
|
1
|
89 |
while ((currentLine = lineReader.readLine()) != null) {
|
|
90 |
if (currentLine.startsWith("[") && currentLine.endsWith("]")) {
|
|
91 |
// Found a title, i.e. new section set the new section
|
|
92 |
// to be current, the old one will be garbage-collected
|
0
|
93 |
currentSection = new Section();
|
|
94 |
currentSection.setTitle(currentLine);
|
|
95 |
sections.add(currentSection);
|
1
|
96 |
} else// Found a property
|
0
|
97 |
{
|
1
|
98 |
if (currentSection != null) {
|
|
99 |
// if there is no current section, we didn't find a
|
|
100 |
// title first therefore we skip the property
|
0
|
101 |
String[] propPair;// = new String[2];
|
|
102 |
propPair = currentLine.toString().split("= ", 2);
|
1
|
103 |
if (propPair.length == 2) {
|
|
104 |
// If we split successfully, add key-value pair,
|
|
105 |
// if not - skip the line
|
0
|
106 |
currentSection.getProps().put(propPair[0], propPair[1]);
|
|
107 |
}
|
|
108 |
}
|
|
109 |
}
|
|
110 |
}
|
|
111 |
}
|
1
|
112 |
|
0
|
113 |
/**
|
|
114 |
* Saving Sections to a file
|
1
|
115 |
*
|
0
|
116 |
*/
|
1
|
117 |
public void save(FileWriter writer) throws IOException {
|
|
118 |
for (int i = 0; i < sections.size(); i++) {
|
|
119 |
((Section) sections.get(i)).save(writer);
|
0
|
120 |
}
|
|
121 |
writer.flush();
|
|
122 |
}
|
1
|
123 |
|
0
|
124 |
/**
|
|
125 |
* Function returns a property value, specified by section name and key
|
1
|
126 |
*
|
0
|
127 |
* @param sectionName
|
|
128 |
* @param key
|
|
129 |
* @return
|
|
130 |
*/
|
1
|
131 |
public String getValue(String sectionName, String key) {
|
|
132 |
for (int i = 0; i < sections.size(); i++) {
|
|
133 |
if (sectionName.equals(((Section) sections.get(i)).getTitle())) {
|
|
134 |
// Found specified section
|
|
135 |
return ((Section) sections.get(i)).getValue(key);
|
0
|
136 |
}
|
|
137 |
}
|
|
138 |
return null;
|
|
139 |
}
|
|
140 |
|
|
141 |
/**
|
|
142 |
* Function sets a property value, specified by section name and key
|
1
|
143 |
*
|
0
|
144 |
* @param sectionName
|
|
145 |
* @param key
|
|
146 |
* @return 0=success, -1 = error
|
|
147 |
*/
|
1
|
148 |
public int setValue(String sectionName, String key, String value) {
|
|
149 |
for (int i = 0; i < sections.size(); i++) {
|
|
150 |
if (sectionName.equals(((Section) sections.get(i)).getTitle())) {
|
|
151 |
// Found specified section
|
|
152 |
((Section) sections.get(i)).setValue(key, value);
|
|
153 |
return 0;
|
0
|
154 |
}
|
|
155 |
}
|
|
156 |
return -1;
|
1
|
157 |
}
|
|
158 |
|
|
159 |
} |