1 38 39 40 package encoding; 41 42 import com.sun.xml.fastinfoset.sax.SAXDocumentSerializer; 43 import com.sun.xml.fastinfoset.sax.VocabularyGenerator; 44 import com.sun.xml.fastinfoset.vocab.SerializerVocabulary; 45 import java.io.ByteArrayOutputStream ; 46 import java.io.FileOutputStream ; 47 import java.io.InputStream ; 48 import java.net.URI ; 49 import java.net.URL ; 50 import javax.xml.parsers.SAXParser ; 51 import javax.xml.parsers.SAXParserFactory ; 52 import junit.framework.*; 53 54 public class EncodingTest extends TestCase { 55 56 public static final String FINF_SPEC_UBL_XML_RESOURCE = "X.finf/UBL-example.xml"; 57 public static final String FINF_SPEC_UBL_FINF_RESOURCE = "X.finf/UBL-example.finf"; 58 public static final String FINF_SPEC_UBL_FINF_REFVOCAB_RESOURCE = "X.finf/UBL-example-refvocab.finf"; 59 60 public static final String EXTERNAL_VOCABULARY_URI_STRING = "urn:oasis:names:tc:ubl:Order:1:0:joinery:example"; 61 62 private SAXParserFactory _saxParserFactory; 63 private SAXParser _saxParser; 64 private URL _xmlDocumentURL; 65 private URL _finfDocumentURL; 66 private URL _finfRefVocabDocumentURL; 67 68 private byte[] _finfDocument; 69 70 private SAXDocumentSerializer _ds; 71 private SerializerVocabulary _initialVocabulary; 72 73 public EncodingTest(java.lang.String testName) throws Exception { 74 super(testName); 75 76 _saxParserFactory = SAXParserFactory.newInstance(); 77 _saxParserFactory.setNamespaceAware(true); 78 _saxParser = _saxParserFactory.newSAXParser(); 79 80 _xmlDocumentURL = this.getClass().getClassLoader().getResource(FINF_SPEC_UBL_XML_RESOURCE); 81 _finfDocumentURL = this.getClass().getClassLoader().getResource(FINF_SPEC_UBL_FINF_RESOURCE); 82 _finfRefVocabDocumentURL = this.getClass().getClassLoader().getResource(FINF_SPEC_UBL_FINF_REFVOCAB_RESOURCE); 83 84 _ds = new SAXDocumentSerializer(); 85 _ds.setCharacterContentChunkSizeLimit(6); 86 _ds.setAttributeValueSizeLimit(6); 87 _initialVocabulary = new SerializerVocabulary(); 88 } 89 90 public static Test suite() { 91 TestSuite suite = new TestSuite(EncodingTest.class); 92 return suite; 93 } 94 95 public void testEncodeWithVocabulary() throws Exception { 96 SerializerVocabulary externalVocabulary = new SerializerVocabulary(); 97 98 VocabularyGenerator vocabularyGenerator = new VocabularyGenerator(externalVocabulary); 99 vocabularyGenerator.setCharacterContentChunkSizeLimit(0); 100 vocabularyGenerator.setAttributeValueSizeLimit(0); 101 _saxParser.parse(_xmlDocumentURL.openStream(), vocabularyGenerator); 102 103 _initialVocabulary.setExternalVocabulary( 104 new URI (EXTERNAL_VOCABULARY_URI_STRING), 105 externalVocabulary, false); 106 107 _finfDocument = parse(); 108 FileOutputStream foas = new FileOutputStream ("new-UBL-example-refvocab.finf"); 109 foas.write(_finfDocument); 110 111 compare(obtainBytesFromStream(_finfRefVocabDocumentURL.openStream())); 112 } 113 114 public void testEncodeWithoutVocabulary() throws Exception { 115 _finfDocument = parse(); 116 FileOutputStream foas = new FileOutputStream ("new-UBL-example.finf"); 117 foas.write(_finfDocument); 118 119 compare(obtainBytesFromStream(_finfDocumentURL.openStream())); 120 } 121 122 private byte[] parse() throws Exception { 123 _ds.setVocabulary(_initialVocabulary); 124 125 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 126 _ds.setOutputStream(baos); 127 128 _saxParser.parse(_xmlDocumentURL.openStream(), _ds); 129 130 return baos.toByteArray(); 131 } 132 133 private void compare(byte[] specFiDocument) throws Exception { 134 this.assertTrue("Fast infoset document is not the same length as the X.finf specification", 135 _finfDocument.length == specFiDocument.length); 136 137 System.out.println(_finfDocument.length); 138 System.out.println(specFiDocument.length); 139 boolean passed = true; 140 for (int i = 0; i < _finfDocument.length; i++) { 141 if (_finfDocument[i] != specFiDocument[i]) { 142 System.err.println(Integer.toHexString(i) + ": " + 143 Integer.toHexString(_finfDocument[i] & 0xFF) + " " + 144 Integer.toHexString(specFiDocument[i] & 0xFF)); 145 passed = false; 146 } 147 } 148 149 assertTrue("Fast infoset document does not have the same content as the X.finf specification", 150 passed); 151 152 } 153 154 static byte[] obtainBytesFromStream(InputStream s) throws Exception { 155 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 156 157 byte[] buffer = new byte[1024]; 158 159 int bytesRead = 0; 160 while ((bytesRead = s.read(buffer)) != -1) { 161 baos.write(buffer, 0, bytesRead); 162 } 163 return baos.toByteArray(); 164 } 165 } 166
| Popular Tags
|