2 * Sax 2 Xsd-types converter
4 * License : The MIT License
5 * Copyright(c) 2013 MikuToga Partners
8 package jp.sfjp.mikutoga.xml;
10 import javax.xml.bind.DatatypeConverter;
11 import org.xml.sax.Attributes;
14 * XSD各種型のSAX属性値をJavaプリミティブ型へ変換する。
16 public final class SaxAttr {
23 throw new AssertionError();
28 * 属性名に対応する属性値があるか否か判定する。
31 * @return 属性名に対応する属性値がある場合はtrue
33 public static boolean hasAttr(Attributes attr, String name){
34 if(attr.getValue(name) == null) return false;
39 * xsd:string型属性値の読み込み。
42 * @return 属性値。該当する属性が無ければnull。
44 public static String getStringAttr(Attributes attr, String name){
45 String attrVal = attr.getValue(name);
50 * xsd:boolean型属性値の読み込み。
54 * @throws IllegalArgumentException boolean型表記ではない
56 public static boolean getBooleanAttr(Attributes attr, String name)
57 throws IllegalArgumentException{
58 String attrVal = attr.getValue(name);
60 bVal = DatatypeConverter.parseBoolean(attrVal);
65 * xsd:boolean型属性値の読み込み。
68 * @param def 属性が無い場合のデフォルト値
70 * @throws IllegalArgumentException boolean型表記ではない
72 public static boolean getBooleanAttr(Attributes attr,
75 throws IllegalArgumentException{
76 String attrVal = attr.getValue(name);
77 if(attrVal == null) return def;
80 bVal = DatatypeConverter.parseBoolean(attrVal);
90 * @throws NumberFormatException byte型表記ではない
92 public static byte getByteAttr(Attributes attr, String name)
93 throws NumberFormatException{
94 String attrVal = attr.getValue(name);
96 bVal = DatatypeConverter.parseByte(attrVal);
101 * xsd:float型属性値の読み込み。
105 * @throws NumberFormatException float型表記ではない
107 public static float getFloatAttr(Attributes attr, String name)
108 throws NumberFormatException {
109 String attrVal = attr.getValue(name);
111 fVal = DatatypeConverter.parseFloat(attrVal);
120 * @throws NumberFormatException int型表記ではない
122 public static int getIntAttr(Attributes attr, String name)
123 throws NumberFormatException {
124 String attrVal = attr.getValue(name);
126 iVal = DatatypeConverter.parseInt(attrVal);