OSDN Git Service

ff5e2d5e4e7f53b9dd7d4916c958246399d7428c
[mikutoga/TogaGem.git] / src / main / java / jp / sourceforge / mikutoga / typical / TypicalObject.java
1 /*
2  * typical object information
3  *
4  * License : The MIT License
5  * Copyright(c) 2011 MikuToga Partners
6  */
7
8 package jp.sourceforge.mikutoga.typical;
9
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.util.ArrayList;
13 import java.util.Collections;
14 import java.util.Comparator;
15 import java.util.LinkedList;
16 import java.util.List;
17 import javax.xml.parsers.DocumentBuilder;
18 import javax.xml.parsers.DocumentBuilderFactory;
19 import javax.xml.parsers.ParserConfigurationException;
20 import org.w3c.dom.Document;
21 import org.w3c.dom.Element;
22 import org.xml.sax.SAXException;
23
24 /**
25  * 各種標準オブジェクトの実装基板。
26  * <p>国産モデルではプライマリ名に日本語名が収められることが多い。
27  * プライマリ名は必ず一つ以上なければならない。
28  * <p>国産モデルではグローバル名に英語名が収められることが多いが、
29  * プライマリ名と同一の日本語名が収められている場合も多い。
30  */
31 class TypicalObject {
32
33     /** オーダ番号によるコンパレータ。 */
34     public static final Comparator<TypicalObject> ORDER_COMPARATOR =
35             new OrderComparator();
36
37     protected final List<String> primaryList;
38     protected final List<String> globalList;
39
40     protected final List<String> umodPrimaryList;
41     protected final List<String> umodGlobalList;
42
43     protected int orderNo;
44
45
46     /**
47      * コンストラクタ。
48      * <p>各初期数が0以下の場合は、状況に応じて伸長する連結リストが用意される。
49      * @param primaryNo プライマリ名初期数。
50      * @param globalNo グローバル名初期数。
51      */
52     protected TypicalObject(int primaryNo, int globalNo){
53         super();
54
55         if(primaryNo <= 0){
56             this.primaryList = new LinkedList<String>();
57         }else{
58             this.primaryList = new ArrayList<String>(primaryNo);
59         }
60
61         if(globalNo <= 0){
62             this.globalList  = new LinkedList<String>();
63         }else{
64             this.globalList  = new ArrayList<String>(globalNo);
65         }
66
67         this.umodPrimaryList =
68                 Collections.unmodifiableList(this.primaryList);
69         this.umodGlobalList =
70                 Collections.unmodifiableList(this.globalList);
71
72         return;
73     }
74
75     /**
76      * コンストラクタ。
77      * <p>プライマリ名、グローバル名共、状況に応じて伸長する連結リストが用意される。
78      */
79     protected TypicalObject(){
80         this(0, 0);
81         return;
82     }
83
84
85     /**
86      * XMLドキュメントをロードする。
87      * @param is 入力
88      * @return 最上位要素
89      * @throws ParserConfigurationException XMLの構成が変
90      * @throws SAXException XMLの内容が変
91      * @throws IOException 入力エラー
92      */
93     protected static Element loadXml(InputStream is)
94             throws ParserConfigurationException, SAXException, IOException {
95         DocumentBuilderFactory factory;
96         factory = DocumentBuilderFactory.newInstance();
97         factory.setNamespaceAware(true);
98
99         DocumentBuilder builder = factory.newDocumentBuilder();
100         Document doc = builder.parse(is);
101
102         Element top = doc.getDocumentElement();
103
104         return top;
105     }
106
107
108     /**
109      * プライマリ名をひとつ返す。
110      * @return 最初のプライマリ名
111      */
112     public String getTopPrimaryName(){
113         String result = this.primaryList.get(0);
114         return result;
115     }
116
117     /**
118      * グローバル名をひとつ返す。
119      * @return 最初のグローバル名。ひとつもなければnull
120      */
121     public String getTopGlobalName(){
122         String result;
123         if(this.globalList.isEmpty()) result = null;
124         else                          result = this.globalList.get(0);
125         return result;
126     }
127
128     /**
129      * 全プライマリ名を返す。
130      * @return 全プライマリ名リスト。(不可変)
131      */
132     public List<String> getPrimaryList(){
133         return this.umodPrimaryList;
134     }
135
136     /**
137      * 全グローバル名を返す。
138      * @return 全グローバル名リスト。(不可変)
139      */
140     public List<String> getGlobalList(){
141         return this.umodGlobalList;
142     }
143
144     /**
145      * オーダ番号によるコンパレータ。
146      */
147     private static class OrderComparator
148             implements Comparator<TypicalObject> {
149
150         /**
151          * コンストラクタ。
152          */
153         OrderComparator(){
154             super();
155             return;
156         }
157
158         /**
159          * オーダ番号を順序づける。
160          * @param o1 {@inheritDoc}
161          * @param o2 {@inheritDoc}
162          * @return {@inheritDoc}
163          */
164         @Override
165         public int compare(TypicalObject o1, TypicalObject o2){
166             int result = o1.orderNo - o2.orderNo;
167             return result;
168         }
169
170     }
171
172 }