1 /*
   2  * Copyright (c) 1997, 2013, 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 
  26 package com.sun.tools.internal.ws.wsdl.parser;
  27 
  28 import com.sun.tools.internal.ws.api.wsdl.TWSDLExtensible;
  29 import com.sun.tools.internal.ws.api.wsdl.TWSDLParserContext;
  30 import com.sun.tools.internal.ws.util.xml.XmlUtil;
  31 import com.sun.tools.internal.ws.wsdl.document.WSDLConstants;
  32 import com.sun.tools.internal.ws.wsdl.document.mime.*;
  33 import org.w3c.dom.Element;
  34 
  35 import java.util.Iterator;
  36 import java.util.Map;
  37 
  38 /**
  39  * The MIME extension handler for WSDL.
  40  *
  41  * @author WS Development Team
  42  */
  43 public class MIMEExtensionHandler extends AbstractExtensionHandler {
  44 
  45     public MIMEExtensionHandler(Map<String, AbstractExtensionHandler> extensionHandlerMap) {
  46         super(extensionHandlerMap);
  47     }
  48 
  49     public String getNamespaceURI() {
  50         return Constants.NS_WSDL_MIME;
  51     }
  52 
  53     @Override
  54     public boolean doHandleExtension(
  55         TWSDLParserContext context,
  56         TWSDLExtensible parent,
  57         Element e) {
  58         if (parent.getWSDLElementName().equals(WSDLConstants.QNAME_OUTPUT)) {
  59             return handleInputOutputExtension(context, parent, e);
  60         } else if (parent.getWSDLElementName().equals(WSDLConstants.QNAME_INPUT)) {
  61             return handleInputOutputExtension(context, parent, e);
  62         } else if (parent.getWSDLElementName().equals(MIMEConstants.QNAME_PART)) {
  63             return handleMIMEPartExtension(context, parent, e);
  64         } else {
  65 //            context.fireIgnoringExtension(
  66 //                new QName(e.getNamespaceURI(), e.getLocalName()),
  67 //                parent.getWSDLElementName());
  68             return false;
  69         }
  70     }
  71 
  72     protected boolean handleInputOutputExtension(
  73         TWSDLParserContext context,
  74         TWSDLExtensible parent,
  75         Element e) {
  76         if (XmlUtil.matchesTagNS(e, MIMEConstants.QNAME_MULTIPART_RELATED)) {
  77             context.push();
  78             context.registerNamespaces(e);
  79 
  80             MIMEMultipartRelated mpr = new MIMEMultipartRelated(context.getLocation(e));
  81 
  82             for (Iterator iter = XmlUtil.getAllChildren(e); iter.hasNext();) {
  83                 Element e2 = Util.nextElement(iter);
  84                 if (e2 == null)
  85                     break;
  86 
  87                 if (XmlUtil.matchesTagNS(e2, MIMEConstants.QNAME_PART)) {
  88                     context.push();
  89                     context.registerNamespaces(e2);
  90 
  91                     MIMEPart part = new MIMEPart(context.getLocation(e2));
  92 
  93                     String name =
  94                         XmlUtil.getAttributeOrNull(e2, Constants.ATTR_NAME);
  95                     if (name != null) {
  96                         part.setName(name);
  97                     }
  98 
  99                     for (Iterator iter2 = XmlUtil.getAllChildren(e2);
 100                          iter2.hasNext();
 101                         ) {
 102                         Element e3 = Util.nextElement(iter2);
 103                         if (e3 == null)
 104                             break;
 105 
 106                         AbstractExtensionHandler h = getExtensionHandlers().get(e3.getNamespaceURI());
 107                         boolean handled = false;
 108                         if (h != null) {
 109                             handled = h.doHandleExtension(context, part, e3);
 110                         }
 111 
 112                         if (!handled) {
 113                             String required =
 114                                 XmlUtil.getAttributeNSOrNull(
 115                                     e3,
 116                                     Constants.ATTR_REQUIRED,
 117                                     Constants.NS_WSDL);
 118                             if (required != null
 119                                 && required.equals(Constants.TRUE)) {
 120                                 Util.fail(
 121                                     "parsing.requiredExtensibilityElement",
 122                                     e3.getTagName(),
 123                                     e3.getNamespaceURI());
 124                             } else {
 125 //                                context.fireIgnoringExtension(
 126 //                                    new QName(
 127 //                                        e3.getNamespaceURI(),
 128 //                                        e3.getLocalName()),
 129 //                                    part.getElementName());
 130                             }
 131                         }
 132                     }
 133 
 134                     mpr.add(part);
 135                     context.pop();
 136 //                    context.fireDoneParsingEntity(
 137 //                        MIMEConstants.QNAME_PART,
 138 //                        part);
 139                 } else {
 140                     Util.fail(
 141                         "parsing.invalidElement",
 142                         e2.getTagName(),
 143                         e2.getNamespaceURI());
 144                 }
 145             }
 146 
 147             parent.addExtension(mpr);
 148             context.pop();
 149 //            context.fireDoneParsingEntity(
 150 //                MIMEConstants.QNAME_MULTIPART_RELATED,
 151 //                mpr);
 152             return true;
 153         } else if (XmlUtil.matchesTagNS(e, MIMEConstants.QNAME_CONTENT)) {
 154             MIMEContent content = parseMIMEContent(context, e);
 155             parent.addExtension(content);
 156             return true;
 157         } else if (XmlUtil.matchesTagNS(e, MIMEConstants.QNAME_MIME_XML)) {
 158             MIMEXml mimeXml = parseMIMEXml(context, e);
 159             parent.addExtension(mimeXml);
 160             return true;
 161         } else {
 162             Util.fail(
 163                 "parsing.invalidExtensionElement",
 164                 e.getTagName(),
 165                 e.getNamespaceURI());
 166             return false; // keep compiler happy
 167         }
 168     }
 169 
 170     @Override
 171     protected boolean handleMIMEPartExtension(
 172         TWSDLParserContext context,
 173         TWSDLExtensible parent,
 174         Element e) {
 175         if (XmlUtil.matchesTagNS(e, MIMEConstants.QNAME_CONTENT)) {
 176             MIMEContent content = parseMIMEContent(context, e);
 177             parent.addExtension(content);
 178             return true;
 179         } else if (XmlUtil.matchesTagNS(e, MIMEConstants.QNAME_MIME_XML)) {
 180             MIMEXml mimeXml = parseMIMEXml(context, e);
 181             parent.addExtension(mimeXml);
 182             return true;
 183         } else {
 184             Util.fail(
 185                 "parsing.invalidExtensionElement",
 186                 e.getTagName(),
 187                 e.getNamespaceURI());
 188             return false; // keep compiler happy
 189         }
 190     }
 191 
 192     protected MIMEContent parseMIMEContent(TWSDLParserContext context, Element e) {
 193         context.push();
 194         context.registerNamespaces(e);
 195 
 196         MIMEContent content = new MIMEContent(context.getLocation(e));
 197 
 198         String part = XmlUtil.getAttributeOrNull(e, Constants.ATTR_PART);
 199         if (part != null) {
 200             content.setPart(part);
 201         }
 202 
 203         String type = XmlUtil.getAttributeOrNull(e, Constants.ATTR_TYPE);
 204         if (type != null) {
 205             content.setType(type);
 206         }
 207 
 208         context.pop();
 209 //        context.fireDoneParsingEntity(MIMEConstants.QNAME_CONTENT, content);
 210         return content;
 211     }
 212 
 213     protected MIMEXml parseMIMEXml(TWSDLParserContext context, Element e) {
 214         context.push();
 215         context.registerNamespaces(e);
 216 
 217         MIMEXml mimeXml = new MIMEXml(context.getLocation(e));
 218 
 219         String part = XmlUtil.getAttributeOrNull(e, Constants.ATTR_PART);
 220         if (part != null) {
 221             mimeXml.setPart(part);
 222         }
 223 
 224         context.pop();
 225 //        context.fireDoneParsingEntity(MIMEConstants.QNAME_MIME_XML, mimeXml);
 226         return mimeXml;
 227     }
 228 }