View Javadoc
1   /*
2    * pre-defined avatar info
3    *
4    * License : The MIT License
5    * Copyright(c) 2009 olyutorskii
6    */
7   
8   package jp.sourceforge.jindolf.corelib;
9   
10  import java.io.IOException;
11  import java.util.ArrayList;
12  import java.util.Collections;
13  import java.util.List;
14  import javax.xml.parsers.DocumentBuilder;
15  import org.w3c.dom.Element;
16  import org.xml.sax.SAXException;
17  
18  /**
19   * プリセット済みAvatarに関する情報。
20   * F国運用の時点で20キャラクタ。
21   */
22  public final class PreDefAvatar{
23  
24      private final String avatarId;
25      private final String fullName;
26      private final String jobTitle;
27      private final String shortName;
28      private final int serialNo;
29  
30  
31      /**
32       * コンストラクタ。
33       * @param avatarId Avatar識別子
34       * @param fullName フルネーム
35       * @param jobTitle 職業名
36       * @param shortName 省略名
37       * @param serialNo 通し番号
38       */
39      private PreDefAvatar(String avatarId,
40                           String fullName,
41                           String jobTitle,
42                           String shortName,
43                           int serialNo ){
44          super();
45  
46          if(    avatarId  == null
47              || fullName  == null
48              || jobTitle  == null
49              || shortName == null ){
50              throw new NullPointerException();
51          }
52  
53          if(    avatarId.length() <= 0
54              || fullName.length() <= 0
55              || serialNo < 0 ){
56              throw new IllegalArgumentException();
57          }
58  
59          this.avatarId  = avatarId.intern();
60          this.fullName  = fullName.intern();
61          this.jobTitle  = jobTitle.intern();
62          this.shortName = shortName.intern();
63          this.serialNo  = serialNo;
64  
65          return;
66      }
67  
68  
69      /**
70       * プリセット済みAvatar一覧リストを生成する。
71       * @param builder DOMビルダ
72       * @return プリセット済みAvatar一覧リスト
73       * @throws IOException IOエラー
74       * @throws SAXException パースエラー
75       */
76      public static List<PreDefAvatar> buildPreDefAvatarList(
77              DocumentBuilder builder)
78              throws IOException,
79                     SAXException {
80          List<Element> elemList = DomUtils.loadElemList(
81                  builder, XmlResource.I_URL_AVATARDEF, "preDefinedAvatar");
82  
83          List<PreDefAvatar> result = new ArrayList<>(elemList.size());
84  
85          for(Element elem : elemList){
86              PreDefAvatar avatar = buildAvatar(elem);
87              result.add(avatar);
88          }
89  
90          result = Collections.unmodifiableList(result);
91  
92          return result;
93      }
94  
95      /**
96       * 個々のプリセットAvatar定義をオブジェクトに変換する。
97       * @param avatarDef プリセットAvatar定義要素
98       * @return プリセットAvatar定義オブジェクト
99       * @throws SAXException パースエラー
100      */
101     private static PreDefAvatar buildAvatar(Element avatarDef)
102             throws SAXException {
103         String avatarId  = avatarDef.getAttribute("avatarId");
104         String jobTitle  = avatarDef.getAttribute("jobTitle");
105         String shortName = avatarDef.getAttribute("shortName");
106         String serialNum = avatarDef.getAttribute("serialNum");
107 
108         String fullName = jobTitle + "\u0020" + shortName;
109 
110         int serialNo;
111         try{
112             serialNo = Integer.parseInt(serialNum);
113         }catch(NumberFormatException e){
114             throw new SAXException("illegal number form", e);
115         }
116 
117         PreDefAvatar avatar = new PreDefAvatar(avatarId,
118                                                fullName,
119                                                jobTitle,
120                                                shortName,
121                                                serialNo );
122 
123         return avatar;
124     }
125 
126 
127     /**
128      * Avatar識別子を返す。
129      * @return Avatar識別子
130      */
131     public String getAvatarId(){
132         return this.avatarId;
133     }
134 
135     /**
136      * フルネームを返す。
137      * @return フルネーム
138      */
139     public String getFullName(){
140         return this.fullName;
141     }
142 
143     /**
144      * 職業名を返す。
145      * @return 職業名
146      */
147     public String getJobTitle(){
148         return this.jobTitle;
149     }
150 
151     /**
152      * 省略名を返す。
153      * @return 省略名
154      */
155     public String getShortName(){
156         return this.shortName;
157     }
158 
159     /**
160      * 通し番号を返す。
161      * @return 通し番号
162      */
163     public int getSerialNo(){
164         return this.serialNo;
165     }
166 
167 }