/*
* This file is part of the WfMOpen project.
* Copyright (C) 2001-2006 Danet GmbH (www.danet.de), BU BTS.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: SOAPBuilder.java 2185 2007-01-30 11:56:16Z drmlipp $
*
* $Log$
* Revision 1.1.2.1 2006/12/20 14:37:58 schnelle
* Implemented Factory GetDefinition.
*
*/
package de.danet.an.util.soap;
import java.util.ArrayList;
import java.util.List;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import de.danet.an.util.sax.HandlerStack;
import de.danet.an.util.sax.StackedHandler;
/**
* Helper for building SOAP contents from SAX events.
*
* @author <a href="mailto:lipp@danet.de">Michael Lipp</a>
* @version $Revision: 2185 $
*/
public class SOAPBuilder extends StackedHandler {
private List elemStack = new ArrayList();
/**
* Create a new builder with all attributes initialized to the given
* values.
*/
public SOAPBuilder(SOAPElement parent) {
elemStack.add(0, parent);
}
/**
* Receive notification of the beginning of an element.
*
* @param namespaceURI The Namespace URI, or the empty string if the
* element has no Namespace URI or if Namespace
* processing is not being performed.
* @param localName The local name (without prefix), or the
* empty string if Namespace processing is not being
* performed.
* @param qName The qualified name (with prefix), or the
* empty string if qualified names are not available.
* @param atts The attributes attached to the element. If
* there are no attributes, it shall be an empty
* Attributes object.
* @throws SAXException Any SAX exception, possibly
* wrapping another exception.
* @see #endElement
* @see org.xml.sax.Attributes
*/
public void startElement (String namespaceURI, String localName,
String qName, Attributes atts)
throws SAXException {
try {
HandlerStack stack = getStack();
SOAPEnvelope env = (SOAPEnvelope) stack.getContextData ("envelope");
String prefix = stack.getPrefixForURI(namespaceURI);
Name name = env.createName (localName, prefix, namespaceURI);
SOAPElement parent = (SOAPElement)elemStack.get(0);
SOAPElement current = null;
if (parent instanceof SOAPBody) {
current = ((SOAPBody)parent).addBodyElement (name);
} else if (parent instanceof SOAPHeader) {
current = ((SOAPHeader)parent).addHeaderElement (name);
} else {
current = parent.addChildElement (name);
}
for (int i = 0; i < atts.getLength(); i++) {
String uri = atts.getURI(i);
if (uri != null && uri.length() > 0) {
current.setAttributeNS
(uri, atts.getQName(i), atts.getValue(i));
} else {
current.setAttribute
(atts.getLocalName(i), atts.getValue(i));
}
}
elemStack.add (0, current);
} catch (SOAPException e) {
throw new SAXException (e);
}
}
/**
* Receive notification of the end of an element.
*
* @param uri the Namespace URI, or the empty string if the
* element has no Namespace URI or if Namespace processing is not
* being performed.
* @param loc the local name (without prefix), or the empty string
* if Namespace processing is not being performed.
* @param raw the raw XML 1.0 name (with prefix), or the empty
* string if raw names are not available.
* @throws SAXException not thrown.
*/
public void endElement(String uri, String loc, String raw)
throws SAXException {
try {
SOAPElement current = (SOAPElement)elemStack.remove(0);
current.addTextNode (text());
} catch (SOAPException e) {
throw new SAXException (e);
}
}
}