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.portal.xsl;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
018    import com.liferay.portal.kernel.template.TemplateResource;
019    import com.liferay.portal.kernel.util.HashUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    
022    import java.io.IOException;
023    import java.io.ObjectInput;
024    import java.io.ObjectOutput;
025    import java.io.Reader;
026    
027    /**
028     * @author Tina Tian
029     */
030    public class XSLTemplateResource implements TemplateResource {
031    
032            /**
033             * The empty constructor is required by {@link java.io.Externalizable}. Do
034             * not use this for any other purpose.
035             */
036            public XSLTemplateResource() {
037            }
038    
039            public XSLTemplateResource(
040                    String templateId, String xsl, XSLURIResolver xslURIResolver,
041                    String xml) {
042    
043                    if (Validator.isNull(templateId)) {
044                            throw new IllegalArgumentException("Template ID is null");
045                    }
046    
047                    if (Validator.isNull(xsl)) {
048                            throw new IllegalArgumentException("XSL is null");
049                    }
050    
051                    if (Validator.isNull(xml)) {
052                            throw new IllegalArgumentException("XML is null");
053                    }
054    
055                    _templateId = templateId;
056                    _xsl = xsl;
057                    _xslURIResolver = xslURIResolver;
058                    _xml = xml;
059            }
060    
061            @Override
062            public boolean equals(Object obj) {
063                    if (this == obj) {
064                            return true;
065                    }
066    
067                    if (!(obj instanceof XSLTemplateResource)) {
068                            return false;
069                    }
070    
071                    XSLTemplateResource xslTemplateResource = (XSLTemplateResource)obj;
072    
073                    if (_templateId.equals(xslTemplateResource._templateId) &&
074                            _xsl.equals(xslTemplateResource._xsl) &&
075                            Validator.equals(
076                                    _xslURIResolver, xslTemplateResource._xslURIResolver) &&
077                            _xml.equals(xslTemplateResource._xml)) {
078    
079                            return true;
080                    }
081    
082                    return false;
083            }
084    
085            @Override
086            public long getLastModified() {
087                    return _lastModified;
088            }
089    
090            @Override
091            public Reader getReader() {
092                    return new UnsyncStringReader(_xsl);
093            }
094    
095            @Override
096            public String getTemplateId() {
097                    return _templateId;
098            }
099    
100            public Reader getXMLReader() {
101                    return new UnsyncStringReader(_xml);
102            }
103    
104            public XSLURIResolver getXSLURIResolver() {
105                    return _xslURIResolver;
106            }
107    
108            @Override
109            public int hashCode() {
110                    int hashCode = HashUtil.hash(0, _templateId);
111    
112                    hashCode = HashUtil.hash(hashCode, _xsl);
113                    hashCode = HashUtil.hash(hashCode, _xslURIResolver);
114                    hashCode = HashUtil.hash(hashCode, _xml);
115    
116                    return hashCode;
117            }
118    
119            @Override
120            public void readExternal(ObjectInput objectInput)
121                    throws ClassNotFoundException, IOException {
122    
123                    _templateId = objectInput.readUTF();
124                    _lastModified = objectInput.readLong();
125                    _xsl = objectInput.readUTF();
126                    _xslURIResolver = (XSLURIResolver)objectInput.readObject();
127                    _xml = objectInput.readUTF();
128            }
129    
130            @Override
131            public void writeExternal(ObjectOutput objectOutput) throws IOException {
132                    objectOutput.writeUTF(_templateId);
133                    objectOutput.writeLong(_lastModified);
134                    objectOutput.writeUTF(_xsl);
135                    objectOutput.writeObject(_xslURIResolver);
136                    objectOutput.writeUTF(_xml);
137            }
138    
139            private long _lastModified = System.currentTimeMillis();
140            private String _templateId;
141            private String _xml;
142            private String _xsl;
143            private XSLURIResolver _xslURIResolver;
144    
145    }