Author: bpoussin Date: 2012-03-09 00:48:59 +0100 (Fri, 09 Mar 2012) New Revision: 1449 Url: http://nuiton.org/repositories/revision/wikitty/1449 Log: Evolution #2003: toString tagValue must support preload wikitty access Modified: trunk/wikitty-api/src/main/java/org/nuiton/wikitty/WikittyUtil.java Modified: trunk/wikitty-api/src/main/java/org/nuiton/wikitty/WikittyUtil.java =================================================================== --- trunk/wikitty-api/src/main/java/org/nuiton/wikitty/WikittyUtil.java 2012-03-07 23:36:33 UTC (rev 1448) +++ trunk/wikitty-api/src/main/java/org/nuiton/wikitty/WikittyUtil.java 2012-03-08 23:48:59 UTC (rev 1449) @@ -76,6 +76,9 @@ import org.apache.commons.lang.time.DateUtils; import org.apache.commons.lang.time.FastDateFormat; +import org.apache.commons.lang3.ObjectUtils; +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.nuiton.wikitty.entities.WikittyTypes; @@ -393,7 +396,7 @@ /** Pattern de recherche des field dans les chaines de formatage */ static protected Pattern formatMatcher = - Pattern.compile("%[^0-9][^|$]*(\\|(.*?))?\\$[0-9]*?[a-zA-Z]"); + Pattern.compile("%([^0-9][^|$]*)(?:\\|(.*?))?(\\$[0-9]*?[a-zA-Z])"); /** * Format wikitty for string representation. * exemple: @@ -401,11 +404,11 @@ * <li> "Hello %Person.firstName|unknow$s" if firstName field doesn't exist, unknow is used * * @param format format as {@link http://download.oracle.com/javase/6/docs/api/java/util/Formatter.html#syntax} - * execpt that position is replace with fq field name. + * except that position is replace with fq field name. * @param w wikitty to format * @return String that represent the wikitty */ - static public String format(String formatWikitty, Wikitty w) { + static public String format1(String formatWikitty, Wikitty w) { Set<String> fields = w.getAllFieldNames(); Object[] values = new Object[fields.size()]; @@ -430,6 +433,88 @@ } /** + * Format wikitty for string representation. + * exemple: + * <li> "%Person.lastName$s %Person.firstName$s: %Person.birthday$tm %Person.birthday$te,%Person.birthday$tY" + * <li> "Hello %Person.firstName|unknown$s" if firstName field doesn't exist, unknow is used + * <li> "My company %Employee.company,Company.name|unknown$s" if employee has company field and this company has name field + * + * @param format format as {@link http://download.oracle.com/javase/6/docs/api/java/util/Formatter.html#syntax} + * except that position is replace with fq field name. + * @param w wikitty to format + * @return String that represent the wikitty + */ + static public String format(String formatWikitty, Wikitty w) { + List<Pair<String, String>> fields = new ArrayList<Pair<String, String>>(); + + StringBuffer formatBuffer = new StringBuffer(); + int i = 0; + // on collect tous les champs demande et leur valeur par defaut + // et en meme temps on cree la chaine formatBuffer qui substitue les + // champs par des numeros + Matcher matcher = formatMatcher.matcher(formatWikitty); + while(matcher.find()) { + String fieldAsked = matcher.group(1); + String defaultValue = StringUtils.defaultString(matcher.group(2)); + fields.add(new ImmutablePair<String, String>(fieldAsked, defaultValue)); + // i is incremented before replace, because String.format start at 1 not 0 + matcher.appendReplacement(formatBuffer, "%"+(++i)+"$3"); + } + matcher.appendTail(formatBuffer); + // la vrai chaine de formatage pour String.format + String format = formatBuffer.toString(); + + List<Object> values = new ArrayList<Object>(fields.size()); + // on recherche la valeur de chaque champs demande + for (Pair<String, String> pair : fields) { + String fieldAsked = pair.getKey(); + String defaultValue = pair.getValue(); + Wikitty wtmp = w; + if (StringUtils.contains(fieldAsked, ",")) { + String path = StringUtils.substringBeforeLast(fieldAsked, ","); + fieldAsked = StringUtils.substringAfterLast(fieldAsked, ","); + for (String field : StringUtils.split(path, ",")) { + if (wtmp.hasField(field)) { + String extName = WikittyExtension.extractExtensionName(field); + String fieldName = WikittyExtension.extractFieldName(field); + wtmp = wtmp.getFieldAsWikitty(extName, fieldName, false); + } else { + wtmp = null; + } + if (wtmp == null) { + break; + } + } + } + + Object value = null; + if(wtmp != null) { + if (wtmp.hasField(fieldAsked)) { + String extName = WikittyExtension.extractExtensionName(fieldAsked); + String fieldName = WikittyExtension.extractFieldName(fieldAsked); + // on essai toujours de recuperer l'objet plutot que l'id si + // possible en 1er + value = wtmp.getFieldAsWikitty(extName, fieldName, false); + if (value == null) { + // pas un wikitty ou pas pre-charge, on demande simplement le champs + value = wtmp.getFqField(fieldAsked); + } + } + } + values.add(ObjectUtils.defaultIfNull(value, defaultValue)); + } + + if (log.isDebugEnabled()) { + log.debug(String.format("%s -> %s (with %s)", formatWikitty, format, fields)); + } + System.out.println(String.format("%s -> %s (with %s)", formatWikitty, format, fields)); + + // use standard String.format + String result = String.format(format, values.toArray()); + return result; + } + + /** * Get value as Binary. * * @param value null and empty return empty byte[]
participants (1)
-
bpoussin@users.nuiton.org