View Javadoc

1   package org.peaseplate.internal.parser;
2   
3   import org.peaseplate.TemplateEngine;
4   import org.peaseplate.TemplateException;
5   import org.peaseplate.chunk.Chunk;
6   import org.peaseplate.internal.model.CompileContext;
7   import org.peaseplate.lang.Designator;
8   
9   public class Token {
10  
11  	private final TokenType type;
12  	private final Designator designator;
13  	private final char[] source;
14  	private final int offset;
15  	private final int length;
16  	private final int line;
17  	private final int column;
18  	
19  	public Token(TokenType type, Designator designator, char[] source, int offset, int length, int line, int column) {
20  		super();
21  		
22  		this.type = type;
23  		this.designator = designator;
24  		this.source = source;
25  		this.offset = offset;
26  		this.length = length;
27  		this.line = line;
28  		this.column = column;
29  	}
30  
31  	public TokenType getType() {
32  		return type;
33  	}
34  
35  	public Designator getDesignator() {
36  		return designator;
37  	}
38  
39  	public char[] getSource() {
40  		return source;
41  	}
42  
43  	public int getOffset() {
44  		return offset;
45  	}
46  
47  	public int getLength() {
48  		return length;
49  	}
50  
51  	public int getLine() {
52  		return line;
53  	}
54  
55  	public int getColumn() {
56  		return column;
57  	}
58  	
59  	public boolean isSeparator() {
60  		switch (getType()) {
61  			case LINE_SEPARATOR:
62  			case END_OF_SOURCE:
63  				return true;
64  				
65  			default:
66  				return false;
67  		}
68  	}
69  	
70  	/**
71  	 * Returns true if the token is visible.
72  	 * A content token is not visible if it just contains whitespaces.
73  	 * Any other token checks the designator if it is visible.
74  	 * 
75  	 * @return true if visible
76  	 */
77  	public boolean isVisible() {
78  		switch (getType()) {
79  			case CONTENT:
80  			    for (int i=0; i<length; i+=1)
81  			    	if ((source[offset + i] != ' ') && (source[offset + i] != '\t'))
82  			    		return true;
83  			    
84  			    return false;
85  
86  			case END_OF_SOURCE:
87  				return false;
88  				
89  			default:
90  				return getDesignator().isVisible();
91  		}
92  	}
93  	
94  	/**
95  	 * Returns true if the token is essential for rendering.
96  	 * A code token is always essential.
97  	 * A comment token is not essential.
98  	 * A content token is not essential if it just contains whitespaces.
99  	 * A line separator token is not essential.
100 	 * A end of source token is essential
101 	 * 
102 	 * @return true if essential
103 	 */
104 	public boolean isEssential() {
105 		switch (getType()) {
106 			case CODE:
107 			case END_OF_SOURCE:
108 				return true;
109 				
110 			case CONTENT:
111 			    for (int i=0; i<length; i+=1)
112 			    	if ((source[offset + i] != ' ') && (source[offset + i] != '\t'))
113 			    		return true;
114 			    
115 			    return false;
116 				
117 			default:
118 				return false;
119 		}
120 	}
121 
122 	public boolean isBlockHead() {
123 		return (getDesignator() != null) && (getDesignator().isBlockHead());
124 	}
125 	
126 	public boolean isBlockTail() {
127 		return (getDesignator() != null) && (getDesignator().isBlockTail());
128 	}
129 	
130 	public boolean isBlockExpansion() {
131 		return (getDesignator() != null) && (getDesignator().isBlockExpansion());
132 	}
133 
134 	public boolean isExpandableBlock() {
135 		return (getDesignator() != null) && (getDesignator().isExpandableBlock());
136 	}
137 	
138 	public Chunk compile(TemplateEngine engine, CompileContext context) throws TemplateException {
139 		return getDesignator().compile(engine, context, this);
140 	}
141 	
142 	/**
143 	 * @see java.lang.Object#toString()
144 	 */
145 	@Override
146 	public String toString() {
147 		return String.format(
148 			"[%-8s | %-48s | %4d | %3d | %3d | %3d] %s", 
149 			getType(), (getDesignator() != null) ? getDesignator().getClass() : null,
150 			getOffset(), getLength(),
151 			getLine(), getColumn(),
152 			new String(getSource(), getOffset(), getLength())
153 		);
154 	}
155 	
156 }