001: /*
002: * @(#)scanLines.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.nio;
010:
011: import java.io.*;
012: import java.nio.*;
013: import java.util.*;
014: import java.net.*;
015: import pnuts.lang.*;
016: import pnuts.io.CharacterEncoding;
017: import org.pnuts.text.*;
018: import org.pnuts.lib.PathHelper;
019: import org.pnuts.io.URLHelper;
020:
021: /*
022: * function scanLines((inputStream|reader|file|fileName|url), (func(line) | collection) {, includeNewLine })
023: */
024: public class scanLines extends PnutsFunction {
025:
026: public scanLines() {
027: super ("scanLines");
028: }
029:
030: public boolean defined(int narg) {
031: return (narg == 1 || narg == 2 || narg == 3);
032: }
033:
034: static boolean requiresDoubleByte(Context context) {
035: String enc = CharacterEncoding.getCharacterEncoding(context)[0];
036:
037: return !(enc != null && "ascii".equals(enc.toLowerCase()));
038: }
039:
040: static LineProcessor getLineProcessor(Object arg,
041: LineHandler handler, Context context) throws IOException {
042: AbstractLineReader lineReader;
043:
044: if (arg instanceof InputStream) {
045: if (requiresDoubleByte(context)) {
046: return new LineReader(CharacterEncoding.getReader(
047: (InputStream) arg, context), handler, false);
048: } else {
049: return new LineInputStream((InputStream) arg, handler,
050: false);
051: }
052: } else if (arg instanceof Reader) {
053: return new LineReader((Reader) arg, handler, false);
054: } else if (arg instanceof File) {
055: if (requiresDoubleByte(context)) {
056: return new LineReader(CharacterEncoding.getReader(
057: new FileInputStream((File) arg), context),
058: handler, true);
059: } else {
060: return new LineInputStream(new FileInputStream(
061: (File) arg), handler, true);
062: }
063: } else if (arg instanceof String) {
064: if (requiresDoubleByte(context)) {
065: return new LineReader(CharacterEncoding.getReader(
066: new FileInputStream(PathHelper.getFile(
067: (String) arg, context)), context),
068: handler, true);
069: } else {
070: return new LineInputStream(new FileInputStream(
071: PathHelper.getFile((String) arg, context)),
072: handler, true);
073: }
074: } else if (arg instanceof URL) {
075: return new LineReader(URLHelper.getReader((URL) arg,
076: context), handler, true);
077: } else if (arg instanceof ByteBuffer) {
078: return new ByteBufferLineInputStream((ByteBuffer) arg,
079: handler, false);
080: } else if (arg instanceof CharBuffer) {
081: lineReader = new CharBufferLineReader((CharBuffer) arg);
082: } else if (arg instanceof CharSequence) {
083: lineReader = new CharBufferLineReader(CharBuffer
084: .wrap((CharSequence) arg));
085: } else {
086: throw new IllegalArgumentException();
087: }
088: return lineReader;
089: }
090:
091: static LineHandler getLineHandler(Object arg, Context context) {
092: if (arg instanceof PnutsFunction) {
093: return new CallbackLineHandler((PnutsFunction) arg, context);
094: } else if (arg instanceof Collection) {
095: return new CollectionLineHandler((Collection) arg);
096: } else if (arg == null) {
097: return new LineHandler() {
098: public void process(char[] c, int offset, int length) {
099: }
100:
101: public void process(byte[] b, int offset, int length) {
102: }
103: };
104: } else {
105: throw new IllegalArgumentException();
106: }
107: }
108:
109: protected Object exec(final Object args[], final Context context) {
110: boolean includeNewLine = false;
111: final Object arg0 = args[0];
112: LineProcessor lineReader = null;
113: switch (args.length) {
114: case 3:
115: includeNewLine = ((Boolean) args[2]).booleanValue();
116: case 2:
117: try {
118: LineHandler handler = getLineHandler(args[1], context);
119: lineReader = getLineProcessor(arg0, handler, context);
120: return new Integer(lineReader
121: .processAll(includeNewLine));
122: } catch (IOException e) {
123: throw new PnutsException(e, context);
124: }
125: case 1:
126: final boolean newline = includeNewLine;
127: return new Generator() {
128: public Object apply(final PnutsFunction closure,
129: final Context context) {
130: LineProcessor lineReader = null;
131: try {
132: lineReader = getLineProcessor(arg0,
133: getLineHandler(new PnutsFunction() {
134: protected Object exec(
135: Object[] args, Context c) {
136: closure.call(args, context);
137: return null;
138: }
139: }, context), context);
140: lineReader.processAll(newline);
141: } catch (IOException e) {
142: throw new PnutsException(e, context);
143: }
144: return null;
145: }
146: };
147: default:
148: undefined(args, context);
149: return null;
150: }
151: }
152:
153: public String toString() {
154: return "function scanLines((inputStream|reader|file|fileName|url) {, (func(line) | collection) {, includeNewLine }})";
155: }
156: }
|