MTOMDecorator.java revision 524:dcaa586ab756
1/*
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.xml.internal.bind.v2.runtime.unmarshaller;
27
28import javax.activation.DataHandler;
29import javax.xml.bind.attachment.AttachmentUnmarshaller;
30import javax.xml.namespace.NamespaceContext;
31
32import com.sun.xml.internal.bind.v2.WellKnownNamespace;
33
34import org.xml.sax.SAXException;
35
36/**
37 * Decorator of {@link XmlVisitor} that performs XOP processing.
38 * Used to support MTOM.
39 *
40 * @author Kohsuke Kawaguchi
41 */
42final class MTOMDecorator implements XmlVisitor {
43
44    private final XmlVisitor next;
45
46    private final AttachmentUnmarshaller au;
47
48    private UnmarshallerImpl parent;
49
50    private final Base64Data base64data = new Base64Data();
51
52    /**
53     * True if we are between the start and the end of xop:Include
54     */
55    private boolean inXopInclude;
56
57    /**
58     * UGLY HACK: we need to ignore the whitespace that follows
59     * the attached base64 image.
60     *
61     * This happens twice; once before </xop:Include>, another
62     * after </xop:Include>. The spec guarantees that
63     * no valid pcdata can follow </xop:Include>.
64     */
65    private boolean followXop;
66
67    public MTOMDecorator(UnmarshallerImpl parent,XmlVisitor next, AttachmentUnmarshaller au) {
68        this.parent = parent;
69        this.next = next;
70        this.au = au;
71    }
72
73    public void startDocument(LocatorEx loc, NamespaceContext nsContext) throws SAXException {
74        next.startDocument(loc,nsContext);
75    }
76
77    public void endDocument() throws SAXException {
78        next.endDocument();
79    }
80
81    public void startElement(TagName tagName) throws SAXException {
82        if(tagName.local.equals("Include") && tagName.uri.equals(WellKnownNamespace.XOP)) {
83            // found xop:Include
84            String href = tagName.atts.getValue("href");
85            DataHandler attachment = au.getAttachmentAsDataHandler(href);
86            if(attachment==null) {
87                // report an error and ignore
88                parent.getEventHandler().handleEvent(null);
89                // TODO
90            }
91            base64data.set(attachment);
92            next.text(base64data);
93            inXopInclude = true;
94            followXop = true;
95        } else
96            next.startElement(tagName);
97    }
98
99    public void endElement(TagName tagName) throws SAXException {
100        if(inXopInclude) {
101            // consume </xop:Include> by ourselves.
102            inXopInclude = false;
103            followXop = true;
104            return;
105        }
106        next.endElement(tagName);
107    }
108
109    public void startPrefixMapping(String prefix, String nsUri) throws SAXException {
110        next.startPrefixMapping(prefix,nsUri);
111    }
112
113    public void endPrefixMapping(String prefix) throws SAXException {
114        next.endPrefixMapping(prefix);
115    }
116
117    public void text( CharSequence pcdata ) throws SAXException {
118        if(!followXop)
119            next.text(pcdata);
120        else
121            followXop = false;
122    }
123
124    public UnmarshallingContext getContext() {
125        return next.getContext();
126    }
127
128    public TextPredictor getPredictor() {
129        return next.getPredictor();
130    }
131}
132