1 38 39 40 package alphabet; 41 42 import com.sun.xml.fastinfoset.sax.AttributesHolder; 43 import com.sun.xml.fastinfoset.sax.SAXDocumentParser; 44 import com.sun.xml.fastinfoset.sax.SAXDocumentSerializer; 45 import com.sun.xml.fastinfoset.util.CharArrayString; 46 import com.sun.xml.fastinfoset.vocab.ParserVocabulary; 47 import com.sun.xml.fastinfoset.vocab.SerializerVocabulary; 48 import java.io.ByteArrayInputStream ; 49 import java.io.ByteArrayOutputStream ; 50 import java.io.InputStream ; 51 import java.net.URI ; 52 import java.util.HashMap ; 53 import java.util.Map ; 54 import junit.framework.*; 55 import org.jvnet.fastinfoset.FastInfosetParser; 56 import org.jvnet.fastinfoset.sax.FastInfosetDefaultHandler; 57 import org.xml.sax.Attributes ; 58 import org.xml.sax.SAXException ; 59 60 public class AlphabetTest extends TestCase { 61 protected static final String EXTERNAL_VOCABULARY_URI_STRING = "urn:external-vocabulary"; 62 63 protected AttributesHolder _attributes = new AttributesHolder(); 64 65 public AlphabetTest(String testName) { 66 super(testName); 67 } 68 69 protected void setUp() throws java.lang.Exception { 70 } 71 72 protected void tearDown() throws java.lang.Exception { 73 } 74 75 public static junit.framework.Test suite() { 76 junit.framework.TestSuite suite = new junit.framework.TestSuite(AlphabetTest.class); 77 78 return suite; 79 } 80 81 public void testBuiltInAlphabet1() throws Exception { 82 String n = "E"; 83 String d = "Z"; 84 _testBuiltInAlphabet(n, d); 85 } 86 87 public void testBuiltInAlphabet2() throws Exception { 88 String n = "E "; 89 String d = "Z "; 90 _testBuiltInAlphabet(n, d); 91 } 92 93 public void testBuiltInAlphabet3() throws Exception { 94 String n = "E 9"; 95 String d = "Z 9"; 96 _testBuiltInAlphabet(n, d); 97 } 98 99 public void testBuiltInAlphabet4() throws Exception { 100 String n = "E 91"; 101 String d = "Z 91"; 102 _testBuiltInAlphabet(n, d); 103 } 104 105 public void testBuiltInAlphabet5() throws Exception { 106 String n = "E 912"; 107 String d = "Z 912"; 108 _testBuiltInAlphabet(n, d); 109 } 110 111 public void testBuiltInAlphabet30() throws Exception { 112 String n = "0123456789-+.E 0123456789-+.E "; 113 String d = "0123456789-:TZ 0123456789-:TZ "; 114 _testBuiltInAlphabet(n, d); 115 } 116 117 public void _testBuiltInAlphabet(String n, String d) throws Exception { 118 byte[] b = createBuiltInFastInfosetDocument(n, d); 119 InputStream bais = new ByteArrayInputStream (b); 120 121 SAXDocumentParser p = new SAXDocumentParser(); 122 123 BuiltInTestHandler h = new BuiltInTestHandler(n, d); 124 125 p.setContentHandler(h); 126 127 p.parse(bais); 128 } 129 130 protected byte[] createBuiltInFastInfosetDocument(String numericString, String dateTimeString) throws Exception { 131 SAXDocumentSerializer s = new SAXDocumentSerializer(); 132 133 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 134 s.setOutputStream(baos); 135 136 _attributes.clear(); 137 138 s.startDocument(); 139 140 s.startElement("", "e", "e", _attributes); 141 142 s.startElement("", "numeric", "numeric", _attributes); 143 s.numericCharacters(numericString.toCharArray(), 0, numericString.length()); 144 s.endElement("", "numeric", "numeric"); 145 146 s.startElement("", "dateTime", "dateTime", _attributes); 147 s.dateTimeCharacters(dateTimeString.toCharArray(), 0, dateTimeString.length()); 148 s.endElement("", "dateTime", "dateTime"); 149 150 s.endElement("", "e", "e"); 151 152 s.endDocument(); 153 154 return baos.toByteArray(); 155 } 156 157 public class BuiltInTestHandler extends FastInfosetDefaultHandler { 158 String _localName; 159 160 String _numericString; 161 162 String _dateTimeString; 163 164 public BuiltInTestHandler(String numericString, String dateTimeString) { 165 _numericString = numericString; 166 _dateTimeString = dateTimeString; 167 } 168 169 171 public final void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 172 _localName = localName; 173 } 174 175 public final void characters(char[] ch, int start, int length) throws SAXException { 176 if (_localName == "numeric") { 177 assertEquals(_numericString.length(), length); 178 assertEquals(_numericString, new String (ch, start, length)); 179 } else if (_localName == "dateTime") { 180 assertEquals(_dateTimeString.length(), length); 181 assertEquals(_dateTimeString, new String (ch, start, length)); 182 } 183 } 184 } 185 186 public void testApplicationNumericAlphabet() throws Exception { 187 _testApplicationAlphabet("0123456789-+.E ", "0123456789-+.E 0123456789-+.E "); 188 } 189 190 public void testApplicationTwoCharAlphabet() throws Exception { 191 _testApplicationAlphabet("01", "1"); 192 _testApplicationAlphabet("01", "0"); 193 _testApplicationAlphabet("01", "1010101010101010101"); 194 } 195 196 public void testApplicationThreeCharAlphabet() throws Exception { 197 _testApplicationAlphabet("01X", "1"); 198 _testApplicationAlphabet("01X", "10"); 199 _testApplicationAlphabet("01X", "01X01X01X01X01X01X01X01X"); 200 } 201 202 public void testApplicationFourCharAlphabet() throws Exception { 203 _testApplicationAlphabet("01XY", "0"); 204 _testApplicationAlphabet("01XY", "Y0"); 205 _testApplicationAlphabet("01XY", "01XY01XY01XY01XY01XY01XY01XY"); 206 } 207 208 public void testApplicationSixteenCharAlphabet() throws Exception { 209 _testApplicationAlphabet("0123456789ABCDEF", "0"); 210 _testApplicationAlphabet("0123456789ABCDEF", "F0"); 211 _testApplicationAlphabet("0123456789ABCDEF", "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"); 212 } 213 214 public void _testApplicationAlphabet(String a, String s) throws Exception { 215 byte[] b = createApplicationFastInfosetDocument(a, s); 216 InputStream bais = new ByteArrayInputStream (b); 217 218 SAXDocumentParser p = new SAXDocumentParser(); 219 220 ParserVocabulary externalVocabulary = new ParserVocabulary(); 221 externalVocabulary.restrictedAlphabet.add(new CharArrayString(a)); 222 223 Map externalVocabularies = new HashMap (); 224 externalVocabularies.put(EXTERNAL_VOCABULARY_URI_STRING, externalVocabulary); 225 p.setProperty(FastInfosetParser.EXTERNAL_VOCABULARIES_PROPERTY, externalVocabularies); 226 227 ApplicationTestHandler h = new ApplicationTestHandler(a, s); 228 229 p.setContentHandler(h); 230 231 p.parse(bais); 232 } 233 234 protected byte[] createApplicationFastInfosetDocument(String alphabet, String value) throws Exception { 235 SAXDocumentSerializer s = new SAXDocumentSerializer(); 236 237 SerializerVocabulary externalVocabulary = new SerializerVocabulary(); 238 externalVocabulary.restrictedAlphabet.add(alphabet); 239 240 SerializerVocabulary initialVocabulary = new SerializerVocabulary(); 241 initialVocabulary.setExternalVocabulary( 242 new URI (EXTERNAL_VOCABULARY_URI_STRING), 243 externalVocabulary, false); 244 245 s.setVocabulary(initialVocabulary); 246 247 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 248 s.setOutputStream(baos); 249 250 _attributes.clear(); 251 252 s.startDocument(); 253 254 s.startElement("", "content", "content", _attributes); 255 s.alphabetCharacters(alphabet, value.toCharArray(), 0, value.length()); 256 s.endElement("", "content", "content"); 257 258 s.endDocument(); 259 260 return baos.toByteArray(); 261 } 262 263 public class ApplicationTestHandler extends FastInfosetDefaultHandler { 264 String _alphabet; 265 266 String _string; 267 268 public ApplicationTestHandler(String alphabet, String string) { 269 _alphabet = alphabet; 270 _string = string; 271 } 272 273 275 public final void characters(char[] ch, int start, int length) throws SAXException { 276 assertEquals(_string.length(), length); 277 assertEquals(_string, new String (ch, start, length)); 278 } 279 } 280 281 } 282 | Popular Tags |