net.sourceforge.pldoc.SourceCodeScraper Maven / Gradle / Ivy
package net.sourceforge.pldoc;
import java.io.*;
/**
* Siphon off the source code in the Reader and write out as XML to a Writer.
*
*This makes database source code available for subsequent display or processing.
*
*
*
*
*line 1 text
*line 2 text
*line text
*
%
*
*
*
*
* Cribbed fron {@link apache.commons.io.input.TeeInputSteam}
*/
public class SourceCodeScraper extends FilterReader
{
/** Additional destination for Read contents.
*/
private PrintWriter writer;
/** Close sink when this Reader closes.
*/
private boolean autoclose;
/* Clone the content of the Reader into the Writer as XML, optionally adding an XSLT stylesheet reference.
*
* @param reader the source code to be cloned
* @param writer the source code to be cloned
* @param autoclose Should the Writer be closed when the Reader closes?
* @param xsltHref Optional location of an XSLT stylesheet to view the source code.
*/
public SourceCodeScraper (LineNumberReader reader, PrintWriter writer, boolean autoclose, String xsltHref)
throws IOException
{
super(reader);
this.writer = writer;
this.autoclose = autoclose;
this.writer.println("");
if ( null != xsltHref )
{
this.writer.println(String.format("", xsltHref ) );
}
this.writer.println("");
}
/* Clone the content of the Reader into the Writer as XML, optionally adding an XSLT stylesheet reference.
*
* @param reader the source code to be cloned
* @param writer the source code to be cloned
* @param autoclose Should the Writer be closed when the Reader closes?
* @param xsltHref Optional location of an XSLT stylesheet to view the source code.
*/
public SourceCodeScraper (Reader reader, Writer writer, boolean autoclose, String xsltHref)
throws IOException
{
this(new LineNumberReader(reader), new PrintWriter(writer), autoclose, xsltHref );
}
/* Clone the content of the Reader into the Writer as XML, optionally adding an XSLT stylesheet reference.
*
* @param reader the source code to be cloned
* @param writer the source code to be cloned
* @param autoclose Should the Writer be closed when the Reader closes?
* @param xsltFile Optional location of an XSLT stylesheet to view the source code.
*/
public SourceCodeScraper (LineNumberReader reader, PrintWriter writer, boolean autoclose, File xsltFile)
throws IOException
{
this(reader, writer, autoclose, (null == xsltFile ) ? null : xsltFile.getCanonicalPath() );
}
/* Clone the content of the Reader into the Writer as XML, optionally adding an XSLT stylesheet reference.
*
* @param reader the source code to be cloned
* @param writer the source code to be cloned
* @param autoclose Should the Writer be closed when the Reader closes?
* @param xsltFile Optional location of an XSLT stylesheet to view the source code.
*/
public SourceCodeScraper (Reader reader, Writer writer, boolean autoclose, File xsltFile)
throws IOException
{
this(new LineNumberReader(reader), new PrintWriter(writer), autoclose, xsltFile);
}
/* Clone the content of the Reader into the Writer as XML, optionally adding an XSLT stylesheet reference.
*
* @param reader the source code to be cloned
* @param writer the source code to be cloned
* @param xsltFile Optional location of an XSLT stylesheet to view the source code.
*/
public SourceCodeScraper (LineNumberReader reader, PrintWriter writer, File xsltFile)
throws IOException
{
this(reader, writer,false, xsltFile);
}
/* Clone the content of the Reader into the Writer as XML, optionally adding an XSLT stylesheet reference.
*
* @param reader the source code to be cloned
* @param writer the source code to be cloned
* @param xsltFile Optional location of an XSLT stylesheet to view the source code.
*/
public SourceCodeScraper (Reader reader, Writer writer, File xsltFile)
throws IOException
{
this(new LineNumberReader(reader), new PrintWriter(writer),false, xsltFile);
}
public void close()
throws IOException
{
super.close();
writer.println("]]>\n ");
if (autoclose)
{
writer.flush();
writer.close();
}
}
public int read()
throws IOException
{
int c = super.read();
if ( -1 == c )
{
if (autoclose)
{
writer.println("]]>\n");
writer.close();
}
}
else
{
if ( '\n' == c )
{
writer.print("]]>");
}
writer.write(c);
if ( '\n' == c )
{
writer.print(String.format(" \n");
writer.flush();
writer.close();
}
}
else
{
StringBuilder stringBuilder = new StringBuilder();
for (int offset = 0 ; offset < charactersRead ; offset++)
{
if ('\n' == cbuf[offset] )
{
if ( lineNumber > 0 )
{
stringBuilder.append("]]>");
}
lineNumber++;
stringBuilder.append(cbuf[offset]);
stringBuilder.append(String.format(" \n");
writer.flush();
writer.close();
}
}
else
{
//writer.write(cbuf,off,charactersRead);
StringBuilder stringBuilder = new StringBuilder();
for (int offset = 0 ; offset < charactersRead ; offset++)
{
if ('\n' == cbuf[offset])
{
if ( lineNumber > 0 )
{
stringBuilder.append("]]>");
}
lineNumber++;
stringBuilder.append(cbuf[offset]);
stringBuilder.append(String.format(" \n");
writer.close();
}
}
else
{
writer.println(String.format(""
,((LineNumberReader) in).getLineNumber()
,line
)
);
}
return line;
}
}