Skip to main content
summaryrefslogtreecommitdiffstats
blob: 26bbf6e3096bbfe61e1a5b19b156aa3171e66e7c (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
/*******************************************************************************
 * Copyright (c) 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
 * yyyymmdd bug      Email and other contact information
 * -------- -------- -----------------------------------------------------------
 * 20060131   123963 andyzhai@ca.ibm.com - Andy Zhai
 *******************************************************************************/

package org.eclipse.jst.ws.util;

import java.util.ArrayList;
import java.util.HashMap;

import javax.xml.soap.Name;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * @author xinzhai
 */
public class SoapElementSaxHandler extends DefaultHandler
{
	//Stores all values of pairs of prefix and URI in the document
	private HashMap prefixURIMapping = new HashMap();
	//Stores a list of URIs for a particular SOAP element
	private ArrayList uris = new ArrayList();
	//Represents the whole(root) SOAP document
	private SOAPElement rootElement = null;
	//Represents the current SOAP element where SAX parser is reading
	private SOAPElement currentElement = null;
	private SOAPFactory soapFactory;
	
	public SOAPElement getSOAPElement()
	{
		return rootElement;
	}
	
	public void startDocument() throws SAXException
	{
		try 
		{
			soapFactory = SOAPFactory.newInstance();
		}
		catch (SOAPException e) 
		{
			throw new SAXException("Can't create a SOAPFactory instance", e);
		}	
	}

	public void startPrefixMapping(String prefix, String uri)
	{
		prefixURIMapping.put(uri,prefix);
		uris.add(uri);
	}
	
	public void characters(char[] ch, int start, int length) throws SAXException
	{
		String str = String.valueOf(ch);
		//Add non-trivial text as a text node
		if (length > 1)
		{
			try 
			{
				currentElement.addTextNode(str.substring(start,start+length));
			} 
			catch (SOAPException e) 
			{
				throw new SAXException("Can't add a text node into SOAPElement from text", e);
			}
		}
	}
	
	public void endElement (String uri, String localName, String qName)
	{
		if (currentElement != rootElement )
		{
			currentElement = currentElement.getParentElement();
		}
	}
	public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException
	{
		String prefix = (String)prefixURIMapping.get(namespaceURI);
		try
		{
			// Create and/or add child element
			if (rootElement == null && currentElement == null)
			{
				rootElement = soapFactory.createElement(localName,prefix,namespaceURI);
				currentElement = rootElement;
			} 
			else
			{
				currentElement = currentElement.addChildElement(localName,prefix,namespaceURI);
			}
			
			// Add namespace declaration
			if (uris.size() > 0)
			{
				for (int i = 0; i < uris.size(); i++)
				{
					String uri = (String)uris.get(i);
					String pre = (String)prefixURIMapping.get(uri);
					currentElement.addNamespaceDeclaration(pre,uri);
				}
				// Need to reset uris as we will use it for next element.
				uris.clear(); 
			}
			
			// Add attibutes
			for (int i =0; i<atts.getLength(); i++)
			{
				Name attriName;
				if (atts.getURI(i) != null)
				{
					String attriPre = (String)prefixURIMapping.get(atts.getURI(i));
					attriName = soapFactory.createName(atts.getLocalName(i),attriPre,atts.getURI(i));
				}
				else
				{
					attriName = soapFactory.createName(atts.getLocalName(i));
				}
				currentElement.addAttribute(attriName, atts.getValue(i));
			}
		}
		catch (SOAPException e) 
		{
			throw new SAXException(e);
		}
	}
}

Back to the top