/* ========================================================================
*
* This file is part of CODEC, which is a Java package for encoding
* and decoding ASN.1 data structures.
*
* Author: Fraunhofer Institute for Computer Graphics Research IGD
* Department A8: Security Technology
* Fraunhoferstr. 5, 64283 Darmstadt, Germany
*
* Rights: Copyright (c) 2004 by Fraunhofer-Gesellschaft
* zur Foerderung der angewandten Forschung e.V.
* Hansastr. 27c, 80686 Munich, Germany.
*
* ------------------------------------------------------------------------
*
* The software package is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of the
* License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software package; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA or obtain a copy of the license at
* http://www.fsf.org/licensing/licenses/lgpl.txt.
*
* ------------------------------------------------------------------------
*
* The CODEC library can solely be used and distributed according to
* the terms and conditions of the GNU Lesser General Public License .
*
* The CODEC library has not been tested for the use or application
* for a determined purpose. It is a developing version that can
* possibly contain errors. Therefore, Fraunhofer-Gesellschaft zur
* Foerderung der angewandten Forschung e.V. does not warrant that the
* operation of the CODEC library will be uninterrupted or error-free.
* Neither does Fraunhofer-Gesellschaft zur Foerderung der angewandten
* Forschung e.V. warrant that the CODEC library will operate and
* interact in an uninterrupted or error-free way together with the
* computer program libraries of third parties which the CODEC library
* accesses and which are distributed together with the CODEC library.
*
* Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
* does not warrant that the operation of the third parties's computer
* program libraries themselves which the CODEC library accesses will
* be uninterrupted or error-free.
*
* Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
* shall not be liable for any errors or direct, indirect, special,
* incidental or consequential damages, including lost profits resulting
* from the combination of the CODEC library with software of any user
* or of any third party or resulting from the implementation of the
* CODEC library in any products, systems or services of any user or
* of any third party.
*
* Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
* does not provide any warranty nor any liability that utilization of
* the CODEC library will not interfere with third party intellectual
* property rights or with any other protected third party rights or will
* cause damage to third parties. Fraunhofer Gesellschaft zur Foerderung
* der angewandten Forschung e.V. is currently not aware of any such
* rights.
*
* The CODEC library is supplied without any accompanying services.
*
* ========================================================================
*/
package codec.gen;
import codec.asn1.ASN1ObjectIdentifier;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.StringTokenizer;
import de.fhg.igd.logging.Logger;
import de.fhg.igd.logging.LoggerFactory;
/**
* @author Frank Lautenschl�ger
*
* Created on 10.10.2004
*
* The Util class is used as a java extension by several
* XSL transformations and contains methods for recurrent tasks which
* are easier to handle inside of an extansion class.
*
*/
public final class Util
{
/**
* Logger for this class
*/
private static Logger log = LoggerFactory.getLogger();
/**
* Formate date for JavaDoc.
*/
private static SimpleDateFormat sdf =
new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
/**
* Formate date for prepend to dump file path.
*/
private static SimpleDateFormat sdf2 =
new SimpleDateFormat("yyyyMMdd_hhmmssSSS");
/**
* maximum length of register indentifiers
*/
private static int maxIndentifierLength = 0;
/**
* maximum length of register asn1Types
*/
private static int maxASN1Type = 0;
/**
* No-one can instantiate this class.
*/
private Util()
{
}
/**
* This method checks if a given string could be used
* to instandiate an codec.asn1.ASN1ObjectIdentifier.
*
* @param oid to check
*
* @return true if oid is a valid codec.asn1.ASN1ObjectIdentifier
*/
public static boolean isObjectIdentifier(String oid)
{
ASN1ObjectIdentifier aSN1ObjectIdentifier = null;
try
{
aSN1ObjectIdentifier = new ASN1ObjectIdentifier(oid);
}
catch (Exception e)
{
return false;
}
return true;
}
/**
* Returns the current date with following format dd-MM-yyyy hh:mm:ss
* @return current date
*/
public static String getCurrentDate()
{
return sdf.format(new GregorianCalendar().getTime());
}
/**
* Returns the current date with following format yyyyMMdd_hhmmss
* @return current date
*/
public static String getShortDate()
{
return sdf2.format(new GregorianCalendar().getTime());
}
/**
* This method converts a given ASN.1 identifier to
* a valid Java identifier.
*
* @param str The ASN.1 identifier
*
* @return a Java identifier
*/
public static String buildClassName(String str)
{
str = firstToUpper(str);
str = str.replace('-', '_');
return str;
}
/**
* This method converts a given ASN.1 identifier to
* a valid uppercase Java identifier.
*
* @param str The ASN.1 identifier
*
* @return a uppercase Java identifier
*/
public static String buildUpperCaseJavaIdentifier(String str)
{
return buildClassName(str).toUpperCase();
}
/**
* This method
*
* @param str DOCUMENT ME!
*
* @return DOCUMENT ME!
*/
private static String firstToUpper(String str)
{
return str.substring(0, 1).toUpperCase() + str.substring(1);
}
/**
* This method replaces the dots in package_ by '/'.
*
* @param package_ A Java package structure
*
* @return A corresponding path for the given package structure
*/
public static String packageToPath(String package_)
{
if ((package_ == null) || package_.equals(""))
{
return "";
}
return package_.replace('.', '/') + '/';
}
/**
* DOCUMENT ME!
*
* @param str DOCUMENT ME!
*/
public static void calcIndentifierLength(String str)
{
if (str.length() > maxIndentifierLength)
{
maxIndentifierLength = str.length();
}
}
/**
* DOCUMENT ME!
*
* @param str DOCUMENT ME!
*/
public static void calcASN1TypeLength(String str)
{
if (str.length() > maxASN1Type)
{
maxASN1Type = str.length();
}
}
/**
* DOCUMENT ME!
*/
public static void reset()
{
maxIndentifierLength = 0;
maxASN1Type = 0;
}
/**
* DOCUMENT ME!
*
* @param identifier DOCUMENT ME!
* @param number DOCUMENT ME!
*
* @return DOCUMENT ME!
*/
public static String format(String identifier, String number)
{
StringBuffer buf = new StringBuffer("* " + identifier);
if ((identifier != null) && !identifier.trim().equals(""))
{
for (int i = identifier.length(); i < (maxIndentifierLength + 1);
i++)
{
buf.append(' ');
}
}
buf.append('(' + number + ')');
return ' '+ buf.toString().trim();
}
/**
* Returns a correct indented Javadoc line used for the ASN.1 definition.
*
* @param identifier The ASN.1 type identifier
* @param number The ASN.1 tag
* @param asn1Type The ASN.1 type
* @param optional The optional flag
* @param default_ The default flag
*
* @return A correct indented Javadoc line used for the ASN.1 definition.
*/
public static String format(
String identifier,
String number,
String asn1Type,
String optional,
String default_)
{
StringBuffer buf = new StringBuffer("* " + identifier);
if ((identifier != null) && !identifier.trim().equals(""))
{
for (int i = identifier.length(); i < (maxIndentifierLength + 1);
i++)
{
buf.append(' ');
}
}
int currentBufferLength = buf.length();
if ((number != null) && !number.trim().equals(""))
{
buf.append("[" + number.trim() + "] ");
}
else
{
buf.append(" ");
}
buf.append(asn1Type);
if ((asn1Type != null) && !asn1Type.trim().equals(""))
{
for (int i = asn1Type.length(); i < (maxASN1Type + 1); i++)
{
buf.append(' ');
}
}
if (optional.trim().equals("true"))
{
buf.append(" OPTIONAL");
}
if (default_.trim().equals("true"))
{
buf.append(" DEFAULT ");
}
return ' ' + buf.toString().trim();
}
/**
* Replaces the System path separator by / and prepend a slash
* if nessecary.
* @param targetFile relative path to convert
* @return absolute path
*/
public static String replacePathSeparator(String targetFile)
{
String absolutePath = new File(targetFile).getAbsolutePath();
if (File.separatorChar != '/')
{
StringTokenizer st =
new StringTokenizer(absolutePath, File.separator);
StringBuffer buffer = new StringBuffer();
while (st.hasMoreTokens())
{
String token = st.nextToken();
buffer.append("/");
buffer.append(token);
}
return buffer.append("/").toString();
}
else
{
return absolutePath + '/';
}
}
}