diff --git a/logback-core/src/main/java/ch/qos/logback/core/joran/event/stax/StaxEventRecorder.java b/logback-core/src/main/java/ch/qos/logback/core/joran/event/stax/StaxEventRecorder.java deleted file mode 100755 index 0a0e8a7e31..0000000000 --- a/logback-core/src/main/java/ch/qos/logback/core/joran/event/stax/StaxEventRecorder.java +++ /dev/null @@ -1,117 +0,0 @@ -/** - * Logback: the reliable, generic, fast and flexible logging framework. - * Copyright (C) 1999-2015, QOS.ch. All rights reserved. - * - * This program and the accompanying materials are dual-licensed under - * either the terms of the Eclipse Public License v1.0 as published by - * the Eclipse Foundation - * - * or (per the licensee's choosing) - * - * under the terms of the GNU Lesser General Public License version 2.1 - * as published by the Free Software Foundation. - */ -package ch.qos.logback.core.joran.event.stax; - -import java.io.InputStream; -import java.util.ArrayList; -import java.util.List; - -import javax.xml.stream.XMLEventReader; -import javax.xml.stream.XMLInputFactory; -import javax.xml.stream.XMLStreamException; -import javax.xml.stream.events.Characters; -import javax.xml.stream.events.EndElement; -import javax.xml.stream.events.StartElement; -import javax.xml.stream.events.XMLEvent; - -import ch.qos.logback.core.Context; -import ch.qos.logback.core.joran.spi.ElementPath; -import ch.qos.logback.core.joran.spi.JoranException; -import ch.qos.logback.core.spi.ContextAwareBase; - -public class StaxEventRecorder extends ContextAwareBase { - - List eventList = new ArrayList(); - ElementPath globalElementPath = new ElementPath(); - - public StaxEventRecorder(Context context) { - setContext(context); - } - - public void recordEvents(InputStream inputStream) throws JoranException { - try { - XMLEventReader xmlEventReader = XMLInputFactory.newInstance().createXMLEventReader(inputStream); - read(xmlEventReader); - } catch (XMLStreamException e) { - throw new JoranException("Problem parsing XML document. See previously reported errors.", e); - } - } - - public List getEventList() { - return eventList; - } - - private void read(XMLEventReader xmlEventReader) throws XMLStreamException { - while (xmlEventReader.hasNext()) { - XMLEvent xmlEvent = xmlEventReader.nextEvent(); - switch (xmlEvent.getEventType()) { - case XMLEvent.START_ELEMENT: - addStartElement(xmlEvent); - break; - case XMLEvent.CHARACTERS: - addCharacters(xmlEvent); - break; - case XMLEvent.END_ELEMENT: - addEndEvent(xmlEvent); - break; - default: - break; - } - } - } - - private void addStartElement(XMLEvent xmlEvent) { - StartElement se = xmlEvent.asStartElement(); - String tagName = se.getName().getLocalPart(); - this.globalElementPath.push(tagName); - ElementPath current = globalElementPath.duplicate(); - StartEvent startEvent = new StartEvent(current, tagName, se.getAttributes(), se.getLocation()); - eventList.add(startEvent); - } - - private void addCharacters(XMLEvent xmlEvent) { - Characters characters = xmlEvent.asCharacters(); - StaxEvent lastEvent = getLastEvent(); - - if (lastEvent instanceof BodyEvent) { - BodyEvent be = (BodyEvent) lastEvent; - be.append(characters.getData()); - } else { - // ignore space only text if the previous event is not a BodyEvent - if (!characters.isWhiteSpace()) { - BodyEvent bodyEvent = new BodyEvent(characters.getData(), xmlEvent.getLocation()); - eventList.add(bodyEvent); - } - } - } - - private void addEndEvent(XMLEvent xmlEvent) { - EndElement ee = xmlEvent.asEndElement(); - String tagName = ee.getName().getLocalPart(); - EndEvent endEvent = new EndEvent(tagName, ee.getLocation()); - eventList.add(endEvent); - this.globalElementPath.pop(); - } - - StaxEvent getLastEvent() { - if (eventList.isEmpty()) { - return null; - } - int size = eventList.size(); - if (size == 0) - return null; - return eventList.get(size - 1); - } - -} diff --git a/logback-core/src/test/java/ch/qos/logback/core/joran/event/stax/StaxEventRecorderTest.java b/logback-core/src/test/java/ch/qos/logback/core/joran/event/stax/StaxEventRecorderTest.java deleted file mode 100755 index c2aab97694..0000000000 --- a/logback-core/src/test/java/ch/qos/logback/core/joran/event/stax/StaxEventRecorderTest.java +++ /dev/null @@ -1,89 +0,0 @@ -/** - * Logback: the reliable, generic, fast and flexible logging framework. - * Copyright (C) 1999-2015, QOS.ch. All rights reserved. - * - * This program and the accompanying materials are dual-licensed under - * either the terms of the Eclipse Public License v1.0 as published by - * the Eclipse Foundation - * - * or (per the licensee's choosing) - * - * under the terms of the GNU Lesser General Public License version 2.1 - * as published by the Free Software Foundation. - */ -package ch.qos.logback.core.joran.event.stax; - -import java.io.FileInputStream; -import java.util.List; - -import javax.xml.stream.events.Attribute; - -import ch.qos.logback.core.Context; -import ch.qos.logback.core.ContextBase; -import ch.qos.logback.core.status.Status; -import ch.qos.logback.core.testUtil.CoreTestConstants; -import ch.qos.logback.core.status.testUtil.StatusChecker; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -public class StaxEventRecorderTest { - - Context context = new ContextBase(); - StatusChecker statusChecker = new StatusChecker(context); - - public List doTest(String filename) throws Exception { - StaxEventRecorder recorder = new StaxEventRecorder(context); - FileInputStream fis = new FileInputStream(CoreTestConstants.TEST_SRC_PREFIX + "input/joran/" + filename); - recorder.recordEvents(fis); - return recorder.getEventList(); - } - - public void dump(List seList) { - for (StaxEvent se : seList) { - System.out.println(se); - } - } - - @Test - public void testParsingOfXMLWithAttributesAndBodyText() throws Exception { - List seList = doTest("event1.xml"); - Assertions.assertTrue(statusChecker.getHighestLevel(0) == Status.INFO); - // dump(seList); - Assertions.assertEquals(11, seList.size()); - Assertions.assertEquals("test", seList.get(0).getName()); - Assertions.assertEquals("badBegin", seList.get(1).getName()); - StartEvent startEvent = (StartEvent) seList.get(7); - Assertions.assertEquals("John Doe", startEvent.getAttributeByName("name").getValue()); - Assertions.assertEquals("XXX&", ((BodyEvent) seList.get(8)).getText()); - } - - @Test - public void testProcessingOfTextWithEntityCharacters() throws Exception { - List seList = doTest("ampEvent.xml"); - Assertions.assertTrue(statusChecker.getHighestLevel(0) == Status.INFO); - // dump(seList); - Assertions.assertEquals(3, seList.size()); - - BodyEvent be = (BodyEvent) seList.get(1); - Assertions.assertEquals("xxx & yyy", be.getText()); - } - - @Test - public void testAttributeProcessing() throws Exception { - List seList = doTest("inc.xml"); - Assertions.assertTrue(statusChecker.getHighestLevel(0) == Status.INFO); - Assertions.assertEquals(4, seList.size()); - StartEvent se = (StartEvent) seList.get(1); - Attribute attr = se.getAttributeByName("increment"); - Assertions.assertNotNull(attr); - Assertions.assertEquals("1", attr.getValue()); - } - - @Test - public void bodyWithSpacesAndQuotes() throws Exception { - List seList = doTest("spacesAndQuotes.xml"); - Assertions.assertEquals(3, seList.size()); - BodyEvent be = (BodyEvent) seList.get(1); - Assertions.assertEquals("[x][x] \"xyz\"%n", be.getText()); - } -}