GRAILS-1019: Allowing expressions to be used with the 'disabled' attribute for g...
[grails.git] / src / web / org / codehaus / groovy / grails / web / pages / GSPWriter.java
blobd9ced9a523dbc390f02936fdeaffbf20424e0ed6
1 /*
2 * Copyright 2004-2005 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.codehaus.groovy.grails.web.pages;
18 import java.io.PrintWriter;
19 import java.io.Writer;
21 /**
22 * A PrintWriter used in the generation of GSP pages that allows printing to the target output stream and
23 * maintains a record of the current line number during usage.
25 * @author Graeme Rocher
26 * @since 13-Jan-2006
28 public class GSPWriter extends PrintWriter {
29 private int lineNumber = 1;
30 private int[] lineNumbers = new int[1000];
31 //private static final Pattern LINE_BREAK = Pattern.compile("\\r\\n|\\n|\\r");
32 private Parse parse;
34 public GSPWriter(Writer out, Parse parse) {
35 super(out);
36 this.parse = parse;
39 public void write(char buf[], int off, int len) {
40 super.write(buf, off, len);
43 public void printlnToResponse(String s) {
44 if(s == null) s = "''";
45 super.print("out.print(");
46 super.print(s);
47 super.print(")");
48 println();
51 public void printlnToBuffer(String s, int index) {
52 if(s == null) s = "''";
53 super.print("buf"+index+" << ");
54 super.print(s);
55 println();
58 public void println() {
59 if(lineNumber >= lineNumbers.length) {
60 lineNumbers = (int[])resizeArray(lineNumbers, lineNumbers.length * 2);
62 else {
63 lineNumbers[lineNumber - 1] = parse.getCurrentOutputLineNumber();
64 lineNumber++;
66 super.println();
69 private Object resizeArray (Object oldArray, int newSize) {
70 int oldSize = java.lang.reflect.Array.getLength(oldArray);
71 Class elementType = oldArray.getClass().getComponentType();
72 Object newArray = java.lang.reflect.Array.newInstance(
73 elementType,newSize);
74 int preserveLength = Math.min(oldSize,newSize);
75 if (preserveLength > 0)
76 System.arraycopy (oldArray,0,newArray,0,preserveLength);
77 return newArray;
80 public int getCurrentLineNumber() {
81 return this.lineNumber;
84 public int[] getLineNumbers() {
85 return lineNumbers;