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.HashUtil;
022    import com.liferay.portal.kernel.util.HttpUtil;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.xsl.XSLURIResolver;
026    
027    import java.io.Externalizable;
028    import java.io.IOException;
029    import java.io.ObjectInput;
030    import java.io.ObjectOutput;
031    
032    import java.util.Map;
033    
034    import javax.xml.transform.Source;
035    import javax.xml.transform.stream.StreamSource;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     */
040    public class JournalXSLURIResolver implements Externalizable, XSLURIResolver {
041    
042            public JournalXSLURIResolver(
043                    Map<String, String> tokens, String languageId) {
044    
045                    if (tokens == null) {
046                            throw new IllegalArgumentException("Tokens map is null");
047                    }
048    
049                    _tokens = tokens;
050                    _languageId = languageId;
051            }
052    
053            @Override
054            public boolean equals(Object obj) {
055                    if (this == obj) {
056                            return true;
057                    }
058    
059                    if (!(obj instanceof JournalXSLURIResolver)) {
060                            return false;
061                    }
062    
063                    JournalXSLURIResolver journalXSLURIResolver =
064                            (JournalXSLURIResolver)obj;
065    
066                    if (Validator.equals(_languageId, journalXSLURIResolver._languageId) &&
067                            _tokens.equals(journalXSLURIResolver._tokens)) {
068    
069                            return true;
070                    }
071    
072                    return false;
073            }
074    
075            @Override
076            public String getLanguageId() {
077                    return _languageId;
078            }
079    
080            @Override
081            public int hashCode() {
082                    int hashCode = HashUtil.hash(0, _languageId);
083    
084                    return HashUtil.hash(hashCode, _tokens);
085            }
086    
087            @Override
088            public void readExternal(ObjectInput objectInput)
089                    throws ClassNotFoundException, IOException {
090    
091                    _languageId = objectInput.readUTF();
092    
093                    if (_languageId.equals(StringPool.BLANK)) {
094                            _languageId = null;
095                    }
096    
097                    _tokens = (Map<String, String>)objectInput.readObject();
098            }
099    
100            @Override
101            public Source resolve(String href, String base) {
102                    try {
103                            String content = null;
104    
105                            int templatePathIndex = href.indexOf(_PATH_GET_TEMPLATE);
106    
107                            if (templatePathIndex >= 0) {
108                                    long articleGroupId = GetterUtil.getLong(
109                                            _tokens.get("article_group_id"));
110    
111                                    int templateIdIndex =
112                                            templatePathIndex + _PATH_GET_TEMPLATE.length();
113    
114                                    String templateId = href.substring(templateIdIndex);
115    
116                                    content = JournalUtil.getTemplateScript(
117                                            articleGroupId, templateId, _tokens, _languageId);
118                            }
119                            else {
120                                    content = HttpUtil.URLtoString(href);
121                            }
122    
123                            return new StreamSource(new UnsyncStringReader(content));
124                    }
125                    catch (Exception e) {
126                            _log.error(href + " does not reference a valid template");
127    
128                            return null;
129                    }
130            }
131    
132            @Override
133            public void writeExternal(ObjectOutput objectOutput) throws IOException {
134                    if (_languageId == null) {
135                            objectOutput.writeUTF(StringPool.BLANK);
136                    }
137                    else {
138                            objectOutput.writeUTF(_languageId);
139                    }
140    
141                    objectOutput.writeObject(_tokens);
142            }
143    
144            private static final String _PATH_GET_TEMPLATE =
145                    "/c/journal/get_template?template_id=";
146    
147            private static Log _log = LogFactoryUtil.getLog(
148                    JournalXSLURIResolver.class);
149    
150            private String _languageId;
151            private Map<String, String> _tokens;
152    
153    }