001: /*
002: * Copyright 2007 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.oxm.support;
018:
019: import java.io.IOException;
020: import javax.xml.transform.Source;
021: import javax.xml.transform.sax.SAXResult;
022: import javax.xml.transform.sax.SAXSource;
023:
024: import org.springframework.oxm.Marshaller;
025: import org.springframework.util.Assert;
026: import org.springframework.xml.sax.AbstractXmlReader;
027: import org.xml.sax.InputSource;
028: import org.xml.sax.SAXException;
029: import org.xml.sax.SAXParseException;
030:
031: /**
032: * {@link Source} implementation that uses a {@link Marshaller}.Can be constructed with a <code>Marshaller</code> and an
033: * object to be marshalled.
034: * <p/>
035: * Even though <code>StaxSource</code> extends from <code>SAXSource</code>, calling the methods of
036: * <code>SAXSource</code> is <strong>not supported</strong>. In general, the only supported operation on this class is
037: * to use the <code>XMLReader</code> obtained via {@link #getXMLReader()} to parse the input source obtained via {@link
038: * #getInputSource()}. Calling {@link #setXMLReader(org.xml.sax.XMLReader)} or {@link
039: * #setInputSource(org.xml.sax.InputSource)} will result in <code>UnsupportedOperationException</code>s.
040: *
041: * @author Arjen Poutsma
042: * @see javax.xml.transform.Transformer
043: * @since 1.0.0
044: */
045: public class MarshallingSource extends SAXSource {
046:
047: private final Marshaller marshaller;
048:
049: private final Object content;
050:
051: /**
052: * Creates a new <code>MarshallingSource</code> with the given marshaller and content.
053: *
054: * @param marshaller the marshaller to use
055: * @param content the object to be marshalled
056: */
057: public MarshallingSource(Marshaller marshaller, Object content) {
058: Assert.notNull(marshaller, "'marshaller' must not be null");
059: Assert.notNull(content, "'content' must not be null");
060: this .marshaller = marshaller;
061: this .content = content;
062: setXMLReader(new MarshallingXmlReader());
063: setInputSource(new InputSource());
064: }
065:
066: /** Returns the <code>Marshaller</code> used by this <code>MarshallingSource</code>. */
067: public Marshaller getMarshaller() {
068: return marshaller;
069: }
070:
071: /** Returns the object to be marshalled. */
072: public Object getContent() {
073: return content;
074: }
075:
076: private class MarshallingXmlReader extends AbstractXmlReader {
077:
078: public void parse(InputSource input) throws IOException,
079: SAXException {
080: parse();
081: }
082:
083: public void parse(String systemId) throws IOException,
084: SAXException {
085: parse();
086: }
087:
088: private void parse() throws SAXException {
089: SAXResult result = new SAXResult(getContentHandler());
090: result.setLexicalHandler(getLexicalHandler());
091: try {
092: marshaller.marshal(content, result);
093: } catch (IOException ex) {
094: SAXParseException saxException = new SAXParseException(
095: ex.getMessage(), null, null, -1, -1, ex);
096: if (getErrorHandler() != null) {
097: getErrorHandler().fatalError(saxException);
098: } else {
099: throw saxException;
100: }
101: }
102: }
103:
104: }
105: }
|