Fields Summary |
---|
private static final int | BUFFER_SIZE |
private static final org.apache.tools.ant.util.FileUtils | FILE_UTILS |
private static final org.apache.tools.ant.types.resources.selectors.ResourceSelector | EXISTS |
private static final org.apache.tools.ant.types.resources.selectors.ResourceSelector | NOT_EXISTS |
private File | destinationFileThe destination of the stream. If null , the system
console is used. |
private boolean | appendWhether or not the stream should be appended if the destination file
exists.
Defaults to false . |
private String | encodingStores the input file encoding. |
private String | outputEncodingStores the output file encoding. |
private boolean | binaryStores the binary attribute |
private StringBuffer | textBufferThis buffer stores the text within the 'concat' element. |
private org.apache.tools.ant.types.resources.Resources | rcStores a collection of file sets and/or file lists, used to
select multiple files for concatenation. |
private Vector | filterChainsfor filtering the concatenated |
private boolean | forceOverwriteignore dates on input files |
private TextElement | footerString to place at the start of the concatented stream |
private TextElement | headerString to place at the end of the concatented stream |
private boolean | fixLastLineadd missing line.separator to files |
private String | eolStringendofline for fixlast line |
private Writer | outputWriteroutputwriter |
Methods Summary |
---|
public void | add(org.apache.tools.ant.types.ResourceCollection c)Add an arbitrary ResourceCollection.
rc = rc == null ? new Resources() : rc;
rc.add(c);
|
public void | addFilelist(org.apache.tools.ant.types.FileList list)List of files to concatenate.
add(list);
|
public void | addFileset(org.apache.tools.ant.types.FileSet set)Set of files to concatenate.
add(set);
|
public void | addFilterChain(org.apache.tools.ant.types.FilterChain filterChain)Adds a FilterChain.
if (filterChains == null) {
filterChains = new Vector();
}
filterChains.addElement(filterChain);
|
public void | addFooter(org.apache.tools.ant.taskdefs.Concat$TextElement footerToAdd)Add a footer to the concatenated output
this.footer = footerToAdd;
|
public void | addHeader(org.apache.tools.ant.taskdefs.Concat$TextElement headerToAdd)Add a header to the concatenated output
this.header = headerToAdd;
|
public void | addText(java.lang.String text)This method adds text which appears in the 'concat' element.
if (textBuffer == null) {
// Initialize to the size of the first text fragment, with
// the hopes that it's the only one.
textBuffer = new StringBuffer(text.length());
}
// Append the fragment -- we defer property replacement until
// later just in case we get a partial property in a fragment.
textBuffer.append(text);
|
private void | binaryCat(org.apache.tools.ant.types.ResourceCollection c)perform the binary concatenation
log("Binary concatenation of " + c.size()
+ " resources to " + destinationFile);
FileOutputStream out = null;
InputStream in = null;
try {
try {
out = new FileOutputStream(destinationFile);
} catch (Exception t) {
throw new BuildException("Unable to open "
+ destinationFile + " for writing", t);
}
in = new ConcatResourceInputStream(c);
((ConcatResourceInputStream) in).setManagingComponent(this);
Thread t = new Thread(new StreamPumper(in, out));
t.start();
try {
t.join();
} catch (InterruptedException e) {
try {
t.join();
} catch (InterruptedException ee) {
// Empty
}
}
} finally {
FileUtils.close(in);
if (out != null) {
try {
out.close();
} catch (Exception ex) {
throw new BuildException(
"Unable to close " + destinationFile, ex);
}
}
}
|
private void | cat(org.apache.tools.ant.types.ResourceCollection c)perform the concatenation
OutputStream os = null;
char[] buffer = new char[BUFFER_SIZE];
try {
PrintWriter writer = null;
if (outputWriter != null) {
writer = new PrintWriter(outputWriter);
} else {
if (destinationFile == null) {
// Log using WARN so it displays in 'quiet' mode.
os = new LogOutputStream(this, Project.MSG_WARN);
} else {
// ensure that the parent dir of dest file exists
File parent = destinationFile.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
os = new FileOutputStream(destinationFile.getAbsolutePath(),
append);
}
if (outputEncoding == null) {
writer = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(os)));
} else {
writer = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(os, outputEncoding)));
}
}
if (header != null) {
if (header.getFiltering()) {
concatenate(
buffer, writer, new StringReader(header.getValue()));
} else {
writer.print(header.getValue());
}
}
if (c.size() > 0) {
concatenate(buffer, writer, new MultiReader(c));
}
if (footer != null) {
if (footer.getFiltering()) {
concatenate(
buffer, writer, new StringReader(footer.getValue()));
} else {
writer.print(footer.getValue());
}
}
writer.flush();
if (os != null) {
os.flush();
}
} catch (IOException ioex) {
throw new BuildException("Error while concatenating: "
+ ioex.getMessage(), ioex);
} finally {
FileUtils.close(os);
}
|
private void | concatenate(char[] buffer, java.io.Writer writer, java.io.Reader in)Concatenate a single reader to the writer using buffer
if (filterChains != null) {
ChainReaderHelper helper = new ChainReaderHelper();
helper.setBufferSize(BUFFER_SIZE);
helper.setPrimaryReader(in);
helper.setFilterChains(filterChains);
helper.setProject(getProject());
in = new BufferedReader(helper.getAssembledReader());
}
while (true) {
int nRead = in.read(buffer, 0, buffer.length);
if (nRead == -1) {
break;
}
writer.write(buffer, 0, nRead);
}
writer.flush();
|
public org.apache.tools.ant.types.Path | createPath()Path of files to concatenate.
Path path = new Path(getProject());
add(path);
return path;
|
public void | execute()Execute the concat task.
ResourceCollection c = validate();
if (c == null) {
return;
}
// Do nothing if no resources (including nested text)
if (c.size() < 1 && header == null && footer == null) {
log("No existing resources and no nested text, doing nothing",
Project.MSG_INFO);
return;
}
if (binary) {
binaryCat(c);
} else {
cat(c);
}
|
public void | reset()Reset state to default.
append = false;
forceOverwrite = true;
destinationFile = null;
encoding = null;
outputEncoding = null;
fixLastLine = false;
filterChains = null;
footer = null;
header = null;
binary = false;
outputWriter = null;
textBuffer = null;
eolString = System.getProperty("line.separator");
rc = null;
|
private void | sanitizeText()Treat empty nested text as no text.
Depending on the XML parser, addText may have been called
for "ignorable whitespace" as well.
if (textBuffer != null) {
if (textBuffer.substring(0).trim().length() == 0) {
textBuffer = null;
}
}
|
public void | setAppend(boolean append)Sets the behavior when the destination file exists. If set to
true the stream data will be appended to the
existing file, otherwise the existing file will be
overwritten. Defaults to false .
this.append = append;
|
public void | setBinary(boolean binary)Set the binary attribute. If true, concat will concatenate the files
byte for byte. This mode does not allow any filtering or other
modifications to the input streams. The default value is false.
this.binary = binary;
|
public void | setDestfile(java.io.File destinationFile)Sets the destination file, or uses the console if not specified.
this.destinationFile = destinationFile;
|
public void | setEncoding(java.lang.String encoding)Sets the character encoding
this.encoding = encoding;
if (outputEncoding == null) {
outputEncoding = encoding;
}
|
public void | setEol(FixCRLF.CrLf crlf)Specify the end of line to find and to add if
not present at end of each input file. This attribute
is used in conjunction with fixlastline.
String s = crlf.getValue();
if (s.equals("cr") || s.equals("mac")) {
eolString = "\r";
} else if (s.equals("lf") || s.equals("unix")) {
eolString = "\n";
} else if (s.equals("crlf") || s.equals("dos")) {
eolString = "\r\n";
}
|
public void | setFixLastLine(boolean fixLastLine)Append line.separator to files that do not end
with a line.separator, default false.
this.fixLastLine = fixLastLine;
|
public void | setForce(boolean force)Force overwrite existing destination file
this.forceOverwrite = force;
|
public void | setOutputEncoding(java.lang.String outputEncoding)Sets the character encoding for outputting
this.outputEncoding = outputEncoding;
|
public void | setWriter(java.io.Writer outputWriter)Set the output writer. This is to allow
concat to be used as a nested element.
this.outputWriter = outputWriter;
|
private org.apache.tools.ant.types.ResourceCollection | validate()Validate configuration options.
// treat empty nested text as no text
sanitizeText();
// if binary check if incompatible attributes are used
if (binary) {
if (destinationFile == null) {
throw new BuildException(
"destfile attribute is required for binary concatenation");
}
if (textBuffer != null) {
throw new BuildException(
"Nested text is incompatible with binary concatenation");
}
if (encoding != null || outputEncoding != null) {
throw new BuildException(
"Seting input or output encoding is incompatible with binary"
+ " concatenation");
}
if (filterChains != null) {
throw new BuildException(
"Setting filters is incompatible with binary concatenation");
}
if (fixLastLine) {
throw new BuildException(
"Setting fixlastline is incompatible with binary concatenation");
}
if (header != null || footer != null) {
throw new BuildException(
"Nested header or footer is incompatible with binary concatenation");
}
}
if (destinationFile != null && outputWriter != null) {
throw new BuildException(
"Cannot specify both a destination file and an output writer");
}
// Sanity check our inputs.
if (rc == null && textBuffer == null) {
// Nothing to concatenate!
throw new BuildException(
"At least one resource must be provided, or some text.");
}
if (rc != null) {
// If using resources, disallow inline text. This is similar to
// using GNU 'cat' with file arguments -- stdin is simply
// ignored.
if (textBuffer != null) {
throw new BuildException(
"Cannot include inline text when using resources.");
}
Restrict noexistRc = new Restrict();
noexistRc.add(NOT_EXISTS);
noexistRc.add(rc);
for (Iterator i = noexistRc.iterator(); i.hasNext();) {
log(i.next() + " does not exist.", Project.MSG_ERR);
}
if (destinationFile != null) {
for (Iterator i = rc.iterator(); i.hasNext();) {
Object o = i.next();
if (o instanceof FileResource) {
File f = ((FileResource) o).getFile();
if (FILE_UTILS.fileNameEquals(f, destinationFile)) {
throw new BuildException("Input file \""
+ f + "\" is the same as the output file.");
}
}
}
}
Restrict existRc = new Restrict();
existRc.add(EXISTS);
existRc.add(rc);
boolean outofdate = destinationFile == null || forceOverwrite;
if (!outofdate) {
for (Iterator i = existRc.iterator(); !outofdate && i.hasNext();) {
Resource r = (Resource) i.next();
outofdate =
(r.getLastModified() == 0L
|| r.getLastModified() > destinationFile.lastModified());
}
}
if (!outofdate) {
log(destinationFile + " is up-to-date.", Project.MSG_VERBOSE);
return null; // no need to do anything
}
return existRc;
} else {
StringResource s = new StringResource();
s.setProject(getProject());
s.setValue(textBuffer.toString());
return s;
}
|