001/* 002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a> 018 * 019 * $Id: ValueExpressionHelper.java 28460 2008-01-03 15:34:05Z sfermigier $ 020 */ 021 022package org.nuxeo.ecm.platform.forms.layout.facelets; 023 024import org.nuxeo.ecm.platform.el.DocumentModelResolver; 025import org.nuxeo.ecm.platform.forms.layout.api.FieldDefinition; 026import org.nuxeo.ecm.platform.ui.web.util.ComponentTagUtils; 027 028/** 029 * Helper for managing value expressions. 030 * 031 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a> 032 */ 033public class ValueExpressionHelper { 034 035 private ValueExpressionHelper() { 036 } 037 038 /** 039 * Returns true if given expression contains some special characters, in which case no transformation of the widget 040 * field definition will be done to make it compliant with {@link DocumentModelResolver} lookups when handling 041 * document fields. Special characters are: 042 * <ul> 043 * <li>".": this makes it possible to resolve subelements, for instance "myfield.mysubfield".</li> 044 * <li>"[": this makes it possible to include map or array sub elements, for instance 045 * "contextData['request/comment']" to fill a document model context map.</li> 046 * </ul> 047 * 048 * @throws NullPointerException if expression is null 049 */ 050 public static boolean isFormattedAsELExpression(String expression) { 051 if (expression.contains(".") || expression.contains("[")) { 052 return true; 053 } 054 return false; 055 } 056 057 /** 058 * Returns the value expression string representation without the surrounding brackets, for instance: 059 * "value.property" instead of #{value.property}. 060 */ 061 public static String createBareExpressionString(String valueName, FieldDefinition field) { 062 if (field == null || "".equals(field.getPropertyName())) { 063 return valueName; 064 } 065 066 String fieldName = field.getFieldName(); 067 if (ComponentTagUtils.isStrictValueReference(fieldName)) { 068 // already an EL expression => ignore schema name, do not resolve 069 // field, ignore previous expression elements 070 return ComponentTagUtils.getBareValueName(fieldName); 071 } else if (isFormattedAsELExpression(fieldName)) { 072 // already formatted as an EL expression => ignore schema name, do 073 // not resolve field and do not modify expression format 074 StringBuilder builder = new StringBuilder(); 075 builder.append(valueName); 076 if (!fieldName.startsWith(".") && !fieldName.startsWith("[")) { 077 builder.append("."); 078 } 079 builder.append(fieldName); 080 return builder.toString(); 081 } else { 082 StringBuilder builder = new StringBuilder(); 083 builder.append(valueName); 084 085 // try to resolve schema name/prefix 086 String schemaName = field.getSchemaName(); 087 if (schemaName == null) { 088 String propertyName = field.getFieldName(); 089 String[] s = propertyName.split(":"); 090 if (s.length == 2) { 091 schemaName = s[0]; 092 fieldName = s[1]; 093 } 094 } 095 096 if (schemaName != null) { 097 builder.append("['").append(schemaName).append("']"); 098 } 099 100 // handle xpath expressions 101 String[] splittedFieldName = fieldName.split("/"); 102 for (String item : splittedFieldName) { 103 builder.append("["); 104 try { 105 builder.append(Integer.parseInt(item)); 106 } catch (NumberFormatException e) { 107 builder.append("'").append(item).append("'"); 108 } 109 builder.append("]"); 110 } 111 112 return builder.toString(); 113 } 114 } 115 116 public static String createExpressionString(String valueName, FieldDefinition field) { 117 String bareExpression = createBareExpressionString(valueName, field); 118 return "#{" + bareExpression + "}"; 119 } 120 121}