1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package javax.xml.stream.EntitiesTest;
  25 
  26 import java.io.IOException;
  27 import java.io.InputStreamReader;
  28 import java.io.LineNumberReader;
  29 import java.io.Reader;
  30 import java.io.StringReader;
  31 import java.net.URL;
  32 
  33 import javax.xml.stream.XMLInputFactory;
  34 import javax.xml.stream.XMLStreamReader;
  35 import javax.xml.stream.events.XMLEvent;
  36 
  37 import org.testng.Assert;
  38 import org.testng.annotations.AfterMethod;
  39 import org.testng.annotations.BeforeMethod;
  40 import org.testng.annotations.Test;
  41 
  42 /*
  43  * @summary Test StAX parses entity.
  44  */
  45 public class EntityTest {
  46 
  47     XMLInputFactory factory = null;
  48     String output = "";
  49 
  50     @BeforeMethod
  51     protected void setUp() {
  52         try {
  53             factory = XMLInputFactory.newInstance();
  54         } catch (Exception ex) {
  55             Assert.fail("Could not create XMLInputFactory");
  56         }
  57     }
  58 
  59     @AfterMethod
  60     protected void tearDown() {
  61         factory = null;
  62     }
  63 
  64     @Test
  65     public void testProperties() {
  66         Assert.assertTrue(factory.isPropertySupported("javax.xml.stream.isReplacingEntityReferences"));
  67     }
  68 
  69     @Test
  70     public void testCharacterReferences() {
  71         try {
  72             URL fileName = EntityTest.class.getResource("testCharRef.xml");
  73             URL outputFileName = EntityTest.class.getResource("testCharRef.xml.output");
  74             XMLStreamReader xmlr = factory.createXMLStreamReader(new InputStreamReader(fileName.openStream()));
  75             int eventType = 0;
  76             while (xmlr.hasNext()) {
  77                 eventType = xmlr.next();
  78                 handleEvent(xmlr, eventType);
  79             }
  80             System.out.println("Output:");
  81             System.out.println(output);
  82             Assert.assertTrue(compareOutput(new InputStreamReader(outputFileName.openStream()), new StringReader(output)));
  83         } catch (Exception ex) {
  84             ex.printStackTrace();
  85             Assert.fail(ex.getMessage());
  86         }
  87     }
  88 
  89     private void handleEvent(XMLStreamReader xmlr, int eventType) {
  90         switch (eventType) {
  91             case XMLEvent.START_ELEMENT:
  92                 handleStartElement(xmlr);
  93                 break;
  94             case XMLEvent.END_ELEMENT:
  95                 handleEndElement(xmlr);
  96                 break;
  97             case XMLEvent.CHARACTERS:
  98                 handleCharacters(xmlr);
  99                 break;
 100             case XMLEvent.COMMENT:
 101                 handleComment(xmlr);
 102                 break;
 103             case XMLEvent.ENTITY_REFERENCE:
 104                 break;
 105             case XMLEvent.ATTRIBUTE:
 106                 break;
 107             case XMLEvent.DTD:
 108                 break;
 109             case XMLEvent.CDATA:
 110                 break;
 111             default:
 112                 break;
 113         }
 114     }
 115 
 116     private void handleStartElement(XMLStreamReader xmlr) {
 117         output += "<";
 118         output += xmlr.getLocalName();
 119         if (xmlr.hasText())
 120             output += xmlr.getText();
 121         printAttributes(xmlr);
 122         output += ">";
 123     }
 124 
 125     private void handleEndElement(XMLStreamReader xmlr) {
 126         output += "</";
 127         output += xmlr.getLocalName();
 128         output += ">";
 129     }
 130 
 131     private void handleComment(XMLStreamReader xmlr) {
 132         if (xmlr.hasText())
 133             output += xmlr.getText();
 134     }
 135 
 136     private void handleCharacters(XMLStreamReader xmlr) {
 137         if (xmlr.hasText())
 138             output += xmlr.getText();
 139     }
 140 
 141     private void printAttributes(XMLStreamReader xmlr) {
 142         if (xmlr.getAttributeCount() > 0) {
 143             int count = xmlr.getAttributeCount();
 144             for (int i = 0; i < count; i++) {
 145                 output += xmlr.getAttributeName(i);
 146                 output += "=";
 147                 output += xmlr.getAttributeValue(i);
 148                 /*
 149                  * String name = xmlr.getAttributeName(i) ; String value =
 150                  * xmlr.getAttributeValue(i) ;
 151                  * System.out.println(name+"="+value);
 152                  */
 153             }
 154         }
 155     }
 156 
 157     protected boolean compareOutput(Reader expected, Reader actual) throws IOException {
 158         LineNumberReader expectedOutput = new LineNumberReader(expected);
 159         LineNumberReader actualOutput = new LineNumberReader(actual);
 160 
 161         while (expectedOutput.ready() && actualOutput.ready()) {
 162             String expectedLine = expectedOutput.readLine();
 163             String actualLine = actualOutput.readLine();
 164             if (!expectedLine.equals(actualLine)) {
 165                 System.out.println("Entityreference expansion failed, line no: " + expectedOutput.getLineNumber());
 166                 System.out.println("Expected: " + expectedLine);
 167                 System.out.println("Actual  : " + actualLine);
 168                 return false;
 169             }
 170         }
 171         expectedOutput.close();
 172         actualOutput.close();
 173         return true;
 174     }
 175 }