OSDN Git Service

MMD Ver7.40 対応
[mikutoga/TogaGem.git] / src / main / java / jp / sfjp / mikutoga / xml / SaxAttr.java
1 /*
2  * Sax 2 Xsd-types converter
3  *
4  * License : The MIT License
5  * Copyright(c) 2013 MikuToga Partners
6  */
7
8 package jp.sfjp.mikutoga.xml;
9
10 import javax.xml.bind.DatatypeConverter;
11 import org.xml.sax.Attributes;
12
13 /**
14  * XSD各種型のSAX属性値をJavaプリミティブ型へ変換する。
15  */
16 public final class SaxAttr {
17
18     /**
19      * 隠しコンストラクタ。
20      */
21     private SaxAttr(){
22         assert false;
23         throw new AssertionError();
24     }
25
26
27     /**
28      * 属性名に対応する属性値があるか否か判定する。
29      * @param attr 属性群
30      * @param name 属性名
31      * @return 属性名に対応する属性値がある場合はtrue
32      */
33     public static boolean hasAttr(Attributes attr, String name){
34         if(attr.getValue(name) == null) return false;
35         return true;
36     }
37
38     /**
39      * xsd:string型属性値の読み込み。
40      * @param attr 属性群
41      * @param name 属性名
42      * @return 属性値。該当する属性が無ければnull。
43      */
44     public static String getStringAttr(Attributes attr, String name){
45         String attrVal = attr.getValue(name);
46         return attrVal;
47     }
48
49     /**
50      * xsd:boolean型属性値の読み込み。
51      * @param attr 属性群
52      * @param name 属性名
53      * @return 属性値。
54      * @throws IllegalArgumentException boolean型表記ではない
55      */
56     public static boolean getBooleanAttr(Attributes attr, String name)
57             throws IllegalArgumentException{
58         String attrVal = attr.getValue(name);
59         boolean bVal;
60         bVal = DatatypeConverter.parseBoolean(attrVal);
61         return bVal;
62     }
63
64     /**
65      * xsd:boolean型属性値の読み込み。
66      * @param attr 属性群
67      * @param name 属性名
68      * @param def 属性が無い場合のデフォルト値
69      * @return 属性値。
70      * @throws IllegalArgumentException boolean型表記ではない
71      */
72     public static boolean getBooleanAttr(Attributes attr,
73                                            String name,
74                                            boolean def )
75             throws IllegalArgumentException{
76         String attrVal = attr.getValue(name);
77         if(attrVal == null) return def;
78
79         boolean bVal;
80         bVal = DatatypeConverter.parseBoolean(attrVal);
81
82         return bVal;
83     }
84
85     /**
86      * xsd:byte型属性の読み込み。
87      * @param attr 属性群
88      * @param name 属性名
89      * @return 属性値。
90      * @throws NumberFormatException byte型表記ではない
91      */
92     public static byte getByteAttr(Attributes attr, String name)
93             throws NumberFormatException{
94         String attrVal = attr.getValue(name);
95         byte bVal;
96         bVal = DatatypeConverter.parseByte(attrVal);
97         return bVal;
98     }
99
100     /**
101      * xsd:float型属性値の読み込み。
102      * @param attr 属性群
103      * @param name 属性名
104      * @return 属性値。
105      * @throws NumberFormatException float型表記ではない
106      */
107     public static float getFloatAttr(Attributes attr, String name)
108             throws NumberFormatException {
109         String attrVal = attr.getValue(name);
110         float fVal;
111         fVal = DatatypeConverter.parseFloat(attrVal);
112         return fVal;
113     }
114
115     /**
116      * xsd:int型属性値の読み込み。
117      * @param attr 属性群
118      * @param name 属性名
119      * @return 属性値。
120      * @throws NumberFormatException int型表記ではない
121      */
122     public static int getIntAttr(Attributes attr, String name)
123             throws NumberFormatException {
124         String attrVal = attr.getValue(name);
125         int iVal;
126         iVal = DatatypeConverter.parseInt(attrVal);
127         return iVal;
128     }
129
130 }