Как я могу получить значения вложенных объектов в XStream? - PullRequest
0 голосов
/ 02 ноября 2011

У меня есть класс пользователя со следующими атрибутами:

/** The username. */
private String username;
/** The password. */
private String password;
/** The list of role names the user has. */
private List<String> roleNames;
/** The list of partners with the associated database */
private DbPartner dbPartner;
/** The list of message types with the associated database */
private DbMessageType dbMessageType;

Я использую XStream для записи значений в файл и получения их из него.Проблема в том, что значения вложенных объектов (dbPartner и dbMessageTypes) не заполнены, и они равны нулю, когда я получаю пользователя из xml.Что мне делать?

Этот метод используется для получения данных из файла:

 private void lazyLoad() {
    synchronized (ConfigurationDAOImpl.class) {
        // Has the configuration been loaded
        if (configuration == null) {
            if (filename.exists()) {
                try {
                    XStream xStream = new XStream(new DomDriver());
                    xStream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);

                    xStream.alias("configuration", Configuration.class);
                    xStream.alias("user", User.class);
                    xStream.alias("dbPartner", DbPartner.class);
                    xStream.alias("dbMessageType", DbMessageType.class);

                    configuration = (Configuration) xStream
                            .fromXML(filename.getInputStream());

                    LOGGER.debug("Loaded configuration from {}.", filename);
                } catch (Exception e) {
                    LOGGER.error("Failed to load configuration.", e);
                }
            } else {
                LOGGER.debug("{} does not exist.", filename);
                LOGGER.debug("Creating blank configuration.");

                configuration = new Configuration();
                configuration.setUsers(new ArrayList<User>());

                // and store it
                store();
            }
        }
    }
}

private void store() {
    XStream xStream = new XStream();
    xStream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);

    xStream.alias("configuration", Configuration.class);
    xStream.alias("user", User.class);
    xStream.alias("dbPartner", DbPartner.class);
    xStream.alias("dbMessageType", DbMessageType.class);

    synchronized (ConfigurationDAOImpl.class) {
        try {
            xStream.toXML(configuration, new FileOutputStream(filename.getFile()));
        } catch (IOException e) {
            throw new RuntimeException("Failed to write to " + filename, e);
        }
    }
}
...