1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48:
49:
54: public class NodeReader {
55:
56: private final BufferedReader br;
57: private String line = "";
58:
59: private final PreferencesFactory factory;
60:
61: public NodeReader(Reader r, PreferencesFactory factory) {
62: if(r instanceof BufferedReader) {
63: br = (BufferedReader) r;
64: } else {
65: br = new BufferedReader(r);
66: }
67: this.factory = factory;
68: }
69:
70: public NodeReader(InputStream is, PreferencesFactory factory) {
71: this(new InputStreamReader(is), factory);
72: }
73:
74: public void importPreferences()
75: throws InvalidPreferencesFormatException, IOException
76: {
77: readPreferences();
78: }
79:
80: private void readPreferences()
81: throws InvalidPreferencesFormatException, IOException
82: {
83:
84: skipTill("<preferences");
85:
86: readRoot();
87:
88:
89: skipTill("</preferences>");
90: }
91:
92: private void readRoot()
93: throws InvalidPreferencesFormatException, IOException
94: {
95:
96: skipTill("<root");
97:
98:
99: skipTill("type=\"");
100: String type = readTill("\"");
101: Preferences root;
102: if ("user".equals(type)) {
103: root = factory.userRoot();
104: } else if ("system".equals(type)) {
105: root = factory.systemRoot();
106: } else {
107: throw new InvalidPreferencesFormatException("Unknown type: "
108: + type);
109: }
110:
111:
112: readMap(root);
113: readNodes(root);
114:
115:
116: skipTill("</root>");
117: }
118:
119: private void readNodes(Preferences node)
120: throws InvalidPreferencesFormatException, IOException
121: {
122: while ("node".equals(nextTag())) {
123: skipTill("<node");
124: skipTill("name=\"");
125: String name = readTill("\"");
126: Preferences subnode = node.node(name);
127: System.out.println("Found subnode: " + subnode.absolutePath());
128: readMap(subnode);
129: readNodes(subnode);
130: skipTill("</node>");
131: }
132:
133: }
134:
135: private void readMap(Preferences node)
136: throws InvalidPreferencesFormatException, IOException
137: {
138:
139: skipTill("<map");
140:
141:
142: if (line.startsWith("/>")) {
143: line = line.substring(2);
144: return;
145: }
146:
147:
148: readEntries(node);
149:
150:
151: skipTill("</map>");
152: }
153:
154: private void readEntries(Preferences node)
155: throws InvalidPreferencesFormatException, IOException
156: {
157: while ("entry".equals(nextTag())) {
158: skipTill("<entry");
159: skipTill("key=\"");
160: String key = readTill("\"");
161: skipTill("value=\"");
162: String value = readTill("\"");
163: System.out.println("Key: " + key + " Value: " + value);
164: node.put(key, value);
165: }
166: }
167:
168: private void skipTill(String s)
169: throws InvalidPreferencesFormatException, IOException
170: {
171: while(true) {
172: if (line == null)
173: throw new InvalidPreferencesFormatException(s + " not found");
174:
175: int index = line.indexOf(s);
176: if (index == -1) {
177: line = br.readLine();
178: } else {
179: line = line.substring(index+s.length());
180: return;
181: }
182: }
183: }
184:
185: private String readTill(String s)
186: throws InvalidPreferencesFormatException
187: {
188: int index = line.indexOf(s);
189: if (index == -1)
190: throw new InvalidPreferencesFormatException(s + " not found");
191:
192: String read = line.substring(0, index);
193: line = line.substring(index+s.length());
194:
195: return read;
196: }
197:
198: private String nextTag()
199: throws InvalidPreferencesFormatException, IOException
200: {
201: while(true) {
202: if (line == null)
203: throw new InvalidPreferencesFormatException("unexpected EOF");
204:
205: int start = line.indexOf("<");
206: if (start == -1) {
207: line = br.readLine();
208: } else {
209:
210: int end = start+1;
211: while (end != line.length()
212: && " \t\r\n".indexOf(line.charAt(end)) == -1) {
213: end++;
214: }
215:
216: String tag = line.substring(start+1,end);
217: line = line.substring(start);
218: return tag;
219: }
220: }
221: }
222:
223: }