1 /*
   2  * Copyright (c) 2005, 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.xml.internal.stream.events;
  27 
  28 import javax.xml.stream.events.Namespace;
  29 import javax.xml.stream.events.XMLEvent;
  30 import javax.xml.namespace.QName;
  31 
  32 import javax.xml.XMLConstants;
  33 /**
  34  *
  35  * @author  Neeraj Bajaj,K.Venugopal@sun.com  Sun Microsystems.
  36  */
  37 public class NamespaceImpl extends AttributeImpl implements Namespace{
  38 
  39     public NamespaceImpl( ) {
  40         init();
  41     }
  42 
  43     /** Creates a new instance of NamespaceImpl */
  44     public NamespaceImpl(String namespaceURI) {
  45         super(XMLConstants.XMLNS_ATTRIBUTE,XMLConstants.XMLNS_ATTRIBUTE_NS_URI,XMLConstants.DEFAULT_NS_PREFIX,namespaceURI,null);
  46         init();
  47     }
  48 
  49     public NamespaceImpl(String prefix, String namespaceURI){
  50         super(XMLConstants.XMLNS_ATTRIBUTE,XMLConstants.XMLNS_ATTRIBUTE_NS_URI,prefix,namespaceURI,null);
  51         init();
  52     }
  53 
  54     public boolean isDefaultNamespaceDeclaration() {
  55         QName name = this.getName();
  56 
  57         if(name != null && (name.getLocalPart().equals(XMLConstants.DEFAULT_NS_PREFIX)))
  58             return true;
  59         return false;
  60     }
  61 
  62     void setPrefix(String prefix){
  63         if(prefix == null)
  64             setName(new QName(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,XMLConstants.DEFAULT_NS_PREFIX,XMLConstants.XMLNS_ATTRIBUTE));
  65         else// new QName(uri, localpart, prefix)
  66             setName(new QName(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,prefix,XMLConstants.XMLNS_ATTRIBUTE));
  67     }
  68 
  69     public String getPrefix() {
  70         //for a namespace declaration xmlns:prefix="uri" to get the prefix we have to get the
  71         //local name if this declaration is stored as QName.
  72         QName name = this.getName();
  73         if(name != null)
  74             return name.getLocalPart();
  75         return null;
  76     }
  77 
  78     public String getNamespaceURI() {
  79         //we are treating namespace declaration as attribute -- so URI is stored as value
  80         //xmlns:prefix="Value"
  81         return this.getValue();
  82     }
  83 
  84     void setNamespaceURI(String uri) {
  85         //we are treating namespace declaration as attribute -- so URI is stored as value
  86         //xmlns:prefix="Value"
  87         this.setValue(uri);
  88     }
  89 
  90     protected void init(){
  91         setEventType(XMLEvent.NAMESPACE);
  92     }
  93 
  94     public int getEventType(){
  95         return XMLEvent.NAMESPACE;
  96     }
  97 
  98     public boolean isNamespace(){
  99         return true;
 100     }
 101 }