01: /*
02: * Copyright 2006-2007 Pentaho Corporation. All rights reserved.
03: * This software was developed by Pentaho Corporation and is provided under the terms
04: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
05: * this file except in compliance with the license. If you need a copy of the license,
06: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt.
07: *
08: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
09: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
10: * the license for the specific language governing your rights and limitations.
11: *
12: * Additional Contributor(s): Martin Schmid gridvision engineering GmbH
13: */
14: package org.pentaho.reportdesigner.crm.report.tests;
15:
16: import java.io.BufferedReader;
17: import java.io.File;
18: import java.io.FileReader;
19: import java.io.IOException;
20: import java.util.Scanner;
21:
22: /**
23: * User: Martin
24: * Date: 20.08.2006
25: * Time: 10:09:38
26: */
27: @SuppressWarnings({"ALL"})
28: public class ClosureTest {
29: public static void main(String[] args) throws IOException {
30: File file = new File("C:\\temp\\invoicePRD-2.xml");
31:
32: LineProcessor lineOut1 = new LineProcessor() {
33: public void processLine(String line) {
34: System.out.println("line = " + line);
35: }
36: };
37:
38: ClosureTest.readLines(file, lineOut1);
39:
40: ClosureTest.readLines(file, getSysoutProcessor());
41: ClosureTest.readLines(file, getNullProcessor());
42:
43: /*
44: //closure as in: http://blogs.sun.com/roller/resources/ahe/closures.pdf
45: void lineOut2(String line)
46: {
47: System.out.println("line = " + line);
48: }
49:
50: ClosureTest.readLines(file, lineOut2);
51: */
52:
53: Scanner sc = new Scanner(file);
54: while (sc.hasNextLine()) {
55: System.out.println(sc.nextLine());
56: }
57: sc.close();
58: }
59:
60: private static LineProcessor getSysoutProcessor() {
61: return new LineProcessor() {
62: public void processLine(String line) {
63: System.out.println(line);
64: }
65: };
66: }
67:
68: private static LineProcessor getNullProcessor() {
69: return new LineProcessor() {
70: public void processLine(String line) {
71: }
72: };
73: }
74:
75: private static interface LineProcessor {
76: void processLine(String line);
77: }
78:
79: private static void readLines(File file, LineProcessor lp)
80: throws IOException {
81: BufferedReader br = null;
82: try {
83: br = new BufferedReader(new FileReader(file));
84: String line;
85: while ((line = br.readLine()) != null) {
86: lp.processLine(line);
87: }
88: } finally {
89: if (br != null) {
90: br.close();
91: }
92: }
93: }
94: }
|