001: /*
002: * Copyright 2005 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.xml.internal.stream.events;
027:
028: import javax.xml.stream.events.Namespace;
029: import javax.xml.stream.events.XMLEvent;
030: import javax.xml.namespace.QName;
031:
032: import javax.xml.XMLConstants;
033:
034: /**
035: *
036: * @author Neeraj Bajaj,K.Venugopal@sun.com Sun Microsystems.
037: */
038: public class NamespaceImpl extends AttributeImpl implements Namespace {
039:
040: public NamespaceImpl() {
041: init();
042: }
043:
044: /** Creates a new instance of NamespaceImpl */
045: public NamespaceImpl(String namespaceURI) {
046: super (XMLConstants.XMLNS_ATTRIBUTE,
047: XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
048: XMLConstants.DEFAULT_NS_PREFIX, namespaceURI, null);
049: init();
050: }
051:
052: public NamespaceImpl(String prefix, String namespaceURI) {
053: super (XMLConstants.XMLNS_ATTRIBUTE,
054: XMLConstants.XMLNS_ATTRIBUTE_NS_URI, prefix,
055: namespaceURI, null);
056: init();
057: }
058:
059: public boolean isDefaultNamespaceDeclaration() {
060: QName name = this .getName();
061:
062: if (name != null
063: && (name.getLocalPart()
064: .equals(XMLConstants.DEFAULT_NS_PREFIX)))
065: return true;
066: return false;
067: }
068:
069: void setPrefix(String prefix) {
070: if (prefix == null)
071: setName(new QName(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
072: XMLConstants.DEFAULT_NS_PREFIX,
073: XMLConstants.XMLNS_ATTRIBUTE));
074: else
075: // new QName(uri, localpart, prefix)
076: setName(new QName(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
077: prefix, XMLConstants.XMLNS_ATTRIBUTE));
078: }
079:
080: public String getPrefix() {
081: //for a namespace declaration xmlns:prefix="uri" to get the prefix we have to get the
082: //local name if this declaration is stored as QName.
083: QName name = this .getName();
084: if (name != null)
085: return name.getLocalPart();
086: return null;
087: }
088:
089: public String getNamespaceURI() {
090: //we are treating namespace declaration as attribute -- so URI is stored as value
091: //xmlns:prefix="Value"
092: return this .getValue();
093: }
094:
095: void setNamespaceURI(String uri) {
096: //we are treating namespace declaration as attribute -- so URI is stored as value
097: //xmlns:prefix="Value"
098: this .setValue(uri);
099: }
100:
101: protected void init() {
102: setEventType(XMLEvent.NAMESPACE);
103: }
104:
105: public int getEventType() {
106: return XMLEvent.NAMESPACE;
107: }
108:
109: public boolean isNamespace() {
110: return true;
111: }
112: }
|