001: /* Namespace.java
002:
003: {{IS_NOTE
004:
005: Purpose: Namespace
006: Description:
007: History:
008: 2001/10/21 16:06:46, Create, Tom M. Yeh
009: }}IS_NOTE
010:
011: Copyright (C) 2001 Potix Corporation. All Rights Reserved.
012:
013: {{IS_RIGHT
014: This program is distributed under GPL Version 2.0 in the hope that
015: it will be useful, but WITHOUT ANY WARRANTY.
016: }}IS_RIGHT
017: */
018: package org.zkoss.idom;
019:
020: import java.util.HashMap;
021: import java.util.Map;
022: import java.io.Serializable;
023: import java.io.IOException;
024: import java.io.ObjectInputStream;
025: import java.io.ObjectOutputStream;
026:
027: /**
028: * Represents the namespace.
029: * A namespace is immutable, so you have to get a new one
030: *
031: * @author tomyeh
032: * @see Item
033: */
034: public final class Namespace implements Serializable, Cloneable {
035: private static final long serialVersionUID = 20060622L;
036:
037: /** The <code>Namespace</code> for when <i>not</i> in a namespace
038: */
039: public static final Namespace NO_NAMESPACE = newSpecialNamespace(
040: "", "");
041: /** The xml namespace.
042: */
043: public static final Namespace XML_NAMESPACE = newSpecialNamespace(
044: "xml", "http://www.w3.org/XML/1998/namespace");
045: /** The xmlns namespace.
046: */
047: public static final Namespace XMLNS_NAMESPACE = newSpecialNamespace(
048: "xmlns", "http://www.w3.org/XML/1998/namespace");
049:
050: /** The prefix mapped to this namespace */
051: private String _prefix;
052: /** The URI for this namespace */
053: private String _uri;
054:
055: /** Returns the special namespace if prefix is special, or null if not.
056: */
057: public static Namespace getSpecial(String prefix) {
058: if (prefix.equals("xml"))
059: return Namespace.XML_NAMESPACE;
060: if (prefix.equals("xmlns"))
061: return Namespace.XMLNS_NAMESPACE;
062: return null;
063: }
064:
065: /** Assigns a spacial namespace whose name starts with xml.
066: * We need it because the verifier will reject it.
067: */
068: private static final Namespace newSpecialNamespace(String prefix,
069: String uri) {
070: Namespace ns = new Namespace("", uri);
071: ns._prefix = prefix; //assign directly to avoid checkNam...
072: return ns;
073: }
074:
075: /**
076: * Contructor.
077: *
078: * @param prefix String prefix to map to this namespace.
079: * @param uri String URI for namespace.
080: * @exception DOMException with NAMESPACE_ERR if the given prefix and uri
081: * is invalid
082: */
083: public Namespace(String prefix, String uri) {
084: Verifier.checkNamespacePrefix(prefix, null);
085: Verifier.checkNamespaceURI(uri, null);
086:
087: if (prefix.length() != 0 && uri.length() == 0)
088: throw new DOMException(DOMException.NAMESPACE_ERR,
089: "Non-empty prefix, " + prefix + ", requires a URI");
090: _prefix = prefix;
091: _uri = uri;
092: }
093:
094: /**
095: * Gets the tag name of the giving local name.
096: */
097: public final String tagNameOf(String name) {
098: assert (name.indexOf(':') < 0);
099:
100: int len = _prefix.length();
101: return len == 0 ? name : _prefix + ':' + name;
102: }
103:
104: /**
105: * Gets the prefix mapped to this Namespace.
106: */
107: public final String getPrefix() {
108: return _prefix;
109: }
110:
111: /**
112: * Gets the namespace URI for this Namespace.
113: */
114: public final String getURI() {
115: return _uri;
116: }
117:
118: /** Tests whether two namespace are the same in both prefix
119: * and namespace URI.
120: * On the other hand, equals check only the namespace URI.
121: *
122: * <p>Note: unlike equals, it throws DOMException if prefix
123: * is the same but URI is different.
124: *
125: * @exception DOMException if they have the same prefix
126: * but with different namespace URI
127: */
128: public final boolean equalsAll(Namespace ns) {
129: if (_prefix.equals(ns._prefix))
130: if (_uri.equals(ns._uri))
131: return true;
132: else
133: throw new DOMException(DOMException.NAMESPACE_ERR,
134: "The same prefix, " + _prefix
135: + ", cannot have different URI: "
136: + _uri + " vs " + ns._uri);
137: return false;
138: }
139:
140: //-- cloneable --//
141: public Object clone() {
142: try {
143: return super .clone();
144: } catch (CloneNotSupportedException e) {
145: throw new InternalError();
146: }
147: }
148:
149: //-- Object --//
150: /** Note: equals() is based on URI only. */
151: public boolean equals(Object o) {
152: return this == o
153: || ((o instanceof Namespace) && _uri
154: .equals(((Namespace) o)._uri));
155: }
156:
157: /** Note: hashCode() is based on URI only. */
158: public int hashCode() {
159: return _uri.hashCode();
160: }
161:
162: public String toString() {
163: return "[Namespace: \"" + _prefix + "\", \"" + _uri + "\"]";
164: }
165: }
|