Skip to main content
summaryrefslogtreecommitdiffstats
blob: d0906c691b42217272b539abb7a3ff178a6390c9 (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
/*******************************************************************************
 * Copyright (c) 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.common.environment.uri;

import java.net.URL;
import java.util.Hashtable;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.osgi.util.NLS;
import org.eclipse.wst.common.internal.environment.eclipse.Messages;
import org.eclipse.wst.common.internal.environment.relative.RelativeScheme;

/**
 * 
 * This class provides a default implementation of the IURIFactory interface.
 *
 */
public class SimpleURIFactory implements IURIFactory
{
  private Hashtable    schemes_ = new Hashtable();  
  
  /**
   * Creates and returns a new IURI for the given string.
   */
  public IURI newURI(String uri) throws URIException
  {
    IURIScheme scheme = newURIScheme( uri, false );
    
    return scheme.newURI( uri );
  }

  /**
   * Creates and returns a new IURI for the given URL.
   */
  public IURI newURI(URL url) throws URIException
  {
    IURIScheme scheme = newURIScheme( url.toString(), false );
    
    return scheme.newURI( url );
  }

  /**
   * Creates and returns a new IURIScheme for the given scheme string.
   * If the string contains no colons, the entire string is interpretted
   * as the name of the scheme. If the string contains a colon, then the
   * substring up to but excluding the first colon is interpretted as the
   * name of the scheme, meaning the caller can pass in any IURI string in
   * order to get a IURIScheme object.
   */
  public IURIScheme newURIScheme(String schemeOrURI) throws URIException
  {
    return newURIScheme( schemeOrURI, true );
  }
  
  public void registerScheme( String protocol, IURIScheme scheme )
  {
    schemes_.put( protocol, scheme );
  }
  
  private IURIScheme newURIScheme(String schemeOrURI, boolean checkForScheme ) throws URIException
  {
    IURIScheme newScheme = null;
    
    if( schemeOrURI == null )
    {
      throw new URIException( 
              new Status( IStatus.ERROR, "id", 0, 
                 NLS.bind( Messages.MSG_NULL_ARG_SPECIFIED, "newURIScheme"), null ) );
    }
    
    int colon    = schemeOrURI.indexOf(':');
    int slash    = schemeOrURI.indexOf('/');
    
    // A protocol was specified.  Note: a colon appearing after a path is not
    // considered part of the protocol for this IURI.
    if( ( checkForScheme && colon == -1 ) ||
        ( colon != -1 && slash == -1) || 
        ( colon != -1 && colon < slash ) )
    {
      // If colon is -1 then we will treat the entire input parameter as the protocol.
      if( colon == -1 ) colon = schemeOrURI.length();
      
      String protocol = schemeOrURI.substring(0, colon );
      newScheme       = (IURIScheme)schemes_.get( protocol );
      
      if( newScheme == null )
      {
        throw new URIException( 
            new Status( IStatus.ERROR, "id", 0, 
                NLS.bind( Messages.MSG_SCHEME_NOT_FOUND, schemeOrURI ), null ) );
                
      }
    }
    else if( schemeOrURI.startsWith( "/") )
    {
      throw new URIException( 
          new Status( IStatus.ERROR, "id", 0,
              NLS.bind( Messages.MSG_ABSOLUTE_PATH_WITHOUT_SCHEME, schemeOrURI ), null ) );
      
    }
    else
    {
      newScheme = new RelativeScheme();
    }
    
    return newScheme;
  }
}

Back to the top