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.portlet.journal.util;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.HttpUtil;
022    
023    import java.util.Map;
024    
025    import javax.xml.transform.Source;
026    import javax.xml.transform.stream.StreamSource;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class URIResolver implements javax.xml.transform.URIResolver {
032    
033            public URIResolver(Map<String, String> tokens, String languageId) {
034                    _tokens = tokens;
035                    _languageId = languageId;
036            }
037    
038            @Override
039            public Source resolve(String href, String base) {
040                    try {
041                            String content = null;
042    
043                            int templatePathIndex = href.indexOf(_GET_TEMPLATE_PATH);
044    
045                            if (templatePathIndex >= 0) {
046                                    int templateIdIndex =
047                                            templatePathIndex + _GET_TEMPLATE_PATH.length();
048    
049                                    long groupId = GetterUtil.getLong(_tokens.get("group_id"));
050                                    String templateId = href.substring(templateIdIndex);
051    
052                                    content = JournalUtil.getTemplateScript(
053                                            groupId, templateId, _tokens, _languageId);
054                            }
055                            else {
056                                    content = HttpUtil.URLtoString(href);
057                            }
058    
059                            return new StreamSource(new UnsyncStringReader(content));
060                    }
061                    catch (Exception e) {
062                            _log.error(href + " does not reference a valid template");
063    
064                            return null;
065                    }
066            }
067    
068            private static final String _GET_TEMPLATE_PATH =
069                    "/c/journal/get_template?template_id=";
070    
071            private static Log _log = LogFactoryUtil.getLog(URIResolver.class);
072    
073            private String _languageId;
074            private Map<String, String> _tokens;
075    
076    }