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       *
34       * @param avatarId Avatar識別子
35       * @param fullName フルネーム
36       * @param jobTitle 職業名
37       * @param shortName 省略名
38       * @param serialNo 通し番号
39       */
40      private PreDefAvatar(String avatarId,
41                           String fullName,
42                           String jobTitle,
43                           String shortName,
44                           int serialNo ){
45          super();
46  
47          if(    avatarId  == null
48              || fullName  == null
49              || jobTitle  == null
50              || shortName == null ){
51              throw new NullPointerException();
52          }
53  
54          if(    avatarId.length() <= 0
55              || fullName.length() <= 0
56              || serialNo < 0 ){
57              throw new IllegalArgumentException();
58          }
59  
60          this.avatarId  = avatarId.intern();
61          this.fullName  = fullName.intern();
62          this.jobTitle  = jobTitle.intern();
63          this.shortName = shortName.intern();
64          this.serialNo  = serialNo;
65  
66          return;
67      }
68  
69  
70      /**
71       * プリセット済みAvatar一覧リストを生成する。
72       *
73       * @param builder DOMビルダ
74       * @return プリセット済みAvatar一覧リスト
75       * @throws IOException IOエラー
76       * @throws SAXException パースエラー
77       */
78      public static List<PreDefAvatar> buildPreDefAvatarList(
79              DocumentBuilder builder)
80              throws IOException,
81                     SAXException {
82          List<Element> elemList = DomUtils.loadElemList(
83                  builder, XmlResource.I_URL_AVATARDEF, "preDefinedAvatar");
84  
85          List<PreDefAvatar> result = new ArrayList<>(elemList.size());
86  
87          for(Element elem : elemList){
88              PreDefAvatar avatar = buildAvatar(elem);
89              result.add(avatar);
90          }
91  
92          result = Collections.unmodifiableList(result);
93  
94          return result;
95      }
96  
97      /**
98       * 個々のプリセットAvatar定義をオブジェクトに変換する。
99       *
100      * @param avatarDef プリセットAvatar定義要素
101      * @return プリセットAvatar定義オブジェクト
102      * @throws SAXException パースエラー
103      */
104     private static PreDefAvatar buildAvatar(Element avatarDef)
105             throws SAXException {
106         String avatarId  = avatarDef.getAttribute("avatarId");
107         String jobTitle  = avatarDef.getAttribute("jobTitle");
108         String shortName = avatarDef.getAttribute("shortName");
109         String serialNum = avatarDef.getAttribute("serialNum");
110 
111         String fullName = jobTitle + "\u0020" + shortName;
112 
113         int serialNo;
114         try{
115             serialNo = Integer.parseInt(serialNum);
116         }catch(NumberFormatException e){
117             throw new SAXException("illegal number form", e);
118         }
119 
120         PreDefAvatar avatar = new PreDefAvatar(avatarId,
121                                                fullName,
122                                                jobTitle,
123                                                shortName,
124                                                serialNo );
125 
126         return avatar;
127     }
128 
129 
130     /**
131      * Avatar識別子を返す。
132      *
133      * @return Avatar識別子
134      */
135     public String getAvatarId(){
136         return this.avatarId;
137     }
138 
139     /**
140      * フルネームを返す。
141      *
142      * @return フルネーム
143      */
144     public String getFullName(){
145         return this.fullName;
146     }
147 
148     /**
149      * 職業名を返す。
150      *
151      * @return 職業名
152      */
153     public String getJobTitle(){
154         return this.jobTitle;
155     }
156 
157     /**
158      * 省略名を返す。
159      *
160      * @return 省略名
161      */
162     public String getShortName(){
163         return this.shortName;
164     }
165 
166     /**
167      * 通し番号を返す。
168      *
169      * @return 通し番号
170      */
171     public int getSerialNo(){
172         return this.serialNo;
173     }
174 
175 }