Tuesday, August 14, 2012

SPEL : Regular Expressions


Data.java:
package com.lnn.spel.re;

public class Data {
      private boolean validPhone;
      private boolean onlyZero;

      public boolean isValidPhone() {
            return validPhone;
      }

      public void setValidPhone(boolean validPhone) {
            this.validPhone = validPhone;
      }

      public boolean isOnlyZero() {
            return onlyZero;
      }

      public void setOnlyZero(boolean onlyZero) {
            this.onlyZero = onlyZero;
      }
}
re.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
      <bean name="data" class="com.lnn.spel.re.Data">
            <property name ="validPhone"
            value="#{'123-456-7890' matches '[0-9]{3}[-]{0,1}[0-9]{3}[-]{0,1}[0-9]{4}'}" />
            <property name ="onlyZero" value="#{'000000' matches '[0]+'}" />
      </bean>
</beans>
Test.java:
package com.lnn.spel.re;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
      public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("spel/re.xml");
            Data data = context.getBean("data", Data.class);
            System.out.println(data.isOnlyZero());
            System.out.println(data.isValidPhone());
      }
}

No comments:

Post a Comment