Skip to main content
summaryrefslogtreecommitdiffstats
blob: 75dcfbea6c93b76cf7ea4590d2e2b0da4ee41e9a (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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
 * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) 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:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.emf.cdo.internal.ui.views;

import org.eclipse.emf.cdo.common.lob.CDOBlob;
import org.eclipse.emf.cdo.common.lob.CDOClob;
import org.eclipse.emf.cdo.eresource.CDOBinaryResource;
import org.eclipse.emf.cdo.eresource.CDOFileResource;
import org.eclipse.emf.cdo.eresource.CDOTextResource;
import org.eclipse.emf.cdo.internal.ui.bundle.OM;

import org.eclipse.net4j.util.WrappedException;
import org.eclipse.net4j.util.io.IORuntimeException;
import org.eclipse.net4j.util.io.IOUtil;

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.filesystem.provider.FileStore;
import org.eclipse.core.filesystem.provider.FileSystem;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.PlatformObject;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IPersistableElement;
import org.eclipse.ui.IURIEditorInput;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.CharArrayReader;
import java.io.CharArrayWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.WeakHashMap;

/**
 * @author Eike Stepper
 */
public class CDOLobEditorInput extends PlatformObject implements IURIEditorInput
{
  private static final String SCHEME = "cdo.lob";

  private static final Map<CDOLobEditorInput, URI> inputs = new WeakHashMap<CDOLobEditorInput, URI>();

  private static int lastID;

  private CDOFileResource<?> resource;

  private URI uri;

  public CDOLobEditorInput(CDOFileResource<?> resource)
  {
    this.resource = resource;

    try
    {
      uri = new URI(SCHEME + "://" + ++lastID);
    }
    catch (URISyntaxException ex)
    {
      throw WrappedException.wrap(ex);
    }

    synchronized (inputs)
    {
      inputs.put(this, uri);
    }
  }

  public CDOFileResource<?> getResource()
  {
    return resource;
  }

  public boolean exists()
  {
    return true;
  }

  public ImageDescriptor getImageDescriptor()
  {
    return null;
  }

  public String getName()
  {
    return resource.getName();
  }

  public IPersistableElement getPersistable()
  {
    return null;
  }

  public String getToolTipText()
  {
    return resource.getPath();
  }

  public URI getURI()
  {
    return uri;
  }

  /**
   * @author Eike Stepper
   */
  public static class LobFileSystem extends FileSystem
  {
    @Override
    public IFileStore getStore(URI uri)
    {
      return new LobFileStore(uri);
    }
  }

  /**
   * @author Eike Stepper
   */
  public static class LobFileStore extends FileStore
  {
    private static final String[] NO_CHILDREN = new String[0];

    private final URI uri;

    private CDOFileResource<?> resource;

    private FileInfo info;

    public LobFileStore(URI uri)
    {
      this.uri = uri;
    }

    @Override
    public URI toURI()
    {
      return uri;
    }

    private CDOFileResource<?> getResource()
    {
      if (resource == null)
      {
        synchronized (inputs)
        {
          for (Entry<CDOLobEditorInput, URI> entry : inputs.entrySet())
          {
            if (uri.equals(entry.getValue()))
            {
              resource = entry.getKey().getResource();
              break;
            }
          }
        }
      }

      return resource;
    }

    private String getEncoding(CDOTextResource textResource)
    {
      String encoding = textResource.getEncoding();
      if (encoding == null)
      {
        try
        {
          encoding = ResourcesPlugin.getWorkspace().getRoot().getDefaultCharset();
        }
        catch (CoreException ex)
        {
          OM.LOG.error(ex);
        }
      }
      return encoding;
    }

    @Override
    public IFileInfo fetchInfo(int options, IProgressMonitor monitor) throws CoreException
    {
      if (info == null)
      {
        info = new FileInfo(getName());
        info.setLastModified(0L);
        info.setExists(true);
        info.setDirectory(false);
        info.setLength(EFS.NONE);
        info.setAttribute(EFS.ATTRIBUTE_READ_ONLY, false);
        info.setAttribute(EFS.ATTRIBUTE_HIDDEN, false);
      }

      return info;
    }

    @Override
    public InputStream openInputStream(int options, IProgressMonitor monitor) throws CoreException
    {
      try
      {
        CDOFileResource<?> resource = getResource();
        if (resource instanceof CDOTextResource)
        {
          CDOTextResource textResource = (CDOTextResource)resource;
          CDOClob clob = textResource.getContents();
          if (clob == null)
          {
            return new ByteArrayInputStream(new byte[0]);
          }

          Reader reader = clob.getContents();
          CharArrayWriter writer = new CharArrayWriter();
          IOUtil.copyCharacter(reader, writer);

          String encoding = getEncoding(textResource);
          byte[] bytes = writer.toString().getBytes(encoding);
          return new ByteArrayInputStream(bytes);
        }

        CDOBlob blob = ((CDOBinaryResource)resource).getContents();
        if (blob == null)
        {
          return new ByteArrayInputStream(new byte[0]);
        }

        InputStream inputStream = blob.getContents();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        IOUtil.copy(inputStream, outputStream);

        byte[] bytes = outputStream.toByteArray();
        return new ByteArrayInputStream(bytes);
      }
      catch (IOException ex)
      {
        throw new IORuntimeException(ex);
      }
    }

    @Override
    public OutputStream openOutputStream(int options, IProgressMonitor monitor) throws CoreException
    {
      final CDOFileResource<?> resource = getResource();
      return new ByteArrayOutputStream()
      {
        @Override
        public void close() throws IOException
        {
          if (resource instanceof CDOTextResource)
          {
            CDOTextResource textResource = (CDOTextResource)resource;
            String encoding = getEncoding(textResource);

            String string = toString(encoding);
            CDOClob clob = new CDOClob(new CharArrayReader(string.toCharArray()));
            textResource.setContents(clob);
          }
          else
          {
            byte[] bytes = toByteArray();
            CDOBlob blob = new CDOBlob(new ByteArrayInputStream(bytes));
            ((CDOBinaryResource)resource).setContents(blob);
          }
        }
      };
    }

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

    @Override
    public IFileStore getParent()
    {
      return null; // This is a flat file system
    }

    @Override
    public IFileStore getChild(String name)
    {
      return null; // This is a flat file system
    }

    @Override
    public String[] childNames(int options, IProgressMonitor monitor) throws CoreException
    {
      return NO_CHILDREN; // This is a flat file system
    }
  }
}

Back to the top