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.util.StringBundler;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.util.StringUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    
022    import java.util.LinkedList;
023    
024    /**
025     * @author Raymond Aug??
026     * @author Eduardo Lundgren
027     */
028    public abstract class BaseAggregateContext implements AggregateContext {
029    
030            @Override
031            public String getFullPath(String path) {
032                    String listPath = _generatePathFromList();
033    
034                    return listPath.concat(path);
035            }
036    
037            @Override
038            public String getResourcePath(String path) {
039                    return getFullPath(path);
040            }
041    
042            @Override
043            public String popPath() {
044                    if (_list.isEmpty()) {
045                            return null;
046                    }
047    
048                    return _list.pop();
049            }
050    
051            @Override
052            public void pushPath(String path) {
053                    if (Validator.isNotNull(path)) {
054                            _list.push(path);
055                    }
056            }
057    
058            @Override
059            public String shiftPath() {
060                    if (_list.isEmpty()) {
061                            return null;
062                    }
063    
064                    return _list.removeLast();
065            }
066    
067            @Override
068            public void unshiftPath(String path) {
069                    if (Validator.isNotNull(path)) {
070                            _list.addLast(path);
071                    }
072            }
073    
074            private String _generatePathFromList() {
075                    StringBundler sb = new StringBundler(_list.size());
076    
077                    for (int i = _list.size() - 1; i >= 0; i--) {
078                            String path = _list.get(i);
079    
080                            sb.append(path);
081    
082                            if (!path.endsWith(StringPool.SLASH)) {
083                                    sb.append(StringPool.SLASH);
084                            }
085                    }
086    
087                    return StringUtil.replace(
088                            sb.toString(), StringPool.DOUBLE_SLASH, StringPool.SLASH);
089            }
090    
091            private LinkedList<String> _list = new LinkedList<String>();
092    
093    }