From: Chad B. <cwb...@us...> - 2008-05-14 22:22:07
|
User: cwbrandon Date: 08/05/14 15:22:13 Modified: andromda-jsf2/src/main/resources/templates/jsf2/utils MessagePhaseListener.java.vsl Log: message handling improvement Revision Changes Path 1.4 +19 -52 cartridges/andromda-jsf2/src/main/resources/templates/jsf2/utils/MessagePhaseListener.java.vsl Index: MessagePhaseListener.java.vsl =================================================================== RCS file: /cvsroot/andromdaplugins/cartridges/andromda-jsf2/src/main/resources/templates/jsf2/utils/MessagePhaseListener.java.vsl,v retrieving revision 1.3 retrieving revision 1.4 diff -u -w -r1.3 -r1.4 --- MessagePhaseListener.java.vsl 14 May 2008 20:29:14 -0000 1.3 +++ MessagePhaseListener.java.vsl 14 May 2008 22:22:12 -0000 1.4 @@ -14,6 +14,8 @@ { private static final long serialVersionUID = 1L; + private static final String ARGUMENT_PREFIX = "arg:"; + @Override protected void handleBeforePhase(javax.faces.event.PhaseEvent event) { @@ -23,6 +25,22 @@ { final String clientId = (String) iterator.next(); final javax.faces.component.UIComponent component = root.findComponent(clientId); + final java.util.Collection<Object> arguments = new java.util.ArrayList<Object>(); + for (final java.util.Iterator iterator2 = component.getChildren().iterator(); iterator2.hasNext();) + { + final Object child = iterator2.next(); + if (child instanceof javax.faces.component.UIParameter) + { + final javax.faces.component.UIParameter parameter = (javax.faces.component.UIParameter)child; + if (parameter.getName() != null) + { + if (parameter.getName().startsWith(ARGUMENT_PREFIX)) + { + arguments.add(parameter.getValue()); + } + } + } + } for (final java.util.Iterator iterator2 = context.getMessages(clientId); iterator2.hasNext();) { final javax.faces.application.FacesMessage facesMessage = (javax.faces.application.FacesMessage)iterator2.next(); @@ -30,17 +48,7 @@ final String newMessage = Messages.get(messageKey, null); if (!newMessage.equals(messageKey)) { - final int maxIndex = this.getMaxIndex(newMessage); - if (maxIndex > N0_ARGUMENTS) - { - final Object[] arguments = new Object[maxIndex + 1]; - for (int ctr = 0; ctr <= maxIndex; ctr++) - { - final Object argument = component.getAttributes().get("arg" + ctr); - arguments[ctr] = argument == null ? "{" + ctr + "}" : argument; - } - facesMessage.setDetail(java.text.MessageFormat.format(newMessage, arguments)); - } + facesMessage.setDetail(java.text.MessageFormat.format(newMessage, arguments.toArray(new Object[0]))); } } } @@ -87,45 +95,4 @@ { return detail != null ? detail.replaceAll(".*:", "").replace(".", "").trim().replaceAll("\\s+", ".").toLowerCase() : null; } - - private static final String ARGUMENT_PATTERN = "(\\{\\s*\\d+\\s*\\})"; - - private static final String REPLACEMENT_PATTERN = "\\{\\s*|\\}\\s*"; - - private static final int N0_ARGUMENTS = -1; - - /** - * Gets the max index from the given string (the max index of the arguments in the format - * {0}, {1}, etc). - * - * @param string the string from which to retrieve the max index. - * @return the max index. - */ - private int getMaxIndex(String string) - { - int maxIndex = N0_ARGUMENTS; - final java.util.regex.Matcher matcher = java.util.regex.Pattern.compile(ARGUMENT_PATTERN).matcher(string); - if (matcher.find()) - { - do - { - final String indexString = matcher.group().replaceAll(REPLACEMENT_PATTERN, ""); - try - { - int index = Integer.valueOf(indexString); - if (index > maxIndex) - { - maxIndex = index; - } - } - catch (NumberFormatException exception) - { - // - ignore, just don't attempt replacement - } - - } - while (matcher.find()); - } - return maxIndex; - } } \ No newline at end of file |