You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xmlbeans-user@xml.apache.org by "Blohm, Ed" <ed...@transcore.com> on 2004/06/25 19:51:29 UTC

problem with validation of some regex patterns

I am having some problems getting xml beans to validate using a regex pattern.  The pattern validates correctly using the java.util.regex.Pattern and also using org.apache.xmlbeans.impl.regex.RegularExpression.  When I validate using the actual xmlbeans class that is generated, the validation fails.  I just wanted to see if I was missing something here.  I am confused why the pattern works for the xmlbeans regex engine but not for the generated xmlbeans class.  Thanks for the help.

Ed


-- Schema Definition --

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
  targetNamespace="http://www.tcore.com/test.xsd"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:types="http://www.tcore.com/test.xsd"
  elementFormDefault="qualified">
  <!-- -->    
  <xs:element name="PhoneNumber">
    <xs:simpleType>
      <xs:restriction base="xs:string">
        <xs:pattern value="^1?\s*-?\s*(\d{3}|\(\s*\d{3}\s*\))\s*-?\s*\d{3}\s*-?\s*\d{4}$"/>
	</xs:restriction>
    </xs:simpleType>
  </xs:element>    
</xs:schema>

-- JUnit Test Class --

public class TestRegex extends junit.framework.TestCase
{
    public static final String REGEX_PATTERN = 
      "^1?\\s*-?\\s*(\\d{3}|\\(\\s*\\d{3}\\s*\\))\\s*-?\\s*\\d{3}\\s*-?\\s*\\d{4}$";
    public static final String PHONE_NUMBER = "123-456-7890";

    /**
     * Validation passes
     */
    public void testXmlBeansRegex()
    {
        org.apache.xmlbeans.impl.regex.RegularExpression regularExpression = new 
         org.apache.xmlbeans.impl.regex.RegularExpression(REGEX_PATTERN);
        assertTrue(regularExpression.matches(PHONE_NUMBER));
    }

    /**
     * Validation passes
     */
    public void testJavaRegex()
    {
        assertTrue(java.util.regex.Pattern.matches(REGEX_PATTERN, PHONE_NUMBER));
    }

    /**
     * Validation fails
     */
    public void testXmlBeansDocumentValidation()
    {
        com.tcore.test.PhoneNumberDocument doc = com.tcore.test.PhoneNumberDocument.Factory.newInstance();
        doc.setPhoneNumber(PHONE_NUMBER);
        assertTrue(doc.validate());
    }
}


- ---------------------------------------------------------------------
To unsubscribe, e-mail:   xmlbeans-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xmlbeans-user-help@xml.apache.org
Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/