0
|
1 package nabble.naml.dom;
|
|
2
|
|
3 import fschmidt.util.java.Identity;
|
|
4 import fschmidt.util.java.Interner;
|
|
5
|
|
6 import java.util.ArrayList;
|
|
7 import java.util.List;
|
|
8
|
|
9
|
|
10 public final class ElementName {
|
|
11
|
|
12 public static final class Part extends Text {
|
|
13 private final Identity<Part> identity = new Identity<Part>();
|
|
14
|
|
15 public Part(String text) {
|
|
16 super(text);
|
|
17 }
|
|
18
|
|
19 @Override public boolean equals(Object obj) {
|
|
20 if( !(obj instanceof Part) )
|
|
21 return false;
|
|
22 Part part = (Part)obj;
|
|
23 return part.text==text;
|
|
24 }
|
|
25
|
|
26 @Override public int hashCode() {
|
|
27 return text.hashCode();
|
|
28 }
|
|
29
|
|
30 private static final Interner<Part> interner = new Interner<Part>();
|
|
31
|
|
32 public Part intern() {
|
|
33 return interner.intern(this);
|
|
34 }
|
|
35
|
|
36 public Identity<Part> identity() {
|
|
37 return identity;
|
|
38 }
|
|
39 }
|
|
40
|
|
41 private final List<Part> parts;
|
|
42 private final boolean endsWithDot;
|
|
43 private final String stringVal;
|
|
44 private final String lowerCaseStringVal;
|
|
45
|
|
46 public ElementName(boolean endsWithDot,List<Part> parts) {
|
|
47 this.parts = parts;
|
|
48 this.endsWithDot = endsWithDot;
|
|
49 this.stringVal = stringVal().intern();
|
|
50 this.lowerCaseStringVal = stringVal.toLowerCase().intern();
|
|
51 }
|
|
52
|
|
53 public ElementName(String s) {
|
|
54 this.stringVal = s.intern();
|
|
55 this.lowerCaseStringVal = stringVal.toLowerCase().intern();
|
|
56 this.parts = new ArrayList<Part>();
|
|
57 int i = 0;
|
|
58 int i2;
|
|
59 while( (i2 = s.indexOf('.',i)) != -1 ) {
|
|
60 parts.add( new Part( s.substring(i,i2) ) );
|
|
61 i = i2 + 1;
|
|
62 }
|
|
63 this.endsWithDot = i == s.length();
|
|
64 if( !endsWithDot )
|
|
65 parts.add( new Part( s.substring(i) ) );
|
|
66 // yuck
|
|
67 Part firstPart = parts.get(0);
|
|
68 if( !firstPart.text.equals("t") )
|
|
69 parts.set( 0, firstPart.intern() );
|
|
70 }
|
|
71
|
|
72 public List<Part> parts() {
|
|
73 return parts;
|
|
74 }
|
|
75
|
|
76 public boolean endsWithDot() {
|
|
77 return endsWithDot;
|
|
78 }
|
|
79
|
|
80 private String stringVal() {
|
|
81 StringBuilder buf = new StringBuilder();
|
|
82 for( Part part : parts ) {
|
|
83 buf.append( part ).append( '.' );
|
|
84 }
|
|
85 if( !endsWithDot )
|
|
86 buf.setLength( buf.length() - 1 );
|
|
87 return buf.toString();
|
|
88 }
|
|
89
|
|
90 public String toString() {
|
|
91 return stringVal;
|
|
92 }
|
|
93
|
|
94 public String toLowerCaseString() {
|
|
95 return lowerCaseStringVal;
|
|
96 }
|
|
97
|
|
98 @Override public boolean equals(Object obj) {
|
|
99 if( !(obj instanceof ElementName) )
|
|
100 return false;
|
|
101 ElementName name = (ElementName)obj;
|
|
102 return name.parts.equals(parts) && name.endsWithDot == endsWithDot;
|
|
103 }
|
|
104
|
|
105 @Override public int hashCode() {
|
|
106 return parts.hashCode();
|
|
107 }
|
|
108
|
|
109 }
|