From: Chad B. <cwb...@us...> - 2008-05-14 20:29:08
|
User: cwbrandon Date: 08/05/14 13:29:14 Modified: andromda-jsf2/src/main/resources/templates/jsf2/utils MessagePhaseListener.java.vsl Log: Add ability to set override default jsf validation messages. Revision Changes Path 1.3 +73 -1 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.2 retrieving revision 1.3 diff -u -w -r1.2 -r1.3 --- MessagePhaseListener.java.vsl 21 Mar 2008 16:07:13 -0000 1.2 +++ MessagePhaseListener.java.vsl 14 May 2008 20:29:14 -0000 1.3 @@ -17,11 +17,37 @@ @Override protected void handleBeforePhase(javax.faces.event.PhaseEvent event) { + final javax.faces.context.FacesContext context = event.getFacesContext(); + final javax.faces.component.UIViewRoot root = context.getViewRoot(); + for (final java.util.Iterator iterator = context.getClientIdsWithMessages(); iterator.hasNext();) + { + final String clientId = (String) iterator.next(); + final javax.faces.component.UIComponent component = root.findComponent(clientId); + for (final java.util.Iterator iterator2 = context.getMessages(clientId); iterator2.hasNext();) + { + final javax.faces.application.FacesMessage facesMessage = (javax.faces.application.FacesMessage)iterator2.next(); + final String messageKey = this.getMessageKey(facesMessage.getDetail()); + 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)); + } + } + } + } #if ($standalone) org.apache.myfaces.trinidad.context.RequestContext context = org.apache.myfaces.trinidad.context.RequestContext.getCurrentInstance(); final Object form = context != null ? context.getPageFlowScope().get("$actionFormKey") : null; #else - final javax.faces.context.FacesContext context = event.getFacesContext(); final Object form = context != null ? context.getApplication().getVariableResolver().resolveVariable(context, "$actionFormKey") : null; #end if (form != null) @@ -56,4 +82,50 @@ { return javax.faces.event.PhaseId.RENDER_RESPONSE; } + + private String getMessageKey(final String detail) + { + 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 |