Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2005-2012 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.springframework.ws.soap.axiom.support;
 
 
 
Collection of generic utility methods to work with Axiom. Includes conversion from OMNamespaces to QNames.

Author(s):
Arjen Poutsma
Tareq Abed Rabbo
Since:
1.0.0
See also:
org.apache.axiom.om.OMNamespace
javax.xml.namespace.QName
 
 @SuppressWarnings("Since15")
 public abstract class AxiomUtils {

    
Converts a javax.xml.namespace.QName to a org.apache.axiom.om.OMNamespace. A OMElement is used to resolve the namespace, or to declare a new one.

Parameters:
qName the QName to convert
resolveElement the element used to resolve the Q
Returns:
the converted SAAJ Name
Throws:
org.apache.axiom.om.OMException if conversion is unsuccessful
java.lang.IllegalArgumentException if qName is not fully qualified
 
     public static OMNamespace toNamespace(QName qNameOMElement resolveElementthrows OMException {
         String prefix = QNameUtils.getPrefix(qName);
         if (StringUtils.hasLength(qName.getNamespaceURI()) && StringUtils.hasLength(prefix)) {
             return resolveElement.declareNamespace(qName.getNamespaceURI(), prefix);
         }
         else if (StringUtils.hasLength(qName.getNamespaceURI())) {
             // check for existing namespace, and declare if necessary
             return resolveElement.declareNamespace(qName.getNamespaceURI(), "");
         }
         else {
             throw new IllegalArgumentException("qName [" + qName + "] does not contain a namespace");
         }
     }

    
Converts the given locale to a xml:lang string, as used in Axiom Faults.

Parameters:
locale the locale
Returns:
the language string
 
     public static String toLanguage(Locale locale) {
         return locale.toString().replace('_''-');
     }

    
Converts the given locale to a xml:lang string, as used in Axiom Faults.

Parameters:
language the language string
Returns:
the locale
 
     public static Locale toLocale(String language) {
        language = language.replace('-''_');
        return StringUtils.parseLocaleString(language);
    }

    
Removes the contents (i.e. children) of the container.
    public static void removeContents(OMContainer container) {
        for (Iterator<?> iterator = container.getChildren(); iterator.hasNext();) {
            iterator.next();
            iterator.remove();
        }
    }

    
Converts a given AXIOM org.apache.axiom.soap.SOAPEnvelope to a org.w3c.dom.Document.

Parameters:
envelope the SOAP envelope to be converted
Returns:
the converted document
Throws:
java.lang.IllegalArgumentException in case of errors
See also:
org.apache.rampart.util.Axis2Util.getDocumentFromSOAPEnvelope(SOAPEnvelope, boolean)
    public static Document toDocument(SOAPEnvelope envelope) {
        try {
            if (envelope instanceof Element) {
                return ((Elementenvelope).getOwnerDocument();
            }
            else {
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                envelope.build();
                envelope.serialize(bos);
                ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
                DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
                documentBuilderFactory.setNamespaceAware(true);
                return documentBuilderFactory.newDocumentBuilder().parse(bis);
            }
        }
        catch (Exception ex) {
            throw new IllegalArgumentException("Error in converting SOAP Envelope to Document"ex);
        }
    }

    
Converts a given org.w3c.dom.Document to an AXIOM org.apache.axiom.soap.SOAPEnvelope.

Parameters:
document the document to be converted
Returns:
the converted envelope
Throws:
java.lang.IllegalArgumentException in case of errors
See also:
org.apache.rampart.util.Axis2Util.getSOAPEnvelopeFromDOMDocument(Document, boolean)
    public static SOAPEnvelope toEnvelope(Document document) {
        try {
            DOMImplementation implementation = document.getImplementation();
            Assert.isInstanceOf(DOMImplementationLS.classimplementation);
            DOMImplementationLS loadSaveImplementation = (DOMImplementationLSimplementation;
            LSOutput output = loadSaveImplementation.createLSOutput();
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            output.setByteStream(bos);
            LSSerializer serializer = loadSaveImplementation.createLSSerializer();
            serializer.write(documentoutput);
            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
            StAXSOAPModelBuilder stAXSOAPModelBuilder =
                    new StAXSOAPModelBuilder(XMLInputFactory.newInstance().createXMLStreamReader(bis), null);
            SOAPEnvelope envelope = stAXSOAPModelBuilder.getSOAPEnvelope();
            // Necessary to build a correct Axiom tree, see SWS-483
            envelope.serialize(new NullOutputStream());
            return envelope;
        }
        catch (Exception ex) {
            IllegalArgumentException iaex =
                    new IllegalArgumentException("Error in converting Document to SOAP Envelope");
            iaex.initCause(ex);
            throw iaex;
        }
    }

    
OutputStream that does nothing.
    private static class NullOutputStream extends OutputStream {
        @Override
        public void write(int bthrows IOException {
        }
        @Override
        public void write(byte[] bthrows IOException {
        }
        @Override
        public void write(byte[] bint offint lenthrows IOException {
        }
    }
New to GrepCode? Check out our FAQ X