001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.impl.wsdl.submit.filters;
014:
015: import org.apache.log4j.Logger;
016: import org.apache.xmlbeans.XmlCursor;
017: import org.apache.xmlbeans.XmlObject;
018: import org.w3c.dom.Element;
019: import org.w3c.dom.NamedNodeMap;
020: import org.w3c.dom.Node;
021:
022: import com.eviware.soapui.SoapUI;
023: import com.eviware.soapui.impl.wsdl.WsdlRequest;
024: import com.eviware.soapui.impl.wsdl.submit.RequestFilter;
025: import com.eviware.soapui.impl.wsdl.submit.transports.http.BaseHttpRequestTransport;
026: import com.eviware.soapui.model.iface.SubmitContext;
027: import com.eviware.soapui.support.xml.XmlUtils;
028:
029: /**
030: * RequestFilter for removing empty elements/attributes
031: *
032: * @author Ole.Matzura
033: */
034:
035: public class RemoveEmptyContentRequestFilter implements RequestFilter {
036: @SuppressWarnings("unused")
037: private final static Logger log = Logger
038: .getLogger(PropertyExpansionRequestFilter.class);
039:
040: public void filterRequest(SubmitContext context,
041: WsdlRequest wsdlRequest) {
042: if (wsdlRequest != null && !wsdlRequest.isRemoveEmptyContent())
043: return;
044:
045: String content = (String) context
046: .getProperty(BaseHttpRequestTransport.REQUEST_CONTENT);
047: content = removeEmptyContent(content);
048: if (content != null)
049: context.setProperty(
050: BaseHttpRequestTransport.REQUEST_CONTENT, content);
051: }
052:
053: public static String removeEmptyContent(String content) {
054: XmlCursor cursor = null;
055:
056: try {
057: XmlObject xmlObject = XmlObject.Factory.parse(content);
058: cursor = xmlObject.newCursor();
059:
060: cursor.toNextToken();
061:
062: // skip root element
063: cursor.toNextToken();
064: boolean removed = false;
065:
066: while (!cursor.isEnddoc()) {
067: if (cursor.isContainer()) {
068: Element elm = (Element) cursor.getDomNode();
069: NamedNodeMap attributes = elm.getAttributes();
070: if (attributes != null
071: && attributes.getLength() > 0) {
072: for (int c = 0; c < attributes.getLength(); c++) {
073: Node node = attributes.item(c);
074: if (node.getNodeValue() == null
075: || node.getNodeValue().trim()
076: .length() == 0) {
077: cursor.removeAttribute(XmlUtils
078: .getQName(node));
079: removed = true;
080: }
081: }
082: }
083:
084: if (cursor.getTextValue() == null
085: || cursor.getTextValue().trim().length() == 0
086: && XmlUtils.getFirstChildElement(elm) == null) {
087: if (cursor.removeXml()) {
088: removed = true;
089: }
090: }
091: }
092:
093: cursor.toNextToken();
094: }
095:
096: if (removed) {
097: return xmlObject.xmlText();
098: }
099: } catch (Exception e) {
100: SoapUI.logError(e);
101: } finally {
102: if (cursor != null)
103: cursor.dispose();
104: }
105:
106: return null;
107: }
108: }
|