PersistenceStateComponent: XML has duplicate lines
This is a crosspost of http://stackoverflow.com/questions/15723824/intellij-idea-plugin-persistence-state-xml-has-duplicate-lines
I have the following Component:
@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;
}
...
}
That's working just fine and generates the target file `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>
But if the state gets saved again with different data, old lines are not removed. For example, if i open another file and the state gets saved the xml looks like this:
<?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>
Which is clearly wrong. Any ideas how i can save only the current state while removing old lines?
Please sign in to leave a comment.
This looks like a bug in your code (i.e. you actually have those duplicate files in your session.files array). We don't have any code in our serialization framework that would cause this kind of behavior.
Also, could you please not cross-post the same question to StackOverflow and the forums? We read both, and it's annoying to have to cross-post the answer or leave one of the questions unanswered.
First, you are right with the fact that i created the duplicates myself in the session.files array. After fixing it, everything works as expected.
I am sorry for crossposting. I will create an answer on Stackoverflow to clarify the problem. In the future, i will ask only in the devnet forums.