#!/usr/bin/python

import unittest
import FragmentFilter
import copy
import xml.sax.saxutils
#from Ft.Lib.pDomlette import PyExpatReader
from xml.sax import make_parser, parseString, parse
from Ft.Xml import InputSource
from Ft.Xml.Domlette import NonvalidatingReader
from StringIO import StringIO
"""
The contents of this file are subject to the Mozilla Public License  Version 1.1 (the "License"); you may not use this file except in  compliance with the License. 
You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
Software distributed under the License is distributed on an "AS IS"  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the  License for the specific language governing rights and limitations under  the License. 

The Original Code is available at http://downloads.xmlschemata.org/python/xvif/

The Initial Developer of the Original Code is Eric van der Vlist. Portions  created by Eric van der Vlist are Copyright (C) 2002. All Rights Reserved. 

The name, specification and XML syntax of Regular Fragmentation is from Simon St.Laurent and his original Java implementation available at http://simonstl.com/projects/fragment/original .

Contributor(s): 
"""


class TestRegFrag(unittest.TestCase):
	
	def rulesParser(self, rulesString, valid=1, display=0):
		parser = make_parser()
		rules = FragmentFilter.RulesLoader()
		parser.setContentHandler(rules)
		parser.setFeature("http://xml.org/sax/features/namespaces", 1)
		factory = InputSource.DefaultFactory
		isrc=factory.fromString(rulesString, "dummy")
		exception = 0
		display=1
		if display:
			print "-------------------------"
			print rulesString
		msg=""
		try:
			parser.parse(isrc.stream)
			if display:
				print rules
		except FragmentFilter.InvalidRuleException, msg:
			exception=1
		if valid:
			self.failUnless(exception==0,
				'the rules %s should be valid (%s)' % (rulesString, msg))
		else:
			self.failUnless(exception==1,
				'the rules %s should be invalid' % rulesString)
		return rules

	def fragmentDocument(self, rules, instanceString, valid=1, expected=""):
		parser = make_parser()
		fragFilter = FragmentFilter.FragmentFilter(rules)
		parser.setContentHandler(fragFilter)
		parser.setFeature("http://xml.org/sax/features/namespaces", 1)
		out = StringIO()
		fragFilter.setContentHandler(xml.sax.saxutils.XMLGenerator(out))
		factory = InputSource.DefaultFactory
		isrc=factory.fromString(instanceString, "dummy")
		msg = ""
		try:
			parser.parse(isrc.stream)
			exception = 0
		except FragmentFilter.InvalidFragmentException, msg:
			exception = 1
		if valid:
			self.failUnless(exception == 0,
				'Unexpected exception :'+ msg)
			self.failUnless(expected.strip() == out.getvalue().strip(),
				'Unexpected result:\n'+ out.getvalue())
		else:
			self.failUnless(exception == 1,
				"Shouldn't have been valid:\n" + instanceString)

		
	def test1element(self):
		rules = self.rulesParser("""
<fragmentRules xmlns="http://simonstl.com/ns/fragments/">

<fragmentRule pattern="(\d{2,5})(\d{2})-(\d{2})">
<applyTo>
  <element nsURI="http://simonstl.com/ns/test/" localName="gYearMonth"/>
  <element nsURI="http://simonstl.com/ns/test/" localName="myYearMonth"/>
</applyTo>
<produce>
  <element nsURI="http://simonstl.com/ns/types/" localName="century" prefix="type" />
  <element nsURI="http://simonstl.com/ns/types/" localName="year" prefix="type" />
  <element nsURI="http://simonstl.com/ns/types/" localName="month" prefix="type" />
</produce>
</fragmentRule>

</fragmentRules>		""")
		self.fragmentDocument(rules, """
		<test xmlns="http://simonstl.com/ns/test/" xmlns:type="http://simonstl.com/ns/types/">
<message>Hello!  This document contains a gYearMonth.</message>
<gYearMonth>1970-11</gYearMonth>
<myYearMonth>1970-11</myYearMonth>
</test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<test xmlns="http://simonstl.com/ns/test/" xmlns:type="http://simonstl.com/ns/types/">
<message>Hello!  This document contains a gYearMonth.</message>
<gYearMonth><type:century>19</type:century><type:year>70</type:year><type:month>11</type:month></gYearMonth>
<myYearMonth><type:century>19</type:century><type:year>70</type:year><type:month>11</type:month></myYearMonth>
</test>
""")
		self.fragmentDocument(rules, """
		<test xmlns="http://simonstl.com/ns/test/">
<message>Hello!  This document contains a gYearMonth.</message>
<gYearMonth>1970-11</gYearMonth>
<myYearMonth>1970-11</myYearMonth>
</test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<test xmlns="http://simonstl.com/ns/test/">
<message>Hello!  This document contains a gYearMonth.</message>
<gYearMonth><type:century xmlns:type="http://simonstl.com/ns/types/">19</type:century><type:year xmlns:type="http://simonstl.com/ns/types/">70</type:year><type:month xmlns:type="http://simonstl.com/ns/types/">11</type:month></gYearMonth>
<myYearMonth><type:century xmlns:type="http://simonstl.com/ns/types/">19</type:century><type:year xmlns:type="http://simonstl.com/ns/types/">70</type:year><type:month xmlns:type="http://simonstl.com/ns/types/">11</type:month></myYearMonth>
</test>""")
		self.fragmentDocument(rules, """
		<test xmlns="http://simonstl.com/ns/test/" xmlns:type="foo">
<message>Hello!  This document contains a gYearMonth.</message>
<gYearMonth>1970-11</gYearMonth>
<myYearMonth>1970-11</myYearMonth>
</test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<test xmlns="http://simonstl.com/ns/test/" xmlns:type="foo">
<message>Hello!  This document contains a gYearMonth.</message>
<gYearMonth><type:century xmlns:type="http://simonstl.com/ns/types/">19</type:century><type:year xmlns:type="http://simonstl.com/ns/types/">70</type:year><type:month xmlns:type="http://simonstl.com/ns/types/">11</type:month></gYearMonth>
<myYearMonth xmlns:type="foo"><type:century xmlns:type="http://simonstl.com/ns/types/">19</type:century><type:year xmlns:type="http://simonstl.com/ns/types/">70</type:year><type:month xmlns:type="http://simonstl.com/ns/types/">11</type:month></myYearMonth>
</test>""")
		self.fragmentDocument(rules, """
		<test xmlns="http://simonstl.com/ns/test/" xmlns:type="foo">
<message>Hello!  This document contains a gYearMonth.</message>
<gYearMonth>1970<foo/>-11</gYearMonth>
<myYearMonth>1970-11</myYearMonth>
</test>""", valid=0)
		
	def test2(self):
		rules = self.rulesParser("""
<fragmentRules xmlns="http://simonstl.com/ns/fragments/">

<fragmentRule pattern="(\d{2,5})(\d{2})-(\d{2})">
<applyTo>
  <element nsURI="http://simonstl.com/ns/test/" localName="gYearMonth"/>
  <element nsURI="http://simonstl.com/ns/test/" localName="myYearMonth"/>
</applyTo>
<produce>
  <element nsURI="http://simonstl.com/ns/types/" localName="century" prefix="type" />
  <element nsURI="http://simonstl.com/ns/types/" localName="year" prefix="type" />
  <element nsURI="http://simonstl.com/ns/types/" localName="month" prefix="type" />
</produce>
</fragmentRule>
<fragmentRule pattern="(\d{1})(\d{1})">
<applyTo>
<element nsURI="http://simonstl.com/ns/types/" localName="century"  />
<element nsURI="http://simonstl.com/ns/types/" localName="year"  />
<element nsURI="http://simonstl.com/ns/types/" localName="month"  />
</applyTo>
<produce>
<element nsURI="http://simonstl.com/ns/types/" localName="digit" prefix="type" />
<element nsURI="http://simonstl.com/ns/types/" localName="digit" prefix="type" />
</produce>
</fragmentRule>
</fragmentRules>		""")
		self.fragmentDocument(rules, """
		<test xmlns="http://simonstl.com/ns/test/" xmlns:type="http://simonstl.com/ns/types/">
<message>Hello!  This document contains a gYearMonth.</message>
<gYearMonth>1970-11</gYearMonth>
<myYearMonth>1970-11</myYearMonth>
</test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<test xmlns="http://simonstl.com/ns/test/" xmlns:type="http://simonstl.com/ns/types/">
<message>Hello!  This document contains a gYearMonth.</message>
<gYearMonth><type:century><type:digit>1</type:digit><type:digit>9</type:digit></type:century><type:year><type:digit>7</type:digit><type:digit>0</type:digit></type:year><type:month><type:digit>1</type:digit><type:digit>1</type:digit></type:month></gYearMonth>
<myYearMonth><type:century><type:digit>1</type:digit><type:digit>9</type:digit></type:century><type:year><type:digit>7</type:digit><type:digit>0</type:digit></type:year><type:month><type:digit>1</type:digit><type:digit>1</type:digit></type:month></myYearMonth>
</test>
""")
		self.fragmentDocument(rules, """
		<test xmlns="http://simonstl.com/ns/test/">
<message>Hello!  This document contains a gYearMonth.</message>
<gYearMonth>1970-11</gYearMonth>
<myYearMonth>1970-11</myYearMonth>
</test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<test xmlns="http://simonstl.com/ns/test/">
<message>Hello!  This document contains a gYearMonth.</message>
<gYearMonth><type:century xmlns:type="http://simonstl.com/ns/types/"><type:digit>1</type:digit><type:digit>9</type:digit></type:century><type:year xmlns:type="http://simonstl.com/ns/types/"><type:digit>7</type:digit><type:digit>0</type:digit></type:year><type:month xmlns:type="http://simonstl.com/ns/types/"><type:digit>1</type:digit><type:digit>1</type:digit></type:month></gYearMonth>
<myYearMonth><type:century xmlns:type="http://simonstl.com/ns/types/"><type:digit>1</type:digit><type:digit>9</type:digit></type:century><type:year xmlns:type="http://simonstl.com/ns/types/"><type:digit>7</type:digit><type:digit>0</type:digit></type:year><type:month xmlns:type="http://simonstl.com/ns/types/"><type:digit>1</type:digit><type:digit>1</type:digit></type:month></myYearMonth>
</test>""")
		self.fragmentDocument(rules, """
		<test xmlns="http://simonstl.com/ns/test/" xmlns:type="foo">
<message>Hello!  This document contains a gYearMonth.</message>
<gYearMonth>1970-11</gYearMonth>
<myYearMonth>1970-11</myYearMonth>
</test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<test xmlns="http://simonstl.com/ns/test/" xmlns:type="foo">
<message>Hello!  This document contains a gYearMonth.</message>
<gYearMonth><type:century xmlns:type="http://simonstl.com/ns/types/"><type:digit>1</type:digit><type:digit>9</type:digit></type:century><type:year xmlns:type="http://simonstl.com/ns/types/"><type:digit>7</type:digit><type:digit>0</type:digit></type:year><type:month xmlns:type="http://simonstl.com/ns/types/"><type:digit>1</type:digit><type:digit>1</type:digit></type:month></gYearMonth>
<myYearMonth xmlns:type="foo"><type:century xmlns:type="http://simonstl.com/ns/types/"><type:digit>1</type:digit><type:digit>9</type:digit></type:century><type:year xmlns:type="http://simonstl.com/ns/types/"><type:digit>7</type:digit><type:digit>0</type:digit></type:year><type:month xmlns:type="http://simonstl.com/ns/types/"><type:digit>1</type:digit><type:digit>1</type:digit></type:month></myYearMonth>
</test>""")
		self.fragmentDocument(rules, """
		<test xmlns="http://simonstl.com/ns/test/" xmlns:type="foo">
<message>Hello!  This document contains a gYearMonth.</message>
<gYearMonth>1970<foo/>-11</gYearMonth>
<myYearMonth>1970-11</myYearMonth>
</test>""", valid=0)
		
	def test2bis(self):
		rules = self.rulesParser("""
<fragmentRules xmlns="http://simonstl.com/ns/fragments/">

<fragmentRule pattern="(\d{2,5})(\d{2})-(\d{2})" break="true">
<applyTo>
  <element nsURI="http://simonstl.com/ns/test/" localName="gYearMonth"/>
  <element nsURI="http://simonstl.com/ns/test/" localName="myYearMonth"/>
</applyTo>
<produce>
  <element nsURI="http://simonstl.com/ns/types/" localName="century" prefix="type" />
  <element nsURI="http://simonstl.com/ns/types/" localName="year" prefix="type" />
  <element nsURI="http://simonstl.com/ns/types/" localName="month" prefix="type" />
</produce>
</fragmentRule>
<fragmentRule pattern="(\d{1})(\d{1})">
<applyTo>
<element nsURI="http://simonstl.com/ns/types/" localName="century"  />
<element nsURI="http://simonstl.com/ns/types/" localName="year"  />
<element nsURI="http://simonstl.com/ns/types/" localName="month"  />
</applyTo>
<produce>
<element nsURI="http://simonstl.com/ns/types/" localName="digit" prefix="type" />
<element nsURI="http://simonstl.com/ns/types/" localName="digit" prefix="type" />
</produce>
</fragmentRule>
</fragmentRules>		""")
		self.fragmentDocument(rules, """
		<test xmlns="http://simonstl.com/ns/test/" xmlns:type="http://simonstl.com/ns/types/">
<message>Hello!  This document contains a gYearMonth.</message>
<gYearMonth>1970-11</gYearMonth>
<myYearMonth>1970-11</myYearMonth>
</test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<test xmlns="http://simonstl.com/ns/test/" xmlns:type="http://simonstl.com/ns/types/">
<message>Hello!  This document contains a gYearMonth.</message>
<gYearMonth><type:century>19</type:century><type:year>70</type:year><type:month>11</type:month></gYearMonth>
<myYearMonth><type:century>19</type:century><type:year>70</type:year><type:month>11</type:month></myYearMonth>
</test>""")
		self.fragmentDocument(rules, """
		<test xmlns="http://simonstl.com/ns/test/" xmlns:type="http://simonstl.com/ns/types/">
<message>Hello!  This document contains a gYearMonth.</message>
<gYearMonth>1970-11</gYearMonth>
<type:year>02</type:year>
</test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<test xmlns="http://simonstl.com/ns/test/" xmlns:type="http://simonstl.com/ns/types/">
<message>Hello!  This document contains a gYearMonth.</message>
<gYearMonth><type:century>19</type:century><type:year>70</type:year><type:month>11</type:month></gYearMonth>
<type:year><type:digit>0</type:digit><type:digit>2</type:digit></type:year>
</test>""")

	def test3(self):
		rules = self.rulesParser("""
<fragmentRules xmlns="http://simonstl.com/ns/fragments/">
<fragmentRule pattern="(\d{2,5})(\d{2})-(\d{2})">
<applyTo>
<element nsURI="http://simonstl.com/ns/test/" localName="gYearMonth"/>
<element nsURI="http://simonstl.com/ns/test/" localName="myYearMonth"/>
</applyTo>
<produce>
<element nsURI="http://simonstl.com/ns/types/" localName="century" prefix="type" />
<skip/>
<element nsURI="http://simonstl.com/ns/types/" localName="month" prefix="type" />
</produce>
</fragmentRule>
</fragmentRules>		""")
		self.fragmentDocument(rules, """
		<test xmlns="http://simonstl.com/ns/test/" xmlns:type="http://simonstl.com/ns/types/">
<message>Hello!  This document contains a gYearMonth.</message>
<gYearMonth>1970-11</gYearMonth>
<myYearMonth>1970-11</myYearMonth>
</test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<test xmlns="http://simonstl.com/ns/test/" xmlns:type="http://simonstl.com/ns/types/">
<message>Hello!  This document contains a gYearMonth.</message>
<gYearMonth><type:century>19</type:century><type:month>11</type:month></gYearMonth>
<myYearMonth><type:century>19</type:century><type:month>11</type:month></myYearMonth>
</test>""")
		self.fragmentDocument(rules, """
		<test xmlns="http://simonstl.com/ns/test/" xmlns:type="foo">
<message>Hello!  This document contains a gYearMonth.</message>
<gYearMonth>1970<foo/>-11</gYearMonth>
<myYearMonth>1970-11</myYearMonth>
</test>""", valid=0)
		
	def test4(self):
		rules = self.rulesParser("""
<fragmentRules xmlns="http://simonstl.com/ns/fragments/">
<fragmentRule pattern="(\d{2,5})(\d{2})-(\d{2})">
<applyTo>
<element nsURI="http://simonstl.com/ns/test/" localName="gYearMonth"/>
<element nsURI="http://simonstl.com/ns/test/" localName="myYearMonth"/>
</applyTo>
<produce>
<element nsURI="http://simonstl.com/ns/types/" localName="century" prefix="type" />
<chars/>
<element nsURI="http://simonstl.com/ns/types/" localName="month" prefix="type" />
</produce>
</fragmentRule>
</fragmentRules>		""")
		self.fragmentDocument(rules, """
		<test xmlns="http://simonstl.com/ns/test/" xmlns:type="http://simonstl.com/ns/types/">
<message>Hello!  This document contains a gYearMonth.</message>
<gYearMonth>1970-11</gYearMonth>
<myYearMonth>1970-11</myYearMonth>
</test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<test xmlns="http://simonstl.com/ns/test/" xmlns:type="http://simonstl.com/ns/types/">
<message>Hello!  This document contains a gYearMonth.</message>
<gYearMonth><type:century>19</type:century>70<type:month>11</type:month></gYearMonth>
<myYearMonth><type:century>19</type:century>70<type:month>11</type:month></myYearMonth>
</test>""")
		
	def test5(self):
		rules = self.rulesParser("""
<fragmentRules xmlns="http://simonstl.com/ns/fragments/">
<fragmentRule pattern="(\d{2,5})(\d{2})-(\d{2})">
<applyTo>
<element nsURI="http://simonstl.com/ns/test/" localName="gYearMonth"/>
<element nsURI="http://simonstl.com/ns/test/" localName="myYearMonth"/>
</applyTo>
<produce>
<element nsURI="http://simonstl.com/ns/types/" localName="century" prefix="type" />
<chars before="year: " after="!"/><!--includes year as characters -->
<element nsURI="http://simonstl.com/ns/types/" localName="month" prefix="type" />
</produce>
</fragmentRule>
</fragmentRules>		""")
		self.fragmentDocument(rules, """
		<test xmlns="http://simonstl.com/ns/test/" xmlns:type="http://simonstl.com/ns/types/">
<message>Hello!  This document contains a gYearMonth.</message>
<gYearMonth>1970-11</gYearMonth>
<myYearMonth>1970-11</myYearMonth>
</test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<test xmlns="http://simonstl.com/ns/test/" xmlns:type="http://simonstl.com/ns/types/">
<message>Hello!  This document contains a gYearMonth.</message>
<gYearMonth><type:century>19</type:century>year: 70!<type:month>11</type:month></gYearMonth>
<myYearMonth><type:century>19</type:century>year: 70!<type:month>11</type:month></myYearMonth>
</test>""")
		
	def test6(self):
		rules = self.rulesParser("""
<fragmentRules xmlns="http://simonstl.com/ns/fragments/">
<fragmentRule pattern="(\d{2,5})(\d{2})-(\d{2})">
<applyTo>
<element nsURI="http://simonstl.com/ns/test/" localName="gYearMonth"/>
<element nsURI="http://simonstl.com/ns/test/" localName="myYearMonth"/>
</applyTo>
<produce>
<attribute nsURI="http://simonstl.com/ns/types/" localName="century" prefix="type" />
<attribute nsURI="http://simonstl.com/ns/types/" localName="year" prefix="type" />
<attribute nsURI="http://simonstl.com/ns/types/" localName="month" prefix="type" />
</produce>
</fragmentRule>
</fragmentRules>		""")
		self.fragmentDocument(rules, """
		<test xmlns="http://simonstl.com/ns/test/" >
<message>Hello!  This document contains a gYearMonth.</message>
<gYearMonth>1970-11</gYearMonth>
<myYearMonth>1970-11</myYearMonth>
</test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<test xmlns="http://simonstl.com/ns/test/">
<message>Hello!  This document contains a gYearMonth.</message>
<gYearMonth xmlns:type="http://simonstl.com/ns/types/" type:month="11" type:century="19" type:year="70"></gYearMonth>
<myYearMonth xmlns:type="http://simonstl.com/ns/types/" type:month="11" type:century="19" type:year="70"></myYearMonth>
</test>""")
		self.fragmentDocument(rules, """
		<test xmlns="http://simonstl.com/ns/test/" xmlns:type="http://simonstl.com/ns/types/" >
<message>Hello!  This document contains a gYearMonth.</message>
<gYearMonth>1970-11</gYearMonth>
<myYearMonth>1970-11</myYearMonth>
</test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<test xmlns="http://simonstl.com/ns/test/" xmlns:type="http://simonstl.com/ns/types/">
<message>Hello!  This document contains a gYearMonth.</message>
<gYearMonth type:month="11" type:century="19" type:year="70"></gYearMonth>
<myYearMonth type:month="11" type:century="19" type:year="70"></myYearMonth>
</test>""")
		self.fragmentDocument(rules, """
		<test xmlns="http://simonstl.com/ns/test/" xmlns:type="foo" xmlns:bar="http://simonstl.com/ns/types/" >
<message>Hello!  This document contains a gYearMonth.</message>
<gYearMonth>1970-11</gYearMonth>
<myYearMonth>1970-11</myYearMonth>
</test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<test xmlns="http://simonstl.com/ns/test/" xmlns:type="foo" xmlns:bar="http://simonstl.com/ns/types/">
<message>Hello!  This document contains a gYearMonth.</message>
<gYearMonth bar:month="11" bar:century="19" bar:year="70"></gYearMonth>
<myYearMonth bar:month="11" bar:century="19" bar:year="70"></myYearMonth>
</test>""")
		self.fragmentDocument(rules, """
		<test xmlns="http://simonstl.com/ns/test/" xmlns:type="foo">
<message>Hello!  This document contains a gYearMonth.</message>
<gYearMonth>1970-11</gYearMonth>
<myYearMonth>1970-11</myYearMonth>
</test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<test xmlns="http://simonstl.com/ns/test/" xmlns:type="foo">
<message>Hello!  This document contains a gYearMonth.</message>
<gYearMonth xmlns:type1="http://simonstl.com/ns/types/" type1:month="11" type1:century="19" type1:year="70"></gYearMonth>
<myYearMonth xmlns:type1="http://simonstl.com/ns/types/" type1:month="11" type1:century="19" type1:year="70"></myYearMonth>
</test>""")


	def test7(self):
		rules = self.rulesParser("""
<fragmentRules xmlns="http://simonstl.com/ns/fragments/">

<fragmentRule pattern="(\d{2,5})(\d{2})-(\d{2})">
<applyTo>
  <element nsURI="http://simonstl.com/ns/test/" localName="gYearMonth"/>
  <element nsURI="http://simonstl.com/ns/test/" localName="myYearMonth"/>
</applyTo>
<produce>
  <element nsURI="http://simonstl.com/ns/types/" localName="century" prefix="" />
  <element nsURI="http://simonstl.com/ns/types/" localName="year" prefix="" />
  <element nsURI="http://simonstl.com/ns/types/" localName="month" prefix="" />
</produce>
</fragmentRule>

</fragmentRules>		""")
		self.fragmentDocument(rules, """
		<test xmlns="http://simonstl.com/ns/test/">
<message>Hello!  This document contains a gYearMonth.</message>
<gYearMonth>1970-11</gYearMonth>
<myYearMonth>1970-11</myYearMonth>
</test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<test xmlns="http://simonstl.com/ns/test/">
<message>Hello!  This document contains a gYearMonth.</message>
<gYearMonth><century xmlns="http://simonstl.com/ns/types/">19</century><year xmlns="http://simonstl.com/ns/types/">70</year><month xmlns="http://simonstl.com/ns/types/">11</month></gYearMonth>
<myYearMonth xmlns="http://simonstl.com/ns/test/"><century xmlns="http://simonstl.com/ns/types/">19</century><year xmlns="http://simonstl.com/ns/types/">70</year><month xmlns="http://simonstl.com/ns/types/">11</month></myYearMonth>
</test>""")
		self.fragmentDocument(rules, """
		<t:test xmlns:t="http://simonstl.com/ns/test/" xmlns="http://simonstl.com/ns/types/">
<t:message>Hello!  This document contains a gYearMonth.</t:message>
<t:gYearMonth>1970-11</t:gYearMonth>
<t:myYearMonth>1970-11</t:myYearMonth>
</t:test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<t:test xmlns:t="http://simonstl.com/ns/test/" xmlns="http://simonstl.com/ns/types/">
<t:message>Hello!  This document contains a gYearMonth.</t:message>
<t:gYearMonth><century>19</century><year>70</year><month>11</month></t:gYearMonth>
<t:myYearMonth><century>19</century><year>70</year><month>11</month></t:myYearMonth>
</t:test>""")
	
	def test8(self):
		rules = self.rulesParser("""
<fragmentRules xmlns="http://simonstl.com/ns/fragments/">

<fragmentRule pattern="(\d{2,5})(\d{2})-(\d{2})">
<applyTo>
  <element nsURI="http://simonstl.com/ns/test/" localName="gYearMonth"/>
  <element nsURI="http://simonstl.com/ns/test/" localName="myYearMonth"/>
</applyTo>
<produce>
  <element nsURI="" localName="century" prefix="" />
  <element nsURI="" localName="year" prefix="" />
  <element localName="month"  />
</produce>
</fragmentRule>

</fragmentRules>		""")
		self.fragmentDocument(rules, """
		<test xmlns="http://simonstl.com/ns/test/">
<message>Hello!  This document contains a gYearMonth.</message>
<gYearMonth>1970-11</gYearMonth>
<myYearMonth>1970-11</myYearMonth>
</test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<test xmlns="http://simonstl.com/ns/test/">
<message>Hello!  This document contains a gYearMonth.</message>
<gYearMonth><century xmlns="">19</century><year xmlns="">70</year><month xmlns="">11</month></gYearMonth>
<myYearMonth xmlns="http://simonstl.com/ns/test/"><century xmlns="">19</century><year xmlns="">70</year><month xmlns="">11</month></myYearMonth>
</test>""")
		self.fragmentDocument(rules, """
		<t:test xmlns:t="http://simonstl.com/ns/test/" xmlns="http://simonstl.com/ns/types/">
<t:message>Hello!  This document contains a gYearMonth.</t:message>
<t:gYearMonth>1970-11</t:gYearMonth>
<t:myYearMonth>1970-11</t:myYearMonth>
</t:test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<t:test xmlns:t="http://simonstl.com/ns/test/" xmlns="http://simonstl.com/ns/types/">
<t:message>Hello!  This document contains a gYearMonth.</t:message>
<t:gYearMonth><century xmlns="">19</century><year xmlns="">70</year><month xmlns="">11</month></t:gYearMonth>
<t:myYearMonth xmlns="http://simonstl.com/ns/types/"><century xmlns="">19</century><year xmlns="">70</year><month xmlns="">11</month></t:myYearMonth>
</t:test>""")
		
	def test9(self):
		rules = self.rulesParser("""
<fragmentRules xmlns="http://simonstl.com/ns/fragments/">

<fragmentRule pattern="(\d{2,5})(\d{2})-(\d{2})">
<applyTo>
  <element nsURI="http://simonstl.com/ns/test/" localName="gYearMonth"/>
  <element nsURI="http://simonstl.com/ns/test/" localName="myYearMonth"/>
</applyTo>
<produce>
  <element nsURI="" localName="century" prefix="" />
  <attribute nsURI="" localName="year" prefix="" />
  <attribute localName="month"  />
</produce>
</fragmentRule>

</fragmentRules>		""")
		self.fragmentDocument(rules, """
		<test xmlns="http://simonstl.com/ns/test/">
<message>Hello!  This document contains a gYearMonth.</message>
<gYearMonth>1970-11</gYearMonth>
<myYearMonth>1970-11</myYearMonth>
</test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<test xmlns="http://simonstl.com/ns/test/">
<message>Hello!  This document contains a gYearMonth.</message>
<gYearMonth year="70" month="11"><century xmlns="">19</century></gYearMonth>
<myYearMonth xmlns="http://simonstl.com/ns/test/" year="70" month="11"><century xmlns="">19</century></myYearMonth>
</test>""")
		
	def test10(self):
		rules = self.rulesParser("""<?xml version="1.0" encoding="UTF-8"?>
<fragmentRules xmlns="http://simonstl.com/ns/fragments/">

<fragmentRule pattern="-" split="true" skipFirst="false">
<applyTo>
<element nsURI="http://simonstl.com/ns/test/" localName="threepart"/>
</applyTo>
<produce>
<element nsURI="http://simonstl.com/ns/types/" localName="one" prefix="type" />
<element nsURI="http://simonstl.com/ns/types/" localName="two" prefix="type" />
<element nsURI="http://simonstl.com/ns/types/" localName="three" prefix="type" />
</produce>
</fragmentRule>

</fragmentRules>""")
		self.fragmentDocument(rules, """<test xmlns="http://simonstl.com/ns/test/">
<message>Hello!  This document contains a splittable piece.</message>
<threepart>2100-b4a50-999F-2345-5432-1234</threepart>
</test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<test xmlns="http://simonstl.com/ns/test/">
<message>Hello!  This document contains a splittable piece.</message>
<threepart><type:one xmlns:type="http://simonstl.com/ns/types/">2100</type:one><type:two xmlns:type="http://simonstl.com/ns/types/">b4a50</type:two><type:three xmlns:type="http://simonstl.com/ns/types/">999F</type:three></threepart>
</test>""")
		
		self.fragmentDocument(rules, """<test xmlns="http://simonstl.com/ns/test/">
<message>Hello!  This document contains a splittable piece.</message>
<threepart>2100-b4a50-999F</threepart>
</test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<test xmlns="http://simonstl.com/ns/test/">
<message>Hello!  This document contains a splittable piece.</message>
<threepart><type:one xmlns:type="http://simonstl.com/ns/types/">2100</type:one><type:two xmlns:type="http://simonstl.com/ns/types/">b4a50</type:two><type:three xmlns:type="http://simonstl.com/ns/types/">999F</type:three></threepart>
</test>""")
		

	def test11(self):
		rules = self.rulesParser("""<?xml version="1.0" encoding="UTF-8"?>
<fragmentRules xmlns="http://simonstl.com/ns/fragments/">

<fragmentRule pattern="-" split="true" skipFirst="false" repeat="true">
<applyTo>
<element nsURI="http://simonstl.com/ns/test/" localName="threepart"/>
</applyTo>
<produce>
<element nsURI="http://simonstl.com/ns/types/" localName="one" prefix="type" />
<element nsURI="http://simonstl.com/ns/types/" localName="two" prefix="type" />
<element nsURI="http://simonstl.com/ns/types/" localName="three" prefix="type" />
</produce>
</fragmentRule>

</fragmentRules>""")
		self.fragmentDocument(rules, """<test xmlns="http://simonstl.com/ns/test/">
<message>Hello!  This document contains a splittable piece.</message>
<threepart>2100-b4a50-999F-2345-5432-1234</threepart>
</test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<test xmlns="http://simonstl.com/ns/test/">
<message>Hello!  This document contains a splittable piece.</message>
<threepart><type:one xmlns:type="http://simonstl.com/ns/types/">2100</type:one><type:two xmlns:type="http://simonstl.com/ns/types/">b4a50</type:two><type:three xmlns:type="http://simonstl.com/ns/types/">999F</type:three><type:one xmlns:type="http://simonstl.com/ns/types/">2345</type:one><type:two xmlns:type="http://simonstl.com/ns/types/">5432</type:two><type:three xmlns:type="http://simonstl.com/ns/types/">1234</type:three></threepart>
</test>""")
		self.fragmentDocument(rules, """<test xmlns="http://simonstl.com/ns/test/">
<message>Hello!  This document contains a splittable piece.</message>
<threepart>2100-b4a50-999F</threepart>
</test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<test xmlns="http://simonstl.com/ns/test/">
<message>Hello!  This document contains a splittable piece.</message>
<threepart><type:one xmlns:type="http://simonstl.com/ns/types/">2100</type:one><type:two xmlns:type="http://simonstl.com/ns/types/">b4a50</type:two><type:three xmlns:type="http://simonstl.com/ns/types/">999F</type:three></threepart>
</test>""")
		
	def test12(self):
		rules = self.rulesParser("""<?xml version="1.0" encoding="UTF-8"?>
<fragmentRules xmlns="http://simonstl.com/ns/fragments/">

<fragmentRule pattern="-" split="true" skipFirst="false" repeat="false">
<applyTo>
<element nsURI="http://simonstl.com/ns/test/" localName="threepart"/>
</applyTo>
<produce>
<attribute localName="one"/>
<element nsURI="http://simonstl.com/ns/types/" localName="two" prefix="type" />
<attribute localName="three"/>
</produce>
</fragmentRule>

</fragmentRules>""")
		self.fragmentDocument(rules, """<test xmlns="http://simonstl.com/ns/test/">
<message>Hello!  This document contains a splittable piece.</message>
<threepart>2100-b4a50-999F-2345-5432-1234</threepart>
</test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<test xmlns="http://simonstl.com/ns/test/">
<message>Hello!  This document contains a splittable piece.</message>
<threepart one="2100" three="999F"><type:two xmlns:type="http://simonstl.com/ns/types/">b4a50</type:two></threepart>
</test>""")
		self.fragmentDocument(rules, """<test xmlns="http://simonstl.com/ns/test/">
<message>Hello!  This document contains a splittable piece.</message>
<threepart>2100-b4a50-999F</threepart>
</test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<test xmlns="http://simonstl.com/ns/test/">
<message>Hello!  This document contains a splittable piece.</message>
<threepart one="2100" three="999F"><type:two xmlns:type="http://simonstl.com/ns/types/">b4a50</type:two></threepart>
</test>""")
		self.fragmentDocument(rules, """<test xmlns="http://simonstl.com/ns/test/">
<message>Hello!  This document contains a splittable piece.</message>
<threepart one="foo">2100-b4a50-999F</threepart>
</test>""", valid=0)
		
	def test13(self):
		rules = self.rulesParser("""<?xml version="1.0" encoding="UTF-8"?>
<fragmentRules xmlns="http://simonstl.com/ns/fragments/">
<fragmentRule pattern="(\d)(\d)?(\d)?(\d)?(\d)?" repeat="true">
<applyTo>
<element localName="foo"/>
</applyTo>
<produce>
<element localName="group"/>
</produce>
</fragmentRule>
</fragmentRules>""")
		self.fragmentDocument(rules, """<test>
		<foo>12345</foo>
		<foo>1</foo>
		</test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<test>
		<foo><group xmlns="">1</group><group xmlns="">2</group><group xmlns="">3</group><group xmlns="">4</group><group xmlns="">5</group></foo>
		<foo><group xmlns="">1</group></foo>
		</test>""")

	def test14(self):
		rules = self.rulesParser("""<?xml version="1.0" encoding="UTF-8"?>
<fragmentRules xmlns="http://simonstl.com/ns/fragments/">
<fragmentRule pattern="(\d)(\d)?(\d)?$" >
<applyTo>
<element localName="foo"/>
</applyTo>
<produce>
<element localName="group"/>
<element localName="group"/>
<element localName="group"/>
</produce>
</fragmentRule>
</fragmentRules>""")
		self.fragmentDocument(rules, """<test>
		<foo></foo>
		<foo>azerty</foo>
		<foo>1</foo>
		<foo>12</foo>
		<foo>123</foo>
		<foo>1234</foo>
		<foo>a123</foo>
		</test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<test>
		<foo></foo>
		<foo></foo>
		<foo><group xmlns="">1</group></foo>
		<foo><group xmlns="">1</group><group xmlns="">2</group></foo>
		<foo><group xmlns="">1</group><group xmlns="">2</group><group xmlns="">3</group></foo>
		<foo></foo>
		<foo></foo>
		</test>""")

	def test15(self):
		rules = self.rulesParser("""<?xml version="1.0" encoding="UTF-8"?>
<fragmentRules xmlns="http://simonstl.com/ns/fragments/">
<fragmentRule pattern="(\d)(\d)?(\d)?$" >
<applyTo>
<element localName="foo"/>
</applyTo>
<produce>
<element localName="group"/>
<element localName="group"/>
</produce>
</fragmentRule>
</fragmentRules>""")
		self.fragmentDocument(rules, """<test>
		<foo></foo>
		<foo>azerty</foo>
		<foo>1</foo>
		<foo>12</foo>
		<foo>123</foo>
		<foo>1234</foo>
		<foo>a123</foo>
		</test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<test>
		<foo></foo>
		<foo></foo>
		<foo><group xmlns="">1</group></foo>
		<foo><group xmlns="">1</group><group xmlns="">2</group></foo>
		<foo><group xmlns="">1</group><group xmlns="">2</group></foo>
		<foo></foo>
		<foo></foo>
		</test>""")

	def test15(self):
		rules = self.rulesParser("""<?xml version="1.0" encoding="UTF-8"?>
<fragmentRules xmlns="http://simonstl.com/ns/fragments/">
<fragmentRule pattern="(\d)(\d)?(\d)?$" >
<applyTo>
<element localName="foo"/>
</applyTo>
<produce>
<attribute localName="g1"/>
<attribute localName="g2"/>
</produce>
</fragmentRule>
</fragmentRules>""")
		self.fragmentDocument(rules, """<test>
		<foo></foo>
		<foo>azerty</foo>
		<foo>1</foo>
		<foo>12</foo>
		<foo>123</foo>
		<foo>1234</foo>
		<foo>a123</foo>
		</test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<test>
		<foo></foo>
		<foo></foo>
		<foo g1="1"></foo>
		<foo g1="1" g2="2"></foo>
		<foo g1="1" g2="2"></foo>
		<foo></foo>
		<foo></foo>
		</test>""")


	def test16(self):
		rules = self.rulesParser("""<?xml version="1.0" encoding="UTF-8"?>
<fragmentRules xmlns="http://simonstl.com/ns/fragments/">
<fragmentRule pattern="(\d)(\d)?(\d)?$" >
<applyTo>
<element localName="foo"/>
</applyTo>
<produce>
<chars />
<chars before = ", "/>
</produce>
</fragmentRule>
</fragmentRules>""")
		self.fragmentDocument(rules, """<test>
		<foo></foo>
		<foo>azerty</foo>
		<foo>1</foo>
		<foo>12</foo>
		<foo>123</foo>
		<foo>1234</foo>
		<foo>a123</foo>
		</test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<test>
		<foo></foo>
		<foo></foo>
		<foo>1</foo>
		<foo>1, 2</foo>
		<foo>1, 2</foo>
		<foo></foo>
		<foo></foo>
		</test>""")

	def test17(self):
		rules = self.rulesParser("""<?xml version="1.0" encoding="UTF-8"?>
<fragmentRules xmlns="http://simonstl.com/ns/fragments/">
<fragmentRule pattern="(\d)(\d)?(\d)?" repeat="true">
<applyTo>
<element localName="foo"/>
</applyTo>
<produce>
<chars after = " "/>
</produce>
</fragmentRule>
</fragmentRules>""")
		self.fragmentDocument(rules, """<test>
		<foo></foo>
		<foo>azerty</foo>
		<foo>1</foo>
		<foo>12</foo>
		<foo>123</foo>
		<foo>1234</foo>
		<foo>a123</foo>
		</test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<test>
		<foo></foo>
		<foo></foo>
		<foo>1 </foo>
		<foo>1 2 </foo>
		<foo>1 2 3 </foo>
		<foo>1 2 3 </foo>
		<foo></foo>
		</test>""")

	def test18(self):
		rules = self.rulesParser("""<?xml version="1.0" encoding="UTF-8"?>
<fragmentRules xmlns="http://simonstl.com/ns/fragments/">
<fragmentRule pattern="(\d)(\d)?(\d)?" repeat="true" skipFirst="true">
<applyTo>
<element localName="foo"/>
</applyTo>
<produce>
<chars after = " "/>
</produce>
</fragmentRule>
</fragmentRules>""")
		self.fragmentDocument(rules, """<test>
		<foo></foo>
		<foo>azerty</foo>
		<foo>1</foo>
		<foo>12</foo>
		<foo>123</foo>
		<foo>1234</foo>
		<foo>a123</foo>
		</test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<test>
		<foo></foo>
		<foo></foo>
		<foo></foo>
		<foo>2 </foo>
		<foo>2 3 </foo>
		<foo>2 3 </foo>
		<foo></foo>
		</test>""")

	def test19(self):
		rules = self.rulesParser("""<?xml version="1.0" encoding="UTF-8"?>
<fragmentRules xmlns="http://simonstl.com/ns/fragments/">
<fragmentRule pattern="(\d)(\d)?(\d)?" repeat="true" skipFirst="true">
<applyTo>
<element localName="foo"/>
</applyTo>
<produce>
<attribute localName="bar"/>
</produce>
</fragmentRule>
</fragmentRules>""")
		self.fragmentDocument(rules, """<test>
		<foo></foo>
		<foo>azerty</foo>
		<foo>1</foo>
		<foo>12</foo>
		</test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<test>
		<foo></foo>
		<foo></foo>
		<foo></foo>
		<foo bar="2"></foo>
		</test>""")
		self.fragmentDocument(rules, """<test>
		<foo bar="">12</foo>
		</test>""", valid=0)
		self.fragmentDocument(rules, """<test>
		<foo>123</foo>
		</test>""", valid=0)


	def test20element(self):
		rules = self.rulesParser("""
<fragmentRules xmlns="http://simonstl.com/ns/fragments/">

<fragmentRule pattern="(\d{2,5})(\d{2})-(\d{2})">
<applyTo>
  <attribute localName="gYearMonth"/>
  <attribute localName="myYearMonth"/>
</applyTo>
<produce>
  <element nsURI="http://simonstl.com/ns/types/" localName="century" prefix="type" />
  <element nsURI="http://simonstl.com/ns/types/" localName="year" prefix="type" />
  <element nsURI="http://simonstl.com/ns/types/" localName="month" prefix="type" />
</produce>
</fragmentRule>

</fragmentRules>		""")
		self.fragmentDocument(rules, """
		<test xmlns="http://simonstl.com/ns/test/" xmlns:type="http://simonstl.com/ns/types/">
<foo gYearMonth="1970-11"/>
<foo myYearMonth="1970-11"/>
<foo gYearMonth="1970-11" myYearMonth="2002-06"/>
</test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<test xmlns="http://simonstl.com/ns/test/" xmlns:type="http://simonstl.com/ns/types/">
<foo><type:century>19</type:century><type:year>70</type:year><type:month>11</type:month></foo>
<foo><type:century>19</type:century><type:year>70</type:year><type:month>11</type:month></foo>
<foo><type:century>19</type:century><type:year>70</type:year><type:month>11</type:month><type:century>20</type:century><type:year>02</type:year><type:month>06</type:month></foo>
</test>
""")

	def test21element(self):
		rules = self.rulesParser("""
<fragmentRules xmlns="http://simonstl.com/ns/fragments/">

<fragmentRule pattern="(\d{2,5})(\d{2})-(\d{2})" repeat="true">
<applyTo>
  <attribute localName="gYearMonth"/>
  <attribute localName="myYearMonth"/>
</applyTo>
<produce>
	<chars after=" "/>
</produce>
</fragmentRule>

</fragmentRules>		""")
		self.fragmentDocument(rules, """
		<test xmlns="http://simonstl.com/ns/test/" xmlns:type="http://simonstl.com/ns/types/">
<foo gYearMonth="1970-11"/>
<foo myYearMonth="1970-11"/>
<foo gYearMonth="1970-11" myYearMonth="2002-06"/>
</test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<test xmlns="http://simonstl.com/ns/test/" xmlns:type="http://simonstl.com/ns/types/">
<foo>19 70 11 </foo>
<foo>19 70 11 </foo>
<foo>19 70 11 20 02 06 </foo>
</test>
""")
	
	def test22element(self):
		rules = self.rulesParser("""
<fragmentRules xmlns="http://simonstl.com/ns/fragments/">
<fragmentRule pattern="(\d{2,5})(\d{2})-(\d{2})"> 
<applyTo>
  <attribute localName="gYearMonth"/>
  <attribute localName="myYearMonth"/>
</applyTo>
<produce>
	<attribute localName="century"/>
	<attribute localName="year"/>
	<attribute localName="month"/>
</produce>
</fragmentRule>
</fragmentRules>		""")
		self.fragmentDocument(rules, """
		<test xmlns="http://simonstl.com/ns/test/" xmlns:type="http://simonstl.com/ns/types/">
<foo gYearMonth="1970-11"/>
<foo myYearMonth="1970-11"/>
<foo gYearMonth="1970-11" myYearMonth="2002-06"/>
</test>""", valid=0)
		self.fragmentDocument(rules, """
		<test xmlns="http://simonstl.com/ns/test/" xmlns:type="http://simonstl.com/ns/types/">
<foo gYearMonth="1970-11"/>
<foo myYearMonth="1970-11"/>
</test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<test xmlns="http://simonstl.com/ns/test/" xmlns:type="http://simonstl.com/ns/types/">
<foo century="19" month="11" year="70"></foo>
<foo century="19" month="11" year="70"></foo>
</test>
""")
	
	def test23element(self):
		rules = self.rulesParser("""
<fragmentRules xmlns="http://simonstl.com/ns/fragments/">
<fragmentRule pattern="(\d{2,5})(\d{2})-(\d{2})"> 
<applyTo>
  <attribute localName="gYearMonth"/>
  <attribute localName="myYearMonth"/>
</applyTo>
<produce>
	<attribute localName="century"/>
	<attribute localName="year"/>
	<attribute localName="month"/>
</produce>
</fragmentRule>
<fragmentRule pattern="(\d)(\d)" repeat="true"> 
<applyTo>
  <attribute localName="century"/>
</applyTo>
<produce>
	<chars after=" "/>
</produce>
</fragmentRule>
</fragmentRules>		""")
		self.fragmentDocument(rules, """
		<test xmlns="http://simonstl.com/ns/test/" xmlns:type="http://simonstl.com/ns/types/">
<foo gYearMonth="1970-11"/>
<foo myYearMonth="1970-11"/>
<foo gYearMonth="1970-11" myYearMonth="2002-06"/>
</test>""", valid=0)
		self.fragmentDocument(rules, """
		<test xmlns="http://simonstl.com/ns/test/" xmlns:type="http://simonstl.com/ns/types/">
<foo gYearMonth="1970-11"/>
<foo myYearMonth="1970-11"/>
</test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<test xmlns="http://simonstl.com/ns/test/" xmlns:type="http://simonstl.com/ns/types/">
<foo year="70" month="11">1 9 </foo>
<foo year="70" month="11">1 9 </foo>
</test>
""")
		self.fragmentDocument(rules, """
		<test xmlns="http://simonstl.com/ns/test/" xmlns:type="http://simonstl.com/ns/types/">
<foo gYearMonth="1970-11"/>
<foo myYearMonth="1970-11" month=""/>
</test>""", valid=0)

	def test24element(self):
		rules = self.rulesParser("""
<fragmentRules xmlns="http://simonstl.com/ns/fragments/">
<fragmentRule pattern="(\d{2,5})(\d{2})-(\d{2})"> 
<applyTo>
  <attribute localName="gYearMonth"/>
  <attribute localName="myYearMonth"/>
</applyTo>
<produce>
	<attribute localName="century"/>
	<attribute localName="year"/>
	<attribute localName="month"/>
</produce>
</fragmentRule>
<fragmentRule pattern="(\d)(\d)" > 
<applyTo>
  <attribute localName="century"/>
</applyTo>
<produce>
	<attribute localName="d1"/>
	<attribute localName="d2"/>
</produce>
</fragmentRule>
</fragmentRules>		""")
		self.fragmentDocument(rules, """
		<test xmlns="http://simonstl.com/ns/test/" xmlns:type="http://simonstl.com/ns/types/">
<foo gYearMonth="1970-11"/>
<foo myYearMonth="1970-11"/>
<foo gYearMonth="1970-11" myYearMonth="2002-06"/>
</test>""", valid=0)
		self.fragmentDocument(rules, """
		<test xmlns="http://simonstl.com/ns/test/" xmlns:type="http://simonstl.com/ns/types/">
<foo gYearMonth="1970-11"/>
<foo myYearMonth="1970-11"/>
</test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<test xmlns="http://simonstl.com/ns/test/" xmlns:type="http://simonstl.com/ns/types/">
<foo month="11" d1="1" d2="9" year="70"></foo>
<foo month="11" d1="1" d2="9" year="70"></foo>
</test>
""")
		self.fragmentDocument(rules, """
		<test xmlns="http://simonstl.com/ns/test/" xmlns:type="http://simonstl.com/ns/types/">
<foo gYearMonth="1970-11"/>
<foo myYearMonth="1970-11" d1=""/>
</test>""", valid=0)

	def test25(self):
		rules = self.rulesParser("""
<fragmentRules xmlns="http://simonstl.com/ns/fragments/">
<fragmentRule pattern="(\d{2,5})(\d{2})-(\d{2})" break="true"> 
<applyTo>
  <attribute localName="gYearMonth"/>
  <attribute localName="myYearMonth"/>
</applyTo>
<produce>
	<attribute localName="century"/>
	<attribute localName="year"/>
	<attribute localName="month"/>
</produce>
</fragmentRule>
<fragmentRule pattern="(\d)(\d)" > 
<applyTo>
  <attribute localName="century"/>
</applyTo>
<produce>
	<attribute localName="d1"/>
	<attribute localName="d2"/>
</produce>
</fragmentRule>
</fragmentRules>		""")
		self.fragmentDocument(rules, """
		<test xmlns="http://simonstl.com/ns/test/" xmlns:type="http://simonstl.com/ns/types/">
<foo gYearMonth="1970-11"/>
<foo myYearMonth="1970-11"/>
<foo gYearMonth="1970-11" myYearMonth="2002-06"/>
</test>""", valid=0)
		self.fragmentDocument(rules, """
		<test xmlns="http://simonstl.com/ns/test/" xmlns:type="http://simonstl.com/ns/types/">
<foo gYearMonth="1970-11"/>
<foo myYearMonth="1970-11"/>
</test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<test xmlns="http://simonstl.com/ns/test/" xmlns:type="http://simonstl.com/ns/types/">
<foo century="19" month="11" year="70"></foo>
<foo century="19" month="11" year="70"></foo>
</test>
""")
		self.fragmentDocument(rules, """
		<test xmlns="http://simonstl.com/ns/test/" xmlns:type="http://simonstl.com/ns/types/">
<foo gYearMonth="1970-11"/>
<foo myYearMonth="1970-11" d1=""/>
<foo century="1970-11"/>
</test>""", expected="""<?xml version="1.0" encoding="iso-8859-1"?>
<test xmlns="http://simonstl.com/ns/test/" xmlns:type="http://simonstl.com/ns/types/">
<foo century="19" month="11" year="70"></foo>
<foo d1="" century="19" month="11" year="70"></foo>
<foo d1="1" d2="9"></foo>
</test>
""")

if __name__ == "__main__":
	unittest.main()

