📄 cmslinkprocessor.java
字号:
}
/**
* Starts link processing for the given content in replacement mode.<p>
*
* Links are replaced by macros.<p>
*
* @param content the content to process
* @return the processed content with replaced links
*
* @throws ParserException if something goes wrong
*/
public String replaceLinks(String content) throws ParserException {
m_mode = REPLACE_LINKS;
return process(content, m_encoding);
}
/**
* Visitor method to process a tag (start).<p>
*
* @param tag the tag to process
*/
public void visitTag(Tag tag) {
if (tag instanceof LinkTag) {
processLinkTag((LinkTag)tag);
} else if (tag instanceof ImageTag) {
processImageTag((ImageTag)tag);
}
// append text content of the tag (may have been changed by above methods)
super.visitTag(tag);
}
/**
* Process an image tag.<p>
*
* @param tag the tag to process
*/
protected void processImageTag(ImageTag tag) {
if (tag.getAttribute("src") != null) {
CmsLink link;
switch (m_mode) {
case PROCESS_LINKS:
// macros are replaced with links
link = m_linkTable.getLink(getLinkName(tag.getImageURL()));
if (link != null) {
tag.setImageURL(processLink(link));
}
break;
case REPLACE_LINKS:
// links are replaced with macros
String targetUri = tag.getImageURL();
if (CmsStringUtil.isNotEmpty(targetUri)) {
String internalUri = CmsLinkManager.getSitePath(m_cms, m_relativePath, targetUri);
if (internalUri != null) {
// this is an internal link
link = m_linkTable.addLink(tag.getTagName(), internalUri, true);
} else {
// this is an external link
link = m_linkTable.addLink(tag.getTagName(), targetUri, false);
}
tag.setImageURL(CmsMacroResolver.formatMacro(link.getName()));
// now ensure the image has the "alt" attribute set
boolean hasAltAttrib = (tag.getAttribute("alt") != null);
if (!hasAltAttrib) {
String value = null;
if ((internalUri != null) && (m_rootCms != null)) {
// internal image: try to read the alt text from the "Title" property
try {
value = m_rootCms.readPropertyObject(
internalUri,
CmsPropertyDefinition.PROPERTY_TITLE,
false).getValue();
} catch (CmsException e) {
// property can't be read, ignore
}
}
// some editors add a "/" at the end of the tag, we must make sure to insert before that
Vector attrs = tag.getAttributesEx();
// first element is always the tag name
attrs.add(1, new Attribute(" "));
attrs.add(2, new Attribute("alt", value == null ? "" : value, '"'));
}
}
break;
default: // noop
}
}
}
/**
* Process a link tag.<p>
*
* @param tag the tag to process
*/
protected void processLinkTag(LinkTag tag) {
if (tag.getAttribute("href") != null) {
// href attribute is required
CmsLink link;
switch (m_mode) {
case PROCESS_LINKS:
// macros are replaced with links
link = m_linkTable.getLink(getLinkName(tag.getLink()));
if (link != null) {
tag.setLink(escapeLink(processLink(link)));
}
break;
case REPLACE_LINKS:
// links are replaced with macros
String targetUri = tag.extractLink();
if (CmsStringUtil.isNotEmpty(targetUri)) {
String internalUri = CmsLinkManager.getSitePath(m_cms, m_relativePath, targetUri);
if (internalUri != null) {
// this is an internal link
link = m_linkTable.addLink(tag.getTagName(), internalUri, true);
} else {
// this is an external link
link = m_linkTable.addLink(tag.getTagName(), targetUri, false);
}
tag.setLink(CmsMacroResolver.formatMacro(link.getName()));
}
break;
default: // noop
}
}
}
/**
* Internal method to get the name of a macro string.<p>
*
* @param macro the macro string
*
* @return the name of the macro
*/
private String getLinkName(String macro) {
if (CmsStringUtil.isNotEmpty(macro)) {
if (macro.startsWith(MACRO_START) && macro.endsWith(MACRO_END)) {
// make sure the macro really is a macro
return macro.substring(MACRO_START.length(), macro.length() - MACRO_END.length());
}
}
return "";
}
/**
* Returns the processed link of a given link.<p>
*
* @param link the link
* @return processed link
*/
private String processLink(CmsLink link) {
if (link.isInternal()) {
// if we have a local link, leave it unchanged
// cms may be null for unit tests
if ((m_cms == null) || (link.getUri().length() == 0) || (link.getUri().charAt(0) == '#')) {
return link.getUri();
}
// Explanation why the "m_processEditorLinks" variable is required:
// If the VFS is browsed in the root site, this indicates that a user has switched
// the context to the / in the Workplace. In this case the workplace site must be
// the active site. If normal link processing would be used, the site root in the link
// would be replaced with server name / port for the other sites. But if a user clicks
// on such a link he would leave the workplace site and loose his session.
// A result is that the "direct edit" mode does not work since he in not longer logged in.
// Therefore if the user is NOT in the editor, but in the root site, the links are generated
// without server name / port. However, if the editor is opened, the links are generated
// _with_ server name / port so that the source code looks identical to code
// that would normally created when running in a regular site.
// we are in the opencms root site but not in edit mode - use link as stored
if (!m_processEditorLinks && (m_cms.getRequestContext().getSiteRoot().length() == 0)) {
return OpenCms.getLinkManager().substituteLink(m_cms, link.getUri());
}
// otherwise get the desired site root from the stored link
// if there is no site root, we have a /system link (or the site was deleted),
// return the link prefixed with the opencms context
String siteRoot = link.getSiteRoot();
if (siteRoot == null) {
return OpenCms.getLinkManager().substituteLink(m_cms, link.getUri());
}
// return the link with the server prefix, if necessary
return OpenCms.getLinkManager().substituteLink(m_cms, link.getVfsUri(), siteRoot);
} else {
// don't touch external links
return link.getUri();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -