
org.bimserver.shared.GuidCompressor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of PluginBase Show documentation
Show all versions of PluginBase Show documentation
Base project for BIMserver plugin development. Some plugins mights also need the Shared library
package org.bimserver.shared;
/******************************************************************************
* Copyright (C) 2009-2016 BIMserver.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see {@literal }.
*****************************************************************************/
import java.util.UUID;
/**
* This class is a service class providing methods to generation and conversion between compressed
* and uncompressed string representations of GUIDs according to the algorithms used by the
* Industry Foundation Classes (IFC). The algorithm is based on an implementation in c as follows:
* originally proposed by Jim Forester
* implemented previously by Jeremy Tammik using hex-encoding
* Peter Muigg, June 1998
* Janos Maros, July 2000
* This class is provided as-is with no warranty.
*
* The class GuidCompressor is part of the OPEN IFC JAVA TOOLBOX package. Copyright:
* CCPL BY-NC-SA 3.0 (cc) 2008 Eike Tauscher, Jan Tulke
*
* The OPEN IFC JAVA TOOLBOX package itself (except this class) is licensed under
* Creative Commons
* Attribution-Non-Commercial- Share Alike 3.0 Germany.
* Please visit http://www.openifctools.com for more
* information.
*
* @author Jan Tulke
* @version 1.0 - 24.07.2009
*
*/
public class GuidCompressor {
static char[] cConversionTable = new char[]{'0','1','2','3','4','5','6','7','8','9',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'_','$'};
/**
* Generates a new GUID and returns a compressed string representation as used for IfcGloballyUniqueId
* @return String with a length of 22 characters
*/
public static String getNewIfcGloballyUniqueId(){
Guid guid = getGuidFromUncompressedString(UUID.randomUUID().toString());
String shortString = getCompressedStringFromGuid(guid);
return shortString;
}
/**
* Converts an uncompressed String representation in an Guid-object
* @param uncompressedGuidString the uncompressed String representation of a GUID
* @return an Guid-object
*/
private static Guid getGuidFromUncompressedString(String uncompressedGuidString){
String[] parts = uncompressedGuidString.split("-");
Guid guid = new Guid();
guid.Data1 = Long.parseLong(parts[0], 16);
guid.Data2 = Integer.parseInt(parts[1],16);
guid.Data3 = Integer.parseInt(parts[2], 16);
String temp;
temp = parts[3];
guid.Data4[0] = (char) Integer.parseInt(temp.substring(0, 2), 16);
guid.Data4[1] = (char) Integer.parseInt(temp.substring(2, 4), 16);
temp = parts[4];
guid.Data4[2] = (char) Integer.parseInt(temp.substring(0, 2), 16);
guid.Data4[3] = (char) Integer.parseInt(temp.substring(2, 4), 16);
guid.Data4[4] = (char) Integer.parseInt(temp.substring(4, 6), 16);
guid.Data4[5] = (char) Integer.parseInt(temp.substring(6, 8), 16);
guid.Data4[6] = (char) Integer.parseInt(temp.substring(8, 10), 16);
guid.Data4[7] = (char) Integer.parseInt(temp.substring(10, 12), 16);
return guid;
}
/**
* Converts a Guid-object into a compressed string representation of a GUID
* @param guid the Guid-object
* @return String with a length of 22 characters
*/
private static String getCompressedStringFromGuid(Guid guid)
{
long[] num = new long[6];
char[][] str = new char[6][5];
int i,j,n;
String result = new String();
//
// Creation of six 32 Bit integers from the components of the GUID structure
//
num[0] = (long)(guid.Data1 / 16777216); // 16. byte (pGuid->Data1 / 16777216) is the same as (pGuid->Data1 >> 24)
num[1] = (long)(guid.Data1 % 16777216); // 15-13. bytes (pGuid->Data1 % 16777216) is the same as (pGuid->Data1 & 0xFFFFFF)
num[2] = (long)(guid.Data2 * 256 + guid.Data3 / 256); // 12-10. bytes
num[3] = (long)((guid.Data3 % 256) * 65536 + guid.Data4[0] * 256 + guid.Data4[1]); // 09-07. bytes
num[4] = (long)(guid.Data4[2] * 65536 + guid.Data4[3] * 256 + guid.Data4[4]); // 06-04. bytes
num[5] = (long)(guid.Data4[5] * 65536 + guid.Data4[6] * 256 + guid.Data4[7]); // 03-01. bytes
//
// Conversion of the numbers into a system using a base of 64
//
n = 3;
for (i = 0; i < 6; i++) {
if (!cv_to_64 (num[i], str[i], n)) {
return null;
}
for(j = 0; j 5)
return false;
act = number;
nDigits = len - 1;
for (iDigit = 0; iDigit < nDigits; iDigit++) {
result[nDigits - iDigit - 1] = cConversionTable[(int) (act % 64)];
act /= 64;
}
result[len - 1] = '\0';
if (act != 0)
return false;
for(int i = 0; i
© 2015 - 2025 Weber Informatics LLC | Privacy Policy