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