KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > invicta > type > TypeManager


1 package net.sf.invicta.type;
2
3 import java.io.File JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Map JavaDoc;
8
9 import net.sf.invicta.InvictaConstants;
10
11 /**
12  * Manages the available type definitions. Responsible for loading
13  * type definition files and resolving them.
14  */

15 public class TypeManager {
16     protected Map JavaDoc typeFiles = new HashMap JavaDoc();
17     protected Map JavaDoc types = new HashMap JavaDoc();
18     protected TypeDefinitionLoader typeLoader = new TypeDefinitionLoader();
19             
20     /**
21      *
22      * @param typeFilesList List of available type definition files.
23      */

24     public TypeManager(List JavaDoc typeFilesList) {
25         super();
26         prepareFilesMap(typeFilesList);
27     }
28
29     /**
30      * Returns a resolved type definition with the given name. Loads
31      * and resolve the type definition if neded. Throws exception if
32      * a type with the given name is not defined.
33      * @param typeName
34      * @return TypeDefinition
35      * @throws InvictaTypeException
36      */

37     public TypeDefinition getTypeDefinition(String JavaDoc typeName) throws InvictaTypeException {
38         return getTypeDefinition(typeName, 0);
39     }
40                                 
41     /**
42      * Goes over the list of files and prepares a map between type
43      * names and type definition files.
44      * @param typeFilesList
45      */

46     protected void prepareFilesMap(List JavaDoc typeFilesList) {
47         
48         for (Iterator JavaDoc iter = typeFilesList.iterator(); iter.hasNext();) {
49             String JavaDoc fullPath = (String JavaDoc) iter.next();
50             File JavaDoc fullPathFile = new File JavaDoc(fullPath);
51             String JavaDoc fileName = fullPathFile.getName();
52             int extensionIndex = fileName.lastIndexOf(InvictaConstants.DEFINITION_FILE_EXTENSION);
53             String JavaDoc typeName = fileName.substring(0, extensionIndex);
54             this.typeFiles.put(typeName, fullPath);
55         }
56     }
57                         
58     /**
59      *
60      */

61     protected TypeDefinition getTypeDefinition(String JavaDoc typeName, int level) throws InvictaTypeException {
62         TypeDefinition typeDefinition = (TypeDefinition) this.types.get(typeName);
63         if (typeDefinition != null)
64             return typeDefinition;
65             
66         // Check if we are in an endless loop.
67
if (level > InvictaConstants.MAX_RESOLVE_LEVEL)
68             throw InvictaTypeException.typeInheritanceCycle(typeName);
69         
70         // Look for an xml file in the list of possible files.
71
String JavaDoc typeFilePath = (String JavaDoc)this.typeFiles.get(typeName);
72         
73         // Check if no matching type file is available.
74
if (typeFilePath == null)
75             throw InvictaTypeException.typeDefinitionNotFound(typeName);
76                             
77         // Load the defintion file.
78
typeDefinition =
79             this.typeLoader.load(typeFilePath);
80         
81         // Check if the file name matches the type name.
82
if (!typeDefinition.getName().equals(typeName))
83             System.out.println("** Warning: Type name '" + typeDefinition.getName() + "' does not match the file name '" + typeName);
84             
85         typeDefinition.resolve(this, level);
86                         
87         // Add the loaded type into a 'cache' map.
88
this.types.put(typeName, typeDefinition);
89         
90         return typeDefinition;
91     }
92                         
93 }
94
95
Popular Tags