001    /**
002     * Copyright 2004-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.maven.common;
017    
018    import java.io.File;
019    import java.io.FileInputStream;
020    import java.io.IOException;
021    import java.io.InputStream;
022    import java.io.OutputStream;
023    import java.util.ArrayList;
024    import java.util.List;
025    
026    import org.apache.commons.io.FileUtils;
027    import org.apache.commons.io.IOUtils;
028    import org.springframework.core.io.DefaultResourceLoader;
029    import org.springframework.core.io.Resource;
030    import org.springframework.core.io.ResourceLoader;
031    
032    public class ResourceUtils {
033        ResourceLoader loader = new DefaultResourceLoader();
034    
035        /**
036         * Given a location that can represent either a file on the file system or a Spring style resource, return an
037         * InputStream. The method checks the file system first. If no file exists, it uses Spring resource loading to
038         * obtain an InputStream
039         */
040        public InputStream getInputStream(String location) throws IOException {
041            if (!exists(location)) {
042                throw new IOException("Unable to locate " + location);
043            }
044            File file = new File(location);
045            if (file.exists()) {
046                return new FileInputStream(file);
047            } else {
048                Resource resource = loader.getResource(location);
049                return resource.getInputStream();
050            }
051        }
052    
053        public boolean exists(String location) {
054            File file = new File(location);
055            if (file.exists()) {
056                return true;
057            }
058            Resource resource = loader.getResource(location);
059            return resource.exists();
060        }
061    
062        /**
063         * Copy a URL location to the local file system
064         */
065        public void copy(String location, File file) throws IOException {
066            InputStream in = null;
067            OutputStream out = null;
068            try {
069                in = getInputStream(location);
070                out = FileUtils.openOutputStream(file);
071                IOUtils.copy(in, out);
072            } finally {
073                IOUtils.closeQuietly(in);
074                IOUtils.closeQuietly(out);
075            }
076        }
077    
078        /**
079         * Write the string to the file system
080         */
081        public void write(File file, String contents) throws IOException {
082            OutputStream out = null;
083            try {
084                out = FileUtils.openOutputStream(file);
085                IOUtils.write(contents, out);
086            } finally {
087                IOUtils.closeQuietly(out);
088            }
089    
090        }
091    
092        public List<String> read(List<String> locations) throws IOException {
093            List<String> contents = new ArrayList<String>();
094            for (String location : locations) {
095                String content = read(location);
096                contents.add(content);
097            }
098            return contents;
099        }
100    
101        /**
102         * Read the contents of the URL location into a string
103         */
104        public String read(String location) throws IOException {
105            InputStream in = null;
106            try {
107                in = getInputStream(location);
108                return IOUtils.toString(in);
109            } finally {
110                IOUtils.closeQuietly(in);
111            }
112        }
113    
114        /**
115         * Return a filename for this resource, i.e. typically the last part of the path: for example, "myfile.txt".
116         */
117        public String getFilename(String location) throws IOException {
118            if (!exists(location)) {
119                throw new IOException("Unable to locate " + location);
120            }
121            File file = new File(location);
122            if (file.exists()) {
123                return file.getName();
124            } else {
125                Resource resource = loader.getResource(location);
126                return resource.getFilename();
127            }
128        }
129    }