Package codec.gen

Source Code of codec.gen.Repository

/* ========================================================================
*
*  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.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import de.fhg.igd.logging.LogLevel;
import de.fhg.igd.logging.Logger;
import de.fhg.igd.logging.LoggerFactory;


/**
* @author Frank Lautenschl�ger
*
* Created on 29.10.2004
*
* The Repository class is used to handle import declarations inside of
* an ASN1 module definition. The class is used by XSLgenerateMapping.xml to
* generate a mapping file based on the ASN.1 module definition and
* mapping information inside of loaded property files. During the mapping
* process the 'oid*.map' property files are loaded into the internal hashtable.
* This load process is based on the packages defined in the configuration file.
* Afterwards the class provides this mapping information via
* <code>getQualifiedClassname()</code> and <code>getClassname()</code>.
*/
public final class Repository
{
    /**
     * Logger for this class
     */
    private static Logger log = LoggerFactory.getLogger();

    /**
     * Contains all type references without a corresponding mapping
     */
    private static HashSet missingTyes = new HashSet();

    /**
     * Map contains the loaded mappings of OIDs to classes
     */
    private static Hashtable map = new Hashtable();

    /**
     * The map modules contains a Hashtable for each ASN.1 module
     */
    private static Hashtable modules = new Hashtable();


    /**
     * No-one can instantiate this class.
     */
    private Repository()
    {
    }

    /**
     * This method retrives the corresponding class name
     * from the repository.
     * @param moduleOID oid of module definition
     * @param typereference referenced type within module
     * @return Returns name of class if lookup was succesful
     * otherwise returns ?typereference
     */
    private static String lookupClassname(
        String moduleOID,
        String typereference)
    {
        ASN1ObjectIdentifier aSN1ObjectIdentifier =
            new ASN1ObjectIdentifier(moduleOID.trim());
        Hashtable moduleDefinition                =
            (Hashtable)modules.get(aSN1ObjectIdentifier);

        if (moduleDefinition == null)
        {
            log.debug("lookup '" + moduleOID + "' + '"
                + typereference + "' failed");
            if (!missingTyes.contains(moduleOID + ";? = " + typereference))
            {
                missingTyes.add(moduleOID + ";" + typereference + " = ?");
            }
            return "?" + typereference;
        }

        String classname = (String)moduleDefinition.get(typereference);
        if (classname == null)
        {
            log.debug("lookup '" + moduleOID + "' + '"
                + typereference + "' failed");
            if (!missingTyes.contains(moduleOID + ";? = " + typereference))
            {
                missingTyes.add(moduleOID + ";" + typereference + " = ?");
            }
            return "?" + typereference;
        }
        log.info(
            "mapped '" + moduleOID + "' + '" + typereference
            + "' --> '" + classname + "'");
        return classname;
    }


    /**
     * Retrieves a String containing the full qualified classname
     * of the class for the given OID or <code>null</code>
     * if no such type was found.
     *
     * @param moduleOID oid of module definition
     * @param typereference referenced type within module
     * @return qualified classname
     */
    public static String getQualifiedClassname(
        String moduleOID,
        String typereference)
    {
        return lookupClassname(moduleOID, typereference);
    }


    /**
     * Retrieves a String containing the name of the class for the given OID or
     * <code>null</code> if no such type was found.
     *
     * @param moduleOID oid of module definition
     * @param typereference referenced type within module
     * @return The classname without package information
     */
    public static String getClassName(String moduleOID, String typereference)
    {
        String classname = lookupClassname(moduleOID, typereference);
        int lastDot      = classname.lastIndexOf('.');
        if (lastDot == -1)
        {
            return classname;
        }
        else
        {
            return classname.substring(lastDot + 1);
        }
    }


    /**
     * This method returns true if one or more mappings could not be resolved.
     *
     * @return true if one or more mappings could not be resolved.
     */
    public static boolean hasErrors()
    {
        return !missingTyes.isEmpty();
    }


    /**
     * Returns mapping files containing all typereferences which
     * could not resolved.
     *
     * @return String containing a list of all mappings
     * which could not be mapped.
     */
    public static String getMissingMappings()
    {
        StringBuffer buffer = new StringBuffer();
        buffer.append(
            "#----------------missing mappings----------------------\n");
        log.info("#----------------missing mappings----------------------");
        for (Iterator iter = missingTyes.iterator(); iter.hasNext();)
        {
            String element = (String)iter.next();

            buffer.append(element + "\n");
            log.info(element);
        }
        buffer.append(
            "#------------------------------------------------------\n");
        log.info("#------------------------------------------------------");

        String missingMappings = buffer.toString();

        return missingMappings;
    }


    /**
     * Clear the known mappings.
     *
     */
    public static void reset()
    {
        map.clear();
        modules.clear();
        missingTyes.clear();
    }


    /**
     * Loads all property files of the form 'oid*.map' from the given path.
     * @param path relative to classpath e.g. 'codec/asn1'
     */
    public static void loadOIDMap(String path)
    {
        ASN1ObjectIdentifier oid;
        InputStream in;
        Properties props;
        Map.Entry entry;
        Iterator i;
        String key;
        int n;

        if (path == null)
        {
            throw new NullPointerException("path");
        }

        n      = 0;
        in     = ClassLoader.getSystemResourceAsStream(path + "/oid0.map");

        // trying different loaders and paths
        if (in == null)
        {
            in = Repository.class.getResourceAsStream(path + "/oid0.map");

            if (in == null)
            {
                in = Repository.class.getResourceAsStream(
                        "/" + path + "/oid0.map");

                if (in == null)
                {
                    log.error("Warning: could not get resource at " + path);
                }
            }
        }

        while (in != null)
        {
            try
            {
                props = new Properties();
                props.load(in);

                for (i = props.entrySet().iterator(); i.hasNext();)
                {
                    entry     = (Map.Entry)i.next();
                    key       = (String)entry.getKey();

                    String classname = (String)entry.getValue();
                    String typereference = null;

                    int indexOfSemicolon = key.indexOf(';');
                    if (indexOfSemicolon != -1)
                    {
                        typereference     = key.substring(
                                indexOfSemicolon + 1);
                        key               = key.substring(
                                0,
                                indexOfSemicolon);
                    }
                    oid = new ASN1ObjectIdentifier(key.trim());

                    // oid to class mapping
                    map.put(oid, classname);
                    log.debug("map '" + key + "' -> '" + classname + "'");
                    if (typereference != null)
                    {
                        // module + typereference to class mapping
                        key     = key.substring(0, key.lastIndexOf('.'));
                        oid     = new ASN1ObjectIdentifier(key.trim());

                        Hashtable module = null;

                        if (modules.containsKey(oid))
                        {
                            module = (Hashtable)modules.get(oid);
                        }
                        else
                        {
                            module = new Hashtable();
                            modules.put(oid, module);
                        }
                        module.put(typereference, classname);
                    }
                }
                log.info("mappings from " + path + " processed");
            }
            catch (IOException e)
            {
                log.caught(LogLevel.ERROR, "Bad OID map: " + path + "/oid" + n + ".map", e);
            }
            finally
            {
                try
                {
                    in.close();
                }
                catch (IOException e)
                {
                    log.error("Error occured while closing of inputstream" + e);
                }
            }
            n++;
            in = ClassLoader.getSystemResourceAsStream(
                    path + "/oid" + n + ".map");
        }
        if (map.size() == 0)
        {
            log.log(LogLevel.WARNING, "no OIDs loaded from " + path);
        }
    }
}
TOP

Related Classes of codec.gen.Repository

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.