📄 abstractfieldtag.java
字号:
package org.jahia.taglibs.field;import org.jahia.data.JahiaData;import org.jahia.data.fields.*;import org.jahia.data.containers.JahiaContainer;import org.jahia.exceptions.JahiaException;import org.jahia.taglibs.container.*;import org.jahia.taglibs.util.*;import org.jahia.utils.JahiaConsole;import java.io.*;import java.util.*;import javax.servlet.*;import javax.servlet.jsp.*;import javax.servlet.jsp.tagext.*;/** * Class AbstractFieldTag : defines common code for Jahia fields * * This class check if the field belongs to a container, or it has been declared * directly on the page * * @author Jerome Tamiotti */public abstract class AbstractFieldTag extends TagSupport { protected String name = ""; private String title = ""; private String value = ""; private int pageId = -1; private int pageLevel = -1; private String declarationOnly = "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 setDeclarationOnly(String value) { this.declarationOnly = value; } public void setPageId(String pageId) { try { this.pageId = Integer.parseInt(pageId); } catch (NumberFormatException nfe) { JahiaConsole.println("AbstractFieldTag: 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("AbstractFieldTag: setPageLevel", "The given page level is not a number"); } } public int doStartTag() throws JspTagException { if (this.title.equals("")) { this.title = this.name; } ServletRequest request = pageContext.getRequest(); JspWriter out = pageContext.getOut(); 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("AbstractField.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()) { // must be declared as a field of the enclosing container list, if not already done // 28.02.2002 NK, added the check bellow to avoid multiple declaration // of the same field in the container list structure. if ( !containerListTag.isFieldAlreadyDeclared(this.name) ) { 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, getFieldType(), this.value ); } catch (JahiaException je) { // this case can be valid if we are using the field multiple times... // JahiaConsole.println("AbstractField.doStartTag:declaredField call", "Field was probably already declared"); } } } return SKIP_BODY; } else { } } container = parent.getContainer(); } try { if (parent == null) { JahiaField theField = getField(jData); if (theField != null) { if ( !declarationOnly.equals("true") ) out.print( readValue(jData, theField.getValue()) ); } } else { if (parent.displayBody()) { // must display the field of the current container if (container != null) { //out.print(container.getField(this.name).getValue()); if ( !declarationOnly.equals("true") ) out.print( readValue( jData, container.getField(this.name).getValue() ) ); } } } } catch (IOException ioe) { JahiaConsole.println("AbstractFieldTag: doStartTag","Error while displaying tag '" + this.name + "' : " + ioe.toString()); } catch (JahiaException je) { JahiaConsole.println("AbstractFieldTag: doStartTag","Error while displaying tag '" + this.name + "' : " + je.toString()); } catch (NullPointerException npe) { JahiaConsole.println("AbstractFieldTag: doStartTag","Error while displaying tag '" + this.name + "' : " + npe.toString()); } 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, getFieldType(), 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); } public String readValue(JahiaData jData, String value) { // this method body can change according to the field type // for example, text fields may be truncated, so this method // will be overloaded in the subclass TextFieldTag return value; } // method to implement in subclasses public abstract int getFieldType();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -