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