OSDN Git Service

XML出力改善
[mikutoga/TogaGem.git] / src / main / java / jp / sourceforge / mikutoga / typical / I18nAlias.java
1 /*
2  * internationalization name alias
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.text.Normalizer;
13 import java.util.ArrayList;
14 import java.util.Collections;
15 import java.util.Comparator;
16 import java.util.LinkedList;
17 import java.util.List;
18 import javax.xml.parsers.DocumentBuilder;
19 import javax.xml.parsers.DocumentBuilderFactory;
20 import javax.xml.parsers.ParserConfigurationException;
21 import org.w3c.dom.Document;
22 import org.w3c.dom.Element;
23 import org.xml.sax.SAXException;
24
25 /**
26  * 国際化&別名管理オブジェクトの実装基板。
27  * <p>国産モデルではプライマリ名に日本語名が収められることが多い。
28  * プライマリ名は必ず一つ以上なければならない。
29  * <p>国産モデルではグローバル名に英語名が収められることが多いが、
30  * プライマリ名と同一の日本語名が収められている場合も多い。
31  */
32 class I18nAlias {
33
34     /** オーダ番号によるコンパレータ。 */
35     public static final Comparator<I18nAlias> ORDER_COMPARATOR =
36             new OrderComparator();
37
38
39     private int orderNo;
40
41     private final List<String> primaryList;
42     private final List<String> globalList;
43
44     private final List<String> umodPrimaryList;
45     private final List<String> umodGlobalList;
46
47
48     /**
49      * コンストラクタ。
50      * <p>各初期数が0以下の場合は、
51      * 状況に応じて伸長する連結リストが用意される。
52      * @param primaryNo プライマリ名初期数。
53      * @param globalNo グローバル名初期数。
54      */
55     protected I18nAlias(int primaryNo, int globalNo){
56         super();
57
58         if(primaryNo <= 0){
59             this.primaryList = new LinkedList<String>();
60         }else{
61             this.primaryList = new ArrayList<String>(primaryNo);
62         }
63
64         if(globalNo <= 0){
65             this.globalList  = new LinkedList<String>();
66         }else{
67             this.globalList  = new ArrayList<String>(globalNo);
68         }
69
70         this.umodPrimaryList =
71                 Collections.unmodifiableList(this.primaryList);
72         this.umodGlobalList =
73                 Collections.unmodifiableList(this.globalList);
74
75         return;
76     }
77
78     /**
79      * コンストラクタ。
80      * <p>プライマリ名、グローバル名共、
81      * 状況に応じて伸長する連結リストが用意される。
82      */
83     protected I18nAlias(){
84         this(0, 0);
85         return;
86     }
87
88
89     /**
90      * XMLドキュメントをロードする。
91      * @param is 入力
92      * @return 最上位要素
93      * @throws ParserConfigurationException XMLの構成が変
94      * @throws SAXException XMLの内容が変
95      * @throws IOException 入力エラー
96      */
97     protected static Element loadXml(InputStream is)
98             throws ParserConfigurationException, SAXException, IOException {
99         DocumentBuilderFactory factory;
100         factory = DocumentBuilderFactory.newInstance();
101         factory.setNamespaceAware(true);
102
103         DocumentBuilder builder = factory.newDocumentBuilder();
104         Document doc = builder.parse(is);
105
106         Element top = doc.getDocumentElement();
107
108         return top;
109     }
110
111     /**
112      * NFKC正規化された文字列を返す。
113      * <ul>
114      * <li>「ボーン」は「ボーン」になる
115      * <li>「ホ゛ーン9」は「ボーン9」になる
116      * </ul>
117      * @param name
118      * @return
119      */
120     protected static String normalize(CharSequence name){
121         String result;
122         result = Normalizer.normalize(name, Normalizer.Form.NFKC);
123         return result;
124     }
125
126
127     /**
128      * オーダー番号を返す。
129      * @return
130      */
131     protected int getOrderNo(){
132         return this.orderNo;
133     }
134
135     /**
136      * オーダー番号を設定する。
137      * @param orderNo
138      */
139     protected void setOrderNo(int orderNo){
140         this.orderNo = orderNo;
141         return;
142     }
143
144     /**
145      * プライマリ名の代表をひとつ返す。
146      * @return 最初のプライマリ名
147      */
148     public String getTopPrimaryName(){
149         String result = this.primaryList.get(0);
150         return result;
151     }
152
153     /**
154      * グローバル名の代表をひとつ返す。
155      * @return 最初のグローバル名。ひとつもなければnull
156      */
157     public String getTopGlobalName(){
158         String result;
159         if(this.globalList.isEmpty()) result = null;
160         else                          result = this.globalList.get(0);
161         return result;
162     }
163
164     /**
165      * プライマリ名の全エイリアス文字列リストを返す。
166      * @return 全プライマリ名リスト。(不可変)
167      */
168     public List<String> getPrimaryList(){
169         return this.umodPrimaryList;
170     }
171
172     /**
173      * プライマリ名を追加。
174      * @param name プライマリ名
175      */
176     protected void addPrimaryName(String name){
177         this.primaryList.add(name);
178         return;
179     }
180
181     /**
182      * グローバル名の全エイリアス文字列リストを返す。
183      * @return 全グローバル名リスト。(不可変)
184      */
185     public List<String> getGlobalList(){
186         return this.umodGlobalList;
187     }
188
189     /**
190      * グローバル名を追加。
191      * @param name グローバル名
192      */
193     protected void addGlobalName(String name){
194         this.globalList.add(name);
195         return;
196     }
197
198     /**
199      * オーダ番号によるコンパレータ。
200      */
201     @SuppressWarnings("serial")
202     private static class OrderComparator
203             implements Comparator<I18nAlias> {
204
205         /**
206          * コンストラクタ。
207          */
208         OrderComparator(){
209             super();
210             return;
211         }
212
213         /**
214          * オーダ番号を順序づける。
215          * @param o1 {@inheritDoc}
216          * @param o2 {@inheritDoc}
217          * @return {@inheritDoc}
218          */
219         @Override
220         public int compare(I18nAlias o1, I18nAlias o2){
221             int result = o1.getOrderNo() - o2.getOrderNo();
222             return result;
223         }
224
225     }
226
227 }