OSDN Git Service

440c2e038f8e59de73c299a09ed64670df52f73f
[xdf/git-repos.git] / xdf / src / main / java / jp / ac / aiit / xdf / core / tags / TagLoader.java
1 package jp.ac.aiit.xdf.core.tags;
2 import java.io.File;
3 import java.util.HashMap;
4 import java.util.Locale;
5 import java.util.Map;
6
7 import javax.xml.bind.JAXBContext;
8 import javax.xml.bind.JAXBException;
9 import javax.xml.bind.Unmarshaller;
10
11 import jp.ac.aiit.xdf.core.exceptions.UnexpectedBehaviorException;
12 import jp.ac.aiit.xdf.core.tags.Tagdef.Tag;
13
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 /**
18  * タグ定義ファイルのロード処理である。
19  * XDFフレームワークのタブの構造定義(Tagdef)を従って定義ファイルの取り込み機能を提供する、
20  * loadメソッドを実施後、Tagdefに定義されたすべてのタグ定義(tag)をマップとして返し、
21  * そのマップのキーはタグ定義のタブ名とする。
22  * 
23  * Tagdef構造概要
24  *   ルート:<tagdef>
25  *      タグ定義:<tag name:タブ名>
26  *              モデルクラス :<modelclass >value:モデルクラス
27  *                      マッパークラス:<component-mapper env:ツールキット定義 class:マッパークラス>
28  * サンプル:
29  *      <tag name="xdf-window">
30  *              <modelclass>jp.ac.aiit.xdf.core.model.DefaultObjectModel</modelclass>
31  *              <component-mapper env="common" class="jp.ac.aiit.xdf.component.common.mappers.DoNothingComponentMapper" />
32  *      </tag>
33  *
34  * @author Pin.Yuan
35  */
36 public class TagLoader {
37         private static final Logger log = LoggerFactory.getLogger(TagLoader.class);
38         
39         /**
40          * タグ定義ファイルのロードを行う
41          * @param file タグ定義ファイル
42          * @return タグ定義
43          */
44         public Map<String, Tag> load(File file) {
45                 
46                 try {
47                         JAXBContext context = JAXBContext.newInstance(Tagdef.class);
48                         Unmarshaller um = context.createUnmarshaller();
49                         
50                         Tagdef tagdef = (Tagdef) um.unmarshal(file);
51                         
52                         Map<String, Tag> tagMap = new HashMap<String, Tag>();
53                         
54                         for (Tagdef.Tag tag : tagdef.getTag()){
55                                 // タブ定義のモデルクラスのチェック
56                                 if (checkClassNameExist(tag.getModelclass())){
57                                         // タブ定義の重複チェック
58                                         if (!tagMap.containsKey(tag.getName().toLowerCase(Locale.ENGLISH))){
59                                                 boolean mappingclassexist = true; 
60                                                 // タグ定義のマッピング定義のチェック
61                                                 for (Tagdef.Tag.ComponentMapper componentmapper: tag.getComponentMapper()){
62                                                         if (!checkClassNameExist(componentmapper.getClazz())){
63                                                                 mappingclassexist = false;
64                                                                 log.warn("指定されたクラスが存在しません:"+componentmapper.getClazz());
65                                                         }
66                                                 }
67                                                 if (mappingclassexist) {
68                                                         tagMap.put(tag.getName().toLowerCase(Locale.ENGLISH), tag);
69                                                 }
70
71                                         } else {
72                                                 log.warn("タグ名の定義が重複しています:"+tag.getName());
73                                         }
74                                 } else {
75                                         log.warn("指定されたクラスが存在しません:"+tag.getModelclass());
76                                 }
77                         }
78                 
79                         return tagMap;
80                 } catch(JAXBException e) {
81                         log.error("JAXBを使用できません");
82                         throw new UnexpectedBehaviorException("JAXBを使用できません", e);
83                 }
84         }
85         
86         // タブ定義のクラス名は存在しているかどうか
87         private boolean checkClassNameExist(String clazz) {
88                 try {
89                         Class.forName(clazz);
90                 } catch (ClassNotFoundException e){
91                         return false;
92                 }
93                 return true;
94         }
95 }