Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2682a9593bdeaffbbdb82891b1802907e34f5335 (plain) (blame)
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*******************************************************************************
 * Copyright (c) 2005, 2010 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 * Martin Oberhuber (Wind River) - [294429] Avoid substring baggage in FileInfo
 *******************************************************************************/
package org.eclipse.emf.cdo.internal.efs;

import org.eclipse.emf.cdo.eresource.CDOResourceFolder;
import org.eclipse.emf.cdo.eresource.CDOResourceNode;
import org.eclipse.emf.cdo.internal.efs.bundle.OM;
import org.eclipse.emf.cdo.view.CDOView;

import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileInfo;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.filesystem.provider.FileInfo;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;

import org.xml.sax.InputSource;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Eike Stepper
 */
public final class CDOFileStore extends AbstractFileStore
{
  private static final Path PROJECT_DESCRIPTION_PATH = new Path(IProjectDescription.DESCRIPTION_FILE_NAME);

  private CDOFileRoot root;

  private IPath path;

  public CDOFileStore(CDOFileRoot root, IPath path)
  {
    this.root = root;
    this.path = path;
  }

  @Override
  public CDOFileSystem getFileSystem()
  {
    return root.getFileSystem();
  }

  public CDOFileRoot getRoot()
  {
    return root;
  }

  @Override
  public CDOView getView()
  {
    return root.getView();
  }

  @Override
  protected CDOResourceNode doGetResourceNode()
  {
    return getView().getResourceNode(path.toPortableString());
  }

  @Override
  public IPath getPath()
  {
    return path;
  }

  @Override
  public IFileStore getParent()
  {
    if (path.segmentCount() == 1)
    {
      return root;
    }

    return new CDOFileStore(root, path.removeLastSegments(1));
  }

  @Override
  public String getName()
  {
    return path.lastSegment();
  }

  @Override
  public IFileInfo fetchInfo(int options, IProgressMonitor monitor)
  {
    FileInfo info = new FileInfo(getName());
    info.setLastModified(EFS.NONE);
    info.setLength(EFS.NONE);
    info.setAttribute(EFS.ATTRIBUTE_READ_ONLY, false);

    if (isProjectDescription())
    {
      info.setExists(getProjectDescription() != null);
      info.setDirectory(false);
    }
    else
    {
      try
      {
        CDOResourceNode resourceNode = getResourceNode();
        info.setExists(true);
        info.setDirectory(resourceNode instanceof CDOResourceFolder);
      }
      catch (Exception ex)
      {
        info.setExists(false);
      }
    }

    return info;
  }

  @Override
  public IFileStore getChild(IPath path)
  {
    return new CDOFileStore(root, this.path.append(path));
  }

  @Override
  public IFileStore getChild(String name)
  {
    return new CDOFileStore(root, path.append(name));
  }

  @Override
  public String[] childNames(int options, IProgressMonitor monitor) throws CoreException
  {
    List<String> result = new ArrayList<String>();
    CDOResourceNode resourceNode = getResourceNode();
    if (resourceNode instanceof CDOResourceFolder)
    {
      CDOResourceFolder resourceFolder = (CDOResourceFolder)resourceNode;
      for (CDOResourceNode node : resourceFolder.getNodes())
      {
        result.add(node.getName());
      }
    }

    return result.toArray(new String[result.size()]);
  }

  @Override
  public OutputStream openOutputStream(int options, IProgressMonitor monitor) throws CoreException
  {
    return new ByteArrayOutputStream()
    {
      @SuppressWarnings("restriction")
      @Override
      public void close() throws IOException
      {
        try
        {
          ByteArrayInputStream bais = new ByteArrayInputStream(toByteArray());
          IProjectDescription description = new org.eclipse.core.internal.resources.ProjectDescriptionReader()
              .read(new InputSource(bais));
          OM.associateProjectName(root.toURI(), description.getName());
        }
        catch (RuntimeException ex)
        {
          ex.printStackTrace();
          throw ex;
        }
      }
    };
  }

  @Override
  public InputStream openInputStream(int options, IProgressMonitor monitor) throws CoreException
  {
    if (monitor == null)
    {
      monitor = new NullProgressMonitor();
    }

    try
    {
      monitor.beginTask("", 1); //$NON-NLS-1$
      if (isProjectDescription())
      {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintStream out = new PrintStream(baos);
        out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        out.println("<projectDescription>");
        out.println("  <name>" + OM.getProjectName(root.toURI()) + "</name>");
        out.println("  <comment></comment>");
        out.println("  <projects>");
        out.println("  </projects>");
        out.println("  <buildSpec>");
        out.println("  </buildSpec>");
        out.println("  <natures>");
        out.println("  </natures>");
        out.println("</projectDescription>");
        out.flush();

        return new ByteArrayInputStream(baos.toByteArray());
      }

      return new FileInputStream(path.toPortableString());
    }
    catch (Exception ex)
    {
      ex.printStackTrace();
      throw new CoreException(new Status(IStatus.ERROR, OM.BUNDLE_ID, ex.getLocalizedMessage(), ex));
    }
    finally
    {
      monitor.done();
    }
  }

  private String getProjectDescription()
  {
    return OM.getProjectName(root.toURI());
  }

  private boolean isProjectDescription()
  {
    return path.equals(PROJECT_DESCRIPTION_PATH);
  }

  @Override
  public boolean equals(Object obj)
  {
    if (obj == this)
    {
      return true;
    }

    if (obj.getClass() == CDOFileStore.class)
    {
      CDOFileStore that = (CDOFileStore)obj;
      return root == that.root && path.equals(that.path);
    }

    return false;
  }

  @Override
  protected int createHashCode()
  {
    return root.hashCode() ^ path.hashCode();
  }

  @Override
  public void appendURI(StringBuilder builder)
  {
    root.appendURI(builder);
    builder.append("/");
    builder.append(path.toPortableString());
  }
}

Back to the top