2012年7月25日 星期三

在xsd complexType 加入 simpleType 做條件限制

由於之前寫了一個WebService method 名為searchActiveDepts,我一開始預設是使用String[],由於 java已預設會將String的xsd(XML Schema) maxOccurs 設為 unbound,若企圖改寫xsd並加入simpleType的條件限制,以下為成功的範例碼。


<xs:complexType name="searchActiveDepts">
<xs:sequence>
<xs:element name="keywords" minOccurs="0" maxOccurs="3">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:maxLength value="10"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>
<xs:element name="account" type="tns:userAccount" minOccurs="0"/>
</xs:sequence>
</xs:complexType>


若直接用瀏覽器查看xsd,亦會看到如下

-<xs:complexType name="searchActiveDepts">
  -<xs:sequence>
    -<xs:element name="keywords" minOccurs="0" maxOccurs="3">
      -<xs:simpleType>
        -<xs:restriction base="xs:string">
          <xs:maxLength value="10"/>
        </xs:restriction>
      </xs:simpleType>
    </xs:element>
    <xs:element name="account" type="tns:userAccount" minOccurs="0"/>
  </xs:sequence>
</xs:complexType>

關鍵在於在尚未加入simpleType時,本來是
<xs:element name="keywords" type="xs:string" minOccurs="0" maxOccurs="unbound">

而我為了加入simpleType必須將紅色的部分拿掉
且我的目的是 使用此element(keyword)最多只能一次使用3組,所以設定maxOccurs="3"

並且利用simpleType,將每組keyword的長度不可超出10 (ex:"123456",長度為6)


因此現在我的關鍵字查詢功能 可以設定成至多同時輸入 3組查詢,且每組不超過10的長度


另外 simpleType可以加入許多不同的條件限制,詳見
http://www.w3schools.com/schema/schema_facets.asp





2012年7月18日 星期三

分析空白字串及空字串及空白鍵好用的判別式 in java

某天為了撰寫可以判別空白字串" ",空字串"",null 發現了好東西,不用寫一堆||或&&,是由apache所提供的 import org.apache.commons.lang.StringUtils;
public static boolean isBlank(String str) Checks if a String is whitespace, empty ("") or null. ex1:StringUtils.isBlank(null) = true 
ex2:StringUtils.isBlank("") = true 
ex3:StringUtils.isBlank(" ") = true 
ex4:StringUtils.isBlank("test") = false
ex5:StringUtils.isBlank(" test ") = false //空白+文字+空白
ex6:StringUtils.isBlank(" test") = false  //空白+文字
ex7:StringUtils.isBlank("test ") = false  //文字+空白
所以我只要將程式事先import我要用的package即可使用StringUtils內的isBlank(or isNotBlank) 而我可以利用此方式先用isBlank判斷出前三種情況(ex1~3),而對於ex5~7的情況,先用isBlank判斷後, 再使用trim()方式將ex5~7的字串順利轉成ex4,得到一個不含空白字串或null或空字串的String