Author: bpoussin Date: 2012-03-10 17:09:04 +0100 (Sat, 10 Mar 2012) New Revision: 1450 Url: http://nuiton.org/repositories/revision/wikitty/1450 Log: Evolution #2003: toString tagValue must support preload wikitty access bugfix for date. empty string doesn't work for date format. now put null as default for all type Modified: trunk/wikitty-api/src/main/java/org/nuiton/wikitty/WikittyUtil.java trunk/wikitty-api/src/test/java/org/nuiton/wikitty/WikittyUtilTest.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-08 23:48:59 UTC (rev 1449) +++ trunk/wikitty-api/src/main/java/org/nuiton/wikitty/WikittyUtil.java 2012-03-10 16:09:04 UTC (rev 1450) @@ -54,6 +54,7 @@ import java.util.Collections; import java.util.Date; import java.util.Enumeration; +import java.util.Formatter; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedHashSet; @@ -394,44 +395,12 @@ return result; } - /** Pattern de recherche des field dans les chaines de formatage */ - static protected Pattern formatMatcher = - Pattern.compile("%([^0-9][^|$]*)(?:\\|(.*?))?(\\$[0-9]*?[a-zA-Z])"); - /** - * 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|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} - * except that position is replace with fq field name. - * @param w wikitty to format - * @return String that represent the wikitty + /** Pattern de recherche des field dans les chaines de formatage + * @see {@link Formatter#formatSpecifier} */ - static public String format1(String formatWikitty, Wikitty w) { - Set<String> fields = w.getAllFieldNames(); - Object[] values = new Object[fields.size()]; + static protected Pattern formatMatcher = + Pattern.compile("%([^0-9][^|$]*)(?:\\|(.*?))?(\\$(:?[-#+ 0,(\\<]*)?(:?\\d+)?(:?\\.\\d+)?[tT]?[a-zA-Z])"); - String format = formatWikitty; - // convert Map field to array - int i = 0; - for (String field : fields) { - values[i++] = w.getFqField(field); - // i is incremented before replace, because String.format start at 1 not 0 - format = format.replaceAll("%"+Pattern.quote(field)+"(\\|.*?)?\\$", "\\%"+i+"\\$"); - } - - // on remplace tous les champs non trouve - format = formatMatcher.matcher(format).replaceAll("$2"); - if (log.isDebugEnabled()) { - log.debug(String.format("%s -> %s (with %s)", formatWikitty, format, fields)); - } - - // use standard String.format - String result = String.format(format, values); - return result; - } - /** * Format wikitty for string representation. * exemple: @@ -455,7 +424,7 @@ Matcher matcher = formatMatcher.matcher(formatWikitty); while(matcher.find()) { String fieldAsked = matcher.group(1); - String defaultValue = StringUtils.defaultString(matcher.group(2)); + String defaultValue = 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"); @@ -470,6 +439,7 @@ String fieldAsked = pair.getKey(); String defaultValue = pair.getValue(); Wikitty wtmp = w; + // recherche de l'objet reel sur lequel prendre la valeur du champs if (StringUtils.contains(fieldAsked, ",")) { String path = StringUtils.substringBeforeLast(fieldAsked, ","); fieldAsked = StringUtils.substringAfterLast(fieldAsked, ","); @@ -489,6 +459,7 @@ Object value = null; if(wtmp != null) { + // recuperation de la valeur du champs if (wtmp.hasField(fieldAsked)) { String extName = WikittyExtension.extractExtensionName(fieldAsked); String fieldName = WikittyExtension.extractFieldName(fieldAsked); @@ -501,13 +472,14 @@ } } } + // on ajoute dans la liste la valeur du champs values.add(ObjectUtils.defaultIfNull(value, defaultValue)); } if (log.isDebugEnabled()) { - log.debug(String.format("%s -> %s (with %s)", formatWikitty, format, fields)); + log.debug(String.format("%s -> %s (with %s from %s)", formatWikitty, format, values, fields)); } - System.out.println(String.format("%s -> %s (with %s)", formatWikitty, format, fields)); + System.out.println(String.format("%s -> %s (with %s from %s)", formatWikitty, format, values, fields)); // use standard String.format String result = String.format(format, values.toArray()); Modified: trunk/wikitty-api/src/test/java/org/nuiton/wikitty/WikittyUtilTest.java =================================================================== --- trunk/wikitty-api/src/test/java/org/nuiton/wikitty/WikittyUtilTest.java 2012-03-08 23:48:59 UTC (rev 1449) +++ trunk/wikitty-api/src/test/java/org/nuiton/wikitty/WikittyUtilTest.java 2012-03-10 16:09:04 UTC (rev 1450) @@ -104,16 +104,16 @@ result = WikittyUtil.format( "Group Name = %"+WikittyGroupImpl.FQ_FIELD_WIKITTYGROUP_NAME+"toto$s", group.getWikitty()); - Assert.assertEquals("Group Name = ", result); + Assert.assertEquals("Group Name = null", result); // test un cas ou le champs demande n'existe pas dans le wikitty (sans remplacement) // avec deux fois le meme champs result = WikittyUtil.format( "Group Name = %"+WikittyGroupImpl.FQ_FIELD_WIKITTYGROUP_NAME+"toto$s(%"+WikittyGroupImpl.FQ_FIELD_WIKITTYGROUP_NAME+"tata$s)", group.getWikitty()); - Assert.assertEquals("Group Name = ()", result); + Assert.assertEquals("Group Name = null(null)", result); - // test un cas ou le champs demande n'existe pas dans le wikitty (sans remplacement) + // test un cas ou le champs demande n'existe pas dans le wikitty (avec remplacement vide) result = WikittyUtil.format( "Group Name = %"+WikittyGroupImpl.FQ_FIELD_WIKITTYGROUP_NAME+"toto|$s", group.getWikitty());
participants (1)
-
bpoussin@users.nuiton.org