View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2002-2008, DbUnit.org
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2.1 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   *
20   */
21  
22  package org.dbunit.dataset.datatype;
23  
24  import java.util.UUID;
25  import java.util.regex.Matcher;
26  import java.util.regex.Pattern;
27  
28  /**
29   * <p>
30   * A datatype that is capable of storing UUIDs into BINARY fields (big-endian).
31   * </p>
32   * <p>
33   * For the UUID to be detected as such, the string value of the field has to be
34   * in the form of {@code uuid'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'}, where the
35   * x's are the actual string value of the UUID, hex-encoded. Example:
36   * </p>
37   * 
38   * <pre>
39   *     &lt;company id="uuid'791ae85a-d8d0-11e2-8c43-50e549c9b654'" name="ACME"/&gt;
40   * </pre>
41   * 
42   * @author Timur Strekalov
43   */
44  public class UuidAwareBytesDataType extends BytesDataType
45  {
46      /**
47       * The regular expression for a hexadecimal UUID representation.
48       */
49      private static final Pattern UUID_RE =
50              Pattern.compile("uuid'([0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12})'");
51  
52      UuidAwareBytesDataType(final String name, final int sqlType)
53      {
54          super(name, sqlType);
55      }
56  
57      @Override
58      public Object typeCast(final Object value) throws TypeCastException
59      {
60          return super.typeCast(uuidAwareValueOf(value));
61      }
62  
63      private static Object uuidAwareValueOf(final Object value)
64      {
65          if (value instanceof String)
66          {
67              final String s = (String) value;
68              final Matcher m = UUID_RE.matcher(s);
69  
70              if (m.find())
71              {
72                  final UUID uuid = UUID.fromString(m.group(1));
73                  return uuidToBytes(uuid);
74              }
75          }
76  
77          return value;
78      }
79  
80      private static byte[] uuidToBytes(final UUID uuid)
81      {
82          final long msb = uuid.getMostSignificantBits();
83          final long lsb = uuid.getLeastSignificantBits();
84  
85          return new byte[] {extractByte(msb, 0), extractByte(msb, 1),
86                  extractByte(msb, 2), extractByte(msb, 3), extractByte(msb, 4),
87                  extractByte(msb, 5), extractByte(msb, 6), extractByte(msb, 7),
88                  extractByte(lsb, 0), extractByte(lsb, 1), extractByte(lsb, 2),
89                  extractByte(lsb, 3), extractByte(lsb, 4), extractByte(lsb, 5),
90                  extractByte(lsb, 6), extractByte(lsb, 7)};
91      }
92  
93      private static byte extractByte(final long value, final int byteIndex)
94      {
95          return (byte) (value >> (56 - byteIndex * 8) & 0xff);
96      }
97  }