1 /*
   2  * Copyright (c) 1997, 2010, 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.wsdl.document.jaxws.JAXWSBindingsConstants;
  29 import com.sun.tools.internal.xjc.reader.internalizer.LocatorTable;
  30 import com.sun.xml.internal.bind.marshaller.SAX2DOMEx;
  31 import org.w3c.dom.Comment;
  32 import org.w3c.dom.Document;
  33 import org.w3c.dom.Element;
  34 import org.w3c.dom.Node;
  35 import org.xml.sax.Attributes;
  36 import org.xml.sax.Locator;
  37 import org.xml.sax.SAXException;
  38 import org.xml.sax.ext.LexicalHandler;
  39 
  40 import java.util.Set;
  41 
  42 /**
  43  * Builds DOM while keeping the location information.
  44  *
  45  * <p>
  46  * This class also looks for outer most &lt;jaxws:bindings>
  47  * customizations.
  48  *
  49  * @author
  50  *     Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
  51  *     Vivek Pandey
  52  */
  53 class DOMBuilder extends SAX2DOMEx implements LexicalHandler {
  54     /**
  55      * Grows a DOM tree under the given document, and
  56      * stores location information to the given table.
  57      *
  58      * @param outerMostBindings
  59      *      This set will receive newly found outermost
  60      *      jaxb:bindings customizations.
  61      */
  62     public DOMBuilder( Document dom, LocatorTable ltable, Set outerMostBindings ) {
  63         super( dom );
  64         this.locatorTable = ltable;
  65         this.outerMostBindings = outerMostBindings;
  66     }
  67 
  68     /** Location information will be stored into this object. */
  69     private final LocatorTable locatorTable;
  70 
  71     private final Set outerMostBindings;
  72 
  73     private Locator locator;
  74 
  75     public void setDocumentLocator(Locator locator) {
  76         this.locator = locator;
  77         super.setDocumentLocator(locator);
  78     }
  79 
  80 
  81     public void startElement(String namespaceURI, String localName, String qName, Attributes atts) {
  82         super.startElement(namespaceURI, localName, qName, atts);
  83 
  84         Element e = getCurrentElement();
  85         locatorTable.storeStartLocation( e, locator );
  86 
  87         // check if this element is an outer-most <jaxb:bindings>
  88         if( JAXWSBindingsConstants.JAXWS_BINDINGS.getNamespaceURI().equals(e.getNamespaceURI())
  89         &&  "bindings".equals(e.getLocalName()) ) {
  90 
  91             // if this is the root node (meaning that this file is an
  92             // external binding file) or if the parent is XML Schema element
  93             // (meaning that this is an "inlined" external binding)
  94             Node p = e.getParentNode();
  95             if( p instanceof Document) {
  96                 outerMostBindings.add(e);   // remember this value
  97             }
  98         }
  99     }
 100 
 101     public void endElement(String namespaceURI, String localName, String qName) {
 102         locatorTable.storeEndLocation( getCurrentElement(), locator );
 103         super.endElement(namespaceURI, localName, qName);
 104     }
 105 
 106     public void startDTD(String name, String publicId, String systemId) throws SAXException {}
 107 
 108     public void endDTD() throws SAXException {}
 109 
 110     public void startEntity(String name) throws SAXException {}
 111 
 112     public void endEntity(String name) throws SAXException {}
 113 
 114     public void startCDATA() throws SAXException {}
 115 
 116     public void endCDATA() throws SAXException {}
 117 
 118     public void comment(char[] ch, int start, int length) throws SAXException {
 119         //Element e = getCurrentElement(); // does not work as the comments at the top of the document would return Document Node
 120         // instead of Element
 121         Node parent = nodeStack.peek();
 122         Comment comment = document.createComment(new String(ch,start,length));
 123         parent.appendChild(comment);
 124     }
 125 }