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
;
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
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");
34 public GSPWriter(Writer out
, 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(");
51 public void printlnToBuffer(String s
, int index
) {
52 if(s
== null) s
= "''";
53 super.print("buf"+index
+" << ");
58 public void println() {
59 if(lineNumber
>= lineNumbers
.length
) {
60 lineNumbers
= (int[])resizeArray(lineNumbers
, lineNumbers
.length
* 2);
63 lineNumbers
[lineNumber
- 1] = parse
.getCurrentOutputLineNumber();
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(
74 int preserveLength
= Math
.min(oldSize
,newSize
);
75 if (preserveLength
> 0)
76 System
.arraycopy (oldArray
,0,newArray
,0,preserveLength
);
80 public int getCurrentLineNumber() {
81 return this.lineNumber
;
84 public int[] getLineNumbers() {