KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > exception > TemplatedViolatedConstraintNameExtracter


1 // $Id: TemplatedViolatedConstraintNameExtracter.java,v 1.2 2004/11/21 00:11:27 pgmjsd Exp $
2
package org.hibernate.exception;
3
4
5
6 /**
7  * Knows how to extract a violated constraint name from an error message based on the
8  * fact that the constraint name is templated within the message.
9  *
10  * @author Steve Ebersole
11  */

12 public abstract class TemplatedViolatedConstraintNameExtracter implements ViolatedConstraintNameExtracter {
13
14     /**
15      * Extracts the constraint name based on a template (i.e., <i>templateStart</i><b>constraintName</b><i>templateEnd</i>).
16      *
17      * @param templateStart The pattern denoting the start of the constraint name within the message.
18      * @param templateEnd The pattern denoting the end of the constraint name within the message.
19      * @param message The templated error message containing the constraint name.
20      * @return The found constraint name, or null.
21      */

22     protected String JavaDoc extractUsingTemplate(String JavaDoc templateStart, String JavaDoc templateEnd, String JavaDoc 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 }
38
Popular Tags