001: /*
002: * @(#)readLines.java 1.2 04/12/06
003: *
004: * Copyright (c) 2001-2004 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package org.pnuts.text;
010:
011: import pnuts.lang.*;
012: import org.pnuts.lib.PathHelper;
013: import org.pnuts.io.URLHelper;
014: import pnuts.io.CharacterEncoding;
015: import java.io.*;
016: import java.util.*;
017: import java.net.URL;
018:
019: /**
020: * An implementation of readLines().
021: */
022: public class readLines extends PnutsFunction {
023:
024: public readLines() {
025: super ("readLines");
026: }
027:
028: public boolean defined(int narg) {
029: return (narg >= 1 && narg <= 3);
030: }
031:
032: static LineProcessor getLineProcessor(Object arg,
033: LineHandler handler, Context context) throws IOException {
034: if (arg instanceof InputStream) {
035: return new LineReader(CharacterEncoding.getReader(
036: (InputStream) arg, context), handler, false);
037: } else if (arg instanceof Reader) {
038: return new LineReader((Reader) arg, handler, false);
039: } else if (arg instanceof File) {
040: return new LineReader(CharacterEncoding.getReader(
041: new FileInputStream((File) arg), context), handler,
042: true);
043: } else if (arg instanceof String) {
044: return new LineReader(CharacterEncoding.getReader(
045: new FileInputStream(PathHelper.getFile(
046: (String) arg, context)), context), handler,
047: true);
048: } else if (arg instanceof URL) {
049: return new LineReader(URLHelper.getReader((URL) arg,
050: context), handler, true);
051: } else {
052: throw new IllegalArgumentException();
053: }
054: }
055:
056: static LineHandler getLineHandler(Object arg, Context context) {
057: if (arg instanceof PnutsFunction) {
058: return new CallbackLineHandler((PnutsFunction) arg, context);
059: } else if (arg instanceof Collection) {
060: return new CollectionLineHandler((Collection) arg);
061: } else if (arg == null) {
062: return new LineHandler() {
063: public void process(char[] c, int offset, int length) {
064: }
065: };
066: } else {
067: throw new IllegalArgumentException();
068: }
069: }
070:
071: protected Object exec(Object args[], Context context) {
072: boolean includeNewLine = false;
073: final Object arg0 = args[0];
074: LineProcessor lineReader = null;
075: switch (args.length) {
076: case 3:
077: includeNewLine = ((Boolean) args[2]).booleanValue();
078: case 2:
079: try {
080: LineHandler handler = getLineHandler(args[1], context);
081: if (arg0 instanceof String || arg0 instanceof File
082: || arg0 instanceof URL) {
083: }
084: lineReader = getLineProcessor(arg0, handler, context);
085: return new Integer(lineReader
086: .processAll(includeNewLine));
087: } catch (IOException e) {
088: throw new PnutsException(e, context);
089: }
090: case 1:
091: final boolean newline = includeNewLine;
092: return new Generator() {
093: public Object apply(final PnutsFunction closure,
094: final Context context) {
095: LineProcessor lineReader = null;
096: try {
097: lineReader = getLineProcessor(arg0,
098: getLineHandler(closure, context),
099: context);
100: lineReader.processAll(newline);
101: } catch (IOException e) {
102: throw new PnutsException(e, context);
103: }
104: return null;
105: }
106: };
107: default:
108: undefined(args, context);
109: return null;
110: }
111: }
112:
113: public String toString() {
114: return "function readLines((inputStream|reader|file|fileName|url) {, (func(line) | collection) {, includeNewLine }} )";
115: }
116: }
|