summaryrefslogtreecommitdiff
path: root/plugins/svn4idea/src/org/jetbrains/idea/svn/properties/SvnKitPropertyClient.java
blob: 23fedd3c7ac87b26c764b2666e25b7765f2a8580 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package org.jetbrains.idea.svn.properties;

import com.intellij.openapi.vcs.VcsException;
import com.intellij.openapi.vfs.CharsetToolkit;
import com.intellij.util.LineSeparator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.idea.svn.api.BaseSvnClient;
import org.jetbrains.idea.svn.api.Depth;
import org.jetbrains.idea.svn.commandLine.SvnBindException;
import org.tmatesoft.svn.core.*;
import org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions;
import org.tmatesoft.svn.core.wc.*;
import org.tmatesoft.svn.core.wc2.SvnTarget;

import java.io.File;
import java.util.Map;

/**
 * @author Konstantin Kolosovsky.
 */
public class SvnKitPropertyClient extends BaseSvnClient implements PropertyClient {

  public static final ISVNOptions LF_SEPARATOR_OPTIONS = new DefaultSVNOptions() {
    @Override
    public byte[] getNativeEOL() {
      return CharsetToolkit.getUtf8Bytes(LineSeparator.LF.getSeparatorString());
    }
  };

  @Nullable
  @Override
  public PropertyValue getProperty(@NotNull SvnTarget target,
                                   @NotNull String property,
                                   boolean revisionProperty,
                                   @Nullable SVNRevision revision) throws VcsException {
    PropertyData resultData;

    try {
      if (!revisionProperty) {
        if (target.isFile()) {
          resultData = PropertyData.create(createClient().doGetProperty(target.getFile(), property, target.getPegRevision(), revision));
        } else {
          resultData = PropertyData.create(createClient().doGetProperty(target.getURL(), property, target.getPegRevision(), revision));
        }
      } else {
        resultData = getRevisionProperty(target, property, revision);
      }
    }
    catch (SVNException e) {
      throw new VcsException(e);
    }

    return resultData != null ? resultData.getValue() : null;
  }

  @NotNull
  private SVNWCClient createClient() {
    SVNWCClient client = myVcs.getSvnKitManager().createWCClient();
    client.setOptions(LF_SEPARATOR_OPTIONS);

    return client;
  }

  @Override
  public void getProperty(@NotNull SvnTarget target,
                          @NotNull String property,
                          @Nullable SVNRevision revision,
                          @Nullable Depth depth,
                          @Nullable PropertyConsumer handler) throws VcsException {
    runGetProperty(target, property, revision, depth, handler);
  }

  @Override
  public void list(@NotNull SvnTarget target,
                   @Nullable SVNRevision revision,
                   @Nullable Depth depth,
                   @Nullable PropertyConsumer handler) throws VcsException {
    runGetProperty(target, null, revision, depth, handler);
  }

  @Override
  public void setProperty(@NotNull File file,
                          @NotNull String property,
                          @Nullable PropertyValue value,
                          @Nullable Depth depth,
                          boolean force) throws VcsException {
    try {
      createClient().doSetProperty(file, property, toPropertyValue(value), force, toDepth(depth), null, null);
    }
    catch (SVNException e) {
      throw new SvnBindException(e);
    }
  }

  @Override
  public void setProperties(@NotNull File file, @NotNull PropertiesMap properties) throws VcsException {
    final SVNProperties propertiesToSet = toSvnProperties(properties);
    try {
      createClient().doSetProperty(file, new ISVNPropertyValueProvider() {
        @Override
        public SVNProperties providePropertyValues(File path, SVNProperties properties) throws SVNException {
          return propertiesToSet;
        }
      }, true, SVNDepth.EMPTY, null, null);
    }
    catch (SVNException e) {
      throw new SvnBindException(e);
    }
  }

  @Override
  public void setRevisionProperty(@NotNull SvnTarget target,
                                  @NotNull String property,
                                  @NotNull SVNRevision revision,
                                  @Nullable PropertyValue value,
                                  boolean force) throws VcsException {
    try {
      if (target.isFile()) {
        createClient().doSetRevisionProperty(target.getFile(), revision, property, toPropertyValue(value), force, null);
      }
      else {
        createClient().doSetRevisionProperty(target.getURL(), revision, property, toPropertyValue(value), force, null);
      }
    }
    catch (SVNException e) {
      throw new SvnBindException(e);
    }
  }

  @NotNull
  private static SVNProperties toSvnProperties(@NotNull PropertiesMap properties) {
    SVNProperties result = new SVNProperties();

    for (Map.Entry<String, PropertyValue> entry : properties.entrySet()) {
      result.put(entry.getKey(), toPropertyValue(entry.getValue()));
    }

    return result;
  }

  private void runGetProperty(@NotNull SvnTarget target,
                              @Nullable String property,
                              @Nullable SVNRevision revision,
                              @Nullable Depth depth,
                              @Nullable PropertyConsumer handler) throws VcsException {
    SVNWCClient client = createClient();

    try {
      if (target.isURL()) {
        client.doGetProperty(target.getURL(), property, target.getPegRevision(), revision, toDepth(depth), toHandler(handler));
      } else {
        client.doGetProperty(target.getFile(), property, target.getPegRevision(), revision, toDepth(depth), toHandler(handler), null);
      }
    } catch (SVNException e) {
      throw new VcsException(e);
    }
  }

  private PropertyData getRevisionProperty(@NotNull SvnTarget target, @NotNull final String property, @Nullable SVNRevision revision)
    throws SVNException {
    final SVNWCClient client = createClient();
    final PropertyData[] result = new PropertyData[1];
    ISVNPropertyHandler handler = new ISVNPropertyHandler() {
      @Override
      public void handleProperty(File path, SVNPropertyData property) throws SVNException {
        handle(property);
      }

      @Override
      public void handleProperty(SVNURL url, SVNPropertyData property) throws SVNException {
        handle(property);
      }

      @Override
      public void handleProperty(long revision, SVNPropertyData property) throws SVNException {
        handle(property);
      }

      private void handle(@NotNull SVNPropertyData data) {
        if (property.equals(data.getName())) {
          result[0] = PropertyData.create(data);
        }
      }
    };

    if (target.isFile()) {
      client.doGetRevisionProperty(target.getFile(), null, revision, handler);
    } else {
      client.doGetRevisionProperty(target.getURL(), null, revision, handler);
    }

    return result[0];
  }

  @Nullable
  private static ISVNPropertyHandler toHandler(@Nullable final PropertyConsumer consumer) {
    ISVNPropertyHandler result = null;

    if (consumer != null) {
      result = new ISVNPropertyHandler() {
        @Override
        public void handleProperty(File path, SVNPropertyData property) throws SVNException {
          consumer.handleProperty(path, PropertyData.create(property));
        }

        @Override
        public void handleProperty(SVNURL url, SVNPropertyData property) throws SVNException {
          consumer.handleProperty(url, PropertyData.create(property));
        }

        @Override
        public void handleProperty(long revision, SVNPropertyData property) throws SVNException {
          consumer.handleProperty(revision, PropertyData.create(property));
        }
      };
    }

    return result;
  }

  @Nullable
  private static SVNPropertyValue toPropertyValue(@Nullable PropertyValue value) {
    SVNPropertyValue result = null;

    if (value != null) {
      result = SVNPropertyValue.create(value.toString());
    }

    return result;
  }
}