001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.bind.v2.runtime.output;
038:
039: import java.io.IOException;
040:
041: import javax.xml.bind.attachment.AttachmentMarshaller;
042: import javax.xml.stream.XMLStreamException;
043:
044: import com.sun.xml.bind.v2.WellKnownNamespace;
045: import com.sun.xml.bind.v2.runtime.Name;
046: import com.sun.xml.bind.v2.runtime.XMLSerializer;
047: import com.sun.xml.bind.v2.runtime.unmarshaller.Base64Data;
048:
049: import org.xml.sax.SAXException;
050:
051: /**
052: * {@link XmlOutput} decorator that supports MTOM.
053: *
054: * @author Kohsuke Kawaguchi
055: */
056: public final class MTOMXmlOutput extends XmlOutputAbstractImpl {
057:
058: private final XmlOutput next;
059:
060: /**
061: * Remembers the last namespace URI and local name so that we can pass them to
062: * {@link AttachmentMarshaller}.
063: */
064: private String nsUri, localName;
065:
066: public MTOMXmlOutput(XmlOutput next) {
067: this .next = next;
068: }
069:
070: public void startDocument(XMLSerializer serializer,
071: boolean fragment, int[] nsUriIndex2prefixIndex,
072: NamespaceContextImpl nsContext) throws IOException,
073: SAXException, XMLStreamException {
074: super .startDocument(serializer, fragment,
075: nsUriIndex2prefixIndex, nsContext);
076: next.startDocument(serializer, fragment,
077: nsUriIndex2prefixIndex, nsContext);
078: }
079:
080: public void endDocument(boolean fragment) throws IOException,
081: SAXException, XMLStreamException {
082: next.endDocument(fragment);
083: super .endDocument(fragment);
084: }
085:
086: public void beginStartTag(Name name) throws IOException,
087: XMLStreamException {
088: next.beginStartTag(name);
089: this .nsUri = name.nsUri;
090: this .localName = name.localName;
091: }
092:
093: public void beginStartTag(int prefix, String localName)
094: throws IOException, XMLStreamException {
095: next.beginStartTag(prefix, localName);
096: this .nsUri = nsContext.getNamespaceURI(prefix);
097: this .localName = localName;
098: }
099:
100: public void attribute(Name name, String value) throws IOException,
101: XMLStreamException {
102: next.attribute(name, value);
103: }
104:
105: public void attribute(int prefix, String localName, String value)
106: throws IOException, XMLStreamException {
107: next.attribute(prefix, localName, value);
108: }
109:
110: public void endStartTag() throws IOException, SAXException {
111: next.endStartTag();
112: }
113:
114: public void endTag(Name name) throws IOException, SAXException,
115: XMLStreamException {
116: next.endTag(name);
117: }
118:
119: public void endTag(int prefix, String localName)
120: throws IOException, SAXException, XMLStreamException {
121: next.endTag(prefix, localName);
122: }
123:
124: public void text(String value, boolean needsSeparatingWhitespace)
125: throws IOException, SAXException, XMLStreamException {
126: next.text(value, needsSeparatingWhitespace);
127: }
128:
129: public void text(Pcdata value, boolean needsSeparatingWhitespace)
130: throws IOException, SAXException, XMLStreamException {
131: if (value instanceof Base64Data
132: && !serializer.getInlineBinaryFlag()) {
133: Base64Data b64d = (Base64Data) value;
134: String cid;
135: if (b64d.hasData())
136: cid = serializer.attachmentMarshaller
137: .addMtomAttachment(b64d.get(), 0, b64d
138: .getDataLen(), b64d.getMimeType(),
139: nsUri, localName);
140: else
141: cid = serializer.attachmentMarshaller
142: .addMtomAttachment(b64d.getDataHandler(),
143: nsUri, localName);
144:
145: if (cid != null) {
146: nsContext.getCurrent().push();
147: int prefix = nsContext.declareNsUri(
148: WellKnownNamespace.XOP, "xop", false);
149: beginStartTag(prefix, "Include");
150: attribute(-1, "href", cid);
151: endStartTag();
152: endTag(prefix, "Include");
153: nsContext.getCurrent().pop();
154: return;
155: }
156: }
157: next.text(value, needsSeparatingWhitespace);
158: }
159: }
|