📄 filefieldtag.java
字号:
package org.jahia.taglibs.field.file;import org.jahia.data.*;import org.jahia.data.fields.*;import org.jahia.data.files.*;import org.jahia.data.containers.JahiaContainer;import org.jahia.exceptions.JahiaException;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.http.*;import javax.servlet.jsp.*;import javax.servlet.jsp.tagext.*;/** * Class FileFieldTag : retrieves a Jahia file field inside the current container or on the * page, so that the enclosed tags such as <fileDownloadURL> can access * the page attributes and display their values. * * @author Jerome Tamiotti */public class FileFieldTag extends BodyTagSupport { protected String name = ""; protected String title = ""; protected String value = ""; private int pageId = -1; private int pageLevel = -1; private JahiaFileField file = null; private boolean display = false; 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("FileFieldTag: 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("FileFieldTag: setPageLevel", "The given page level is not a number"); } } public JahiaFileField getFile() { return this.file; } public boolean displayBody() { return display; } public int doStartTag() throws JspTagException { if (this.title.equals("")) { this.title = this.name; } HttpServletRequest request = (HttpServletRequest)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("FileFieldTag.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) { ContainerListTag containerListTag = (ContainerListTag)parent.getParent(); if (containerListTag != null) { if (containerListTag.isDeclarationPass()) { // JahiaConsole.println("FileFieldTag.doStartTag", "DECLARATION PASS."); if (containerListTag != null) { // declares the file as a field of the container list 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() ) { try { jData.containers().declareField( this.name, this.title, FieldTypes.FILE, this.value ); } catch (JahiaException je) { JahiaConsole.println("FileFieldTag.doStartTag:declareField call", "Warning : Field " + this.name + " probably already previously declared in template "); } } this.display = false; return EVAL_BODY_BUFFERED; } } container = parent.getContainer(); } JahiaField theField = null; try { // searches for the field that contains the file if (parent == null) { // at root theField = getField(jData); this.display = true; } else { // in a container 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.file = (JahiaFileField) theField.getObject(); if (this.file == null) { if (!this.value.equals("")) { JahiaFile tempFile = new JahiaFile(-1, -1, "", "", "", 0, 0, "", "", ""); this.file = new JahiaFileField(-1, tempFile); this.file.setDownloadUrl(jData.gui().drawHttpJspContext(request) + "/" + this.value); this.display = true; } else { this.display = jData.gui().isEditMode(); } } } if ( this.display ) { return EVAL_BODY_BUFFERED; } } catch (JahiaException je) { JahiaConsole.println("FileFieldTag: doStartTag ", je.toString()); } catch (NullPointerException npe) { JahiaConsole.println("FileFieldTag: 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.FILE, 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 + -