Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6c6e278adb8b97fc3454d1a9088483ffcef8aae2 (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
/*******************************************************************************
 * 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.util;

import java.util.Map;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.Resource.Factory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;

/**
 * This class provides support for the loading of WSDL or XSD resource from
 * URI's that don't end with .xsd or .wsdl extensions.
 * 
 * These URI's foil EMF's standard resource loading mechanism wich assumes that
 * a resource type can always be deduced from the file extension.
 * 
 *  Code Example:
 *    ResourceSet resourceSet = new ResourceSetImpl();
 *    WSDLResourceFactoryRegistry registry = new WSDLResourceFactoryRegistry(Resource.Factory.Registry.INSTANCE);
 *    resourceSet.setResourceFactoryRegistry(registry);
 * <p>
 * <b>Note:</b> This class/interface is part of an interim API that is still under development and expected to
 * change significantly before reaching stability. It is being made available at this early stage to solicit feedback
 * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken
 * (repeatedly) as the API evolves.
 * </p>
 * @since 1.0
 */
public class WSDLResourceFactoryRegistry implements Resource.Factory.Registry
{
  protected Resource.Factory.Registry parent;

  public WSDLResourceFactoryRegistry(Resource.Factory.Registry parent)
  {
    this.parent = parent;
  }

  public Map getExtensionToFactoryMap()
  {
    return parent.getExtensionToFactoryMap();
  }

  public Map getProtocolToFactoryMap()
  {
    return parent.getProtocolToFactoryMap();
  }

  public Factory getFactory(URI uri)
  {
    String uriString = uri.toString();

    Factory factory = parent.getFactory(uri);
    Factory defaultFactory = (Factory) INSTANCE.getFactory(URI.createURI(DEFAULT_EXTENSION));

    // give the parent the first crack at getting the factory
    // if the factory is null or the known 'default' factory then we'll
    // try to compute the factory ourselves
    if (factory == null || factory == defaultFactory)
    {
      if (uriString.endsWith("wsdl") || uriString.endsWith("WSDL"))
      {
        // handle cases like "http://xxx/Distance.jws?wsdl"
        //          	
        factory = parent.getFactory(URI.createURI("*.wsdl"));
      }
      else if (uriString.endsWith("xsd") || uriString.endsWith("XSD"))
      {
        // handle cases like "http://xxx/Distance.jws?xsd"
        //        	
        factory = parent.getFactory(URI.createURI("*.xsd"));
      }
      else //if (uri.fileExtension() == null)
        {
        // handle cases like "http://xxx/example?getQuote"
        //              	
        // finally we must resort to opening the uri's stream and
        // peeking at the content to determine if its an xsd or wsdl
        //         	
        String rootElementName = getRootElementName(uriString);
        if (rootElementName != null)
        {
          if (rootElementName.equals("schema"))
          {
            factory = parent.getFactory(URI.createURI("*.xsd"));
          }
          else if (rootElementName.equals("definitions"))
          {
            factory = parent.getFactory(URI.createURI("*.wsdl"));
          }
        }
      }
    }
    return factory;
  }

  // this method uses a SAX Parser to parser a stream and determine the root element name
  // of any xml content
  private String getRootElementName(String uri)
  {
    RootElementNameContentHandler handler = new RootElementNameContentHandler();
    try
    {
      XMLReader reader = XMLReaderFactory.createXMLReader();
      reader.setContentHandler(handler);
      reader.parse(uri);
    }
    catch (Exception e)
    {
    }

    return handler.rootElementName;
  }

  protected static class RootElementNameContentHandler extends DefaultHandler
  {
    public String rootElementName;

    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
    {
      rootElementName = localName;

      // here I throw an exception to make the SAX Parser to stop parsing
      throw new SAXException("SAXParser intentonally stopped");
    }
  }
}

Back to the top