KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > xml > fastinfoset > algorithm > UUIDEncodingAlgorithm


1 /*
2  * Fast Infoset ver. 0.1 software ("Software")
3  *
4  * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Software is licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License. You may
8  * obtain a copy of the License at:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations.
16  *
17  * Sun supports and benefits from the global community of open source
18  * developers, and thanks the community for its important contributions and
19  * open standards-based technology, which Sun has adopted into many of its
20  * products.
21  *
22  * Please note that portions of Software may be provided with notices and
23  * open source licenses from such communities and third parties that govern the
24  * use of those portions, and any licenses granted hereunder do not alter any
25  * rights and obligations you may have under such open source licenses,
26  * however, the disclaimer of warranty and limitation of liability provisions
27  * in this License will apply to all Software in this distribution.
28  *
29  * You acknowledge that the Software is not designed, licensed or intended
30  * for use in the design, construction, operation or maintenance of any nuclear
31  * facility.
32  *
33  * Apache License
34  * Version 2.0, January 2004
35  * http://www.apache.org/licenses/
36  *
37  */

38
39 package com.sun.xml.fastinfoset.algorithm;
40
41 import com.sun.xml.fastinfoset.algorithm.BuiltInEncodingAlgorithm.WordListener;
42 import java.util.ArrayList JavaDoc;
43 import java.util.List JavaDoc;
44 import java.io.EOFException JavaDoc;
45 import java.io.IOException JavaDoc;
46 import java.io.InputStream JavaDoc;
47 import java.io.OutputStream JavaDoc;
48 import java.nio.CharBuffer JavaDoc;
49 import java.util.ArrayList JavaDoc;
50 import org.jvnet.fastinfoset.EncodingAlgorithmException;
51 import com.sun.xml.fastinfoset.CommonResourceBundle;
52
53 public class UUIDEncodingAlgorithm extends LongEncodingAlgorithm {
54     
55     public final int getPrimtiveLengthFromOctetLength(int octetLength) throws EncodingAlgorithmException {
56         if (octetLength % (LONG_SIZE * 2) != 0) {
57             throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().
58                     getString("message.lengthNotMultipleOfUUID",new Object JavaDoc[]{new Integer JavaDoc(LONG_SIZE * 2)}));
59         }
60         
61         return octetLength / LONG_SIZE;
62     }
63     
64     public final Object JavaDoc convertFromCharacters(char[] ch, int start, int length) {
65         final CharBuffer JavaDoc cb = CharBuffer.wrap(ch, start, length);
66         final List JavaDoc longList = new ArrayList JavaDoc();
67         
68         matchWhiteSpaceDelimnatedWords(cb,
69                 new WordListener() {
70             public void word(int start, int end) {
71                 String JavaDoc uuidValue = cb.subSequence(start, end).toString();
72                 fromUUIDString(uuidValue);
73                 longList.add(new Long JavaDoc(_msb));
74                 longList.add(new Long JavaDoc(_lsb));
75             }
76         }
77         );
78         
79         return generateArrayFromList(longList);
80     }
81     
82     public final void convertToCharacters(Object JavaDoc data, StringBuffer JavaDoc s) {
83         if (!(data instanceof long[])) {
84             throw new IllegalArgumentException JavaDoc(CommonResourceBundle.getInstance().getString("message.dataNotLongArray"));
85         }
86         
87         final long[] ldata = (long[])data;
88
89         final int end = ldata.length - 1;
90         for (int i = 0; i <= end; i += 2) {
91             s.append(toUUIDString(ldata[i], ldata[i + 1]));
92             if (i != end) {
93                 s.append(' ');
94             }
95         }
96     }
97
98     
99     private long _msb;
100     private long _lsb;
101     
102     final void fromUUIDString(String JavaDoc name) {
103         String JavaDoc[] components = name.split("-");
104         if (components.length != 5)
105             throw new IllegalArgumentException JavaDoc(CommonResourceBundle.getInstance().
106                     getString("message.invalidUUID", new Object JavaDoc[]{name}));
107                     
108         for (int i=0; i<5; i++)
109             components[i] = "0x"+components[i];
110
111         _msb = Long.parseLong(components[0], 16);
112         _msb <<= 16;
113         _msb |= Long.parseLong(components[1], 16);
114         _msb <<= 16;
115         _msb |= Long.parseLong(components[2], 16);
116
117         _lsb = Long.parseLong(components[3], 16);
118         _lsb <<= 48;
119         _lsb |= Long.parseLong(components[4], 16);
120     }
121
122     final String JavaDoc toUUIDString(long msb, long lsb) {
123     return (digits(msb >> 32, 8) + "-" +
124         digits(msb >> 16, 4) + "-" +
125         digits(msb, 4) + "-" +
126         digits(lsb >> 48, 4) + "-" +
127         digits(lsb, 12));
128     }
129     
130     final String JavaDoc digits(long val, int digits) {
131     long hi = 1L << (digits * 4);
132     return Long.toHexString(hi | (val & (hi - 1))).substring(1);
133     }
134     
135 }
136
Popular Tags