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