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.i18n.CmsLocaleManager; 034import org.opencms.main.CmsException; 035import org.opencms.main.OpenCms; 036import org.opencms.util.CmsUUID; 037 038import java.util.Locale; 039import java.util.regex.Matcher; 040import java.util.regex.Pattern; 041 042/** 043 * This class is responsbile for creating and parsing links to generated PDFs.<p> 044 */ 045public class CmsPdfLink { 046 047 /** 048 * Exception which is thrown when parsing a link as a PDF link fails.<p< 049 */ 050 public static class CmsPdfLinkParseException extends Exception { 051 052 /** Serial version id. */ 053 private static final long serialVersionUID = 1L; 054 055 } 056 057 /** Group of characters without slashes. */ 058 public static final String NOSLASH_GROUP = "([^/]+)"; 059 060 /** The prefix string for PDF links. */ 061 public static final String PDF_LINK_PREFIX = "pdflink"; 062 063 /** Regular expression for parsing PDF links. */ 064 public static final String PDF_LINK_REGEX = PDF_LINK_PREFIX 065 + "/" 066 + NOSLASH_GROUP 067 + "/" 068 + "(" 069 + CmsUUID.UUID_REGEX 070 + ")" 071 + "/" 072 + NOSLASH_GROUP 073 + "\\.pdf/?"; 074 075 /** Compiled regular expression for parsing PDF links. */ 076 public static final Pattern PDF_LINK_REGEX_COMPILED = Pattern.compile(PDF_LINK_REGEX); 077 078 /** The content resource of the PDF link. */ 079 private CmsResource m_content; 080 081 /** The formatter resource of the PDF link. */ 082 private CmsResource m_formatter; 083 084 /** The PDF link. */ 085 private String m_link; 086 087 /** The locale of the PDF link. */ 088 private Locale m_locale; 089 090 /** 091 * Creates a new PDF link object based on the formatter and content resources and the locale of the current CMS context.<p> 092 * 093 * @param cms the current CMS context 094 * @param formatter the formatter resource 095 * @param content the content resource 096 * @throws CmsException if something goes wrong 097 */ 098 public CmsPdfLink(CmsObject cms, CmsResource formatter, CmsResource content) 099 throws CmsException { 100 101 Locale locale = cms.getRequestContext().getLocale(); 102 m_content = content; 103 m_locale = locale; 104 String detailName = cms.getDetailName( 105 content, 106 cms.getRequestContext().getLocale(), 107 OpenCms.getLocaleManager().getDefaultLocales()); 108 String s = "/" + PDF_LINK_PREFIX + "/" + locale + "/" + formatter.getStructureId() + "/" + detailName + ".pdf"; 109 m_link = OpenCms.getLinkManager().substituteLink(cms, s); 110 } 111 112 /** 113 * Creates a PDF link object by parsing it from a link string.<p> 114 * 115 * @param cms the current CMS context 116 * @param link the link as a string 117 * 118 * @throws CmsPdfLinkParseException if the given link is not a PDF link 119 * @throws CmsException if something else goes wrong 120 */ 121 public CmsPdfLink(CmsObject cms, String link) 122 throws CmsPdfLinkParseException, CmsException { 123 124 Matcher matcher = PDF_LINK_REGEX_COMPILED.matcher(link); 125 m_link = link; 126 if (matcher.find()) { 127 String localeStr = matcher.group(1); 128 String formatterId = matcher.group(2); 129 String detailName = matcher.group(3); 130 CmsUUID id = cms.readIdForUrlName(detailName); 131 m_content = cms.readResource(id, CmsResourceFilter.ignoreExpirationOffline(cms)); 132 m_locale = CmsLocaleManager.getLocale(localeStr); 133 m_formatter = cms.readResource(new CmsUUID(formatterId)); 134 } else { 135 throw new CmsPdfLinkParseException(); 136 } 137 } 138 139 /** 140 * Returns the content.<p> 141 * 142 * @return the content 143 */ 144 public CmsResource getContent() { 145 146 return m_content; 147 } 148 149 /** 150 * Gets the formatter resource.<p> 151 * 152 * @return the formatter resource 153 */ 154 public CmsResource getFormatter() { 155 156 return m_formatter; 157 } 158 159 /** 160 * Returns the link.<p> 161 * 162 * @return the link 163 */ 164 public String getLink() { 165 166 return m_link; 167 } 168 169 /** 170 * Returns the locale.<p> 171 * 172 * @return the locale 173 */ 174 public Locale getLocale() { 175 176 return m_locale; 177 } 178}