Merge from the pain train
[official-gcc.git] / libjava / javax / xml / namespace / QName.java
blobfbb222efc079c5b2b4af0f9af2e94ae419e9f7fa
1 /* QName.java --
2 Copyright (C) 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 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
24 combination.
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 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package javax.xml.namespace;
40 import javax.xml.XMLConstants;
42 /**
43 * An XML
44 * <a href='http://www.w3.org/TR/REC-xml-names/#ns-qualnames'>qualified name</a>.
46 * @author (a href='mailto:dog@gnu.org'>Chris Burdess</a)
47 * @since 1.3
49 public class QName
52 private final String namespaceURI;
53 private final String localPart;
54 private final String prefix;
55 private final String qName;
57 public QName(String namespaceURI, String localPart)
59 this(namespaceURI, localPart, null);
62 public QName(String namespaceURI, String localPart, String prefix)
64 if (namespaceURI == null)
66 namespaceURI = XMLConstants.NULL_NS_URI;
68 if (localPart == null)
70 throw new IllegalArgumentException();
72 if (prefix == null)
74 prefix = XMLConstants.DEFAULT_NS_PREFIX;
76 this.namespaceURI = namespaceURI;
77 this.localPart = localPart;
78 this.prefix = prefix;
80 StringBuffer buf = new StringBuffer();
81 if (namespaceURI != null && namespaceURI.length() > 0)
83 buf.append('{');
84 buf.append(namespaceURI);
85 buf.append('}');
87 if (prefix != null && prefix.length() > 0)
89 buf.append(prefix);
90 buf.append(':');
92 buf.append(localPart);
93 qName = buf.toString();
96 public QName(String localPart)
98 this(null, localPart, null);
101 public String getNamespaceURI()
103 return namespaceURI;
106 public String getLocalPart()
108 return localPart;
111 public String getPrefix()
113 return prefix;
116 public boolean equals(Object obj)
118 if (obj instanceof QName)
120 QName qname = (QName) obj;
121 return qname.getLocalPart().equals(localPart) &&
122 qname.getNamespaceURI().equals(namespaceURI);
124 return false;
127 public final int hashCode()
129 return qName.hashCode();
132 public String toString()
134 return qName;
137 public static QName valueOf(String qNameAsString)
139 String namespaceUri = "", prefix = null;
140 int start = qNameAsString.indexOf('{');
141 int end = qNameAsString.indexOf('}');
142 if (start != -1)
144 if (end < start)
146 throw new IllegalArgumentException(qNameAsString);
148 namespaceUri = qNameAsString.substring(start + 1, end);
149 qNameAsString = qNameAsString.substring(end + 1);
151 start = qNameAsString.indexOf(':');
152 if (start != -1)
154 prefix = qNameAsString.substring(0, start);
155 qNameAsString = qNameAsString.substring(start + 1);
157 return new QName(namespaceUri, qNameAsString, prefix);