]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
5e2f96870296e4f2c3ec4545ecf0e546ecb650fc
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.id.part;
2
3 import java.io.PrintWriter;
4 import java.io.StringWriter;
5 import java.util.IllegalFormatException;
6
7 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory;
9
10 // Uses Java's interpreter for printf-style format strings.
11 // http://java.sun.com/javase/1.6/docs/api/java/util/Formatter.html
12
13 public class JavaPrintfIDPartOutputFormatter implements IDPartOutputFormatter {
14
15     final Logger logger =
16         LoggerFactory.getLogger(JavaPrintfIDPartOutputFormatter.class);
17
18     StringWriter stringwriter = new StringWriter();
19     private int maxOutputLength = DEFAULT_MAX_OUTPUT_LENGTH;
20     private String formatPattern;
21
22     public JavaPrintfIDPartOutputFormatter() {
23     }
24
25     public JavaPrintfIDPartOutputFormatter(String formatPattern) {
26         setFormatPattern(formatPattern);
27     }
28
29     @Override
30     public int getMaxOutputLength() {
31         return this.maxOutputLength;
32     }
33
34     @Override
35     public void setMaxOutputLength(int length) {
36         this.maxOutputLength = length;
37     }
38
39     @Override
40     public String getFormatPattern() {
41         return this.formatPattern;
42     }
43
44     @Override
45     public void setFormatPattern(String pattern) {
46         if (pattern == null || pattern.trim().isEmpty()) {
47             logger.error("Format pattern cannot be null or empty.");
48         } else {
49             this.formatPattern = pattern;
50         }
51     }
52
53
54     @Override
55     public String format(String id) {
56
57         String formattedID = id;
58
59         String pattern = getFormatPattern();
60
61         // If the formatting pattern is empty, just check length.
62         if (pattern == null || pattern.trim().isEmpty()) {
63
64             if (! isValidLength(formattedID)) {
65                 logger.error(
66                     "Formatted ID '" + formattedID +
67                     "' exceeds maximum length of " +
68                     getMaxOutputLength() + " characters." +
69                     "Returning ID without formatting.");
70                 return id;
71             }
72
73         // Otherwise, format the ID using the pattern, then check length.
74         } else {
75             // Clear the StringWriter's buffer from its last usage. 
76             StringBuffer buf = stringwriter.getBuffer();
77             buf.setLength(0);
78             // Apply the formatting pattern to the ID.
79             try {
80                 PrintWriter printwriter = new PrintWriter(stringwriter);
81                 printwriter.printf(id, pattern);
82                 formattedID = stringwriter.toString();
83             } catch(IllegalFormatException e) {
84                 // @TODO Log and handle this exception.
85             }
86             isValidLength(formattedID);
87         }
88
89         return formattedID;
90     }
91
92     // Check whether the formatted ID exceeds the specified maximum length.
93
94     private boolean isValidLength(String id) {
95         if (id.length() <= getMaxOutputLength()) {
96             return true;
97         } else {
98             return false;
99         }
100     }
101
102 }
103