Skip to main content
summaryrefslogtreecommitdiffstats
blob: 36ca130aae2aef199a64f0e43c5372bfa1594c47 (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
/*******************************************************************************
 * Copyright (c) 2002-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 - Initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.wsi.internal.core.xml.schema;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.wst.wsi.internal.core.WSIException;
import org.eclipse.wst.wsi.internal.core.util.TestUtils;
import org.eclipse.wst.wsi.internal.core.xml.XMLUtils;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import com.ibm.wsdl.util.xml.DOM2Writer;

/**
 * This class is used to validate an XML Schema.
 * 
 * @author Peter  Brittenham (peterbr@us.ibm.com)
 * @version 1.0.1
 */
public class XMLSchemaValidator extends XMLSchemaProcessor
{
  private static final String XMLNS_PREFIX = "xmlns:";

  /**
   * Constructor for XMLSchemaValidator.
   * @param context       document context. 
   * @param documentList  cache of previously parsed documents.
   */
  public XMLSchemaValidator(String context)
  {
    super(context);
  }

  protected void processSchema(Element element)
  {
    NamedNodeMap attrList;

    HashMap elementMap = new HashMap();
    HashMap nsMap = new HashMap();

    try
    {
      // Get schema content as a string
      String schema = DOM2Writer.nodeToString(element);

      // Get list of element namespaces
      if ((attrList = element.getAttributes()) != null)
      {
        addNamespaces(attrList, elementMap, null);
      }

      // If elementMap does not contain entry for it's own namespace, then add it
      if (elementMap.get(XMLNS_PREFIX + element.getPrefix()) == null)
      {
        elementMap.put(
          XMLNS_PREFIX + element.getPrefix(),
          element.getNamespaceURI());
      }

      // Get first parent node and then process all of them until you hit the top of the document
      Node parentNode = element.getParentNode();
      while (parentNode != null)
      {
        // Only process Element nodes
        if (parentNode.getNodeType() == Node.ELEMENT_NODE)
        {
          // Get the list of attributes for the parent node
          attrList = parentNode.getAttributes();

          // If there are attributes, look for the xmlns: attributes
          if (attrList != null)
          {
            addNamespaces(attrList, nsMap, elementMap);
          }
        }

        parentNode = parentNode.getParentNode();
      }

      // Build namespace list
      if (nsMap.size() > 0)
      {
        String attr;
        String namespaceList = "";
        Iterator iterator = nsMap.keySet().iterator();
        while (iterator.hasNext())
        {
          attr = (String) iterator.next();
          namespaceList += " " + attr + "=\"" + nsMap.get(attr) + "\"";
        }

        // Add namespace settings, since the DOM2Writer will miss any that are used as attribute values
        int index = schema.indexOf(">");
        if (index > 0)
        {
          String start = schema.substring(0, index);
          String end = schema.substring(index);

          schema = start + namespaceList + end;
        }
      }

      // Schema validate the XML schema document
      XMLUtils.parseXML(schema, TestUtils.getXMLSchemaLocation());
    }

    catch (WSIException we)
    {
      Throwable t = we.getTargetException();
      if (t != null)
      {
        returnList.add(t.getMessage());
      }
    }
  }

  private void addNamespaces(NamedNodeMap attrList, Map nsMap, Map elementMap)
  {
    Node attr;
    String nodeName;

    for (int i = 0; i < attrList.getLength(); i++)
    {
      attr = attrList.item(i);

      // Get the attribute node name
      nodeName = attr.getNodeName();

      // If it starts with xmlns:, then determine if it should be added to the list of namespaces 
      if (nodeName.startsWith(XMLNS_PREFIX)
        && ((elementMap == null
          || (elementMap != null && elementMap.get(nodeName) == null))))
      {
        if (nsMap.get(nodeName) == null)
          nsMap.put(nodeName, attr.getNodeValue());
      }
    }
  }

}

Back to the top