001: /*
002: * This file is part of the WfMOpen project.
003: * Copyright (C) 2001-2006 Danet GmbH (www.danet.de), BU BTS.
004: * All rights reserved.
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * $Id: SOAPBuilder.java,v 1.2 2007/01/30 11:56:13 drmlipp Exp $
021: *
022: * $Log: SOAPBuilder.java,v $
023: * Revision 1.2 2007/01/30 11:56:13 drmlipp
024: * Merged Wf-XML branch.
025: *
026: * Revision 1.1.2.1 2006/12/20 14:37:58 schnelle
027: * Implemented Factory GetDefinition.
028: *
029: */
030: package de.danet.an.util.soap;
031:
032: import java.util.ArrayList;
033: import java.util.List;
034:
035: import javax.xml.soap.Name;
036: import javax.xml.soap.SOAPBody;
037: import javax.xml.soap.SOAPElement;
038: import javax.xml.soap.SOAPEnvelope;
039: import javax.xml.soap.SOAPException;
040: import javax.xml.soap.SOAPHeader;
041:
042: import org.xml.sax.Attributes;
043: import org.xml.sax.SAXException;
044:
045: import de.danet.an.util.sax.HandlerStack;
046: import de.danet.an.util.sax.StackedHandler;
047:
048: /**
049: * Helper for building SOAP contents from SAX events.
050: *
051: * @author <a href="mailto:lipp@danet.de">Michael Lipp</a>
052: * @version $Revision: 1.2 $
053: */
054: public class SOAPBuilder extends StackedHandler {
055:
056: private List elemStack = new ArrayList();
057:
058: /**
059: * Create a new builder with all attributes initialized to the given
060: * values.
061: */
062: public SOAPBuilder(SOAPElement parent) {
063: elemStack.add(0, parent);
064: }
065:
066: /**
067: * Receive notification of the beginning of an element.
068: *
069: * @param namespaceURI The Namespace URI, or the empty string if the
070: * element has no Namespace URI or if Namespace
071: * processing is not being performed.
072: * @param localName The local name (without prefix), or the
073: * empty string if Namespace processing is not being
074: * performed.
075: * @param qName The qualified name (with prefix), or the
076: * empty string if qualified names are not available.
077: * @param atts The attributes attached to the element. If
078: * there are no attributes, it shall be an empty
079: * Attributes object.
080: * @throws SAXException Any SAX exception, possibly
081: * wrapping another exception.
082: * @see #endElement
083: * @see org.xml.sax.Attributes
084: */
085: public void startElement(String namespaceURI, String localName,
086: String qName, Attributes atts) throws SAXException {
087: try {
088: HandlerStack stack = getStack();
089: SOAPEnvelope env = (SOAPEnvelope) stack
090: .getContextData("envelope");
091: String prefix = stack.getPrefixForURI(namespaceURI);
092: Name name = env.createName(localName, prefix, namespaceURI);
093: SOAPElement parent = (SOAPElement) elemStack.get(0);
094: SOAPElement current = null;
095: if (parent instanceof SOAPBody) {
096: current = ((SOAPBody) parent).addBodyElement(name);
097: } else if (parent instanceof SOAPHeader) {
098: current = ((SOAPHeader) parent).addHeaderElement(name);
099: } else {
100: current = parent.addChildElement(name);
101: }
102: for (int i = 0; i < atts.getLength(); i++) {
103: String uri = atts.getURI(i);
104: if (uri != null && uri.length() > 0) {
105: current.setAttributeNS(uri, atts.getQName(i), atts
106: .getValue(i));
107: } else {
108: current.setAttribute(atts.getLocalName(i), atts
109: .getValue(i));
110: }
111: }
112: elemStack.add(0, current);
113: } catch (SOAPException e) {
114: throw new SAXException(e);
115: }
116: }
117:
118: /**
119: * Receive notification of the end of an element.
120: *
121: * @param uri the Namespace URI, or the empty string if the
122: * element has no Namespace URI or if Namespace processing is not
123: * being performed.
124: * @param loc the local name (without prefix), or the empty string
125: * if Namespace processing is not being performed.
126: * @param raw the raw XML 1.0 name (with prefix), or the empty
127: * string if raw names are not available.
128: * @throws SAXException not thrown.
129: */
130: public void endElement(String uri, String loc, String raw)
131: throws SAXException {
132: try {
133: SOAPElement current = (SOAPElement) elemStack.remove(0);
134: current.addTextNode(text());
135: } catch (SOAPException e) {
136: throw new SAXException(e);
137: }
138: }
139: }
|