001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.servlet.filters.aggregate;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.FileUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    
022    import java.io.File;
023    import java.io.IOException;
024    
025    /**
026     * @author Raymond Augé
027     */
028    public class FileAggregateContext implements AggregateContext {
029    
030            public FileAggregateContext(File file) {
031                    _file = file.getParentFile();
032            }
033    
034            public String getContent(String path) {
035                    try {
036                            File file = new File(_file, path);
037    
038                            return FileUtil.read(file);
039                    }
040                    catch (IOException ioe) {
041                            _log.error(ioe, ioe);
042                    }
043    
044                    return null;
045            }
046    
047            public String getFullPath(String path) {
048                    String absolutePath = _file.getAbsolutePath();
049    
050                    return absolutePath.concat(path);
051            }
052    
053            public void popPath(String path) {
054                    if (Validator.isNotNull(path)) {
055                            _file = _file.getParentFile();
056                    }
057            }
058    
059            public void pushPath(String path) {
060                    if (Validator.isNotNull(path)) {
061                            _file = new File(_file, path);
062                    }
063            }
064    
065            private static Log _log = LogFactoryUtil.getLog(FileAggregateContext.class);
066    
067            private File _file;
068    
069    }