1 ////////////////////////////////////////////////////////////////////////////////
2 // MillScript-XML: an Open Spice interpreter and batch website creation tool
3 // Copyright (C) 2005 Kevin Rogers
4 //
5 // This file is part of MillScript-XML.
6 //
7 // MillScript-XML is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 2 of the License, or (at your option)
10 // any later version.
11 //
12 // MillScript-XML is distributed in the hope that it will be useful, but WITHOUT
13 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 // more details.
16 //
17 // You should have received a copy of the GNU General Public License along with
18 // MillScript-XML; if not, write to the Free Software Foundation, Inc., 59 Temple
19 // Place, Suite 330, Boston, MA 02111-1307 USA
20 ////////////////////////////////////////////////////////////////////////////////
21 package org.millscript.commons.xml.tokenizer;
22
23 import java.io.Serializable;
24
25 import org.millscript.commons.alert.AlertDecorator;
26 import org.millscript.commons.util.IMap;
27 import org.millscript.commons.xml.api.Name;
28
29 /**
30 * This class provides a <code>Name</code> implementation for prefixed, or
31 * namespace aware, names.
32 */
33 public abstract class AbstractName implements AlertDecorator, Comparable, Name, Serializable {
34
35 /**
36 * @see java.lang.Comparable#compareTo(java.lang.Object)
37 */
38 public int compareTo( final Object o ) {
39 // Compare the namespaces first
40 final int nscomp = this.getNamespace().compareTo( ((AbstractName) o).getNamespace() );
41 // If the namespaces are the same, then compare the local names otherwise
42 // order by the namespaces
43 return nscomp == 0 ? this.getLocalName().compareTo( ((AbstractName) o).getLocalName() )
44 : nscomp;
45 }
46
47 /**
48 * @see java.lang.Object#equals(java.lang.Object)
49 */
50 @Override
51 public boolean equals( final Object obj ) {
52 if ( this == obj ) {
53 return true;
54 } else if ( obj instanceof Name ) {
55 // Equality is based on the namespace and localName because in a
56 // qualified XML Namespace name the prefix is ignored. The prefix
57 // is only a shorthand for the full namespace.
58 return this.getNamespace().equals(
59 ((Name) obj).getNamespace()
60 ) && this.getLocalName().equals(
61 ((Name) obj).getLocalName()
62 );
63 } else {
64 return false;
65 }
66 }
67
68 /**
69 * @see java.lang.Object#hashCode()
70 */
71 @Override
72 public int hashCode() {
73 // The hashCode is constructed from the namespace and localName because
74 // in a qualified XML Namespace name the prefix is ignored. The prefix
75 // is only a shorthand for the full namespace.
76 return 37 * ( 1369 + this.getNamespace().hashCode() ) + this.getLocalName().hashCode();
77 }
78
79 public abstract AbstractName setNamespace( IMap< String, String > scope );
80
81 }