001: /*
002: * Portions Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.internal.ws.wsdl.framework;
027:
028: import java.util.ArrayList;
029: import java.util.Iterator;
030: import java.util.List;
031:
032: import javax.xml.namespace.QName;
033:
034: import org.w3c.dom.Attr;
035: import org.w3c.dom.Element;
036:
037: import com.sun.xml.internal.ws.util.NamespaceSupport;
038: import com.sun.xml.internal.ws.util.xml.XmlUtil;
039:
040: /**
041: * The context used by parser classes.
042: *
043: * @author WS Development Team
044: */
045: public class ParserContext {
046:
047: private final static String PREFIX_XMLNS = "xmlns";
048:
049: public ParserContext(AbstractDocument doc, ArrayList listeners) {
050: _document = doc;
051: _listeners = listeners;
052: _nsSupport = new NamespaceSupport();
053: _wsdlLocation = new WSDLLocation();
054: }
055:
056: public AbstractDocument getDocument() {
057: return _document;
058: }
059:
060: public boolean getFollowImports() {
061: return _followImports;
062: }
063:
064: public void setFollowImports(boolean b) {
065: _followImports = b;
066: }
067:
068: public void push() {
069: _nsSupport.pushContext();
070: }
071:
072: public void pop() {
073: _nsSupport.popContext();
074: }
075:
076: public String getNamespaceURI(String prefix) {
077: return _nsSupport.getURI(prefix);
078: }
079:
080: public Iterator getPrefixes() {
081: return _nsSupport.getPrefixes();
082: }
083:
084: public String getDefaultNamespaceURI() {
085: return getNamespaceURI("");
086: }
087:
088: public void registerNamespaces(Element e) {
089: for (Iterator iter = XmlUtil.getAllAttributes(e); iter
090: .hasNext();) {
091: Attr a = (Attr) iter.next();
092: if (a.getName().equals(PREFIX_XMLNS)) {
093: // default namespace declaration
094: _nsSupport.declarePrefix("", a.getValue());
095: } else {
096: String prefix = XmlUtil.getPrefix(a.getName());
097: if (prefix != null && prefix.equals(PREFIX_XMLNS)) {
098: String nsPrefix = XmlUtil.getLocalPart(a.getName());
099: String uri = a.getValue();
100: _nsSupport.declarePrefix(nsPrefix, uri);
101: }
102: }
103: }
104: }
105:
106: public QName translateQualifiedName(String s) {
107: if (s == null)
108: return null;
109:
110: String prefix = XmlUtil.getPrefix(s);
111: String uri = null;
112:
113: if (prefix == null) {
114: uri = getDefaultNamespaceURI();
115: } else {
116: uri = getNamespaceURI(prefix);
117: if (uri == null) {
118: throw new ParseException(
119: "parsing.unknownNamespacePrefix", prefix);
120: }
121: }
122:
123: return new QName(uri, XmlUtil.getLocalPart(s));
124: }
125:
126: public void fireIgnoringExtension(QName name, QName parent) {
127: List _targets = null;
128:
129: synchronized (this ) {
130: if (_listeners != null) {
131: _targets = (List) _listeners.clone();
132: }
133: }
134:
135: if (_targets != null) {
136: for (Iterator iter = _targets.iterator(); iter.hasNext();) {
137: ParserListener l = (ParserListener) iter.next();
138: l.ignoringExtension(name, parent);
139: }
140: }
141: }
142:
143: public void fireDoneParsingEntity(QName element, Entity entity) {
144: List _targets = null;
145:
146: synchronized (this ) {
147: if (_listeners != null) {
148: _targets = (List) _listeners.clone();
149: }
150: }
151:
152: if (_targets != null) {
153: for (Iterator iter = _targets.iterator(); iter.hasNext();) {
154: ParserListener l = (ParserListener) iter.next();
155: l.doneParsingEntity(element, entity);
156: }
157: }
158: }
159:
160: //bug fix: 4856674, WSDLLocation context maintainence
161: //and utility funcitons
162: public void pushWSDLLocation() {
163: _wsdlLocation.push();
164: }
165:
166: public void popWSDLLocation() {
167: _wsdlLocation.pop();
168: }
169:
170: public void setWSDLLocation(String loc) {
171: _wsdlLocation.setLocation(loc);
172: }
173:
174: public String getWSDLLocation() {
175: return _wsdlLocation.getLocation();
176: }
177:
178: private boolean _followImports;
179: private AbstractDocument _document;
180: private NamespaceSupport _nsSupport;
181: private ArrayList _listeners;
182: //bug fix:4856674
183: private WSDLLocation _wsdlLocation;
184:
185: }
|