001    /**
002     * Copyright (c) 2000-2010 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            public Source resolve(String href, String base) {
039                    try {
040                            String content = null;
041    
042                            int templatePathIndex = href.indexOf(_GET_TEMPLATE_PATH);
043    
044                            if (templatePathIndex >= 0) {
045                                    int templateIdIndex =
046                                            templatePathIndex + _GET_TEMPLATE_PATH.length();
047    
048                                    long groupId = GetterUtil.getLong(_tokens.get("group_id"));
049                                    String templateId =
050                                            href.substring(templateIdIndex, href.length());
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 Map<String, String> _tokens;
074            private String _languageId;
075    
076    }