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.unmarshaller;
038:
039: import javax.activation.DataHandler;
040: import javax.xml.bind.attachment.AttachmentUnmarshaller;
041: import javax.xml.namespace.NamespaceContext;
042:
043: import com.sun.xml.bind.v2.WellKnownNamespace;
044:
045: import org.xml.sax.SAXException;
046:
047: /**
048: * Decorator of {@link XmlVisitor} that performs XOP processing.
049: * Used to support MTOM.
050: *
051: * @author Kohsuke Kawaguchi
052: */
053: final class MTOMDecorator implements XmlVisitor {
054:
055: private final XmlVisitor next;
056:
057: private final AttachmentUnmarshaller au;
058:
059: private UnmarshallerImpl parent;
060:
061: private final Base64Data base64data = new Base64Data();
062:
063: /**
064: * True if we are between the start and the end of xop:Include
065: */
066: private boolean inXopInclude;
067:
068: /**
069: * UGLY HACK: we need to ignore the whitespace that follows
070: * the attached base64 image.
071: *
072: * This happens twice; once before </xop:Include>, another
073: * after </xop:Include>. The spec guarantees that
074: * no valid pcdata can follow </xop:Include>.
075: */
076: private boolean followXop;
077:
078: public MTOMDecorator(UnmarshallerImpl parent, XmlVisitor next,
079: AttachmentUnmarshaller au) {
080: this .parent = parent;
081: this .next = next;
082: this .au = au;
083: }
084:
085: public void startDocument(LocatorEx loc, NamespaceContext nsContext)
086: throws SAXException {
087: next.startDocument(loc, nsContext);
088: }
089:
090: public void endDocument() throws SAXException {
091: next.endDocument();
092: }
093:
094: public void startElement(TagName tagName) throws SAXException {
095: if (tagName.local == "Include"
096: && tagName.uri == WellKnownNamespace.XOP) {
097: // found xop:Include
098: String href = tagName.atts.getValue("href");
099: DataHandler attachment = au
100: .getAttachmentAsDataHandler(href);
101: if (attachment == null) {
102: // report an error and ignore
103: parent.getEventHandler().handleEvent(null);
104: // TODO
105: }
106: base64data.set(attachment);
107: next.text(base64data);
108: inXopInclude = true;
109: followXop = true;
110: } else
111: next.startElement(tagName);
112: }
113:
114: public void endElement(TagName tagName) throws SAXException {
115: if (inXopInclude) {
116: // consume </xop:Include> by ourselves.
117: inXopInclude = false;
118: followXop = true;
119: return;
120: }
121: next.endElement(tagName);
122: }
123:
124: public void startPrefixMapping(String prefix, String nsUri)
125: throws SAXException {
126: next.startPrefixMapping(prefix, nsUri);
127: }
128:
129: public void endPrefixMapping(String prefix) throws SAXException {
130: next.endPrefixMapping(prefix);
131: }
132:
133: public void text(CharSequence pcdata) throws SAXException {
134: if (!followXop)
135: next.text(pcdata);
136: else
137: followXop = false;
138: }
139:
140: public UnmarshallingContext getContext() {
141: return next.getContext();
142: }
143:
144: public TextPredictor getPredictor() {
145: return next.getPredictor();
146: }
147: }
|