2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.intellij
.openapi
.vcs
.changes
;
18 import com
.intellij
.openapi
.project
.Project
;
19 import com
.intellij
.openapi
.util
.InvalidDataException
;
20 import com
.intellij
.openapi
.util
.WriteExternalException
;
21 import com
.intellij
.openapi
.util
.text
.StringUtil
;
22 import org
.jdom
.Element
;
23 import org
.jetbrains
.annotations
.NonNls
;
26 import java
.util
.List
;
28 class ChangeListManagerSerialization
{
29 @NonNls static final String ATT_ID
= "id";
30 @NonNls static final String ATT_NAME
= "name";
31 @NonNls static final String ATT_COMMENT
= "comment";
32 @NonNls static final String ATT_DEFAULT
= "default";
33 @NonNls static final String ATT_READONLY
= "readonly";
34 @NonNls static final String ATT_VALUE_TRUE
= "true";
35 @NonNls static final String ATT_CHANGE_TYPE
= "type";
36 @NonNls static final String ATT_CHANGE_BEFORE_PATH
= "beforePath";
37 @NonNls static final String ATT_CHANGE_AFTER_PATH
= "afterPath";
38 @NonNls static final String ATT_PATH
= "path";
39 @NonNls static final String ATT_MASK
= "mask";
40 @NonNls static final String NODE_LIST
= "list";
41 @NonNls static final String NODE_IGNORED
= "ignored";
42 @NonNls static final String NODE_CHANGE
= "change";
44 private final IgnoredFilesComponent myIgnoredIdeaLevel
;
45 private final ChangeListWorker myWorker
;
47 ChangeListManagerSerialization(final IgnoredFilesComponent ignoredIdeaLevel
, final ChangeListWorker worker
) {
48 myIgnoredIdeaLevel
= ignoredIdeaLevel
;
52 @SuppressWarnings({"unchecked"})
53 public void readExternal(final Element element
) throws InvalidDataException
{
54 final List
<Element
> listNodes
= (List
<Element
>)element
.getChildren(NODE_LIST
);
55 for (Element listNode
: listNodes
) {
56 readChangeList(listNode
);
58 final List
<Element
> ignoredNodes
= (List
<Element
>)element
.getChildren(NODE_IGNORED
);
59 for (Element ignoredNode
: ignoredNodes
) {
60 readFileToIgnore(ignoredNode
);
64 private void readChangeList(final Element listNode
) {
65 // workaround for loading incorrect settings (with duplicate changelist names)
66 final String changeListName
= listNode
.getAttributeValue(ATT_NAME
);
67 LocalChangeList list
= myWorker
.getCopyByName(changeListName
);
69 list
= myWorker
.addChangeList(listNode
.getAttributeValue(ATT_ID
), changeListName
, listNode
.getAttributeValue(ATT_COMMENT
), false);
71 //noinspection unchecked
72 final List
<Element
> changeNodes
= (List
<Element
>)listNode
.getChildren(NODE_CHANGE
);
73 for (Element changeNode
: changeNodes
) {
75 myWorker
.addChangeToList(changeListName
, readChange(changeNode
), null);
77 catch (OutdatedFakeRevisionException e
) {
78 // Do nothing. Just skip adding outdated revisions to the list.
82 if (ChangeListManagerSerialization
.ATT_VALUE_TRUE
.equals(listNode
.getAttributeValue(ATT_DEFAULT
))) {
83 myWorker
.setDefault(list
.getName());
85 if (ChangeListManagerSerialization
.ATT_VALUE_TRUE
.equals(listNode
.getAttributeValue(ATT_READONLY
))) {
86 list
.setReadOnly(true);
91 private void readFileToIgnore(final Element ignoredNode
) {
92 String path
= ignoredNode
.getAttributeValue(ATT_PATH
);
94 Project project
= myWorker
.getProject();
95 final IgnoredFileBean bean
= path
.endsWith("/") || path
.endsWith(File
.separator
)
96 ? IgnoredBeanFactory
.ignoreUnderDirectory(path
, project
)
97 : IgnoredBeanFactory
.ignoreFile(path
, project
);
98 myIgnoredIdeaLevel
.add(bean
);
100 String mask
= ignoredNode
.getAttributeValue(ATT_MASK
);
102 final IgnoredFileBean bean
= IgnoredBeanFactory
.withMask(mask
);
103 myIgnoredIdeaLevel
.add(bean
);
107 public void writeExternal(Element element
) throws WriteExternalException
{
108 final List
<LocalChangeList
> changeListList
= myWorker
.getListsCopy();
109 for (LocalChangeList list
: changeListList
) {
110 Element listNode
= new Element(NODE_LIST
);
111 element
.addContent(listNode
);
112 if (list
.isDefault()) {
113 listNode
.setAttribute(ATT_DEFAULT
, ATT_VALUE_TRUE
);
115 if (list
.isReadOnly()) {
116 listNode
.setAttribute(ATT_READONLY
, ATT_VALUE_TRUE
);
119 listNode
.setAttribute(ATT_ID
, list
.getId());
120 listNode
.setAttribute(ATT_NAME
, list
.getName());
121 listNode
.setAttribute(ATT_COMMENT
, list
.getComment());
122 for (Change change
: list
.getChanges()) {
123 writeChange(listNode
, change
);
126 final IgnoredFileBean
[] filesToIgnore
= myIgnoredIdeaLevel
.getFilesToIgnore();
127 for(IgnoredFileBean bean
: filesToIgnore
) {
128 Element fileNode
= new Element(NODE_IGNORED
);
129 element
.addContent(fileNode
);
130 String path
= bean
.getPath();
132 fileNode
.setAttribute("path", path
);
134 String mask
= bean
.getMask();
136 fileNode
.setAttribute("mask", mask
);
141 private static void writeChange(final Element listNode
, final Change change
) {
142 Element changeNode
= new Element(NODE_CHANGE
);
143 listNode
.addContent(changeNode
);
144 changeNode
.setAttribute(ATT_CHANGE_TYPE
, change
.getType().name());
146 final ContentRevision bRev
= change
.getBeforeRevision();
147 final ContentRevision aRev
= change
.getAfterRevision();
149 changeNode
.setAttribute(ATT_CHANGE_BEFORE_PATH
, bRev
!= null ? bRev
.getFile().getPath() : "");
150 changeNode
.setAttribute(ATT_CHANGE_AFTER_PATH
, aRev
!= null ? aRev
.getFile().getPath() : "");
153 private static Change
readChange(Element changeNode
) throws OutdatedFakeRevisionException
{
154 String bRev
= changeNode
.getAttributeValue(ATT_CHANGE_BEFORE_PATH
);
155 String aRev
= changeNode
.getAttributeValue(ATT_CHANGE_AFTER_PATH
);
156 return new Change(StringUtil
.isEmpty(bRev
) ?
null : new FakeRevision(bRev
), StringUtil
.isEmpty(aRev
) ?
null : new FakeRevision(aRev
));
159 static final class OutdatedFakeRevisionException
extends Exception
{}