01: // $Id: TemplatedViolatedConstraintNameExtracter.java 4782 2004-11-21 00:11:27Z pgmjsd $
02: package org.hibernate.exception;
03:
04: /**
05: * Knows how to extract a violated constraint name from an error message based on the
06: * fact that the constraint name is templated within the message.
07: *
08: * @author Steve Ebersole
09: */
10: public abstract class TemplatedViolatedConstraintNameExtracter
11: implements ViolatedConstraintNameExtracter {
12:
13: /**
14: * Extracts the constraint name based on a template (i.e., <i>templateStart</i><b>constraintName</b><i>templateEnd</i>).
15: *
16: * @param templateStart The pattern denoting the start of the constraint name within the message.
17: * @param templateEnd The pattern denoting the end of the constraint name within the message.
18: * @param message The templated error message containing the constraint name.
19: * @return The found constraint name, or null.
20: */
21: protected String extractUsingTemplate(String templateStart,
22: String templateEnd, String message) {
23: int templateStartPosition = message.indexOf(templateStart);
24: if (templateStartPosition < 0) {
25: return null;
26: }
27:
28: int start = templateStartPosition + templateStart.length();
29: int end = message.indexOf(templateEnd, start);
30: if (end < 0) {
31: end = message.length();
32: }
33:
34: return message.substring(start, end);
35: }
36:
37: }
|