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 24 kB

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