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.xml.types; 029 030import org.opencms.file.CmsObject; 031import org.opencms.json.JSONException; 032import org.opencms.main.CmsException; 033import org.opencms.main.CmsIllegalArgumentException; 034import org.opencms.main.CmsLog; 035import org.opencms.main.CmsRuntimeException; 036import org.opencms.main.OpenCms; 037import org.opencms.relations.CmsLink; 038import org.opencms.relations.CmsLinkUpdateUtil; 039import org.opencms.relations.CmsRelationType; 040import org.opencms.util.CmsRequestUtil; 041import org.opencms.util.CmsStringUtil; 042import org.opencms.util.CmsUUID; 043import org.opencms.xml.I_CmsXmlDocument; 044import org.opencms.xml.page.CmsXmlPage; 045import org.opencms.xml.xml2json.I_CmsJsonFormattableValue; 046import org.opencms.xml.xml2json.renderer.CmsJsonRendererXmlContent; 047 048import java.util.Locale; 049 050import org.apache.commons.logging.Log; 051 052import org.dom4j.Attribute; 053import org.dom4j.Element; 054 055/** 056 * Describes the XML content type "OpenCmsVfsFile".<p> 057 * 058 * This type allows links to internal VFS resources only.<p> 059 * 060 * @since 7.0.0 061 */ 062public class CmsXmlVfsFileValue extends A_CmsXmlContentValue implements I_CmsJsonFormattableValue { 063 064 /** Value to mark that no link is defined, "none". */ 065 public static final String NO_LINK = "none"; 066 067 /** The name of this type as used in the XML schema. */ 068 public static final String TYPE_NAME = "OpenCmsVfsFile"; 069 070 /** The vfs link type constant. */ 071 public static final String TYPE_VFS_LINK = "vfsLink"; 072 073 /** Logger instance for this class. */ 074 private static final Log LOG = CmsLog.getLog(CmsXmlVfsFileValue.class); 075 076 /** The schema definition String is located in a text for easier editing. */ 077 private static String m_schemaDefinition; 078 079 /** The String value of the element node. */ 080 private String m_stringValue; 081 082 /** 083 * Creates a new, empty schema type descriptor of type "OpenCmsVfsFile".<p> 084 */ 085 public CmsXmlVfsFileValue() { 086 087 // empty constructor is required for class registration 088 } 089 090 /** 091 * Creates a new XML content value of type "OpenCmsVfsFile".<p> 092 * 093 * @param document the XML content instance this value belongs to 094 * @param element the XML element that contains this value 095 * @param locale the locale this value is created for 096 * @param type the type instance to create the value for 097 */ 098 public CmsXmlVfsFileValue(I_CmsXmlDocument document, Element element, Locale locale, I_CmsXmlSchemaType type) { 099 100 super(document, element, locale, type); 101 } 102 103 /** 104 * Creates a new schema type descriptor for the type "OpenCmsVfsFile".<p> 105 * 106 * @param name the name of the XML node containing the value according to the XML schema 107 * @param minOccurs minimum number of occurrences of this type according to the XML schema 108 * @param maxOccurs maximum number of occurrences of this type according to the XML schema 109 */ 110 public CmsXmlVfsFileValue(String name, String minOccurs, String maxOccurs) { 111 112 super(name, minOccurs, maxOccurs); 113 } 114 115 /** 116 * Fills the given element with a {@link CmsXmlVfsFileValue} for the given data.<p> 117 * 118 * @param element the element to fill 119 * @param id the id to use 120 * @param rootPath the path to use 121 * @param type the relation type to use 122 */ 123 public static void fillEntry(Element element, CmsUUID id, String rootPath, CmsRelationType type) { 124 125 CmsLink link = new CmsLink(CmsXmlVfsFileValue.TYPE_VFS_LINK, type, id, rootPath, true); 126 // get xml node 127 Element linkElement = element.element(CmsXmlPage.NODE_LINK); 128 if (linkElement == null) { 129 // create xml node if needed 130 linkElement = element.addElement(CmsXmlPage.NODE_LINK); 131 } 132 // update xml node 133 CmsLinkUpdateUtil.updateXmlForVfsFile(link, linkElement); 134 } 135 136 /** 137 * @see org.opencms.xml.types.A_CmsXmlContentValue#createValue(I_CmsXmlDocument, org.dom4j.Element, Locale) 138 */ 139 public I_CmsXmlContentValue createValue(I_CmsXmlDocument document, Element element, Locale locale) { 140 141 return new CmsXmlVfsFileValue(document, element, locale, this); 142 } 143 144 /** 145 * @see org.opencms.xml.types.I_CmsXmlSchemaType#generateXml(org.opencms.file.CmsObject, org.opencms.xml.I_CmsXmlDocument, org.dom4j.Element, java.util.Locale) 146 */ 147 @Override 148 public Element generateXml(CmsObject cms, I_CmsXmlDocument document, Element root, Locale locale) { 149 150 Element element = root.addElement(getName()); 151 152 // get the default value from the content handler 153 String defaultValue = document.getHandler().getDefault(cms, this, locale); 154 if (defaultValue != null) { 155 I_CmsXmlContentValue value = createValue(document, element, locale); 156 value.setStringValue(cms, defaultValue); 157 } 158 return element; 159 } 160 161 /** 162 * Returns the link object represented by this XML content value.<p> 163 * 164 * @param cms the cms context, can be <code>null</code> but in this case no link check is performed 165 * 166 * @return the link object represented by this XML content value (will return null for an empty link) 167 */ 168 public CmsLink getLink(CmsObject cms) { 169 170 Element linkElement = m_element.element(CmsXmlPage.NODE_LINK); 171 if (linkElement == null) { 172 String textValue = m_element.getText(); 173 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(textValue)) { 174 if (CmsUUID.isValidUUID(textValue)) { 175 setIdValue(cms, new CmsUUID(textValue)); 176 } else { 177 setStringValue(cms, textValue); 178 } 179 } 180 linkElement = m_element.element(CmsXmlPage.NODE_LINK); 181 if (linkElement == null) { 182 return null; 183 } 184 } 185 CmsLinkUpdateUtil.updateType(linkElement, getRelationType(getPath())); 186 CmsLink link = new CmsLink(linkElement); 187 link.checkConsistency(cms); 188 if (CmsStringUtil.isEmptyOrWhitespaceOnly(link.getTarget())) { 189 return null; 190 } 191 return link; 192 } 193 194 /** 195 * @see org.opencms.xml.types.I_CmsXmlContentValue#getPlainText(org.opencms.file.CmsObject) 196 */ 197 @Override 198 public String getPlainText(CmsObject cms) { 199 200 return getStringValue(cms); 201 } 202 203 /** 204 * @see org.opencms.xml.types.I_CmsXmlSchemaType#getSchemaDefinition() 205 */ 206 public String getSchemaDefinition() { 207 208 // the schema definition is located in a separate file for easier editing 209 if (m_schemaDefinition == null) { 210 m_schemaDefinition = readSchemaDefinition("org/opencms/xml/types/XmlVfsFileValue.xsd"); 211 } 212 return m_schemaDefinition; 213 } 214 215 /** 216 * @see org.opencms.xml.types.I_CmsXmlContentValue#getStringValue(CmsObject) 217 */ 218 public String getStringValue(CmsObject cms) throws CmsRuntimeException { 219 220 if (m_stringValue == null) { 221 m_stringValue = createStringValue(cms); 222 } 223 return m_stringValue; 224 } 225 226 /** 227 * @see org.opencms.xml.types.A_CmsXmlContentValue#getTypeName() 228 */ 229 public String getTypeName() { 230 231 return TYPE_NAME; 232 } 233 234 /** 235 * Returns the raw data from the XML value as a CmsLink, without any consistency checks. 236 * 237 * @return the unchecked data from the XML as a CmsLink 238 */ 239 public CmsLink getUncheckedLink() { 240 241 Element linkElement = m_element.element(CmsXmlPage.NODE_LINK); 242 if (linkElement == null) { 243 return null; 244 } 245 return new CmsLink(linkElement); 246 247 } 248 249 /** 250 * @see org.opencms.xml.types.A_CmsXmlContentValue#isSearchable() 251 */ 252 @Override 253 public boolean isSearchable() { 254 255 // there is no point in searching link values 256 return false; 257 } 258 259 /** 260 * @see org.opencms.xml.types.A_CmsXmlContentValue#newInstance(java.lang.String, java.lang.String, java.lang.String) 261 */ 262 public I_CmsXmlSchemaType newInstance(String name, String minOccurs, String maxOccurs) { 263 264 return new CmsXmlVfsFileValue(name, minOccurs, maxOccurs); 265 } 266 267 /** 268 * Sets the value as a structure id.<p> 269 * 270 * @param cms the current CMS context 271 * @param id the structure id which should be stored in the file value 272 */ 273 public void setIdValue(CmsObject cms, CmsUUID id) { 274 275 CmsRelationType type = getRelationType(getPath()); 276 CmsLink link = new CmsLink(TYPE_VFS_LINK, type, id, "@", true); 277 // link management check 278 link.checkConsistency(cms); 279 // update xml node 280 CmsLinkUpdateUtil.updateXmlForVfsFile(link, m_element.addElement(CmsXmlPage.NODE_LINK)); 281 282 } 283 284 /** 285 * @see org.opencms.xml.types.A_CmsXmlContentValue#setStringValue(org.opencms.file.CmsObject, java.lang.String) 286 */ 287 public void setStringValue(CmsObject cms, String value) throws CmsIllegalArgumentException { 288 289 m_element.clearContent(); 290 // ensure the String value is re-calculated next time it's needed 291 m_stringValue = null; 292 if (CmsStringUtil.isEmptyOrWhitespaceOnly(value)) { 293 // no valid value given 294 return; 295 } 296 if (CmsUUID.isValidUUID(value)) { 297 setIdValue(cms, new CmsUUID(value)); 298 return; 299 } 300 String path = value; 301 if (cms != null) { 302 String siteRoot = OpenCms.getSiteManager().getSiteRoot(value); 303 String oldSite = cms.getRequestContext().getSiteRoot(); 304 try { 305 if (siteRoot != null) { 306 // only switch the site if needed 307 cms.getRequestContext().setSiteRoot(siteRoot); 308 // remove the site root, because the link manager call will append it anyway 309 path = cms.getRequestContext().removeSiteRoot(value); 310 if (CmsStringUtil.isEmptyOrWhitespaceOnly(path)) { 311 path = "/"; 312 } 313 } 314 // remove parameters, if not the link manager call might fail 315 String query = ""; 316 int pos = path.indexOf(CmsRequestUtil.URL_DELIMITER); 317 int anchorPos = path.indexOf('#'); 318 if ((pos == -1) || ((anchorPos > -1) && (pos > anchorPos))) { 319 pos = anchorPos; 320 } 321 if (pos > -1) { 322 query = path.substring(pos); 323 path = path.substring(0, pos); 324 } 325 // get the root path 326 path = OpenCms.getLinkManager().getRootPath(cms, path); 327 if (path == null) { 328 path = value; 329 } else { 330 // append parameters again 331 path += query; 332 } 333 } finally { 334 if (siteRoot != null) { 335 cms.getRequestContext().setSiteRoot(oldSite); 336 } 337 } 338 } 339 if (CmsStringUtil.isEmptyOrWhitespaceOnly(path)) { 340 return; 341 } 342 CmsRelationType type = getRelationType(getPath()); 343 CmsLink link = new CmsLink(TYPE_VFS_LINK, type, path, true); 344 // link management check 345 link.checkConsistency(cms); 346 // update xml node 347 CmsLinkUpdateUtil.updateXmlForVfsFile(link, m_element.addElement(CmsXmlPage.NODE_LINK)); 348 } 349 350 /** 351 * @see org.opencms.xml.xml2json.I_CmsJsonFormattableValue#toJson(org.opencms.file.CmsObject) 352 */ 353 public Object toJson(CmsObject cms) { 354 355 try { 356 String link = null; 357 CmsLink linkObj = getLink(cms); 358 if (linkObj != null) { 359 link = linkObj.getLink(cms); 360 } 361 CmsObject rootCms = OpenCms.initCmsObject(cms); 362 String path = getStringValue(rootCms); 363 return CmsJsonRendererXmlContent.linkAndPath(link, path, cms); 364 } catch (JSONException e) { 365 return null; 366 } catch (CmsException e) { 367 LOG.error(e.getLocalizedMessage(), e); 368 return null; 369 } 370 } 371 372 /** 373 * Creates the String value for this vfs file value element.<p> 374 * 375 * @param cms the cms context 376 * 377 * @return the String value for this vfs file value element 378 */ 379 private String createStringValue(CmsObject cms) { 380 381 Attribute enabled = m_element.attribute(CmsXmlPage.ATTRIBUTE_ENABLED); 382 383 String content = ""; 384 if ((enabled == null) || Boolean.valueOf(enabled.getText()).booleanValue()) { 385 CmsLink link = getLink(cms); 386 if (link != null) { 387 content = link.getUri(); 388 if (cms != null) { 389 content = cms.getRequestContext().removeSiteRoot(link.getUri()); 390 } 391 } 392 } 393 return content; 394 } 395 396}