📄 pagefieldtag.java
字号:
package org.jahia.taglibs.field.page;import org.jahia.data.JahiaData;import org.jahia.data.fields.*;import org.jahia.data.containers.*;import org.jahia.exceptions.JahiaException;import org.jahia.registries.ServicesRegistry;import org.jahia.services.pages.*;import org.jahia.taglibs.container.*;import org.jahia.taglibs.util.Utils;import org.jahia.utils.JahiaConsole;import java.io.*;import java.util.*;import javax.servlet.*;import javax.servlet.jsp.*;import javax.servlet.jsp.tagext.*;/** * Class PageFieldTag : retrieves a Jahia page field inside the current container, * so that the enclosed tags such as <pageTitle> can access * the page attributes and display their values. * * @author Jerome Tamiotti */public class PageFieldTag extends BodyTagSupport { protected String name = ""; protected String title = ""; protected String value = ""; private int pageId = -1; private int pageLevel = -1; private boolean display = false; private JahiaPage page = null; public void setName(String name) { this.name = name; } public void setTitle(String title) { this.title = title; } public void setValue(String value) { this.value = value; } public void setPageId(String pageId) { try { this.pageId = Integer.parseInt(pageId); } catch (NumberFormatException nfe) { JahiaConsole.println("PageFieldTag: setPageId", "The given page id is not a number"); } } public void setPageLevel(String pageLevel) { try { this.pageLevel = Integer.parseInt(pageLevel); } catch (NumberFormatException nfe) { JahiaConsole.println("PageFieldTag: setPageLevel", "The given page level is not a number"); } } public JahiaPage getPage() { return this.page; } public boolean displayBody() { return display; } public int doStartTag() throws JspTagException { // JahiaConsole.println("PageFieldTag.doStartTag", "called."); if (this.title.equals("")) { this.title = this.name; } ServletRequest request = pageContext.getRequest(); JahiaData jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData"); if (jData == null) { HashMap tempEngineMap = (HashMap) request.getAttribute("org.jahia.engines.EngineHashMap"); jData = (JahiaData) tempEngineMap.get("jData"); } try { // checks if the field is absolute if (this.pageId == -1) { // check for page level if (this.pageLevel != -1) { this.pageId = jData.gui().getLevelID(this.pageLevel); } } } catch (JahiaException je) { JahiaConsole.printe("PageFieldTag.doStartTag:getLevelID call", je); } // checks if in declaration phase or if there are already container JahiaContainer container = null; // checks if we are inside a container ContainerTag parent = (ContainerTag) findAncestorWithClass(this,ContainerTag.class); if (parent != null) { container = parent.getContainer(); } JahiaField theField = null; try { // searches for the field that contains the page if (parent == null) { // at root theField = getField(jData); this.display = true; } else { // in a container ContainerListTag containerListTag = (ContainerListTag)parent.getParent(); if (containerListTag != null) { // declares the page as a field of the container list if (containerListTag.isDeclarationPass()) { containerListTag.addField(this.name); // declares the field when it is not an absolute field, // or when it is an absolute one and the current page is the home page if ( this.pageId == -1 || this.pageId == jData.page().getID() ) { // and when it's not already declared jData.containers().declareField( this.name, this.title, FieldTypes.PAGE, this.value ); } this.display = false; return EVAL_BODY_BUFFERED; } } if (parent.displayBody()) { // must display the field of the current container if (container != null) { theField = container.getField(this.name); this.display = true; } } } if (theField != null) { this.page = (JahiaPage) theField.getObject(); if (this.page == null) { // not set yet, so use the default value given as parameter if (!this.value.equals("")) { // create a page with the default URL JahiaPage page = jData.page(); this.page = ServicesRegistry.getInstance().getJahiaPageService().createPage( page.getJahiaID(), // site ID page.getID(), // parent ID JahiaPage.TYPE_URL, // remote page this.value, // default title -1, // no default template this.value, // default URL -1, // page link ID jData.params().getUser().getUsername(), // creator page.getAclID(), // parent acl jData.params() ); // paramBean } } if ( this.display ) { return EVAL_BODY_BUFFERED; } } } catch (JahiaException je) { JahiaConsole.println("PageFieldTag: doStartTag ", je.toString()); } catch (NullPointerException npe) { JahiaConsole.println("PageFieldTag: doStartTag ", npe.toString()); } return SKIP_BODY; } public int doAfterBody() throws JspException { if (this.display) { try { bodyContent.writeOut(bodyContent.getEnclosingWriter()); } catch (IOException ioe) { throw new JspTagException(); } } else { bodyContent.clearBody(); } return SKIP_BODY; } private JahiaField getField(JahiaData jData) throws JahiaException { if ( this.pageId == -1 // the field is not absolute || jData.page().getID()== this.pageId ) { // the current page is the page that declares the absolute field // then we have to check if the field is already declared if ( !jData.fields().checkDeclared(this.name) ) { // declare it now ! jData.fields().declareField( this.name, this.title, FieldTypes.PAGE, this.value ); } return jData.fields().getField(this.name); } // according to the previous tests, the only remaining case is // when the field is absolute and the current page is not the declaring page, // so return an absolute field of another page return jData.fields().getAbsoluteField(this.name, this.pageId); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -