You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ResourceUtils.java 27 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package org.apache.tools.ant.util;
  19. import java.io.File;
  20. import java.io.Reader;
  21. import java.io.InputStream;
  22. import java.io.IOException;
  23. import java.io.OutputStream;
  24. import java.io.BufferedReader;
  25. import java.io.BufferedWriter;
  26. import java.io.InputStreamReader;
  27. import java.io.OutputStreamWriter;
  28. import java.io.BufferedInputStream;
  29. import java.util.Arrays;
  30. import java.util.Vector;
  31. import java.util.Iterator;
  32. import org.apache.tools.ant.Project;
  33. import org.apache.tools.ant.ProjectComponent;
  34. import org.apache.tools.ant.filters.util.ChainReaderHelper;
  35. import org.apache.tools.ant.types.Resource;
  36. import org.apache.tools.ant.types.TimeComparison;
  37. import org.apache.tools.ant.types.ResourceFactory;
  38. import org.apache.tools.ant.types.ResourceCollection;
  39. import org.apache.tools.ant.types.FilterSetCollection;
  40. import org.apache.tools.ant.types.resources.Appendable;
  41. import org.apache.tools.ant.types.resources.FileProvider;
  42. import org.apache.tools.ant.types.resources.FileResource;
  43. import org.apache.tools.ant.types.resources.Union;
  44. import org.apache.tools.ant.types.resources.Restrict;
  45. import org.apache.tools.ant.types.resources.Resources;
  46. import org.apache.tools.ant.types.resources.Touchable;
  47. import org.apache.tools.ant.types.resources.selectors.Date;
  48. import org.apache.tools.ant.types.resources.selectors.ResourceSelector;
  49. import org.apache.tools.ant.types.selectors.SelectorUtils;
  50. // CheckStyle:HideUtilityClassConstructorCheck OFF - bc
  51. /**
  52. * This class provides utility methods to process Resources.
  53. *
  54. * @since Ant 1.5.2
  55. */
  56. public class ResourceUtils {
  57. /** Utilities used for file operations */
  58. private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
  59. /**
  60. * Tells which source files should be reprocessed based on the
  61. * last modification date of target files.
  62. * @param logTo where to send (more or less) interesting output.
  63. * @param source array of resources bearing relative path and last
  64. * modification date.
  65. * @param mapper filename mapper indicating how to find the target
  66. * files.
  67. * @param targets object able to map as a resource a relative path
  68. * at <b>destination</b>.
  69. * @return array containing the source files which need to be
  70. * copied or processed, because the targets are out of date or do
  71. * not exist.
  72. */
  73. public static Resource[] selectOutOfDateSources(ProjectComponent logTo,
  74. Resource[] source,
  75. FileNameMapper mapper,
  76. ResourceFactory targets) {
  77. return selectOutOfDateSources(logTo, source, mapper, targets,
  78. FILE_UTILS.getFileTimestampGranularity());
  79. }
  80. /**
  81. * Tells which source files should be reprocessed based on the
  82. * last modification date of target files.
  83. * @param logTo where to send (more or less) interesting output.
  84. * @param source array of resources bearing relative path and last
  85. * modification date.
  86. * @param mapper filename mapper indicating how to find the target
  87. * files.
  88. * @param targets object able to map as a resource a relative path
  89. * at <b>destination</b>.
  90. * @param granularity The number of milliseconds leeway to give
  91. * before deciding a target is out of date.
  92. * @return array containing the source files which need to be
  93. * copied or processed, because the targets are out of date or do
  94. * not exist.
  95. * @since Ant 1.6.2
  96. */
  97. public static Resource[] selectOutOfDateSources(ProjectComponent logTo,
  98. Resource[] source,
  99. FileNameMapper mapper,
  100. ResourceFactory targets,
  101. long granularity) {
  102. Union u = new Union();
  103. u.addAll(Arrays.asList(source));
  104. ResourceCollection rc
  105. = selectOutOfDateSources(logTo, u, mapper, targets, granularity);
  106. return rc.size() == 0 ? new Resource[0] : ((Union) rc).listResources();
  107. }
  108. /**
  109. * Tells which sources should be reprocessed based on the
  110. * last modification date of targets.
  111. * @param logTo where to send (more or less) interesting output.
  112. * @param source ResourceCollection.
  113. * @param mapper filename mapper indicating how to find the target Resources.
  114. * @param targets object able to map a relative path as a Resource.
  115. * @param granularity The number of milliseconds leeway to give
  116. * before deciding a target is out of date.
  117. * @return ResourceCollection.
  118. * @since Ant 1.7
  119. */
  120. public static ResourceCollection selectOutOfDateSources(ProjectComponent logTo,
  121. ResourceCollection source,
  122. FileNameMapper mapper,
  123. ResourceFactory targets,
  124. final long granularity) {
  125. if (source.size() == 0) {
  126. logTo.log("No sources found.", Project.MSG_VERBOSE);
  127. return Resources.NONE;
  128. }
  129. source = Union.getInstance(source);
  130. logFuture(logTo, source, granularity);
  131. Union result = new Union();
  132. for (Iterator iter = source.iterator(); iter.hasNext();) {
  133. final Resource sr = (Resource) iter.next();
  134. String srName = sr.getName();
  135. srName = srName == null
  136. ? srName : srName.replace('/', File.separatorChar);
  137. String[] targetnames = null;
  138. try {
  139. targetnames = mapper.mapFileName(srName);
  140. } catch (Exception e) {
  141. logTo.log("Caught " + e + " mapping resource " + sr,
  142. Project.MSG_VERBOSE);
  143. }
  144. if (targetnames == null || targetnames.length == 0) {
  145. logTo.log(sr + " skipped - don\'t know how to handle it",
  146. Project.MSG_VERBOSE);
  147. continue;
  148. }
  149. for (int i = 0; i < targetnames.length; i++) {
  150. if (targetnames[i] == null) {
  151. targetnames[i] = "(no name)";
  152. }
  153. }
  154. Union targetColl = new Union();
  155. for (int i = 0; i < targetnames.length; i++) {
  156. targetColl.add(targets.getResource(
  157. targetnames[i].replace(File.separatorChar, '/')));
  158. }
  159. //find the out-of-date targets:
  160. Restrict r = new Restrict();
  161. r.add(new ResourceSelector() {
  162. public boolean isSelected(Resource target) {
  163. /* Extra I/O, probably wasted:
  164. if (target.isDirectory()) {
  165. return false;
  166. }
  167. */
  168. return SelectorUtils.isOutOfDate(sr, target, granularity);
  169. }
  170. });
  171. r.add(targetColl);
  172. if (r.size() > 0) {
  173. result.add(sr);
  174. Resource t = (Resource) (r.iterator().next());
  175. logTo.log(sr.getName() + " added as " + t.getName()
  176. + (t.isExists() ? " is outdated." : " doesn\'t exist."),
  177. Project.MSG_VERBOSE);
  178. continue;
  179. }
  180. //log uptodateness of all targets:
  181. logTo.log(sr.getName()
  182. + " omitted as " + targetColl.toString()
  183. + (targetColl.size() == 1 ? " is" : " are ")
  184. + " up to date.", Project.MSG_VERBOSE);
  185. }
  186. return result;
  187. }
  188. /**
  189. * Convenience method to copy content from one Resource to another.
  190. * No filtering is performed.
  191. *
  192. * @param source the Resource to copy from.
  193. * Must not be <code>null</code>.
  194. * @param dest the Resource to copy to.
  195. * Must not be <code>null</code>.
  196. *
  197. * @throws IOException if the copying fails.
  198. *
  199. * @since Ant 1.7
  200. */
  201. public static void copyResource(Resource source, Resource dest) throws IOException {
  202. copyResource(source, dest, null);
  203. }
  204. /**
  205. * Convenience method to copy content from one Resource to another.
  206. * No filtering is performed.
  207. *
  208. * @param source the Resource to copy from.
  209. * Must not be <code>null</code>.
  210. * @param dest the Resource to copy to.
  211. * Must not be <code>null</code>.
  212. * @param project the project instance.
  213. *
  214. * @throws IOException if the copying fails.
  215. *
  216. * @since Ant 1.7
  217. */
  218. public static void copyResource(Resource source, Resource dest, Project project)
  219. throws IOException {
  220. copyResource(source, dest, null, null, false,
  221. false, null, null, project);
  222. }
  223. // CheckStyle:ParameterNumberCheck OFF - bc
  224. /**
  225. * Convenience method to copy content from one Resource to another
  226. * specifying whether token filtering must be used, whether filter chains
  227. * must be used, whether newer destination files may be overwritten and
  228. * whether the last modified time of <code>dest</code> file should be made
  229. * equal to the last modified time of <code>source</code>.
  230. *
  231. * @param source the Resource to copy from.
  232. * Must not be <code>null</code>.
  233. * @param dest the Resource to copy to.
  234. * Must not be <code>null</code>.
  235. * @param filters the collection of filters to apply to this copy.
  236. * @param filterChains filterChains to apply during the copy.
  237. * @param overwrite Whether or not the destination Resource should be
  238. * overwritten if it already exists.
  239. * @param preserveLastModified Whether or not the last modified time of
  240. * the destination Resource should be set to that
  241. * of the source.
  242. * @param inputEncoding the encoding used to read the files.
  243. * @param outputEncoding the encoding used to write the files.
  244. * @param project the project instance.
  245. *
  246. * @throws IOException if the copying fails.
  247. *
  248. * @since Ant 1.7
  249. */
  250. public static void copyResource(Resource source, Resource dest,
  251. FilterSetCollection filters, Vector filterChains,
  252. boolean overwrite, boolean preserveLastModified,
  253. String inputEncoding, String outputEncoding,
  254. Project project)
  255. throws IOException {
  256. copyResource(source, dest, filters, filterChains, overwrite, preserveLastModified, false, inputEncoding, outputEncoding, project);
  257. }
  258. // CheckStyle:ParameterNumberCheck OFF - bc
  259. /**
  260. * Convenience method to copy content from one Resource to another
  261. * specifying whether token filtering must be used, whether filter chains
  262. * must be used, whether newer destination files may be overwritten and
  263. * whether the last modified time of <code>dest</code> file should be made
  264. * equal to the last modified time of <code>source</code>.
  265. *
  266. * @param source the Resource to copy from.
  267. * Must not be <code>null</code>.
  268. * @param dest the Resource to copy to.
  269. * Must not be <code>null</code>.
  270. * @param filters the collection of filters to apply to this copy.
  271. * @param filterChains filterChains to apply during the copy.
  272. * @param overwrite Whether or not the destination Resource should be
  273. * overwritten if it already exists.
  274. * @param preserveLastModified Whether or not the last modified time of
  275. * the destination Resource should be set to that
  276. * of the source.
  277. * @param append Whether to append to an Appendable Resource.
  278. * @param inputEncoding the encoding used to read the files.
  279. * @param outputEncoding the encoding used to write the files.
  280. * @param project the project instance.
  281. *
  282. * @throws IOException if the copying fails.
  283. *
  284. * @since Ant 1.8
  285. */
  286. public static void copyResource(Resource source, Resource dest,
  287. FilterSetCollection filters, Vector filterChains,
  288. boolean overwrite, boolean preserveLastModified, boolean append,
  289. String inputEncoding, String outputEncoding,
  290. Project project)
  291. throws IOException {
  292. if (!(overwrite || SelectorUtils.isOutOfDate(source, dest, FileUtils.getFileUtils()
  293. .getFileTimestampGranularity()))) {
  294. return;
  295. }
  296. final boolean filterSetsAvailable = (filters != null
  297. && filters.hasFilters());
  298. final boolean filterChainsAvailable = (filterChains != null
  299. && filterChains.size() > 0);
  300. if (filterSetsAvailable) {
  301. BufferedReader in = null;
  302. BufferedWriter out = null;
  303. try {
  304. InputStreamReader isr = null;
  305. if (inputEncoding == null) {
  306. isr = new InputStreamReader(source.getInputStream());
  307. } else {
  308. isr = new InputStreamReader(source.getInputStream(),
  309. inputEncoding);
  310. }
  311. in = new BufferedReader(isr);
  312. OutputStream os = getOutputStream(dest, append, project);
  313. OutputStreamWriter osw;
  314. if (outputEncoding == null) {
  315. osw = new OutputStreamWriter(os);
  316. } else {
  317. osw = new OutputStreamWriter(os, outputEncoding);
  318. }
  319. out = new BufferedWriter(osw);
  320. if (filterChainsAvailable) {
  321. ChainReaderHelper crh = new ChainReaderHelper();
  322. crh.setBufferSize(FileUtils.BUF_SIZE);
  323. crh.setPrimaryReader(in);
  324. crh.setFilterChains(filterChains);
  325. crh.setProject(project);
  326. Reader rdr = crh.getAssembledReader();
  327. in = new BufferedReader(rdr);
  328. }
  329. LineTokenizer lineTokenizer = new LineTokenizer();
  330. lineTokenizer.setIncludeDelims(true);
  331. String newline = null;
  332. String line = lineTokenizer.getToken(in);
  333. while (line != null) {
  334. if (line.length() == 0) {
  335. // this should not happen, because the lines are
  336. // returned with the end of line delimiter
  337. out.newLine();
  338. } else {
  339. newline = filters.replaceTokens(line);
  340. out.write(newline);
  341. }
  342. line = lineTokenizer.getToken(in);
  343. }
  344. } finally {
  345. FileUtils.close(out);
  346. FileUtils.close(in);
  347. }
  348. } else if (filterChainsAvailable
  349. || (inputEncoding != null
  350. && !inputEncoding.equals(outputEncoding))
  351. || (inputEncoding == null && outputEncoding != null)) {
  352. BufferedReader in = null;
  353. BufferedWriter out = null;
  354. try {
  355. InputStreamReader isr = null;
  356. if (inputEncoding == null) {
  357. isr = new InputStreamReader(source.getInputStream());
  358. } else {
  359. isr = new InputStreamReader(source.getInputStream(),
  360. inputEncoding);
  361. }
  362. in = new BufferedReader(isr);
  363. OutputStream os = getOutputStream(dest, append, project);
  364. OutputStreamWriter osw;
  365. if (outputEncoding == null) {
  366. osw = new OutputStreamWriter(os);
  367. } else {
  368. osw = new OutputStreamWriter(os, outputEncoding);
  369. }
  370. out = new BufferedWriter(osw);
  371. if (filterChainsAvailable) {
  372. ChainReaderHelper crh = new ChainReaderHelper();
  373. crh.setBufferSize(FileUtils.BUF_SIZE);
  374. crh.setPrimaryReader(in);
  375. crh.setFilterChains(filterChains);
  376. crh.setProject(project);
  377. Reader rdr = crh.getAssembledReader();
  378. in = new BufferedReader(rdr);
  379. }
  380. char[] buffer = new char[FileUtils.BUF_SIZE];
  381. while (true) {
  382. int nRead = in.read(buffer, 0, buffer.length);
  383. if (nRead == -1) {
  384. break;
  385. }
  386. out.write(buffer, 0, nRead);
  387. }
  388. } finally {
  389. FileUtils.close(out);
  390. FileUtils.close(in);
  391. }
  392. } else {
  393. InputStream in = null;
  394. OutputStream out = null;
  395. try {
  396. in = source.getInputStream();
  397. out = getOutputStream(dest, append, project);
  398. byte[] buffer = new byte[FileUtils.BUF_SIZE];
  399. int count = 0;
  400. do {
  401. out.write(buffer, 0, count);
  402. count = in.read(buffer, 0, buffer.length);
  403. } while (count != -1);
  404. } finally {
  405. FileUtils.close(out);
  406. FileUtils.close(in);
  407. }
  408. }
  409. if (preserveLastModified) {
  410. Touchable t = (Touchable) dest.as(Touchable.class);
  411. if (t != null) {
  412. setLastModified(t, source.getLastModified());
  413. }
  414. }
  415. }
  416. // CheckStyle:ParameterNumberCheck ON
  417. /**
  418. * Set the last modified time of an object implementing
  419. * org.apache.tools.ant.types.resources.Touchable .
  420. *
  421. * @param t the Touchable whose modified time is to be set.
  422. * @param time the time to which the last modified time is to be set.
  423. * if this is -1, the current time is used.
  424. * @since Ant 1.7
  425. */
  426. public static void setLastModified(Touchable t, long time) {
  427. t.touch((time < 0) ? System.currentTimeMillis() : time);
  428. }
  429. /**
  430. * Compares the contents of two Resources.
  431. *
  432. * @param r1 the Resource whose content is to be compared.
  433. * @param r2 the other Resource whose content is to be compared.
  434. * @param text true if the content is to be treated as text and
  435. * differences in kind of line break are to be ignored.
  436. *
  437. * @return true if the content of the Resources is the same.
  438. *
  439. * @throws IOException if the Resources cannot be read.
  440. * @since Ant 1.7
  441. */
  442. public static boolean contentEquals(Resource r1, Resource r2, boolean text) throws IOException {
  443. if (r1.isExists() != r2.isExists()) {
  444. return false;
  445. }
  446. if (!r1.isExists()) {
  447. // two not existing files are equal
  448. return true;
  449. }
  450. // should the following two be switched? If r1 and r2 refer to the same file,
  451. // isn't their content equal regardless of whether that file is a directory?
  452. if (r1.isDirectory() || r2.isDirectory()) {
  453. // don't want to compare directory contents for now
  454. return false;
  455. }
  456. if (r1.equals(r2)) {
  457. return true;
  458. }
  459. if (!text) {
  460. long s1 = r1.getSize();
  461. long s2 = r2.getSize();
  462. if (s1 != Resource.UNKNOWN_SIZE && s2 != Resource.UNKNOWN_SIZE
  463. && s1 != s2) {
  464. return false;
  465. }
  466. }
  467. return compareContent(r1, r2, text) == 0;
  468. }
  469. /**
  470. * Compare the content of two Resources. A nonexistent Resource's
  471. * content is "less than" that of an existing Resource; a directory-type
  472. * Resource's content is "less than" that of a file-type Resource.
  473. * @param r1 the Resource whose content is to be compared.
  474. * @param r2 the other Resource whose content is to be compared.
  475. * @param text true if the content is to be treated as text and
  476. * differences in kind of line break are to be ignored.
  477. * @return a negative integer, zero, or a positive integer as the first
  478. * argument is less than, equal to, or greater than the second.
  479. * @throws IOException if the Resources cannot be read.
  480. * @since Ant 1.7
  481. */
  482. public static int compareContent(Resource r1, Resource r2, boolean text) throws IOException {
  483. if (r1.equals(r2)) {
  484. return 0;
  485. }
  486. boolean e1 = r1.isExists();
  487. boolean e2 = r2.isExists();
  488. if (!(e1 || e2)) {
  489. return 0;
  490. }
  491. if (e1 != e2) {
  492. return e1 ? 1 : -1;
  493. }
  494. boolean d1 = r1.isDirectory();
  495. boolean d2 = r2.isDirectory();
  496. if (d1 && d2) {
  497. return 0;
  498. }
  499. if (d1 || d2) {
  500. return d1 ? -1 : 1;
  501. }
  502. return text ? textCompare(r1, r2) : binaryCompare(r1, r2);
  503. }
  504. /**
  505. * Convenience method to turn any fileProvider into a basic
  506. * FileResource with the file's immediate parent as the basedir,
  507. * for tasks that need one.
  508. * @param fileProvider input
  509. * @return fileProvider if it is a FileResource instance, or a new
  510. * FileResource with fileProvider's file.
  511. * @since Ant 1.8
  512. */
  513. public static FileResource asFileResource(FileProvider fileProvider) {
  514. if (fileProvider instanceof FileResource || fileProvider == null) {
  515. return (FileResource) fileProvider;
  516. }
  517. FileResource result = new FileResource(fileProvider.getFile());
  518. result.setProject(Project.getProject(fileProvider));
  519. return result;
  520. }
  521. /**
  522. * Binary compares the contents of two Resources.
  523. * <p>
  524. * simple but sub-optimal comparision algorithm. written for working
  525. * rather than fast. Better would be a block read into buffers followed
  526. * by long comparisions apart from the final 1-7 bytes.
  527. * </p>
  528. *
  529. * @param r1 the Resource whose content is to be compared.
  530. * @param r2 the other Resource whose content is to be compared.
  531. * @return a negative integer, zero, or a positive integer as the first
  532. * argument is less than, equal to, or greater than the second.
  533. * @throws IOException if the Resources cannot be read.
  534. * @since Ant 1.7
  535. */
  536. private static int binaryCompare(Resource r1, Resource r2) throws IOException {
  537. InputStream in1 = null;
  538. InputStream in2 = null;
  539. try {
  540. in1 = new BufferedInputStream(r1.getInputStream());
  541. in2 = new BufferedInputStream(r2.getInputStream());
  542. for (int b1 = in1.read(); b1 != -1; b1 = in1.read()) {
  543. int b2 = in2.read();
  544. if (b1 != b2) {
  545. return b1 > b2 ? 1 : -1;
  546. }
  547. }
  548. return in2.read() == -1 ? 0 : -1;
  549. } finally {
  550. FileUtils.close(in1);
  551. FileUtils.close(in2);
  552. }
  553. }
  554. /**
  555. * Text compares the contents of two Resources.
  556. * Ignores different kinds of line endings.
  557. * @param r1 the Resource whose content is to be compared.
  558. * @param r2 the other Resource whose content is to be compared.
  559. * @return a negative integer, zero, or a positive integer as the first
  560. * argument is less than, equal to, or greater than the second.
  561. * @throws IOException if the Resources cannot be read.
  562. * @since Ant 1.7
  563. */
  564. private static int textCompare(Resource r1, Resource r2) throws IOException {
  565. BufferedReader in1 = null;
  566. BufferedReader in2 = null;
  567. try {
  568. in1 = new BufferedReader(new InputStreamReader(r1.getInputStream()));
  569. in2 = new BufferedReader(new InputStreamReader(r2.getInputStream()));
  570. String expected = in1.readLine();
  571. while (expected != null) {
  572. String actual = in2.readLine();
  573. if (!expected.equals(actual)) {
  574. return expected.compareTo(actual);
  575. }
  576. expected = in1.readLine();
  577. }
  578. return in2.readLine() == null ? 0 : -1;
  579. } finally {
  580. FileUtils.close(in1);
  581. FileUtils.close(in2);
  582. }
  583. }
  584. /**
  585. * Log which Resources (if any) have been modified in the future.
  586. * @param logTo the ProjectComponent to do the logging.
  587. * @param rc the collection of Resources to check.
  588. * @param granularity the timestamp granularity to use.
  589. * @since Ant 1.7
  590. */
  591. private static void logFuture(ProjectComponent logTo,
  592. ResourceCollection rc, long granularity) {
  593. long now = System.currentTimeMillis() + granularity;
  594. Date sel = new Date();
  595. sel.setMillis(now);
  596. sel.setWhen(TimeComparison.AFTER);
  597. Restrict future = new Restrict();
  598. future.add(sel);
  599. future.add(rc);
  600. for (Iterator iter = future.iterator(); iter.hasNext();) {
  601. logTo.log("Warning: " + ((Resource) iter.next()).getName()
  602. + " modified in the future.", Project.MSG_WARN);
  603. }
  604. }
  605. private static OutputStream getOutputStream(Resource resource, boolean append, Project project)
  606. throws IOException {
  607. if (append) {
  608. Appendable a = (Appendable) resource.as(Appendable.class);
  609. if (a != null) {
  610. return a.getAppendOutputStream();
  611. }
  612. project.log("Appendable OutputStream not available for non-appendable resource "
  613. + resource + "; using plain OutputStream", Project.MSG_VERBOSE);
  614. }
  615. return resource.getOutputStream();
  616. }
  617. }