Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 58f639bbdb28f61a28fa079095ff0e50e0a2e63d (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
/*******************************************************************************
 * Copyright (c) 2001, 2004 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
 *******************************************************************************/
package org.eclipse.wst.xsd.ui.internal;

import java.io.IOException;
import java.io.InputStream;

import org.eclipse.core.resources.IFile;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.impl.URIConverterImpl;
import org.eclipse.wst.xml.uriresolver.util.IdResolver;
import org.eclipse.wst.xml.uriresolver.util.IdResolverImpl;

public class XSDURIConverter extends URIConverterImpl
{
  IFile resourceFile;
  public XSDURIConverter(IFile resourceFile)
  {
    super();
    this.resourceFile = resourceFile;
  }
  
  /**
   * @see org.eclipse.emf.ecore.resource.URIConverter#createInputStream(URI)
   */
  public InputStream createInputStream(URI uri) throws IOException
  {
    String scheme = uri.scheme();
    URI mappedURI = uri;
    if (scheme != null && !scheme.equals("file") && !scheme.equals("platform"))
    // if ("http".equals(scheme))
    {
      String theURI = uri.toString();
      IdResolver idResolver = new IdResolverImpl(theURI);
      String result = idResolver.resolveId("/", null, theURI);
      if (result != null)
      {
        mappedURI = createURI(result);
      }  
    }  
    return super.createURLInputStream(mappedURI);
  }
  
  public static URI createURI(String uriString)
  {
    if (hasProtocol(uriString))
      return URI.createURI(uriString);
    else
      return URI.createFileURI(uriString);
  }
  
  private static boolean hasProtocol(String uri)
  {
    boolean result = false;     
    if (uri != null)
    {
      int index = uri.indexOf(":");
      if (index != -1 && index > 2) // assume protocol with be length 3 so that the'C' in 'C:/' is not interpreted as a protocol
      {
        result = true;
      }
    }
    return result;
  }

  private String getRelativePathToSchema(String a, String b)
  {
    String result;
    if (b.startsWith(a))
    {
      result = b.substring(a.length() + 1);
      return result;
    }
    else
    {
      return b;
    }
  }
}

Back to the top