KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > ecore > xml > type > internal > QName


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: QName.java,v 1.3 2005/06/08 06:20:10 nickb Exp $
16  */

17
18 package org.eclipse.emf.ecore.xml.type.internal;
19
20 import org.eclipse.emf.ecore.xml.type.InvalidDatatypeValueException;
21 import org.eclipse.emf.ecore.xml.type.internal.DataValue.XMLChar;
22
23 /**
24  * A structure that holds the components of an XML Namespaces qualified
25  * name.
26  * Two QNames are equal iff they both have same namespaceURI and same localPart.
27  * Note: prefix is not used in QName.equals(Object).
28  * If not specified, the prefix is set to empty string ("").
29  * If not specified, the namespace uri is set to empty string ("");
30  * <p>
31  * NOTE: this class is for internal use only.
32  */

33 public final class QName
34 {
35
36   private String JavaDoc prefix;
37
38   private String JavaDoc localPart;
39
40   private String JavaDoc namespaceURI;
41   
42   /**
43    * Constructs a QName.
44    * @param qname a <a HREF="http://www.w3.org/TR/REC-xml-names/#dt-qname">qualified name</a>
45    * Throws Exception if value is not legal qualified name
46    */

47   public QName (String JavaDoc qname)
48   {
49     String JavaDoc rawname = qname;
50     int index = rawname.indexOf(":");
51
52     String JavaDoc prefix = "";
53     String JavaDoc localName = rawname;
54     if (index != -1)
55     {
56       prefix = rawname.substring(0, index);
57       localName = rawname.substring(index + 1);
58     }
59     // both prefix (if any) a localpart must be valid NCName
60
if (prefix.length() > 0 && !XMLChar.isValidNCName(prefix))
61         throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1: invalid QName: "+qname);
62
63     if(!XMLChar.isValidNCName(localName))
64       throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1: invalid QName: "+qname);
65      
66     setPrefix(prefix);
67     setLocalPart(localName);
68     setNamespaceURI(null);
69   }
70
71   /** Constructs a QName with the specified values. */
72   public QName(String JavaDoc namespaceURI, String JavaDoc localPart, String JavaDoc prefix)
73   {
74     setNamespaceURI(namespaceURI);
75     setPrefix(prefix);
76     setLocalPart(localPart);
77     if (this.prefix.length() > 0 && !XMLChar.isValidNCName(this.prefix))
78       throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1: invalid QName: "+prefix);
79
80     if(!XMLChar.isValidNCName(this.localPart))
81        throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1: invalid QName: "+localPart);
82   }
83
84   /** Returns true if the two objects are equal. */
85   public boolean equals(Object JavaDoc object)
86   {
87     if (object instanceof QName)
88     {
89       QName qname = (QName)object;
90       return namespaceURI.equals(qname.getNamespaceURI()) && localPart.equals(qname.getLocalPart());
91     }
92     return false;
93   }
94   
95   public int hashCode()
96   {
97     return namespaceURI.hashCode() + localPart.hashCode();
98   }
99
100   /** Returns a string representation of this object. */
101   public String JavaDoc toString()
102   {
103     return (prefix.length() >0) ? prefix + ":" + localPart : localPart;
104   }
105
106   /**
107    * @return Returns the localpart.
108    */

109   public String JavaDoc getLocalPart()
110   {
111     return localPart;
112   }
113
114   /**
115    * @param localpart The localpart to set.
116    */

117   public void setLocalPart(String JavaDoc localpart)
118   {
119     if (localpart == null || localpart.length() == 0)
120     {
121       throw new IllegalArgumentException JavaDoc("QName localPart must have value.");
122     }
123     this.localPart = localpart;
124   }
125
126   /**
127    * @return Returns the namespaceURI.
128    */

129   public String JavaDoc getNamespaceURI()
130   {
131     return namespaceURI;
132   }
133
134   /**
135    * @param namespaceUri The namespaceURI to set.
136    */

137   public void setNamespaceURI(String JavaDoc namespaceUri)
138   {
139     if (namespaceUri == null)
140     {
141       this.namespaceURI = "";
142     }
143     else
144     {
145       this.namespaceURI = namespaceUri;
146     }
147     
148   }
149
150   /**
151    * @return Returns the prefix.
152    */

153   public String JavaDoc getPrefix()
154   {
155     return prefix;
156   }
157
158   /**
159    * @param prefix The prefix to set.
160    */

161   public void setPrefix(String JavaDoc prefix)
162   {
163     if (prefix == null)
164     {
165       this.prefix = "";
166     }
167     else
168     {
169       this.prefix = prefix;
170     }
171   }
172 }
173
Popular Tags