001    /**
002     * Copyright (c) 2000-2012 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            public String getLanguageId() {
076                    return _languageId;
077            }
078    
079            @Override
080            public int hashCode() {
081                    int hashCode = HashUtil.hash(0, _languageId);
082    
083                    return HashUtil.hash(hashCode, _tokens);
084            }
085    
086            public void readExternal(ObjectInput objectInput)
087                    throws ClassNotFoundException, IOException {
088    
089                    _languageId = objectInput.readUTF();
090    
091                    if (_languageId.equals(StringPool.BLANK)) {
092                            _languageId = null;
093                    }
094    
095                    _tokens = (Map<String, String>)objectInput.readObject();
096            }
097    
098            public Source resolve(String href, String base) {
099                    try {
100                            String content = null;
101    
102                            int templatePathIndex = href.indexOf(_PATH_GET_TEMPLATE);
103    
104                            if (templatePathIndex >= 0) {
105                                    int templateIdIndex =
106                                            templatePathIndex + _PATH_GET_TEMPLATE.length();
107    
108                                    long groupId = GetterUtil.getLong(_tokens.get("group_id"));
109                                    String templateId = href.substring(templateIdIndex);
110    
111                                    content = JournalUtil.getTemplateScript(
112                                            groupId, templateId, _tokens, _languageId);
113                            }
114                            else {
115                                    content = HttpUtil.URLtoString(href);
116                            }
117    
118                            return new StreamSource(new UnsyncStringReader(content));
119                    }
120                    catch (Exception e) {
121                            _log.error(href + " does not reference a valid template");
122    
123                            return null;
124                    }
125            }
126    
127            public void writeExternal(ObjectOutput objectOutput) throws IOException {
128                    if (_languageId == null) {
129                            objectOutput.writeUTF(StringPool.BLANK);
130                    }
131                    else {
132                            objectOutput.writeUTF(_languageId);
133                    }
134    
135                    objectOutput.writeObject(_tokens);
136            }
137    
138            private static final String _PATH_GET_TEMPLATE =
139                    "/c/journal/get_template?template_id=";
140    
141            private static Log _log = LogFactoryUtil.getLog(
142                    JournalXSLURIResolver.class);
143    
144            private String _languageId;
145            private Map<String, String> _tokens;
146    
147    }