1 38 39 40 package encoding; 41 42 import com.sun.xml.fastinfoset.EncodingConstants; 43 import com.sun.xml.fastinfoset.sax.AttributesHolder; 44 import com.sun.xml.fastinfoset.sax.SAXDocumentParser; 45 import com.sun.xml.fastinfoset.sax.SAXDocumentSerializer; 46 import com.sun.xml.fastinfoset.sax.VocabularyGenerator; 47 import com.sun.xml.fastinfoset.vocab.ParserVocabulary; 48 import com.sun.xml.fastinfoset.vocab.SerializerVocabulary; 49 import java.io.ByteArrayInputStream ; 50 import java.io.ByteArrayOutputStream ; 51 import java.io.InputStream ; 52 import java.net.URI ; 53 import java.net.URL ; 54 import java.util.HashMap ; 55 import java.util.Map ; 56 import javax.xml.parsers.SAXParser ; 57 import javax.xml.parsers.SAXParserFactory ; 58 import junit.framework.Test; 59 import junit.framework.TestCase; 60 import junit.framework.TestSuite; 61 import org.jvnet.fastinfoset.FastInfosetParser; 62 63 public class DecodingTest extends TestCase { 64 65 public static final String FINF_SPEC_UBL_XML_RESOURCE = "X.finf/UBL-example.xml"; 66 public static final String FINF_SPEC_UBL_FINF_RESOURCE = "X.finf/UBL-example.finf"; 67 public static final String FINF_SPEC_UBL_FINF_REFVOCAB_RESOURCE = "X.finf/UBL-example-refvocab.finf"; 68 69 public static final String EXTERNAL_VOCABULARY_URI_STRING = "urn:oasis:names:tc:ubl:Order:1:0:joinery:example"; 70 71 private SAXParserFactory _saxParserFactory; 72 private SAXParser _saxParser; 73 private URL _xmlDocumentURL; 74 private URL _finfDocumentURL; 75 private URL _finfRefVocabDocumentURL; 76 77 78 public DecodingTest() throws Exception { 79 _saxParserFactory = SAXParserFactory.newInstance(); 80 _saxParserFactory.setNamespaceAware(true); 81 _saxParser = _saxParserFactory.newSAXParser(); 82 83 _xmlDocumentURL = this.getClass().getClassLoader().getResource(FINF_SPEC_UBL_XML_RESOURCE); 84 _finfDocumentURL = this.getClass().getClassLoader().getResource(FINF_SPEC_UBL_FINF_RESOURCE); 85 _finfRefVocabDocumentURL = this.getClass().getClassLoader().getResource(FINF_SPEC_UBL_FINF_REFVOCAB_RESOURCE); 86 } 87 88 public static Test suite() { 89 TestSuite suite = new TestSuite(DecodingTest.class); 90 return suite; 91 } 92 93 public void testDecodeWithVocabulary() throws Exception { 94 SerializerVocabulary serializerExternalVocabulary = new SerializerVocabulary(); 95 ParserVocabulary parserExternalVocabulary = new ParserVocabulary(); 96 97 VocabularyGenerator vocabularyGenerator = new VocabularyGenerator(serializerExternalVocabulary, parserExternalVocabulary); 98 vocabularyGenerator.setCharacterContentChunkSizeLimit(0); 99 vocabularyGenerator.setAttributeValueSizeLimit(0); 100 _saxParser.parse(_xmlDocumentURL.openStream(), vocabularyGenerator); 101 102 SerializerVocabulary initialVocabulary = new SerializerVocabulary(); 103 initialVocabulary.setExternalVocabulary( 104 new URI (EXTERNAL_VOCABULARY_URI_STRING), 105 serializerExternalVocabulary, false); 106 107 Map externalVocabularies = new HashMap (); 109 externalVocabularies.put(EXTERNAL_VOCABULARY_URI_STRING, parserExternalVocabulary); 110 111 112 SAXDocumentSerializer documentSerializer = new SAXDocumentSerializer(); 113 documentSerializer.setCharacterContentChunkSizeLimit(6); 114 documentSerializer.setAttributeValueSizeLimit(6); 115 documentSerializer.setVocabulary(initialVocabulary); 116 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 117 documentSerializer.setOutputStream(baos); 118 119 SAXDocumentParser documentParser = new SAXDocumentParser(); 120 documentParser.setProperty(FastInfosetParser.EXTERNAL_VOCABULARIES_PROPERTY, externalVocabularies); 121 documentParser.setContentHandler(documentSerializer); 122 documentParser.parse(_finfRefVocabDocumentURL.openStream()); 123 124 125 byte[] finfDocument = baos.toByteArray(); 126 compare(finfDocument, obtainBytesFromStream(_finfRefVocabDocumentURL.openStream())); 127 } 128 129 public void testDecodeWithoutVocabulary() throws Exception { 130 SerializerVocabulary initialVocabulary = new SerializerVocabulary(); 131 132 SAXDocumentSerializer documentSerializer = new SAXDocumentSerializer(); 133 documentSerializer.setCharacterContentChunkSizeLimit(6); 134 documentSerializer.setAttributeValueSizeLimit(6); 135 documentSerializer.setVocabulary(initialVocabulary); 136 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 137 documentSerializer.setOutputStream(baos); 138 139 SAXDocumentParser documentParser = new SAXDocumentParser(); 140 documentParser.setContentHandler(documentSerializer); 141 documentParser.parse(_finfDocumentURL.openStream()); 142 143 144 byte[] finfDocument = baos.toByteArray(); 145 compare(finfDocument, obtainBytesFromStream(_finfDocumentURL.openStream())); 146 } 147 148 149 private void compare(byte[] fiDocument, byte[] specFiDocument) throws Exception { 150 this.assertTrue("Fast infoset document is not the same length as the X.finf specification", 151 fiDocument.length == specFiDocument.length); 152 153 boolean passed = true; 154 for (int i = 0; i < fiDocument.length; i++) { 155 if (fiDocument[i] != specFiDocument[i]) { 156 System.err.println(Integer.toHexString(i) + ": " + 157 Integer.toHexString(fiDocument[i] & 0xFF) + " " + 158 Integer.toHexString(specFiDocument[i] & 0xFF)); 159 passed = false; 160 } 161 } 162 163 assertTrue("Fast infoset document does not have the same content as the X.finf specification", 164 passed); 165 166 } 167 168 public void testDecodeWithXMLDeclaration() throws Exception { 169 for (int i = 0; i < EncodingConstants.XML_DECLARATION_VALUES.length; i++) { 170 _testDecodeWithXMLDeclaration(EncodingConstants.XML_DECLARATION_VALUES[i]); 171 } 172 } 173 174 public void _testDecodeWithXMLDeclaration(byte[] header) throws Exception { 175 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 176 baos.write(header); 177 178 SAXDocumentSerializer sds = new SAXDocumentSerializer(); 179 sds.setOutputStream(baos); 180 181 sds.startDocument(); 182 sds.startElement("", "element", "element", new AttributesHolder()); 183 sds.characters(new String ("foo").toCharArray(), 0, 3); 184 sds.endElement("", "element", "element"); 185 sds.endDocument(); 186 187 SAXDocumentParser sdp = new SAXDocumentParser(); 188 sdp.setInputStream(new ByteArrayInputStream (baos.toByteArray())); 189 sdp.parse(); 190 } 191 192 193 static byte[] obtainBytesFromStream(InputStream s) throws Exception { 194 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 195 196 byte[] buffer = new byte[1024]; 197 198 int bytesRead = 0; 199 while ((bytesRead = s.read(buffer)) != -1) { 200 baos.write(buffer, 0, bytesRead); 201 } 202 return baos.toByteArray(); 203 } 204 } 205 | Popular Tags |