001: /*
002: * JavuX - Java Component Set
003: * Copyright (c) 2005-2007 Krzysztof A. Sadlocha
004: * e-mail: ksadlocha@programics.com
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: package com.javujavu.tools.ptdoc;
022:
023: import java.io.ByteArrayInputStream;
024: import java.io.ByteArrayOutputStream;
025: import java.io.FilterReader;
026: import java.io.IOException;
027: import java.io.InputStreamReader;
028: import java.io.Reader;
029: import org.apache.tools.ant.types.Parameterizable;
030: import org.apache.tools.ant.types.Parameter;
031: import com.javujavu.javux.proptree.PropTree;
032: import com.javujavu.javux.proptree.PropTreeDocReader;
033:
034: public class AntPtDocFR extends FilterReader implements Parameterizable {
035: private Parameter[] parameters;
036: private Reader in;
037: private Reader out;
038:
039: public AntPtDocFR(Reader in) {
040: super (in);
041: this .in = in;
042: }
043:
044: public int read() throws IOException {
045: return done().read();
046: }
047:
048: public int read(char cbuf[], int off, int len) throws IOException {
049: return done().read(cbuf, off, len);
050: }
051:
052: public long skip(long n) throws IOException {
053: return done().skip(n);
054: }
055:
056: public boolean ready() throws IOException {
057: return done().ready();
058: }
059:
060: public boolean markSupported() {
061: try {
062: return done().markSupported();
063: } catch (IOException e) {
064: return false;
065: }
066: }
067:
068: public void mark(int readAheadLimit) throws IOException {
069: done().mark(readAheadLimit);
070: }
071:
072: public void reset() throws IOException {
073: done().reset();
074: }
075:
076: public void close() throws IOException {
077: done().close();
078: }
079:
080: private Reader done() throws IOException {
081: if (out == null) {
082: boolean nc = false;
083: int rd = 0;
084: String ao = "= ";
085: boolean ni = false;
086: String in = " ";
087: int lw = 1000;
088: String ex = null;
089: for (int i = 0; i < parameters.length; i++) {
090: String n = parameters[i].getName();
091: String v = parameters[i].getValue();
092: if (n.equals("nc"))
093: nc = true;
094: else if (n.equals("ao"))
095: ao = v;
096: else if (n.equals("rd")) {
097: try {
098: rd = Integer.parseInt(v);
099: } catch (NumberFormatException e) {
100: }
101: } else if (n.equals("ni"))
102: ni = true;
103: else if (n.equals("in"))
104: in = v;
105: else if (n.equals("lw")) {
106: try {
107: lw = Integer.parseInt(v);
108: } catch (NumberFormatException e) {
109: }
110: } else if (n.equals("ex"))
111: ex = v;
112: }
113: StringBuffer sb = new StringBuffer();
114: int c;
115: while ((c = this .in.read()) != -1)
116: sb.append((char) c);
117: this .in.close();
118: ByteArrayInputStream bis = new ByteArrayInputStream(sb
119: .toString().getBytes());
120:
121: PropTreeDocReader r = new PropTreeDocReader();
122: r.setSkipComments(nc);
123: PropTree pt = r.load(bis, null);
124: PTDoc w = new PTDoc();
125: w.setAssignment(ao);
126: w.setRelativeDots(rd);
127: w.setIndent(ni ? "" : in);
128: w.setLine(lw);
129: w.setExeclude(ex);
130: ByteArrayOutputStream bos = new ByteArrayOutputStream();
131: w.save(bos, pt);
132: out = new InputStreamReader(new ByteArrayInputStream(bos
133: .toByteArray()));
134: }
135: return out;
136: }
137:
138: public void setParameters(Parameter[] parameters) {
139: this.parameters = parameters;
140: }
141: }
|