001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.pdftools;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.file.CmsResourceFilter;
033import org.opencms.file.CmsVfsResourceNotFoundException;
034import org.opencms.i18n.CmsLocaleManager;
035import org.opencms.main.CmsException;
036import org.opencms.main.OpenCms;
037import org.opencms.util.CmsUUID;
038
039import java.util.Locale;
040import java.util.regex.Matcher;
041import java.util.regex.Pattern;
042
043/**
044 * This class is responsbile for creating and parsing links to generated PDFs.<p>
045 */
046public class CmsPdfLink {
047
048    /**
049     * Exception which is thrown when parsing a link as a PDF link fails.<p<
050     */
051    public static class CmsPdfLinkParseException extends Exception {
052
053        /** Serial version id. */
054        private static final long serialVersionUID = 1L;
055
056    }
057
058    /** Group of characters without slashes. */
059    public static final String NOSLASH_GROUP = "([^/]+)";
060
061    /** The prefix string for PDF links. */
062    public static final String PDF_LINK_PREFIX = "pdflink";
063
064    /** Regular expression for parsing PDF links. */
065    public static final String PDF_LINK_REGEX = PDF_LINK_PREFIX
066        + "/"
067        + NOSLASH_GROUP
068        + "/"
069        + "("
070        + CmsUUID.UUID_REGEX
071        + ")"
072        + "/"
073        + NOSLASH_GROUP
074        + "\\.pdf/?";
075
076    /** Compiled regular expression for parsing PDF links. */
077    public static final Pattern PDF_LINK_REGEX_COMPILED = Pattern.compile(PDF_LINK_REGEX);
078
079    /** The content resource of the PDF link. */
080    private CmsResource m_content;
081
082    /** The formatter resource of the PDF link. */
083    private CmsResource m_formatter;
084
085    /** The PDF link. */
086    private String m_link;
087
088    /** The locale of the PDF link. */
089    private Locale m_locale;
090
091    /**
092     * Creates a new PDF link object based on the formatter and content resources and the locale of the current CMS context.<p>
093     *
094     * @param cms the current CMS context
095     * @param formatter the formatter resource
096     * @param content the content resource
097     * @throws CmsException if something goes wrong
098     */
099    public CmsPdfLink(CmsObject cms, CmsResource formatter, CmsResource content)
100    throws CmsException {
101
102        Locale locale = cms.getRequestContext().getLocale();
103        m_content = content;
104        m_locale = locale;
105        String detailName = cms.getDetailName(
106            content,
107            cms.getRequestContext().getLocale(),
108            OpenCms.getLocaleManager().getDefaultLocales());
109        String s = "/" + PDF_LINK_PREFIX + "/" + locale + "/" + formatter.getStructureId() + "/" + detailName + ".pdf";
110        m_link = OpenCms.getLinkManager().substituteLink(cms, s);
111    }
112
113    /**
114     * Creates a PDF link object by parsing it from a link string.<p>
115     *
116     * @param cms the current CMS context
117     * @param link the link as a string
118     *
119     * @throws CmsPdfLinkParseException if the given link is not a PDF link
120     * @throws CmsException if something else goes wrong
121     */
122    public CmsPdfLink(CmsObject cms, String link)
123    throws CmsPdfLinkParseException, CmsException {
124
125        Matcher matcher = PDF_LINK_REGEX_COMPILED.matcher(link);
126        m_link = link;
127        if (matcher.find()) {
128            String localeStr = matcher.group(1);
129            String formatterId = matcher.group(2);
130            String detailName = matcher.group(3);
131            CmsUUID id = cms.readIdForUrlName(detailName);
132            if (id == null) {
133                throw new CmsVfsResourceNotFoundException(
134                    org.opencms.db.generic.Messages.get().container(
135                        org.opencms.db.generic.Messages.ERR_READ_RESOURCE_1,
136                        detailName));
137            }
138            m_content = cms.readResource(id, CmsResourceFilter.ignoreExpirationOffline(cms));
139            m_locale = CmsLocaleManager.getLocale(localeStr);
140            m_formatter = cms.readResource(new CmsUUID(formatterId));
141        } else {
142            throw new CmsPdfLinkParseException();
143        }
144    }
145
146    /**
147     * Returns the content.<p>
148     *
149     * @return the content
150     */
151    public CmsResource getContent() {
152
153        return m_content;
154    }
155
156    /**
157     * Gets the formatter resource.<p>
158     *
159     * @return the formatter resource
160     */
161    public CmsResource getFormatter() {
162
163        return m_formatter;
164    }
165
166    /**
167     * Returns the link.<p>
168     *
169     * @return the link
170     */
171    public String getLink() {
172
173        return m_link;
174    }
175
176    /**
177     * Returns the locale.<p>
178     *
179     * @return the locale
180     */
181    public Locale getLocale() {
182
183        return m_locale;
184    }
185}