001: /*
002: * Copyright 2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.ws.pox.dom;
018:
019: import java.io.IOException;
020: import java.io.InputStream;
021: import javax.xml.parsers.DocumentBuilder;
022: import javax.xml.parsers.DocumentBuilderFactory;
023: import javax.xml.parsers.ParserConfigurationException;
024: import javax.xml.transform.TransformerConfigurationException;
025:
026: import org.springframework.util.Assert;
027: import org.springframework.ws.WebServiceMessage;
028: import org.springframework.ws.WebServiceMessageFactory;
029: import org.springframework.xml.transform.TransformerObjectSupport;
030: import org.w3c.dom.Document;
031: import org.xml.sax.SAXException;
032:
033: /**
034: * Implementation of the {@link WebServiceMessageFactory} interinterface that creates a {@link DomPoxMessage}.
035: *
036: * @author Arjen Poutsma
037: * @see org.springframework.ws.pox.dom.DomPoxMessage
038: * @since 1.0.0
039: */
040: public class DomPoxMessageFactory extends TransformerObjectSupport
041: implements WebServiceMessageFactory {
042:
043: /** The default content type for the POX messages. */
044: public static final String DEFAULT_CONTENT_TYPE = "application/xml";
045:
046: private DocumentBuilderFactory documentBuilderFactory = documentBuilderFactory = DocumentBuilderFactory
047: .newInstance();
048:
049: private String contentType = DEFAULT_CONTENT_TYPE;
050:
051: public DomPoxMessageFactory() {
052: documentBuilderFactory.setNamespaceAware(true);
053: documentBuilderFactory.setValidating(false);
054: }
055:
056: /** Sets the content-type for the {@link DomPoxMessage}. */
057: public void setContentType(String contentType) {
058: Assert
059: .hasLength(contentType,
060: "'contentType' must not be empty");
061: this .contentType = contentType;
062: }
063:
064: /** Set whether or not the XML parser should be XML namespace aware. Default is <code>true</code>. */
065: public void setNamespaceAware(boolean namespaceAware) {
066: documentBuilderFactory.setNamespaceAware(namespaceAware);
067: }
068:
069: /** Set if the XML parser should validate the document. Default is <code>false</code>. */
070: public void setValidating(boolean validating) {
071: documentBuilderFactory.setValidating(validating);
072: }
073:
074: public WebServiceMessage createWebServiceMessage() {
075: try {
076: DocumentBuilder documentBuilder = documentBuilderFactory
077: .newDocumentBuilder();
078: Document request = documentBuilder.newDocument();
079: return new DomPoxMessage(request, createTransformer(),
080: contentType);
081: } catch (ParserConfigurationException ex) {
082: throw new DomPoxMessageException(
083: "Could not create message context", ex);
084: } catch (TransformerConfigurationException ex) {
085: throw new DomPoxMessageException(
086: "Could not create transormer", ex);
087: }
088: }
089:
090: public WebServiceMessage createWebServiceMessage(
091: InputStream inputStream) throws IOException {
092: try {
093: DocumentBuilder documentBuilder = documentBuilderFactory
094: .newDocumentBuilder();
095: Document request = documentBuilder.parse(inputStream);
096: return new DomPoxMessage(request, createTransformer(),
097: contentType);
098: } catch (ParserConfigurationException ex) {
099: throw new DomPoxMessageException(
100: "Could not create message context", ex);
101: } catch (SAXException ex) {
102: throw new DomPoxMessageException(
103: "Could not parse request message", ex);
104: } catch (TransformerConfigurationException ex) {
105: throw new DomPoxMessageException(
106: "Could not create transormer", ex);
107: }
108: }
109: }
|