001 /* 002 * mesopotamia @mesopotamia.version@ 003 * Multilingual parser and repository. 004 * Copyright (C) 2005 Hammurapi Group 005 * 006 * This program is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 2 of the License, or (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with this library; if not, write to the Free Software 018 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 019 * 020 * URL: http://http://www.hammurapi.biz 021 * e-Mail: support@hammurapi.biz 022 */ 023 024 package org.mesopotamia; 025 026 import org.mesopotamia.sql.TokenType; 027 import org.w3c.dom.Element; 028 029 import biz.hammurapi.xml.dom.DomSerializable; 030 031 /** 032 * Token 033 * @author Pavel Vlasov 034 * 035 */ 036 public class Token implements DomSerializable { 037 038 org.mesopotamia.sql.TokenType type; 039 int col; 040 int line; 041 int position; 042 String text; 043 private SourceUnit su; 044 045 /** 046 * Can be created only by Source unit. 047 * @param type 048 * @param position 049 * @param line 050 * @param col 051 * @param text 052 */ 053 Token(SourceUnit su, TokenType type, int position, int line, int col, String text) { 054 super(); 055 this.type = type; 056 this.position = position; 057 this.line = line; 058 this.col = col; 059 this.text = text; 060 this.su = su; 061 } 062 063 064 065 public void toDom(Element holder) { 066 setAttribute(holder, "column", String.valueOf(col)); 067 setAttribute(holder, "position", String.valueOf(position)); 068 setAttribute(holder, "type", String.valueOf(type.getId())); 069 //type.toDom((Element) holder.appendChild(holder.getOwnerDocument().createElement("type"))); 070 071 if (text!=null) { 072 holder.appendChild(holder.getOwnerDocument().createTextNode(text)); 073 } 074 } 075 076 private void setAttribute(Element holder, String name, String value) { 077 if (value!=null) { 078 holder.setAttribute(name, value); 079 } 080 } 081 082 public String toString() { 083 return "["+getClass().getName()+"] "+getTokenName()+" "+getLine()+":"+getCol()+" "+getText(); 084 } 085 086 public SourceUnit getSourceUnit() { 087 return su; 088 } 089 090 public String getDescription() { 091 return type.getDescription(); 092 } 093 094 public int getTypeId() { 095 return type.getId(); 096 } 097 098 public boolean getIsEof() { 099 return type.getIsEof(); 100 } 101 102 public boolean getIsWhitespace() { 103 return type.getIsWhitespace(); 104 } 105 106 public String getLanguage() { 107 return type.getLanguage(); 108 } 109 110 public String getLanguageVersion() { 111 return type.getLanguageVersion(); 112 } 113 114 public String getRenderStyle() { 115 return type.getRenderStyle(); 116 } 117 118 public String getTokenName() { 119 return type.getTokenName(); 120 } 121 122 public int getTokenType() { 123 return type.getTokenType(); 124 } 125 126 public int getCol() { 127 return col; 128 } 129 130 public int getLine() { 131 return line; 132 } 133 134 public int getPosition() { 135 return position; 136 } 137 138 public String getText() { 139 return text; 140 } 141 }