1 31 32 package org.opencms.staticexport; 33 34 import org.opencms.site.CmsSiteManager; 35 import org.opencms.util.CmsRequestUtil; 36 import org.opencms.util.CmsUriSplitter; 37 import org.opencms.xml.page.CmsXmlPage; 38 39 import java.util.Map ; 40 import java.util.Set ; 41 42 import org.dom4j.Element; 43 44 53 public class CmsLink { 54 55 56 private String m_anchor; 57 58 59 private Element m_element; 60 61 62 private boolean m_internal; 63 64 65 private String m_name; 66 67 68 private Map m_parameters; 69 70 71 private String m_query; 72 73 74 private String m_siteRoot; 75 76 77 private String m_target; 78 79 80 private String m_type; 81 82 83 private String m_uri; 84 85 94 public CmsLink(Element element, String name, String type, String uri, boolean internal) { 95 96 m_element = element; 97 m_name = name; 98 m_type = type; 99 m_internal = internal; 100 101 m_uri = uri; 102 103 setComponents(m_uri); 105 } 106 107 118 public CmsLink( 119 Element element, 120 String name, 121 String type, 122 String target, 123 String anchor, 124 String query, 125 boolean internal) { 126 127 m_element = element; 128 m_name = name; 129 m_type = type; 130 m_internal = internal; 131 132 m_target = target; 133 m_anchor = anchor; 134 setQuery(query); 135 136 m_uri = setUri(m_target, m_anchor, m_query); 138 } 139 140 148 public CmsLink(String name, String type, String uri, boolean internal) { 149 150 this(null, name, type, uri, internal); 151 } 152 153 163 public CmsLink(String name, String type, String target, String anchor, String query, boolean internal) { 164 165 this(null, name, type, target, anchor, query, internal); 166 } 167 168 173 public String getAnchor() { 174 175 return m_anchor; 176 } 177 178 183 public String getName() { 184 185 return m_name; 186 } 187 188 194 public String getParameter(String name) { 195 196 String [] p = (String [])m_parameters.get(name); 197 if (p != null) { 198 return p[0]; 199 } 200 201 return null; 202 } 203 204 209 public Map getParameterMap() { 210 211 return m_parameters; 212 } 213 214 219 public Set getParameterNames() { 220 221 return m_parameters.keySet(); 222 } 223 224 230 public String [] getParameterValues(String name) { 231 232 return (String [])m_parameters.get(name); 233 } 234 235 240 public String getQuery() { 241 242 return m_query; 243 } 244 245 250 public String getSiteRoot() { 251 252 if (m_siteRoot != null) { 253 return m_siteRoot; 254 } 255 if (m_internal) { 256 m_siteRoot = CmsSiteManager.getSiteRoot(m_target); 257 return m_siteRoot; 258 } 259 return null; 260 } 261 262 267 public String getTarget() { 268 269 return m_target; 270 } 271 272 277 public String getType() { 278 279 return m_type; 280 } 281 282 287 public String getUri() { 288 289 return m_uri; 290 } 291 292 297 public String getVfsUri() { 298 299 if (m_internal) { 300 String siteRoot = getSiteRoot(); 301 return m_uri.substring(siteRoot.length()); 302 } 303 304 return null; 305 } 306 307 312 public boolean isInternal() { 313 314 return m_internal; 315 } 316 317 320 public String toString() { 321 322 return m_uri; 323 } 324 325 335 public void updateLink(String uri) { 336 337 m_uri = uri; 339 340 setComponents(m_uri); 342 343 updateXml(); 345 } 346 347 361 public void updateLink(String target, String anchor, String query) { 362 363 m_target = target; 365 m_anchor = anchor; 366 setQuery(query); 367 368 m_uri = setUri(m_target, m_anchor, m_query); 370 371 updateXml(); 373 } 374 375 381 private void setComponents(String uri) { 382 383 CmsUriSplitter splitter = new CmsUriSplitter(uri, true); 384 m_target = splitter.getPrefix(); 385 m_anchor = splitter.getAnchor(); 386 setQuery(splitter.getQuery()); 387 388 m_parameters = CmsRequestUtil.createParameterMap(m_query); 390 } 391 392 397 private void setQuery(String query) { 398 399 m_query = CmsLinkProcessor.unescapeLink(query); 400 } 401 402 411 private String setUri(String target, String anchor, String query) { 412 413 StringBuffer uri = new StringBuffer (64); 414 uri.append(target); 415 if (query != null) { 416 uri.append('?'); 417 uri.append(query); 418 } 419 if (anchor != null) { 420 uri.append('#'); 421 uri.append(anchor); 422 } 423 424 m_parameters = CmsRequestUtil.createParameterMap(query); 426 427 return uri.toString(); 428 } 429 430 433 private void updateXml() { 434 435 if (m_element != null) { 437 438 Element targetElement = m_element.element(CmsXmlPage.NODE_TARGET); 440 targetElement.clearContent(); 441 targetElement.addCDATA(m_target); 442 443 Element anchorElement = m_element.element(CmsXmlPage.NODE_ANCHOR); 445 if (m_anchor != null) { 446 if (anchorElement == null) { 447 m_element.addElement(CmsXmlPage.NODE_ANCHOR).addCDATA(m_anchor); 449 } else { 450 anchorElement.clearContent(); 452 anchorElement.addCDATA(m_anchor); 453 } 454 } else { 455 if (anchorElement != null) { 457 m_element.remove(anchorElement); 459 } 460 } 461 462 Element queryElement = m_element.element(CmsXmlPage.NODE_QUERY); 464 if (m_query != null) { 465 if (queryElement == null) { 466 m_element.addElement(CmsXmlPage.NODE_QUERY).addCDATA(m_query); 468 } else { 469 queryElement.clearContent(); 471 queryElement.addCDATA(m_query); 472 } 473 } else { 474 if (queryElement != null) { 476 m_element.remove(queryElement); 478 } 479 } 480 } 481 } 482 } | Popular Tags |