001package org.biopax.paxtools.model;
002
003import org.apache.commons.logging.Log;
004import org.apache.commons.logging.LogFactory;
005import org.biopax.paxtools.util.IllegalBioPAXArgumentException;
006
007import java.io.InputStream;
008
009/**
010 * Enumeration type for BioPAX levels.
011 */
012
013public enum BioPAXLevel
014{
015        // define enum constants using default BioPAX factories
016        // (L1 is auto-upgraded and uses L2 factory)
017        L1("biopax-level1.owl", new Level2FactoryImpl(), "org.biopax.paxtools.model.level2"),
018        L2("biopax-level2.owl", new Level2FactoryImpl(), "org.biopax.paxtools.model.level2"),
019        L3("biopax-level3.owl", new Level3FactoryImpl(), "org.biopax.paxtools.model.level3");
020
021        // ------------------------------ FIELDS ------------------------------
022        private static Log log = LogFactory.getLog(BioPAXLevel.class);
023
024        private final String filename;
025
026        private BioPAXFactory factory;
027
028        private final String packageName;
029        
030        // default L2 (also for L1) factory implementation
031        private static class Level2FactoryImpl extends BioPAXFactory {
032            @Override
033            public BioPAXLevel getLevel() {
034                return BioPAXLevel.L2;
035            }
036            
037            public String mapClassName(Class<? extends BioPAXElement> aClass) 
038            {
039                String name = "org.biopax.paxtools.impl.level2."
040                        + aClass.getSimpleName()
041                        + "Impl";
042                return name;
043            }  
044        }
045        
046        // default L3 factory implementation
047        private static class Level3FactoryImpl extends BioPAXFactory {
048            @Override
049            public BioPAXLevel getLevel() {
050                return BioPAXLevel.L3;
051            }
052            
053            public String mapClassName(Class<? extends BioPAXElement> aClass) 
054            {
055                String name = "org.biopax.paxtools.impl.level3."
056                        + aClass.getSimpleName()
057                        + "Impl";
058                return name;
059            }  
060        }
061        
062
063        /**
064         * This is the prefix used for all biopax releases.
065         */
066        public static final String BP_PREFIX = "http://www.biopax.org/release/";
067
068// --------------------------- CONSTRUCTORS ---------------------------
069
070        /**
071         * Default constructor
072         * @param filename File name of the owl file.
073         * @param factory BioPAX factory implementation (default)
074         * @param pm package name of the model implementation
075         */
076        BioPAXLevel(String filename, BioPAXFactory factory, String pm)
077        {
078                this.filename = filename;
079                this.packageName = pm;
080                this.factory = factory;
081        }
082
083// --------------------- GETTER / SETTER METHODS ---------------------
084
085        /**
086         * This method returns the filename of the owl file
087         * @return the filename of the owl file
088         */
089        public String getFilename()
090        {
091                return filename;
092        }
093
094        /**
095         * This method returns the default factory for this level
096         * @return he default factory for this level
097         */
098        public BioPAXFactory getDefaultFactory()
099        {
100                return factory;
101        }
102
103// -------------------------- OTHER METHODS --------------------------
104
105        /**
106         * This method loads the level file as resource and returns it as
107         * an input stream
108         * @return an input stream from the owl file.
109         */
110        public InputStream getLevelFileAsStream()
111        {
112                return this.getClass().getResourceAsStream(filename);
113        }
114
115// --------------------- ACCESORS and MUTATORS---------------------
116
117        /**
118         * This method returns the namespace defined for this level.
119         * @return namespace defined for this level.
120         */
121        public String getNameSpace()
122        {
123                return BP_PREFIX + filename + "#";
124        }
125
126        /**
127         * This method returns true if the given string starts with the
128         * BP_PREFIX
129         * @param nameSpace to be checked
130         * @return rue if the given string starts with the BP_PREFIX
131         */
132        public static boolean isInBioPAXNameSpace(String nameSpace)
133        {
134                return nameSpace != null && nameSpace.startsWith(BP_PREFIX);
135        }
136
137        public static BioPAXLevel getLevelFromNameSpace(String namespace)
138        {
139                if (isInBioPAXNameSpace(namespace))
140                {
141                        {
142                                for (BioPAXLevel level : BioPAXLevel.values())
143                                {
144                                        if (namespace.equalsIgnoreCase(level.getNameSpace()))
145                                        {
146                                                return level;
147                                        }
148                                }
149                        }
150                }
151                return null;
152        }
153
154        public String getPackageName()
155        {
156                return packageName;
157        }
158
159        public boolean hasElement(BioPAXElement element)
160        {
161                return element.getModelInterface().getPackage().getName().equals(this.packageName);
162        }
163
164
165        /**
166         * Gets the BioPAX type (java interface) by name.
167         * @param localName a BioPAX type name
168         * @return the BioPAX interface class
169         * @throws IllegalBioPAXArgumentException when there is no such type / class not found.
170         */
171        public Class<? extends BioPAXElement> getInterfaceForName(String localName)
172        {
173        try
174        {
175            Class modelInterface = Class.forName(this.packageName + "." + localName);
176
177            if (BioPAXElement.class.isAssignableFrom(modelInterface))
178            {
179                return modelInterface;
180            } else
181            {
182                throw new IllegalBioPAXArgumentException(
183                        "BioPAXElement is not assignable from class:" + modelInterface.getSimpleName());
184            }
185        }
186        catch (ClassNotFoundException e)
187        {
188            throw new IllegalBioPAXArgumentException("Could not locate interface for:" + localName);
189        }
190    }
191
192}