3 * Copyright (C) 2004 The Free Software Foundation
5 * This file is part of GNU JAXP, a library.
7 * GNU JAXP is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * GNU JAXP is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * Linking this library statically or dynamically with other modules is
22 * making a combined work based on this library. Thus, the terms and
23 * conditions of the GNU General Public License cover the whole
26 * As a special exception, the copyright holders of this library give you
27 * permission to link this library with independent modules to produce an
28 * executable, regardless of the license terms of these independent
29 * modules, and to copy and distribute the resulting executable under
30 * terms of your choice, provided that you also meet, for each linked
31 * independent module, the terms and conditions of the license of that
32 * module. An independent module is a module which is not derived from
33 * or based on this library. If you modify this library, you may extend
34 * this exception to your version of the library, but you are not
35 * obliged to do so. If you do not wish to do so, delete this
36 * exception statement from your version.
39 package javax.xml.namespace;
41 import javax.xml.XMLConstants;
45 * <a href='http://www.w3.org/TR/REC-xml-names/#ns-qualnames'>qualified name</a>.
47 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
53 private final String namespaceURI;
54 private final String localPart;
55 private final String prefix;
56 private final String qName;
59 public QName(String namespaceURI, String localPart)
61 this(namespaceURI, localPart, null);
64 public QName(String namespaceURI, String localPart, String prefix)
66 if (namespaceURI == null)
68 namespaceURI = XMLConstants.NULL_NS_URI;
70 if (localPart == null)
72 throw new IllegalArgumentException();
76 prefix = XMLConstants.DEFAULT_NS_PREFIX;
78 this.namespaceURI = namespaceURI;
79 this.localPart = localPart;
82 StringBuffer buf = new StringBuffer();
83 if (namespaceURI != null && namespaceURI.length() > 0)
86 buf.append(namespaceURI);
89 if (prefix != null && prefix.length() > 0)
94 buf.append(localPart);
95 qName = buf.toString();
98 public QName(String localPart)
100 this(null, localPart, null);
103 public String getNamespaceURI()
108 public String getLocalPart()
113 public String getPrefix()
118 public boolean equals(Object obj)
120 if (obj instanceof QName)
122 QName qname = (QName) obj;
123 return qname.getLocalPart().equals(localPart) &&
124 qname.getNamespaceURI().equals(namespaceURI);
129 public final int hashCode()
133 StringBuffer buf = new StringBuffer();
135 buf.append(namespaceURI);
137 buf.append(localPart);
138 hashCode = buf.toString().hashCode();
143 public String toString()
148 public static QName valueOf(String qNameAsString)
150 String namespaceUri = "", prefix = null;
151 int start = qNameAsString.indexOf('{');
152 int end = qNameAsString.indexOf('}');
157 throw new IllegalArgumentException(qNameAsString);
159 namespaceUri = qNameAsString.substring(start + 1, end);
160 qNameAsString = qNameAsString.substring(end + 1);
162 start = qNameAsString.indexOf(':');
165 prefix = qNameAsString.substring(0, start);
166 qNameAsString = qNameAsString.substring(start + 1);
168 return new QName(namespaceUri, qNameAsString, prefix);