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.tools.ws.wsdl.framework;
038:
039: import com.sun.tools.ws.api.wsdl.TWSDLParserContext;
040: import com.sun.tools.ws.wsdl.parser.DOMForest;
041: import com.sun.tools.ws.wscompile.ErrorReceiver;
042: import com.sun.tools.ws.resources.WsdlMessages;
043: import com.sun.xml.ws.util.NamespaceSupport;
044: import com.sun.xml.ws.util.xml.XmlUtil;
045: import org.w3c.dom.Attr;
046: import org.w3c.dom.Element;
047: import org.xml.sax.Locator;
048:
049: import javax.xml.namespace.QName;
050: import java.util.ArrayList;
051: import java.util.Iterator;
052: import java.util.List;
053:
054: /**
055: * The context used by parser classes.
056: *
057: * @author WS Development Team
058: */
059: public class TWSDLParserContextImpl implements TWSDLParserContext {
060:
061: private final static String PREFIX_XMLNS = "xmlns";
062: private boolean _followImports;
063: private final AbstractDocument _document;
064: private final NamespaceSupport _nsSupport;
065: private final ArrayList<ParserListener> _listeners;
066: private final WSDLLocation _wsdlLocation;
067: private final DOMForest forest;
068: private final ErrorReceiver errorReceiver;
069:
070: public TWSDLParserContextImpl(DOMForest forest,
071: AbstractDocument doc, ArrayList<ParserListener> listeners,
072: ErrorReceiver errReceiver) {
073: this ._document = doc;
074: this ._listeners = listeners;
075: this ._nsSupport = new NamespaceSupport();
076: this ._wsdlLocation = new WSDLLocation();
077: this .forest = forest;
078: this .errorReceiver = errReceiver;
079: }
080:
081: public AbstractDocument getDocument() {
082: return _document;
083: }
084:
085: public boolean getFollowImports() {
086: return _followImports;
087: }
088:
089: public void setFollowImports(boolean b) {
090: _followImports = b;
091: }
092:
093: public void push() {
094: _nsSupport.pushContext();
095: }
096:
097: public void pop() {
098: _nsSupport.popContext();
099: }
100:
101: public String getNamespaceURI(String prefix) {
102: return _nsSupport.getURI(prefix);
103: }
104:
105: public Iterable<String> getPrefixes() {
106: return _nsSupport.getPrefixes();
107: }
108:
109: public String getDefaultNamespaceURI() {
110: return getNamespaceURI("");
111: }
112:
113: public void registerNamespaces(Element e) {
114: for (Iterator iter = XmlUtil.getAllAttributes(e); iter
115: .hasNext();) {
116: Attr a = (Attr) iter.next();
117: if (a.getName().equals(PREFIX_XMLNS)) {
118: // default namespace declaration
119: _nsSupport.declarePrefix("", a.getValue());
120: } else {
121: String prefix = XmlUtil.getPrefix(a.getName());
122: if (prefix != null && prefix.equals(PREFIX_XMLNS)) {
123: String nsPrefix = XmlUtil.getLocalPart(a.getName());
124: String uri = a.getValue();
125: _nsSupport.declarePrefix(nsPrefix, uri);
126: }
127: }
128: }
129: }
130:
131: public Locator getLocation(Element e) {
132: return forest.locatorTable.getStartLocation(e);
133: }
134:
135: public QName translateQualifiedName(Locator locator, String s) {
136: if (s == null)
137: return null;
138:
139: String prefix = XmlUtil.getPrefix(s);
140: String uri = null;
141:
142: if (prefix == null) {
143: uri = getDefaultNamespaceURI();
144: } else {
145: uri = getNamespaceURI(prefix);
146: if (uri == null) {
147: errorReceiver.error(locator, WsdlMessages
148: .PARSING_UNKNOWN_NAMESPACE_PREFIX(prefix));
149: }
150: }
151:
152: return new QName(uri, XmlUtil.getLocalPart(s));
153: }
154:
155: public void fireIgnoringExtension(Element e, Entity entity) {
156: QName name = new QName(e.getNamespaceURI(), e.getLocalName());
157: QName parent = entity.getElementName();
158: List _targets = null;
159:
160: synchronized (this ) {
161: if (_listeners != null) {
162: _targets = (List) _listeners.clone();
163: }
164: }
165:
166: if (_targets != null) {
167: for (Iterator iter = _targets.iterator(); iter.hasNext();) {
168: ParserListener l = (ParserListener) iter.next();
169: l.ignoringExtension(entity, name, parent);
170: }
171: }
172: }
173:
174: public void fireDoneParsingEntity(QName element, Entity entity) {
175: List _targets = null;
176:
177: synchronized (this ) {
178: if (_listeners != null) {
179: _targets = (List) _listeners.clone();
180: }
181: }
182:
183: if (_targets != null) {
184: for (Iterator iter = _targets.iterator(); iter.hasNext();) {
185: ParserListener l = (ParserListener) iter.next();
186: l.doneParsingEntity(element, entity);
187: }
188: }
189: }
190:
191: //bug fix: 4856674, WSDLLocation context maintainence
192: //and utility funcitons
193: public void pushWSDLLocation() {
194: _wsdlLocation.push();
195: }
196:
197: public void popWSDLLocation() {
198: _wsdlLocation.pop();
199: }
200:
201: public void setWSDLLocation(String loc) {
202: _wsdlLocation.setLocation(loc);
203: }
204:
205: public String getWSDLLocation() {
206: return _wsdlLocation.getLocation();
207: }
208: }
|