1 /*
   2  * Copyright (c) 2016, 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 stream.XMLEventReaderTest;
  25 
  26 import java.io.StringReader;
  27 
  28 import javax.xml.stream.XMLEventReader;
  29 import javax.xml.stream.XMLInputFactory;
  30 import javax.xml.stream.XMLStreamException;
  31 import javax.xml.stream.events.XMLEvent;
  32 
  33 import org.testng.Assert;
  34 import org.testng.annotations.Listeners;
  35 import org.testng.annotations.Test;
  36 
  37 import com.sun.org.apache.xerces.internal.impl.XMLEntityManager;
  38 
  39 /*
  40  * @bug 8153781
  41  * @summary Test if method skipDTD of class XMLDTDScannerImpl will correctly skip the DTD section,
  42  *          even if a call to XMLEntityScanner.scanData for skipping to the closing ']' returns true.
  43  */
  44 @Listeners({jaxp.library.BasePolicy.class})
  45 public class Bug8153781 {
  46     public static int DOCTYPE_SECTION_LENGTH = XMLEntityManager.DEFAULT_BUFFER_SIZE * 2;
  47     public static int DOCUMENT_LENGTH = DOCTYPE_SECTION_LENGTH + 4096;
  48 
  49     public String createXMLDocument(int doctypeoffset) {
  50         StringBuilder xmlcontentbuilder = new StringBuilder(DOCUMENT_LENGTH);
  51         xmlcontentbuilder.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\r\n");
  52         xmlcontentbuilder.append("<!DOCTYPE dummy [\r\n");
  53         xmlcontentbuilder.append("  <!ELEMENT dummy EMPTY>\r\n");
  54         xmlcontentbuilder.append("  <!--\r\n");
  55         int doctypelines = DOCTYPE_SECTION_LENGTH / 3;
  56         for (int i = 0; i < doctypeoffset; i++)
  57             xmlcontentbuilder.append('a');
  58         for (int i = 0; i < doctypelines; i++)
  59             xmlcontentbuilder.append("a\r\n");
  60         xmlcontentbuilder.append("  -->\r\n");
  61         xmlcontentbuilder.append("  ]\r\n");
  62         xmlcontentbuilder.append(">\r\n");
  63         xmlcontentbuilder.append("<dummy>\r\n");
  64         xmlcontentbuilder.append("</dummy>\r\n");
  65         System.out.println("Document length:" + xmlcontentbuilder.length());
  66         return xmlcontentbuilder.toString();
  67     }
  68 
  69     public void runReader(XMLInputFactory factory, int offset) throws XMLStreamException {
  70         StringReader stringReader = new StringReader(createXMLDocument(offset));
  71         XMLEventReader reader = factory.createXMLEventReader(stringReader);
  72 
  73         while (reader.hasNext()) {
  74             XMLEvent event = reader.nextEvent();
  75             System.out.println("Event Type: " + event.getEventType());
  76         }
  77     }
  78 
  79     @Test
  80     public void test() {
  81         try {
  82             XMLInputFactory factory = XMLInputFactory.newInstance();
  83             factory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
  84             for (int i = 0; i < 3; i++) {
  85                 runReader(factory, i);
  86             }
  87         } catch (XMLStreamException xe) {
  88             xe.printStackTrace();
  89             Assert.fail(xe.getMessage());
  90         }
  91     }
  92 }