1 17 18 19 20 package org.apache.fop.util; 21 22 import java.io.Serializable ; 23 24 30 public class QName implements Serializable { 31 32 private static final long serialVersionUID = -5225376740044770690L; 33 34 private String namespaceURI; 35 private String localName; 36 private String prefix; 37 private int hashCode; 38 39 45 public QName(String namespaceURI, String prefix, String localName) { 46 if (localName == null) { 47 throw new NullPointerException ("Parameter localName must not be null"); 48 } 49 if (localName.length() == 0) { 50 throw new IllegalArgumentException ("Parameter localName must not be empty"); 51 } 52 this.namespaceURI = namespaceURI; 53 this.prefix = prefix; 54 this.localName = localName; 55 this.hashCode = toHashString().hashCode(); 56 } 57 58 63 public QName(String namespaceURI, String qName) { 64 if (qName == null) { 65 throw new NullPointerException ("Parameter localName must not be null"); 66 } 67 if (qName.length() == 0) { 68 throw new IllegalArgumentException ("Parameter localName must not be empty"); 69 } 70 this.namespaceURI = namespaceURI; 71 int p = qName.indexOf(':'); 72 if (p > 0) { 73 this.prefix = qName.substring(0, p); 74 this.localName = qName.substring(p + 1); 75 } else { 76 this.prefix = null; 77 this.localName = qName; 78 } 79 this.hashCode = toHashString().hashCode(); 80 } 81 82 83 public String getNamespaceURI() { 84 return this.namespaceURI; 85 } 86 87 88 public String getPrefix() { 89 return this.prefix; 90 } 91 92 93 public String getLocalName() { 94 return this.localName; 95 } 96 97 98 public String getQName() { 99 return getPrefix() != null ? getPrefix() + ':' + getLocalName() : getLocalName(); 100 } 101 102 103 public int hashCode() { 104 return this.hashCode; 105 } 106 107 108 public boolean equals(Object obj) { 109 if (obj == null) { 110 return false; 111 } else if (obj == this) { 112 return true; 113 } else { 114 if (obj instanceof QName) { 115 QName other = (QName)obj; 116 if ((getNamespaceURI() == null && other.getNamespaceURI() == null) 117 || getNamespaceURI().equals(other.getNamespaceURI())) { 118 return getLocalName().equals(other.getLocalName()); 119 } 120 } 121 } 122 return false; 123 } 124 125 126 public String toString() { 127 return prefix != null 128 ? (prefix + ":" + localName) 129 : toHashString(); 130 } 131 132 private String toHashString() { 133 return (namespaceURI != null 134 ? ("{" + namespaceURI + "}" + localName) 135 : localName); 136 } 137 138 } 139 | Popular Tags |