001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.xml.internal.bind.v2.runtime.output;
027:
028: import java.io.IOException;
029:
030: import javax.xml.bind.attachment.AttachmentMarshaller;
031: import javax.xml.stream.XMLStreamException;
032:
033: import com.sun.xml.internal.bind.v2.WellKnownNamespace;
034: import com.sun.xml.internal.bind.v2.runtime.Name;
035: import com.sun.xml.internal.bind.v2.runtime.XMLSerializer;
036: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Base64Data;
037:
038: import org.xml.sax.SAXException;
039:
040: /**
041: * {@link XmlOutput} decorator that supports MTOM.
042: *
043: * @author Kohsuke Kawaguchi
044: */
045: public final class MTOMXmlOutput extends XmlOutputAbstractImpl {
046:
047: private final XmlOutput next;
048:
049: /**
050: * Remembers the last namespace URI and local name so that we can pass them to
051: * {@link AttachmentMarshaller}.
052: */
053: private String nsUri, localName;
054:
055: public MTOMXmlOutput(XmlOutput next) {
056: this .next = next;
057: }
058:
059: public void startDocument(XMLSerializer serializer,
060: boolean fragment, int[] nsUriIndex2prefixIndex,
061: NamespaceContextImpl nsContext) throws IOException,
062: SAXException, XMLStreamException {
063: super .startDocument(serializer, fragment,
064: nsUriIndex2prefixIndex, nsContext);
065: next.startDocument(serializer, fragment,
066: nsUriIndex2prefixIndex, nsContext);
067: }
068:
069: public void endDocument(boolean fragment) throws IOException,
070: SAXException, XMLStreamException {
071: next.endDocument(fragment);
072: super .endDocument(fragment);
073: }
074:
075: public void beginStartTag(Name name) throws IOException,
076: XMLStreamException {
077: next.beginStartTag(name);
078: this .nsUri = name.nsUri;
079: this .localName = name.localName;
080: }
081:
082: public void beginStartTag(int prefix, String localName)
083: throws IOException, XMLStreamException {
084: next.beginStartTag(prefix, localName);
085: this .nsUri = nsContext.getNamespaceURI(prefix);
086: this .localName = localName;
087: }
088:
089: public void attribute(Name name, String value) throws IOException,
090: XMLStreamException {
091: next.attribute(name, value);
092: }
093:
094: public void attribute(int prefix, String localName, String value)
095: throws IOException, XMLStreamException {
096: next.attribute(prefix, localName, value);
097: }
098:
099: public void endStartTag() throws IOException, SAXException {
100: next.endStartTag();
101: }
102:
103: public void endTag(Name name) throws IOException, SAXException,
104: XMLStreamException {
105: next.endTag(name);
106: }
107:
108: public void endTag(int prefix, String localName)
109: throws IOException, SAXException, XMLStreamException {
110: next.endTag(prefix, localName);
111: }
112:
113: public void text(String value, boolean needsSeparatingWhitespace)
114: throws IOException, SAXException, XMLStreamException {
115: next.text(value, needsSeparatingWhitespace);
116: }
117:
118: public void text(Pcdata value, boolean needsSeparatingWhitespace)
119: throws IOException, SAXException, XMLStreamException {
120: if (value instanceof Base64Data
121: && !serializer.getInlineBinaryFlag()) {
122: Base64Data b64d = (Base64Data) value;
123: String cid;
124: if (b64d.hasData())
125: cid = serializer.attachmentMarshaller
126: .addMtomAttachment(b64d.get(), 0, b64d
127: .getDataLen(), b64d.getMimeType(),
128: nsUri, localName);
129: else
130: cid = serializer.attachmentMarshaller
131: .addMtomAttachment(b64d.getDataHandler(),
132: nsUri, localName);
133:
134: if (cid != null) {
135: nsContext.getCurrent().push();
136: int prefix = nsContext.declareNsUri(
137: WellKnownNamespace.XOP, "xop", false);
138: beginStartTag(prefix, "Include");
139: attribute(-1, "href", cid);
140: endStartTag();
141: endTag(prefix, "Include");
142: nsContext.getCurrent().pop();
143: return;
144: }
145: }
146: next.text(value, needsSeparatingWhitespace);
147: }
148:
149: public void flush() throws IOException, XMLStreamException {
150: next.flush();
151: }
152: }
|