0

我有以下组件:

@State(
    name = "SessionConfiguration",
    storages = {
        @Storage(id = "default", file = StoragePathMacros.PROJECT_FILE),
        @Storage(id = "dir", file = StoragePathMacros.PROJECT_CONFIG_DIR + "/tabsession.xml", scheme = StorageScheme.DIRECTORY_BASED)
    }
)
public class SessionComponent implements ProjectComponent, PersistentStateComponent<Element> {

...

    @Nullable
    @Override
    public Element getState() {
        LOG.debug("Saving state");

        final Element element = new Element("tabsession");

        // meta configuration information
        final Element stateElement = new Element("state");
        stateElement.setAttribute("version", Integer.toString(STATE_VERSION));
        element.addContent(stateElement);

        // session data
        final Element sessionsElement = new Element("sessions");
        for(SessionState.Session session : sessionState.sessions) {
            final Element sessionElement = new Element("session");
            sessionElement.setAttribute("name", session.name);
            final Element filesElement = new Element("files");
            for(String path : session.files) {
                final Element fileElement = new Element("file");
                fileElement.setAttribute("path", path);
                filesElement.addContent(fileElement);
            }
            filesElement.setAttribute("focusedFile", session.focusedFile);
            sessionElement.addContent(filesElement);
            sessionsElement.addContent(sessionElement);
        }
        element.addContent(sessionsElement);

        return element;
    }

...

}

这工作得很好并生成目标文件tabsession.xml

<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="SessionConfiguration">
    <state version="1" />
    <sessions>
      <session name="first">
        <files focusedFile="$PROJECT_DIR$/src/First.java">
          <file path="$PROJECT_DIR$/src/First.java" />
          <file path="$PROJECT_DIR$/src/Second.java" />
        </files>
      </session>
    </sessions>
  </component>
</project>

但是如果用不同的数据再次保存状态,则不会删除旧行。例如,如果我打开另一个文件并保存状态,则 xml 如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="SessionConfiguration">
    <state version="1" />
    <sessions>
      <session name="first">
        <files focusedFile="$PROJECT_DIR$/src/First.java">
          <file path="$PROJECT_DIR$/src/First.java" />
          <file path="$PROJECT_DIR$/src/Second.java" />
          <file path="$PROJECT_DIR$/src/First.java" />
          <file path="$PROJECT_DIR$/src/Second.java" />
          <file path="$PROJECT_DIR$/src/Third.java" />
        </files>
      </session>
    </sessions>
  </component>
</project>

这显然是错误的。有什么想法可以在删除旧行的同时仅保存当前状态吗?

4

1 回答 1

0

我发现问题出在我创建session.files数组的代码上。我没有检查重复项。之后,一切都按预期工作。

于 2013-03-31T09:06:34.573 回答