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 GmbH & Co. KG, 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.staticexport; 029 030import org.opencms.relations.CmsLink; 031import org.opencms.relations.CmsRelationType; 032 033import java.util.Comparator; 034import java.util.Iterator; 035import java.util.Map; 036import java.util.TreeMap; 037import java.util.regex.Matcher; 038import java.util.regex.Pattern; 039 040import org.apache.commons.lang3.BooleanUtils; 041 042import com.google.common.collect.ComparisonChain; 043 044/** 045 * Maintains a table of links for an element of a CmsXmlPage.<p> 046 * 047 * @since 6.0.0 048 */ 049public class CmsLinkTable { 050 051 /** 052 * Comparator used to deterministically order the link table.<p> 053 */ 054 public static class LinkKeyComparator implements Comparator<String> { 055 056 /** 057 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) 058 */ 059 public int compare(String a, String b) { 060 061 if ((a == null) || (b == null)) { 062 // null is "less" than any other string 063 return BooleanUtils.toInteger(b == null) - BooleanUtils.toInteger(a == null); 064 } 065 Matcher matcherA = LINK_PATTERN.matcher(a); 066 Matcher matcherB = LINK_PATTERN.matcher(b); 067 matcherA.find(); 068 matcherB.find(); 069 String nameA = matcherA.group(1); 070 // prepend "0" so we don't get an exception for the empty string 071 int indexA = Integer.parseInt("0" + matcherA.group(2)); 072 String nameB = matcherB.group(1); 073 int indexB = Integer.parseInt("0" + matcherB.group(2)); 074 int result = ComparisonChain.start().compare(nameA, nameB).compare(indexA, indexB).compare(a, b).result(); 075 return result; 076 077 } 078 } 079 080 /** Prefix to identify a link in the content. */ 081 private static final String LINK_PREFIX = "link"; 082 083 /** Pattern used to extract the index and the part before the index from a link id. */ 084 public static final Pattern LINK_PATTERN = Pattern.compile("^(.*?)([0-9]*)$"); 085 086 /** The map to store the link table in. */ 087 private Map<String, CmsLink> m_linkTable; 088 089 /** 090 * Creates a new CmsLinkTable.<p> 091 */ 092 public CmsLinkTable() { 093 094 m_linkTable = new TreeMap<String, CmsLink>(new LinkKeyComparator()); 095 } 096 097 /** 098 * Adds a new link with a given internal name and internal flag to the link table.<p> 099 * 100 * @param link the <code>CmsLink</code> to add 101 * @return the new link entry 102 */ 103 public CmsLink addLink(CmsLink link) { 104 105 m_linkTable.put(link.getName(), link); 106 return link; 107 } 108 109 /** 110 * Adds a new link to the link table.<p> 111 * 112 * @param type type of the link 113 * @param targetUri link destination 114 * @param internal flag to indicate if the link is a local link 115 * 116 * @return the new link entry 117 */ 118 public CmsLink addLink(CmsRelationType type, String targetUri, boolean internal) { 119 120 CmsLink link = new CmsLink(LINK_PREFIX + m_linkTable.size(), type, targetUri, internal); 121 m_linkTable.put(link.getName(), link); 122 return link; 123 } 124 125 /** 126 * Returns the CmsLink Entry for a given name.<p> 127 * 128 * @param name the internal name of the link 129 * @return the CmsLink entry 130 */ 131 public CmsLink getLink(String name) { 132 133 return m_linkTable.get(name); 134 } 135 136 /** 137 * Returns if the link table is empty.<p> 138 * 139 * @return true if the link table is empty, false otherwise 140 */ 141 public boolean isEmpty() { 142 143 return m_linkTable.isEmpty(); 144 } 145 146 /** 147 * Returns an iterator over the links in the table.<p> 148 * 149 * The objects iterated are of type <code>{@link CmsLink}</code>. 150 * 151 * @return a string iterator for internal link names 152 */ 153 public Iterator<CmsLink> iterator() { 154 155 return m_linkTable.values().iterator(); 156 } 157 158 /** 159 * Returns the size of this link table.<p> 160 * 161 * @return the size of this link table 162 */ 163 public int size() { 164 165 return m_linkTable.size(); 166 } 167}