1 package org.jahia.deprecated.taglibs.field; 2 3 import java.io.IOException ; 4 5 import javax.servlet.ServletRequest ; 6 import javax.servlet.jsp.JspException ; 7 import javax.servlet.jsp.JspTagException ; 8 import javax.servlet.jsp.JspWriter ; 9 import javax.servlet.jsp.tagext.TagSupport ; 10 11 import org.jahia.data.JahiaData; 12 import org.jahia.data.containers.JahiaContainer; 13 import org.jahia.data.fields.JahiaField; 14 import org.jahia.deprecated.taglibs.container.ContainerListTag; 15 import org.jahia.deprecated.taglibs.container.ContainerTag; 16 import org.jahia.exceptions.JahiaException; 17 import org.jahia.utils.JahiaConsole; 18 19 20 28 public abstract class AbstractFieldTag extends TagSupport { 29 30 protected String name = ""; 31 private String title = ""; 32 private String value = ""; 33 private int pageId = -1; 34 private int pageLevel = -1; 35 private String declarationOnly = "false"; 36 37 public void setName(String name) { 38 this.name = name; 39 } 40 41 public void setTitle(String title) { 42 this.title = title; 43 } 44 45 public void setValue(String value) { 46 this.value = value; 47 } 48 49 public void setDeclarationOnly(String value) { 50 this.declarationOnly = value; 51 } 52 53 public void setPageId(String pageId) { 54 try { 55 this.pageId = Integer.parseInt(pageId); 56 } catch (NumberFormatException nfe) { 57 JahiaConsole.println("AbstractFieldTag: setPageId", "The given page id is not a number"); 58 } 59 } 60 61 public void setPageLevel(String pageLevel) { 62 try { 63 this.pageLevel = Integer.parseInt(pageLevel); 64 } catch (NumberFormatException nfe) { 65 JahiaConsole.println("AbstractFieldTag: setPageLevel", "The given page level is not a number"); 66 } 67 } 68 69 public int doStartTag() throws JspTagException { 70 71 if (this.title.equals("")) { 72 this.title = this.name; 73 } 74 75 ServletRequest request = pageContext.getRequest(); 76 JspWriter out = pageContext.getOut(); 77 78 JahiaData jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData"); 79 80 try { 81 if (this.pageId == -1) { 83 if (this.pageLevel != -1) { 85 this.pageId = jData.gui().getLevelID(this.pageLevel); 86 } 87 } 88 } catch (JahiaException je) { 89 JahiaConsole.printe("AbstractField.doStartTag:getLevelID call", je); 90 } 91 92 JahiaContainer container = null; 94 ContainerTag parent = (ContainerTag) findAncestorWithClass(this, ContainerTag.class); 96 if (parent != null) { 97 98 ContainerListTag containerListTag = (ContainerListTag)findAncestorWithClass(this, ContainerListTag.class); 99 if (containerListTag != null) { 100 if (containerListTag.isDeclarationPass()) { 101 if ( !containerListTag.isFieldAlreadyDeclared(this.name) ) { 105 106 containerListTag.addField(this.name); 107 if ( this.pageId == -1 || this.pageId == jData.page().getID() ) { 110 try { 111 jData.containers().declareField( containerListTag.getName(), this.name, this.title, getFieldType(), this.value ); 112 } catch (JahiaException je) { 113 } 116 } 117 } 118 return SKIP_BODY; 119 } else { 120 } 121 } 122 123 container = parent.getContainer(); 124 } 125 126 127 try { 128 129 if (parent == null) { 130 131 JahiaField theField = getField(jData); 132 if (theField != null) { 133 if ( !declarationOnly.equals("true") ){ 134 out.print( readValue(jData, theField.getValue()) ); 135 } 136 } 137 } else { 138 if (parent.displayBody()) { 139 140 if (container != null) { 142 if ( !declarationOnly.equals("true") ){ 144 String val = container.getField(this.name).getValue(); 145 out.print( readValue( jData, val ) ); 146 } 147 } 148 } 149 } 150 } catch (IOException ioe) { 151 JahiaConsole.println("AbstractFieldTag: doStartTag","Error while displaying tag '" + this.name + "' : " + ioe.toString()); 152 } catch (JahiaException je) { 153 JahiaConsole.println("AbstractFieldTag: doStartTag","Error while displaying tag '" + this.name + "' : " + je.toString()); 154 } catch (NullPointerException npe) { 155 JahiaConsole.println("AbstractFieldTag: doStartTag","Error while displaying tag '" + this.name + "' : " + npe.toString()); 156 } 157 return SKIP_BODY; 158 } 159 160 161 private JahiaField getField(JahiaData jData) throws JahiaException { 162 163 if ( this.pageId == -1 || jData.page().getID()== this.pageId ) { 165 166 if ( !jData.fields().checkDeclared(this.name) ) { 169 jData.fields().declareField( this.name, this.title, getFieldType(), this.value ); 171 } 172 return jData.fields().getField(this.name); 173 } 174 return jData.fields().getAbsoluteField(this.name, this.pageId); 178 } 179 180 181 public String readValue(JahiaData jData, String value) { 182 return value; 186 } 187 188 public abstract int getFieldType(); 190 191 public int doEndTag() throws JspException { 192 name = ""; 195 title = ""; 196 value = ""; 197 pageId = -1; 198 pageLevel = -1; 199 declarationOnly = "false"; 200 return EVAL_PAGE; 201 } 202 203 204 } 205 | Popular Tags |