1 | /* |
2 | * $Id: ExamplesXmlTask.java,v 1.2 2005/08/05 20:23:02 adamp Exp $ |
3 | * |
4 | * (c) Copyright, Moebius Solutions, Inc., 2004 |
5 | * |
6 | * All Rights Reserved |
7 | * |
8 | * This material may be reproduced by or for the U. S. Government |
9 | * pursuant to the copyright license under the clause at |
10 | * DFARS 252.227-7014 (OCT 2001). |
11 | */ |
12 | package com.moesol.doc; |
13 | |
14 | import java.io.File; |
15 | import java.io.FileOutputStream; |
16 | import java.io.IOException; |
17 | import java.io.OutputStream; |
18 | import java.util.ArrayList; |
19 | import java.util.List; |
20 | |
21 | import org.apache.tools.ant.BuildException; |
22 | import org.apache.tools.ant.DirectoryScanner; |
23 | import org.apache.tools.ant.Task; |
24 | import org.apache.tools.ant.types.FileSet; |
25 | |
26 | /** |
27 | * @author Hastings |
28 | */ |
29 | public class ExamplesXmlTask extends Task { |
30 | private List filesets = new ArrayList(); |
31 | private File m_toFile; |
32 | private ExamplesXmlBuilder m_builder = new ExamplesXmlBuilder(); |
33 | |
34 | public void addFileset(FileSet set) { |
35 | filesets.add(set); |
36 | } |
37 | public void setTofile(File f) { |
38 | m_toFile = f; |
39 | } |
40 | public void execute() throws BuildException { |
41 | validateAttributes(); |
42 | if (!needsGeneration()) { |
43 | return; |
44 | } |
45 | |
46 | try { |
47 | FileOutputStream tofile = new FileOutputStream(m_toFile); |
48 | try { |
49 | m_builder.setOut(tofile); |
50 | m_builder.startRoot(); |
51 | doFilesets(tofile); |
52 | } finally { |
53 | m_builder.endRoot(); |
54 | m_builder.setOut(null); |
55 | tofile.close(); |
56 | } |
57 | } catch (IOException e) { |
58 | throw new BuildException("Failed to xml'ize", e, getLocation()); |
59 | } |
60 | } |
61 | |
62 | protected void validateAttributes() throws BuildException { |
63 | if (filesets.size() == 0) { |
64 | throw new BuildException("Specify at least one source "); |
65 | } |
66 | if (m_toFile == null) { |
67 | throw new BuildException("Specify a destination"); |
68 | } |
69 | } |
70 | private boolean needsGeneration() { |
71 | for (int i = 0; i < filesets.size(); i++) { |
72 | FileSet fs = (FileSet)filesets.get(i); |
73 | DirectoryScanner ds = fs.getDirectoryScanner(getProject()); |
74 | ds.scan(); |
75 | if (needsGeneration(fs.getDir(getProject()), ds.getIncludedFiles())) { |
76 | return true; |
77 | } |
78 | } |
79 | return false; |
80 | } |
81 | private boolean needsGeneration(File dir, String[] includedFiles) { |
82 | for (int i = 0; i < includedFiles.length; i++) { |
83 | File full_path = new File(dir, includedFiles[i]); |
84 | if (needsGeneration(full_path)) { |
85 | return true; |
86 | } |
87 | } |
88 | return false; |
89 | } |
90 | private boolean needsGeneration(File full_path) { |
91 | if ( !m_toFile.exists() |
92 | || m_toFile.lastModified() < full_path.lastModified()) { |
93 | return true; |
94 | } |
95 | return false; |
96 | } |
97 | |
98 | private void doFilesets(OutputStream tofile) throws IOException { |
99 | for (int i = 0; i < filesets.size(); i++) { |
100 | FileSet fs = (FileSet)filesets.get(i); |
101 | DirectoryScanner ds = fs.getDirectoryScanner(getProject()); |
102 | ds.scan(); |
103 | doFiles(tofile, fs.getDir(getProject()), ds.getIncludedFiles()); |
104 | } |
105 | } |
106 | |
107 | private void doFiles(OutputStream tofile, File dir, String[] files) throws IOException { |
108 | m_builder.setDir(dir); |
109 | m_builder.doFiles(files); |
110 | } |
111 | } |