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 
  26 package com.sun.tools.internal.ws.wsdl.framework;
  27 
  28 import com.sun.tools.internal.ws.api.wsdl.TWSDLParserContext;
  29 import com.sun.tools.internal.ws.wsdl.parser.DOMForest;
  30 import com.sun.tools.internal.ws.wscompile.ErrorReceiver;
  31 import com.sun.tools.internal.ws.resources.WsdlMessages;
  32 import com.sun.xml.internal.ws.util.NamespaceSupport;
  33 import com.sun.xml.internal.ws.util.xml.XmlUtil;
  34 import org.w3c.dom.Attr;
  35 import org.w3c.dom.Element;
  36 import org.xml.sax.Locator;
  37 
  38 import javax.xml.namespace.QName;
  39 import java.util.ArrayList;
  40 import java.util.Iterator;
  41 import java.util.List;
  42 
  43 /**
  44  * The context used by parser classes.
  45  *
  46  * @author WS Development Team
  47  */
  48 public class TWSDLParserContextImpl implements TWSDLParserContext {
  49 
  50     private final static String PREFIX_XMLNS = "xmlns";
  51     private boolean _followImports;
  52     private final AbstractDocument _document;
  53     private final NamespaceSupport _nsSupport;
  54     private final ArrayList<ParserListener> _listeners;
  55     private final WSDLLocation _wsdlLocation;
  56     private final DOMForest forest;
  57     private final ErrorReceiver errorReceiver;
  58 
  59     public TWSDLParserContextImpl(DOMForest forest, AbstractDocument doc, ArrayList<ParserListener> listeners, ErrorReceiver errReceiver) {
  60         this._document = doc;
  61         this._listeners = listeners;
  62         this._nsSupport = new NamespaceSupport();
  63         this._wsdlLocation = new WSDLLocation();
  64         this.forest = forest;
  65         this.errorReceiver = errReceiver;
  66     }
  67 
  68     public AbstractDocument getDocument() {
  69         return _document;
  70     }
  71 
  72     public boolean getFollowImports() {
  73         return _followImports;
  74     }
  75 
  76     public void setFollowImports(boolean b) {
  77         _followImports = b;
  78     }
  79 
  80     public void push() {
  81         _nsSupport.pushContext();
  82     }
  83 
  84     public void pop() {
  85         _nsSupport.popContext();
  86     }
  87 
  88     public String getNamespaceURI(String prefix) {
  89         return _nsSupport.getURI(prefix);
  90     }
  91 
  92     public Iterable<String> getPrefixes() {
  93         return _nsSupport.getPrefixes();
  94     }
  95 
  96     public String getDefaultNamespaceURI() {
  97         return getNamespaceURI("");
  98     }
  99 
 100     public void registerNamespaces(Element e) {
 101         for (Iterator iter = XmlUtil.getAllAttributes(e); iter.hasNext();) {
 102             Attr a = (Attr) iter.next();
 103             if (a.getName().equals(PREFIX_XMLNS)) {
 104                 // default namespace declaration
 105                 _nsSupport.declarePrefix("", a.getValue());
 106             } else {
 107                 String prefix = XmlUtil.getPrefix(a.getName());
 108                 if (prefix != null && prefix.equals(PREFIX_XMLNS)) {
 109                     String nsPrefix = XmlUtil.getLocalPart(a.getName());
 110                     String uri = a.getValue();
 111                     _nsSupport.declarePrefix(nsPrefix, uri);
 112                 }
 113             }
 114         }
 115     }
 116 
 117     public Locator getLocation(Element e) {
 118         return forest.locatorTable.getStartLocation(e);
 119     }
 120 
 121     public QName translateQualifiedName(Locator locator, String s) {
 122         if (s == null)
 123             return null;
 124 
 125         String prefix = XmlUtil.getPrefix(s);
 126         String uri = null;
 127 
 128         if (prefix == null) {
 129             uri = getDefaultNamespaceURI();
 130         } else {
 131             uri = getNamespaceURI(prefix);
 132             if (uri == null) {
 133                 errorReceiver.error(locator, WsdlMessages.PARSING_UNKNOWN_NAMESPACE_PREFIX(prefix));
 134             }
 135         }
 136 
 137         return new QName(uri, XmlUtil.getLocalPart(s));
 138     }
 139 
 140     public void fireIgnoringExtension(Element e, Entity entity) {
 141         QName name = new QName(e.getNamespaceURI(), e.getLocalName());
 142         QName parent = entity.getElementName();
 143         List _targets = null;
 144 
 145         synchronized (this) {
 146             if (_listeners != null) {
 147                 _targets = (List) _listeners.clone();
 148             }
 149         }
 150 
 151         if (_targets != null) {
 152             for (Iterator iter = _targets.iterator(); iter.hasNext();) {
 153                 ParserListener l = (ParserListener) iter.next();
 154                 l.ignoringExtension(entity, name, parent);
 155             }
 156         }
 157     }
 158 
 159     public void fireDoneParsingEntity(QName element, Entity entity) {
 160         List _targets = null;
 161 
 162         synchronized (this) {
 163             if (_listeners != null) {
 164                 _targets = (List) _listeners.clone();
 165             }
 166         }
 167 
 168         if (_targets != null) {
 169             for (Iterator iter = _targets.iterator(); iter.hasNext();) {
 170                 ParserListener l = (ParserListener) iter.next();
 171                 l.doneParsingEntity(element, entity);
 172             }
 173         }
 174     }
 175 
 176     //bug fix: 4856674, WSDLLocation context maintainence
 177     //and utility funcitons
 178     public void pushWSDLLocation() {
 179         _wsdlLocation.push();
 180     }
 181 
 182     public void popWSDLLocation() {
 183         _wsdlLocation.pop();
 184     }
 185 
 186     public void setWSDLLocation(String loc) {
 187         _wsdlLocation.setLocation(loc);
 188     }
 189 
 190     public String getWSDLLocation() {
 191         return _wsdlLocation.getLocation();
 192     }
 193 }