1 31 32 package org.opencms.staticexport; 33 34 import org.opencms.file.CmsObject; 35 import org.opencms.file.CmsPropertyDefinition; 36 import org.opencms.file.CmsRequestContext; 37 import org.opencms.main.CmsException; 38 import org.opencms.main.OpenCms; 39 import org.opencms.util.CmsHtmlParser; 40 import org.opencms.util.CmsMacroResolver; 41 import org.opencms.util.CmsStringUtil; 42 43 import java.util.Vector ; 44 45 import org.htmlparser.Attribute; 46 import org.htmlparser.Tag; 47 import org.htmlparser.tags.ImageTag; 48 import org.htmlparser.tags.LinkTag; 49 import org.htmlparser.util.ParserException; 50 51 61 public class CmsLinkProcessor extends CmsHtmlParser { 62 63 64 public static final String HTML_END = "</body></html>"; 65 66 67 public static final String HTML_START = "<html><body>"; 68 69 70 private static final int PROCESS_LINKS = 1; 71 72 73 private static final int REPLACE_LINKS = 0; 74 75 76 private CmsObject m_cms; 77 78 79 private String m_encoding; 80 81 82 private CmsLinkTable m_linkTable; 83 84 85 private int m_mode; 86 87 88 private boolean m_processEditorLinks; 89 90 91 private String m_relativePath; 92 93 94 private CmsObject m_rootCms; 95 96 104 public CmsLinkProcessor(CmsObject cms, CmsLinkTable linkTable, String encoding, String relativePath) { 105 106 super(true); 108 109 m_cms = cms; 110 if (m_cms != null) { 111 try { 112 m_rootCms = OpenCms.initCmsObject(cms); 113 m_rootCms.getRequestContext().setSiteRoot("/"); 114 } catch (CmsException e) { 115 m_rootCms = null; 117 } 118 } 119 m_linkTable = linkTable; 120 m_encoding = encoding; 121 m_processEditorLinks = ((null != m_cms) && (null != m_cms.getRequestContext().getAttribute( 122 CmsRequestContext.ATTRIBUTE_EDITOR))); 123 m_relativePath = relativePath; 124 } 125 126 132 public static String escapeLink(String source) { 133 134 if (source == null) { 135 return null; 136 } 137 StringBuffer result = new StringBuffer (source.length() * 2); 138 int terminatorIndex; 139 for (int i = 0; i < source.length(); ++i) { 140 char ch = source.charAt(i); 141 switch (ch) { 142 case '&': 143 terminatorIndex = source.indexOf(';', i); 145 if (terminatorIndex > 0) { 146 String substr = source.substring(i + 1, terminatorIndex); 147 if ("amp".equals(substr)) { 148 result.append(ch); 149 } else { 150 result.append("&"); 151 } 152 } else { 153 result.append("&"); 154 } 155 break; 156 default: 157 result.append(ch); 158 } 159 } 160 return new String (result); 161 } 162 163 169 public static String unescapeLink(String source) { 170 171 if (source == null) { 172 return null; 173 } 174 return CmsStringUtil.substitute(source, "&", "&"); 175 176 } 177 178 183 public CmsLinkTable getLinkTable() { 184 185 return m_linkTable; 186 } 187 188 198 public String processLinks(String content) throws ParserException { 199 200 m_mode = PROCESS_LINKS; 201 return process(content, m_encoding); 202 } 203 204 214 public String replaceLinks(String content) throws ParserException { 215 216 m_mode = REPLACE_LINKS; 217 return process(content, m_encoding); 218 } 219 220 225 public void visitTag(Tag tag) { 226 227 if (tag instanceof LinkTag) { 228 processLinkTag((LinkTag)tag); 229 } else if (tag instanceof ImageTag) { 230 processImageTag((ImageTag)tag); 231 } 232 super.visitTag(tag); 234 } 235 236 241 protected void processImageTag(ImageTag tag) { 242 243 if (tag.getAttribute("src") != null) { 244 245 CmsLink link; 246 switch (m_mode) { 247 248 case PROCESS_LINKS: 249 link = m_linkTable.getLink(CmsMacroResolver.stripMacro(tag.getImageURL())); 251 if (link != null) { 252 tag.setImageURL(processLink(link)); 253 } 254 break; 255 256 case REPLACE_LINKS: 257 String targetUri = tag.getImageURL(); 259 if (CmsStringUtil.isNotEmpty(targetUri)) { 260 String internalUri = CmsLinkManager.getSitePath(m_cms, m_relativePath, targetUri); 261 if (internalUri != null) { 262 link = m_linkTable.addLink(tag.getTagName(), internalUri, true); 264 } else { 265 link = m_linkTable.addLink(tag.getTagName(), targetUri, false); 267 } 268 tag.setImageURL(CmsMacroResolver.formatMacro(link.getName())); 269 270 boolean hasAltAttrib = (tag.getAttribute("alt") != null); 272 if (!hasAltAttrib) { 273 String value = null; 274 if ((internalUri != null) && (m_rootCms != null)) { 275 try { 277 value = m_rootCms.readPropertyObject( 278 internalUri, 279 CmsPropertyDefinition.PROPERTY_TITLE, 280 false).getValue(); 281 } catch (CmsException e) { 282 } 284 } 285 Vector attrs = tag.getAttributesEx(); 287 attrs.add(1, new Attribute(" ")); 289 attrs.add(2, new Attribute("alt", value == null ? "" : value, '"')); 290 } 291 } 292 break; 293 294 default: } 296 } 297 } 298 299 304 protected void processLinkTag(LinkTag tag) { 305 306 if (tag.getAttribute("href") != null) { 307 309 CmsLink link; 310 switch (m_mode) { 311 312 case PROCESS_LINKS: 313 link = m_linkTable.getLink(CmsMacroResolver.stripMacro(tag.getLink())); 315 if (link != null) { 316 tag.setLink(escapeLink(processLink(link))); 317 } 318 break; 319 320 case REPLACE_LINKS: 321 String targetUri = tag.extractLink(); 323 if (CmsStringUtil.isNotEmpty(targetUri)) { 324 String internalUri = CmsLinkManager.getSitePath(m_cms, m_relativePath, targetUri); 325 if (internalUri != null) { 326 link = m_linkTable.addLink(tag.getTagName(), internalUri, true); 328 } else { 329 link = m_linkTable.addLink(tag.getTagName(), targetUri, false); 331 } 332 tag.setLink(CmsMacroResolver.formatMacro(link.getName())); 333 } 334 break; 335 336 default: } 338 } 339 } 340 341 347 private String processLink(CmsLink link) { 348 349 if (link.isInternal()) { 350 351 if ((m_cms == null) || (link.getUri().length() == 0) || (link.getUri().charAt(0) == '#')) { 354 return link.getUri(); 355 } 356 357 369 if (!m_processEditorLinks && (m_cms.getRequestContext().getSiteRoot().length() == 0)) { 371 return OpenCms.getLinkManager().substituteLink(m_cms, link.getUri()); 372 } 373 374 String siteRoot = link.getSiteRoot(); 378 if (siteRoot == null) { 379 return OpenCms.getLinkManager().substituteLink(m_cms, link.getUri()); 380 } 381 382 return OpenCms.getLinkManager().substituteLink(m_cms, link.getVfsUri(), siteRoot); 384 } else { 385 386 return link.getUri(); 388 } 389 } 390 } | Popular Tags |