Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 65d87cdc76e0d690dc38e35d21f586fca86b088d (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
/*******************************************************************************
 * Copyright (c) 2004, 2006 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.xml.core.internal.search.quickscan;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

import org.eclipse.wst.common.core.search.document.ComponentDeclarationEntry;
import org.eclipse.wst.common.core.search.document.ComponentReferenceEntry;
import org.eclipse.wst.common.core.search.document.FileReferenceEntry;
import org.eclipse.wst.common.core.search.document.SearchDocument;
import org.eclipse.wst.common.core.search.pattern.QualifiedName;
import org.eclipse.wst.common.core.search.pattern.SearchPattern;
import org.eclipse.wst.xml.core.internal.search.XMLComponentDeclarationPattern;
import org.eclipse.wst.xml.core.internal.search.XMLComponentReferencePattern;
import org.eclipse.wst.xml.core.internal.search.impl.IXMLSearchConstants;
import org.eclipse.wst.xml.core.internal.search.matching.PatternMatcher;
import org.eclipse.wst.xml.core.internal.search.matching.SAXSearchElement;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * This class is a SAX content handler, it should be recycled before scanning a file for the new SearchPattern.
 *
 */
public class XMLQuickScanContentHandler extends DefaultHandler
{
	private Map namespaceMap = new HashMap(); // Map of string prefix keys and namespace
	private String targetNamespace = ""; //$NON-NLS-1$
	
	private SearchPattern pattern;
	private SearchDocument document;  // we'll add useful entries in the search document as we parsing
	private SAXSearchElement searchElement = new SAXSearchElement();

	private boolean hasMatch = false;
	private Stack currentPath = new Stack();
	private PatternMatcher matcher;
	
	public static final String XMLSCHEMA_NAMESPACE = "http://www.w3.org/2001/XMLSchema"; //$NON-NLS-1$
  public static final String WSDL_NAMESPACE = "http://schemas.xmlsoap.org/wsdl/"; //$NON-NLS-1$

	
	public XMLQuickScanContentHandler(PatternMatcher matcher, SearchPattern pattern) {
		super();
		this.pattern = pattern;
		this.matcher = matcher;
	}
	
	public XMLQuickScanContentHandler(SearchDocument document, PatternMatcher matcher, SearchPattern pattern) {
		super();
		this.pattern = pattern;
		this.matcher = matcher;
		this.document = document;
	}
	
	public XMLQuickScanContentHandler() {
		super();
	}

	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException
	{	
		// Search for targetNamespace if we haven't encountered it yet.
		if (targetNamespace.equals("")) //$NON-NLS-1$
		{
			int nAttributes = attributes.getLength();
			for (int i = 0; i < nAttributes; i++)
			{
				if ("targetNamespace".equals(attributes.getQName(i))) //$NON-NLS-1$
				{
					targetNamespace = attributes.getValue(i);
					break;
				}
			}
		}
		
		// collect link info
    
    // TODO This code should be refactored to delegate the responsibility to
    // detect links between files to the search providers/contributors.
    // The current code only handles the XSD and WSDL cases. 
    
		if("import".equals(localName) && namespaceMatches(uri)){ //$NON-NLS-1$
			FileReferenceEntry documentEntry = new FileReferenceEntry();
			documentEntry.setCategory(IXMLSearchConstants.REF);
			documentEntry.setKey("import"); //$NON-NLS-1$
			String namespace = attributes.getValue("namespace"); //$NON-NLS-1$
			String location = attributes.getValue(getLocationAttributeName(uri)); //$NON-NLS-1$
			documentEntry.setPublicIdentifier(namespace);
			documentEntry.setRelativeFilePath(location);            
			document.putEntry(documentEntry);
		}
		if(("redefine".equals(localName)|| "include".equals(localName)) && //$NON-NLS-1$ //$NON-NLS-2$
				namespaceMatches(uri)){
			FileReferenceEntry documentEntry = new FileReferenceEntry();
			documentEntry.setCategory(IXMLSearchConstants.REF);
			documentEntry.setKey("include"); //$NON-NLS-1$
			String location = attributes.getValue(getLocationAttributeName(uri)); //$NON-NLS-1$
			documentEntry.setPublicIdentifier(uri);
			documentEntry.setRelativeFilePath(location);
			document.putEntry(documentEntry);
		}
		
		
        // issue (cs) you may want to try perf measurements to compate reusing the same
        // instance of a SAXSearchElement instead of newing one each time 
		//XMLSearchPattern.SAXSearchElement searchElement = new XMLSearchPattern.SAXSearchElement();
		searchElement.setElementName(localName);
		searchElement.setElementNamespace(uri);
		searchElement.setAttributes(attributes);
		searchElement.setNamespaceMap(namespaceMap);
		searchElement.setTargetNamespace(targetNamespace);
		if (currentPath.size() > 0)
		{
		  String parentName = (String)currentPath.peek();
		  searchElement.setParentName(parentName);
		}			
	

		if(matcher != null){
			if(matcher.matches(pattern, searchElement)){
				hasMatch = true;
				if(pattern instanceof XMLComponentReferencePattern){
					ComponentReferenceEntry documentEntry = new ComponentReferenceEntry();
					documentEntry.setCategory(IXMLSearchConstants.COMPONENT_REF);
					QualifiedName name = new QualifiedName(uri, localName);
					documentEntry.setKey(name.toString());
					documentEntry.setName(name);
					document.putEntry(documentEntry);
				}
				else if(pattern instanceof XMLComponentDeclarationPattern){
					ComponentDeclarationEntry documentEntry = new ComponentDeclarationEntry();
					documentEntry.setCategory(IXMLSearchConstants.COMPONENT_DECL);
                    QualifiedName name = new QualifiedName(targetNamespace, attributes.getValue("name")); //$NON-NLS-1$
					QualifiedName metaName = new QualifiedName(uri, localName);                    
					documentEntry.setKey(name.toString());
                    documentEntry.setName(name);
					documentEntry.setMetaName(metaName);
					document.putEntry(documentEntry);
				}
			}
		}
		currentPath.push(localName); //$NON-NLS-1$		
	}

  private String getLocationAttributeName(String uri)
  {
    if (XMLSCHEMA_NAMESPACE.equals(uri))
    {
      return "schemaLocation";
    }
    
    else if (WSDL_NAMESPACE.equals(uri))
    {
      return "location";
    }
    
    return "";
  }

  private boolean namespaceMatches(String uri)
  {
    return XMLSCHEMA_NAMESPACE.equals(uri) ||
          WSDL_NAMESPACE.equals(uri);
  }
	
	public void endElement(String uri, String localName, String qName)
			throws SAXException
	{
		currentPath.pop();
	}

	/**
	 * Callback for SAX parser
	 * 
	 * @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String,
	 *      java.lang.String)
	 */
	public void startPrefixMapping(String arg0, String arg1)
			throws SAXException
	{
		if (arg0 != null && arg0.length() > 0)
		{
			this.namespaceMap.put(arg0, arg1);
		}
	}

	public String getTargetNamespace() {
		return targetNamespace;
	}

	public void setTargetNamespace(String targetNamespace) {
		this.targetNamespace = targetNamespace;
	}

	public boolean hasMatch() {
		return hasMatch;
	}

	
}

Back to the top