📄 resourceutils.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */package org.apache.tools.ant.util;import java.io.File;import java.io.Reader;import java.io.InputStream;import java.io.IOException;import java.io.OutputStream;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.BufferedInputStream;import java.util.Arrays;import java.util.Vector;import java.util.Iterator;import org.apache.tools.ant.Project;import org.apache.tools.ant.ProjectComponent;import org.apache.tools.ant.filters.util.ChainReaderHelper;import org.apache.tools.ant.types.Resource;import org.apache.tools.ant.types.TimeComparison;import org.apache.tools.ant.types.ResourceFactory;import org.apache.tools.ant.types.ResourceCollection;import org.apache.tools.ant.types.FilterSetCollection;import org.apache.tools.ant.types.resources.Union;import org.apache.tools.ant.types.resources.Restrict;import org.apache.tools.ant.types.resources.Resources;import org.apache.tools.ant.types.resources.Touchable;import org.apache.tools.ant.types.resources.selectors.Not;import org.apache.tools.ant.types.resources.selectors.Date;import org.apache.tools.ant.types.resources.selectors.Exists;import org.apache.tools.ant.types.resources.selectors.ResourceSelector;import org.apache.tools.ant.types.selectors.SelectorUtils;// CheckStyle:HideUtilityClassConstructorCheck OFF - bc/** * This class provides utility methods to process Resources. * * @since Ant 1.5.2 */public class ResourceUtils { /** Utilities used for file operations */ private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); private static final ResourceSelector NOT_EXISTS = new Not(new Exists()); /** * Tells which source files should be reprocessed based on the * last modification date of target files. * @param logTo where to send (more or less) interesting output. * @param source array of resources bearing relative path and last * modification date. * @param mapper filename mapper indicating how to find the target * files. * @param targets object able to map as a resource a relative path * at <b>destination</b>. * @return array containing the source files which need to be * copied or processed, because the targets are out of date or do * not exist. */ public static Resource[] selectOutOfDateSources(ProjectComponent logTo, Resource[] source, FileNameMapper mapper, ResourceFactory targets) { return selectOutOfDateSources(logTo, source, mapper, targets, FILE_UTILS.getFileTimestampGranularity()); } /** * Tells which source files should be reprocessed based on the * last modification date of target files. * @param logTo where to send (more or less) interesting output. * @param source array of resources bearing relative path and last * modification date. * @param mapper filename mapper indicating how to find the target * files. * @param targets object able to map as a resource a relative path * at <b>destination</b>. * @param granularity The number of milliseconds leeway to give * before deciding a target is out of date. * @return array containing the source files which need to be * copied or processed, because the targets are out of date or do * not exist. * @since Ant 1.6.2 */ public static Resource[] selectOutOfDateSources(ProjectComponent logTo, Resource[] source, FileNameMapper mapper, ResourceFactory targets, long granularity) { Union u = new Union(); u.addAll(Arrays.asList(source)); ResourceCollection rc = selectOutOfDateSources(logTo, u, mapper, targets, granularity); return rc.size() == 0 ? new Resource[0] : ((Union) rc).listResources(); } /** * Tells which sources should be reprocessed based on the * last modification date of targets. * @param logTo where to send (more or less) interesting output. * @param source ResourceCollection. * @param mapper filename mapper indicating how to find the target Resources. * @param targets object able to map a relative path as a Resource. * @param granularity The number of milliseconds leeway to give * before deciding a target is out of date. * @return ResourceCollection. * @since Ant 1.7 */ public static ResourceCollection selectOutOfDateSources(ProjectComponent logTo, ResourceCollection source, FileNameMapper mapper, ResourceFactory targets, final long granularity) { if (source.size() == 0) { logTo.log("No sources found.", Project.MSG_VERBOSE); return Resources.NONE; } source = Union.getInstance(source); logFuture(logTo, source, granularity); Union result = new Union(); for (Iterator iter = source.iterator(); iter.hasNext();) { final Resource sr = (Resource) iter.next(); String srName = sr.getName(); srName = srName == null ? srName : srName.replace('/', File.separatorChar); String[] targetnames = null; try { targetnames = mapper.mapFileName(srName); } catch (Exception e) { logTo.log("Caught " + e + " mapping resource " + sr, Project.MSG_VERBOSE); } if (targetnames == null || targetnames.length == 0) { logTo.log(sr + " skipped - don\'t know how to handle it", Project.MSG_VERBOSE); continue; } Union targetColl = new Union(); for (int i = 0; i < targetnames.length; i++) { targetColl.add(targets.getResource( targetnames[i].replace(File.separatorChar, '/'))); } //find the out-of-date targets: Restrict r = new Restrict(); r.add(new ResourceSelector() { public boolean isSelected(Resource target) { /* Extra I/O, probably wasted: if (target.isDirectory()) { return false; } */ return SelectorUtils.isOutOfDate(sr, target, granularity); } }); r.add(targetColl); if (r.size() > 0) { result.add(sr); Resource t = (Resource) (r.iterator().next()); logTo.log(sr.getName() + " added as " + t.getName() + (t.isExists() ? " is outdated." : " doesn\'t exist."), Project.MSG_VERBOSE); continue; } //log uptodateness of all targets: logTo.log(sr.getName() + " omitted as " + targetColl.toString() + (targetColl.size() == 1 ? " is" : " are ") + " up to date.", Project.MSG_VERBOSE); } return result; } /** * Convenience method to copy content from one Resource to another. * No filtering is performed. * * @param source the Resource to copy from. * Must not be <code>null</code>. * @param dest the Resource to copy to. * Must not be <code>null</code>. * * @throws IOException if the copying fails. * * @since Ant 1.7 */ public static void copyResource(Resource source, Resource dest) throws IOException { copyResource(source, dest, null); } /** * Convenience method to copy content from one Resource to another. * No filtering is performed. * * @param source the Resource to copy from. * Must not be <code>null</code>. * @param dest the Resource to copy to. * Must not be <code>null</code>. * @param project the project instance. * * @throws IOException if the copying fails. * * @since Ant 1.7 */ public static void copyResource(Resource source, Resource dest, Project project) throws IOException { copyResource(source, dest, null, null, false, false, null, null, project); } // CheckStyle:ParameterNumberCheck OFF - bc /** * Convenience method to copy content from one Resource to another * specifying whether token filtering must be used, whether filter chains * must be used, whether newer destination files may be overwritten and * whether the last modified time of <code>dest</code> file should be made * equal to the last modified time of <code>source</code>. * * @param source the Resource to copy from. * Must not be <code>null</code>. * @param dest the Resource to copy to. * Must not be <code>null</code>. * @param filters the collection of filters to apply to this copy. * @param filterChains filterChains to apply during the copy. * @param overwrite Whether or not the destination Resource should be * overwritten if it already exists. * @param preserveLastModified Whether or not the last modified time of * the destination Resource should be set to that * of the source. * @param inputEncoding the encoding used to read the files. * @param outputEncoding the encoding used to write the files. * @param project the project instance. * * @throws IOException if the copying fails. * * @since Ant 1.7 */ public static void copyResource(Resource source, Resource dest, FilterSetCollection filters, Vector filterChains, boolean overwrite, boolean preserveLastModified, String inputEncoding, String outputEncoding, Project project) throws IOException { if (!overwrite) { long slm = source.getLastModified(); if (dest.isExists() && slm != 0 && dest.getLastModified() > slm) { return; } } final boolean filterSetsAvailable = (filters != null && filters.hasFilters()); final boolean filterChainsAvailable = (filterChains != null && filterChains.size() > 0); if (filterSetsAvailable) { BufferedReader in = null; BufferedWriter out = null; try { InputStreamReader isr = null; if (inputEncoding == null) { isr = new InputStreamReader(source.getInputStream()); } else { isr = new InputStreamReader(source.getInputStream(), inputEncoding); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -