KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xdm > nodes > Token


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.xdm.nodes;
21
22 /**
23  *
24  * @author Ajit Bhate
25  */

26 public class Token {
27     
28     Token(String JavaDoc val, TokenType type) {
29     value = val;
30     this.type = type;
31     }
32     
33     public String JavaDoc getValue() {
34     return value;
35     }
36     
37     public TokenType getType() {
38     return type;
39     }
40     
41     @Override JavaDoc
42     public int hashCode() {
43     return getValue().hashCode();
44     }
45     
46     @Override JavaDoc
47     public boolean equals(Object JavaDoc obj) {
48     if (!(obj instanceof Token)) return false;
49     Token token = (Token)obj;
50     return((token.getValue().equals(getValue())) &&
51            (token.getType().equals(getType())));
52     }
53     
54     @Override JavaDoc
55     public String JavaDoc toString() {
56     return getType() + " '" + value + "'";
57     }
58     
59     public static Token create(String JavaDoc value, TokenType type) {
60     Token t = null;
61     switch(type) {
62         case TOKEN_ATTR_EQUAL: {
63         t = EQUALS_TOKEN;
64         break;
65         } case TOKEN_ELEMENT_END_TAG: {
66         t = value.length() == 1 ? CLOSE_ELEMENT:SELF_CLOSE_ELEMENT;
67         break;
68         }
69         default: {
70         t = new Token(value,type);
71         }
72     }
73     assert t != null;
74     return t;
75     }
76     
77     private static final Token EQUALS_TOKEN =
78     new Token("=", TokenType.TOKEN_ATTR_EQUAL); //NOI18N
79

80     private static final Token CLOSE_ELEMENT =
81     new Token(">", TokenType.TOKEN_ELEMENT_END_TAG); //NOI18N
82

83     private static final Token SELF_CLOSE_ELEMENT =
84     new Token("/>", TokenType.TOKEN_ELEMENT_END_TAG); //NOI18N
85

86     public static final Token CDATA_START =
87     new Token("<![CDATA[", TokenType.TOKEN_CDATA_VAL); //NOI18N
88

89     public static final Token CDATA_END =
90     new Token("]]>", TokenType.TOKEN_CDATA_VAL); //NOI18N
91

92     public static final Token COMMENT_START =
93     new Token("<!--", TokenType.TOKEN_CDATA_VAL); //NOI18N
94

95     public static final Token COMMENT_END =
96     new Token("-->", TokenType.TOKEN_CDATA_VAL); //NOI18N
97

98     private final String JavaDoc value;
99     private final TokenType type;
100 }
101
Popular Tags