01: /*--------------------------------------------------------------------------*
02: | Copyright (C) 2006 Christopher Kohlhaas |
03: | |
04: | This program is free software; you can redistribute it and/or modify |
05: | it under the terms of the GNU General Public License as published by the |
06: | Free Software Foundation. A copy of the license has been included with |
07: | these distribution in the COPYING file, if not go to www.fsf.org . |
08: | |
09: | As a special exception, you are granted the permissions to link this |
10: | program with every library, which license fulfills the Open Source |
11: | Definition as published by the Open Source Initiative (OSI). |
12: *--------------------------------------------------------------------------*/
13:
14: package org.rapla.storage.xml;
15:
16: import java.io.IOException;
17: import java.util.Iterator;
18:
19: import org.rapla.entities.RaplaObject;
20: import org.rapla.entities.RaplaType;
21: import org.rapla.entities.User;
22: import org.rapla.entities.configuration.Preferences;
23: import org.rapla.entities.configuration.internal.PreferencesImpl;
24: import org.rapla.framework.RaplaContext;
25: import org.rapla.framework.RaplaException;
26:
27: public class PreferenceWriter extends RaplaXMLWriter {
28: public static final String WRITERMAP = PreferenceWriter.class
29: .getPackage().getName()
30: + ".writerMap";
31:
32: public PreferenceWriter(RaplaContext sm) throws RaplaException {
33: super (sm);
34: }
35:
36: protected void printPreferences(Preferences preferences)
37: throws IOException, RaplaException {
38: if (!preferences.isEmpty()) {
39: openTag("rapla:preferences");
40: if (isIdOnly()) {
41: att("id", getId(preferences));
42: User owner = preferences.getOwner();
43: if (owner != null) {
44: att("ownerid", getId(owner));
45: }
46: }
47: printVersion(preferences);
48:
49: closeTag();
50: Iterator it = ((PreferencesImpl) preferences)
51: .getPreferenceEntries();
52: while (it.hasNext()) {
53: String role = (String) it.next();
54: Object entry = preferences.getEntry(role);
55: if (entry instanceof String) {
56: openTag("rapla:entry");
57: att("key", role);
58: att("value", (String) entry);
59: closeElementTag();
60: }
61: if (entry instanceof RaplaObject) {
62: openTag("rapla:entry");
63: att("key", role);
64: closeTag();
65: RaplaType raplaType = ((RaplaObject) entry)
66: .getRaplaType();
67: RaplaXMLWriter writer = getWriterFor(raplaType);
68: writer.writeObject((RaplaObject) entry);
69: closeElement("rapla:entry");
70: }
71: }
72: closeElement("rapla:preferences");
73: }
74: }
75:
76: public void writeObject(RaplaObject object) throws IOException,
77: RaplaException {
78: printPreferences((Preferences) object);
79: }
80:
81: }
|