Skip to main content
summaryrefslogtreecommitdiffstats
blob: c9f3638aeafc15ca1fd3f86ea287a400d4ced8b7 (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
/*******************************************************************************
 * Copyright (c) 2001, 2005 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.wsdl.validation.internal.resolver;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.xerces.xni.XMLResourceIdentifier;
import org.apache.xerces.xni.XNIException;
import org.apache.xerces.xni.parser.XMLEntityResolver;
import org.apache.xerces.xni.parser.XMLInputSource;
import org.eclipse.wst.wsdl.validation.internal.util.LazyURLInputStream;
import org.eclipse.wst.wsdl.validation.internal.xml.XMLCatalog;

/**
 * This is the main URI resolver that calls out to all of the registered
 * external URI resolvers to locate an entity. If none of the external resolvers
 * can locate the entity the resolver will ask the internal WSDL validator XML
 * catalog to resolve the location.
 */
public class URIResolver implements IExtensibleURIResolver, XMLEntityResolver
{
  private List extURIResolversList = new ArrayList();

  /**
   * Constructor. 
   */
  public URIResolver()
  {
  }


  /**
   * Add an extension URI resolver.
   * 
   * @param uriResolver
   *          The extension URI resolver.
   */
  public void addURIResolver(IExtensibleURIResolver uriResolver)
  {
    extURIResolversList.add(uriResolver);
  }

  
  /**
   * @see org.eclipse.wst.wsdl.validation.internal.resolver.IExtensibleURIResolver#resolve(java.lang.String, java.lang.String, java.lang.String, org.eclipse.wst.wsdl.validation.internal.resolver.IURIResolutionResult)
   */
  public void resolve(String baseLocation, String publicId, String systemId, IURIResolutionResult result)
  {
    Iterator resolverIter = extURIResolversList.iterator();
    while(resolverIter.hasNext())
    {
      IExtensibleURIResolver resolver = (IExtensibleURIResolver)resolverIter.next();
      if (resolver == null)
      {
        continue;
      }
      resolver.resolve(baseLocation, publicId, systemId, result);
      if (result.getLogicalLocation() != null && !result.getPhysicalLocation().equals(systemId))
      {
        break;
      }
    }

    // If we haven't been able to locate the result yet ask the internal XML
    // catalog.
    if (result.getLogicalLocation() == null && (publicId != null || systemId != null))
    {
      String tempresult = XMLCatalog.getInstance().resolveEntityLocation(publicId, systemId);
      if(tempresult != null)
      {
    	result.setLogicalLocation(tempresult);
    	result.setPhysicalLocation(tempresult);
      }
    }
    if(result.getLogicalLocation() == null)
    {
      result.setLogicalLocation(normalize(baseLocation, systemId));
      result.setPhysicalLocation(result.getLogicalLocation());
    }
  }
  
  public IURIResolutionResult resolve(String baseLocation, String publicId, String systemId)
  {
	IURIResolutionResult result= new URIResolutionResult();
	resolve(baseLocation, publicId, systemId, result);
	return result;
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.xerces.xni.parser.XMLEntityResolver#resolveEntity(org.apache.xerces.xni.XMLResourceIdentifier)
   */
  public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier) throws XNIException, IOException
  {
    String publicId = resourceIdentifier.getPublicId();
    String systemId = resourceIdentifier.getLiteralSystemId();
    if (publicId == null || publicId.equals(""))
    {
      publicId = resourceIdentifier.getNamespace();
    }
    IURIResolutionResult result = resolve(resourceIdentifier.getBaseSystemId(), publicId, systemId);
    XMLInputSource xmlInputSource = null;
    if (result != null)
    {
      LazyURLInputStream is = new LazyURLInputStream(result.getPhysicalLocation());
      xmlInputSource = new XMLInputSource(publicId, result.getLogicalLocation(), result.getLogicalLocation(), is, null);
    }
    return xmlInputSource;
  }
  
  /**
   * Normalize the systemId. Make it absolute with respect to the
   * baseLocation if necessary.
   * 
   * @param baseLocation The base location of the file.
   * @param systemId The system id of the file.
   * @return A normalized version of the system id.
   */
  protected String normalize(String baseLocation, String systemId)
  {
  	if(systemId == null)
  	{
  	  return systemId;
  	}
  	// Try to find a scheme in the systemId.
  	int schemaLoc = systemId.indexOf(':');
  	if(schemaLoc != -1 && systemId.charAt(schemaLoc+1) == '/')
  	{
  	  // A scheme has been found. The systemId is an
  	  // absolute location so return it.
  	  return systemId;
  	}
  	if(baseLocation == null)
  	{
  	  return baseLocation;
  	}
  	
  	String result = "";
  	
  	// Ensure all slashes in the locations are /.
  	baseLocation = baseLocation.replace('\\','/');
  	systemId = systemId.replace('\\','/');
  	
  	// Remove the trailing section of the baseLocation.
	int lastSlash = baseLocation.lastIndexOf('/');
  	String tempresult = baseLocation.substring(0, lastSlash+1);
  	
  	if(systemId.startsWith("/"))
  	{
  	  systemId = systemId.substring(1);
  	}
  	
  	// Join the base location with the systemid
  	tempresult = tempresult + systemId;
  	
  	// While the relative location starts with a ../ or ./ change
  	// the result and the relative location.
  	int loc;
  	while((loc = tempresult.lastIndexOf("./")) != -1)
  	{
  	  result = tempresult.substring(loc + 2) + result;
  	  if(tempresult.charAt(loc - 1) == '.')
  	  {
  	  	if(tempresult.charAt(loc - 2) == '/')
  	  	{
  	  	  String temp = tempresult.substring(0, loc - 2);
  	  	  int loc2 = temp.lastIndexOf('/');
  	  	  if(loc2 == -1)
  	  	  {
  	  	  	// If there is no other / before this the URL must start with scheme:/../
  	  	  	result = "../" + result;
  	  	  	tempresult = tempresult.substring(0, loc - 1);
  	  	  }
  	  	  else
  	  	  {
  	  	  	// Remove the section that comes before this one from tempresult unless it's ../.
  	  	    tempresult = tempresult.substring(0, loc - 1);
  	  	    int numSectsToRemove = 1;
  	  	    
  	  	    while(tempresult.endsWith("./"))
  	  	    {
  	  	      int tempreslen = tempresult.length();
  	  	      if(tempreslen > 2 && tempresult.charAt(tempreslen -3) == '.')
  	  	      {
  	  	      	if(tempreslen > 3 && tempresult.charAt(tempreslen - 4) == '/')
  	  	      	{
  	  	      	  numSectsToRemove++;
  	  	      	  tempresult = tempresult.substring(0, tempresult.length() -3);
  	  	        }
  	  	      	else
  	  	      	{
  	  	      	  break;
  	  	      	}
  	  	      }
  	  	      else
  	  	      {
  	  	      	if(tempresult.charAt(tempresult.length() -2) == '/')
  	  	      	{
  	  	      	  tempresult = tempresult.substring(0, tempresult.length() -2);
  	  	      	}
  	  	      	else
  	  	      	{
  	  	      	  break;
  	  	      	}
  	  	      }
  	  	    }
  	  	    // Remove the sections.
  	  	    for(int i = 0; i < numSectsToRemove; i++)
  	  	    {
  	  	      String temp2 = tempresult.substring(0,tempresult.length()-1);
  	  	      int loc3 = temp2.lastIndexOf('/');
  	  	      if(loc3 == -1)
  	  	      {
  	  	      	break;
  	  	      }
  	  	      tempresult = tempresult.substring(0, loc3+1);
  	  	    }
  	  	  } 
  	  	}
  	  	else
  	  	{
  	  	  // The URI is of the form file://somedir../ so copy it as is
  	  	  String temp = tempresult.substring(0, loc - 1);
	  	  int loc2 = temp.lastIndexOf('/');
	  	  if(loc2 == -1)
	  	  {
	  	  	// The URI must look like file:../ or ../ so copy over the whole tempresult.
	  	  	result = tempresult.substring(0,loc+2) + result;
	  	  	tempresult = "";
	  	  }
	  	  else
	  	  {
	  	  	// Copy over the whole somedir../
	  	  	result = tempresult.substring(loc2 + 1, tempresult.length());
	  	    tempresult = tempresult.substring(0, loc2+1);
	  	  }
  	  	}
  	  }
  	  else
  	  {
  	    if(tempresult.charAt(loc -1 ) == '/')
	  	{
  	  	  // Result is of the form file://something/./something so remove the ./
  	      tempresult = tempresult.substring(0,loc);
	  	}
  	    else
  	    {
  	      // Result URI is of form file://somedir./ so copy over the whole section.
    	  String temp = tempresult.substring(0, loc - 1);
  	  	  int loc2 = temp.lastIndexOf('/');
  	  	  if(loc2 == -1)
  	  	  {
  	  	  	// The URI must look like file:./ or ./ so copy over the whole tempresult.
  	  	  	result = tempresult.substring(0, loc) + result;
  	  	  	tempresult = "";
  	  	  }
  	  	  else
  	  	  {
  	  	  	// Copy over the whole somedir./
  	  	  	result = tempresult.substring(loc2 + 1, tempresult.length());
  	  	    tempresult = tempresult.substring(0, loc2+1);
  	  	  }
  	    }
  	  }
  	}
  	result = tempresult + result;
  	return result;
  }

}

Back to the top