Skip to main content
summaryrefslogtreecommitdiffstats
blob: 268a8f745980b705dd04049c712e1d662683c56b (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
/*
 * Copyright (c) 2002 IBM Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *   IBM - Initial API and implementation
 *   Jens Lukowski/Innoopract - initial renaming/restructuring
 * 
 */
package org.eclipse.wst.xml.core.internal.catalog;

import java.io.IOException;
import java.net.MalformedURLException;
import org.eclipse.core.resources.IFile;
import org.eclipse.wst.common.uriresolver.internal.provisional.URIResolverExtension;
import org.eclipse.wst.xml.core.internal.Logger;
import org.eclipse.wst.xml.core.internal.XMLCoreMessages;
import org.eclipse.wst.xml.core.internal.XMLCorePlugin;
import org.eclipse.wst.xml.core.internal.catalog.provisional.ICatalog;

/**
 * This class is used to inject the XMLCatalog resolution behaviour into the
 * Common Extensible URI Resolver. This class is referenced in the XML Catalog
 * plugin's plugin.xml file.
 */
public class XMLCatalogURIResolverExtension implements URIResolverExtension
{
  public String resolve(IFile file, String baseLocation, String publicId, String systemId)
  {
    // if we have catalog in a project we may add it
    // to the catalog manager first
    ICatalog catalog = XMLCorePlugin.getDefault().getDefaultXMLCatalog();
    if (catalog == null)
    {
      Logger.log(Logger.ERROR_DEBUG, XMLCoreMessages.Catalog_resolution_null_catalog);
      return null;
    }
    String resolved = null;
    if (systemId != null)
    {
      try
      {
        resolved = catalog.resolveSystem(systemId);
        if (resolved == null)
        {
          resolved = catalog.resolveURI(systemId);
        }
      }
      catch (MalformedURLException me)
      {
        Logger.log(Logger.ERROR_DEBUG, XMLCoreMessages.Catalog_resolution_malformed_url);
        resolved = null;
      }
      catch (IOException ie)
      {
        Logger.log(Logger.ERROR_DEBUG, XMLCoreMessages.Catalog_resolution_io_exception);
        resolved = null;
      }
    }
    if (resolved == null)
    {
      if (publicId != null)
      {
        // CS : this is a temporary workaround for bug 96772
        //
        // For schemas we always use locations where available and only use
        // namespace when no location is specified.  For XML entities (such as DOCTYPE) 
        // default always utilize the public catalog entry.
        //
        // This lame test below roughly discriminate between schema and XML entities.
        // TODO (bug 103243) remove this lame test once we move to the new URIResolver API
        // since the new API is explicit about namespace and publicId
        // 
        if (!(systemId != null && systemId.endsWith(".xsd"))) //$NON-NLS-1$
        {
          try
          {
            resolved = catalog.resolvePublic(publicId, systemId);
            if (resolved == null)
            {
              resolved = catalog.resolveURI(publicId);
            }
          }
          catch (MalformedURLException me)
          {
            Logger.log(Logger.ERROR_DEBUG, XMLCoreMessages.Catalog_resolution_malformed_url);
            resolved = null;
          }
          catch (IOException ie)
          {
            Logger.log(Logger.ERROR_DEBUG, XMLCoreMessages.Catalog_resolution_io_exception);
            resolved = null;
          }
        }
      }
    }
    return resolved;
  }
}

Back to the top