Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9c5b7b8596ebb75028e01a0ca7112dc708399d5a (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
/*******************************************************************************
 * 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.ws.internal.parser.disco;

import java.util.Vector;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

/**
 * See http://msdn.microsoft.com/msdnmag/issues/02/02/xml/default.aspx for more
 * details on DISCO.
 */
public class DISCOParser
{
  private DocumentBuilder parser_;
  private final String NS_DISCO = "http://schemas.xmlsoap.org/disco/";
  private final String DISCOVERY = "discovery";
  private final String DISCOVERY_REF = "discoveryRef";
  private final String NS_CONTRACT_REF = "http://schemas.xmlsoap.org/disco/scl/";
  private final String CONTRACT_REF = "contractRef";
  private final String REF = "ref";
  private final String DOC_REF = "docRef";

  public DISCOParser()
  {
    try
    {
      DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
      docBuilderFactory.setNamespaceAware(true);
      parser_ = docBuilderFactory.newDocumentBuilder();
    }
    catch (ParserConfigurationException pce)
    {
      parser_ = null;
    }
  }

  public DISCOReference[] parse(String discoURI, InputSource source) throws Exception
  {
    if (parser_ != null)
    {
      Document doc = parser_.parse(source);
      Element rootElement = doc.getDocumentElement();
      // Root element must by <disco:discovery
      // xmlns:disco="http://schemas.xmlsoap.org/disco/">
      if (rootElement != null && rootElement.getNamespaceURI().equals(NS_DISCO) && rootElement.getLocalName().equals(DISCOVERY))
      {
        NodeList childNodes = rootElement.getChildNodes();
        Vector discoReferences = new Vector();
        for (int i = 0; i < childNodes.getLength(); i++)
        {
          Node childNode = childNodes.item(i);
          if (childNode instanceof Element)
          {
            Element element = (Element)childNode;
            String localName = element.getLocalName();
            String nsURI = element.getNamespaceURI();
            if (nsURI.equals(NS_DISCO) && localName.equals(DISCOVERY_REF))
            {
              // DISCO link.
              String ref = convertToAbsoluteURI(discoURI, element.getAttribute(REF));
              discoReferences.addElement(new DISCOReference(ref));
            }
            else if (nsURI.equals(NS_CONTRACT_REF) && localName.equals(CONTRACT_REF))
            {
              // WSDL link.
              String ref = convertToAbsoluteURI(discoURI, element.getAttribute(REF));
              String docRef = convertToAbsoluteURI(discoURI, element.getAttribute(DOC_REF));
              discoReferences.addElement(new DISCOContractReference(ref, docRef));
            }
          }
        }
        int numberOfDISCOReferences = discoReferences.size();
        if (numberOfDISCOReferences > 0)
        {
          DISCOReference[] references = new DISCOReference[numberOfDISCOReferences];
          discoReferences.copyInto(references);
          return references;
        }
      }
      else
      {
        // The document is not a valid DISCO document.
        throw new Exception();
      }
    }
    return null;
  }

  private String convertToAbsoluteURI(String discoURI, String refURI)
  {
    if (refURI != null)
    {
      if (refURI.indexOf("://") > -1)
      {
        // refURI is already absolute.
        return refURI;
      }
      else
      {
        StringBuffer absoluteURI = new StringBuffer(discoURI.substring(0, Math.max(discoURI.lastIndexOf('\\'), discoURI.lastIndexOf('/') + 1)));
        absoluteURI.append(refURI);
        return absoluteURI.toString();
      }
    }
    return null;
  }
}

Back to the top