--- old/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/SAX2StAXBaseWriter.java 2020-03-30 22:38:35.714187389 +0000 +++ new/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/SAX2StAXBaseWriter.java 2020-03-30 22:38:35.130172787 +0000 @@ -1,6 +1,5 @@ /* - * Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved. - * @LastModified: Oct 2017 + * Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -36,213 +35,231 @@ import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.xml.sax.ext.LexicalHandler; +import org.xml.sax.ext.Locator2; import org.xml.sax.helpers.DefaultHandler; - public abstract class SAX2StAXBaseWriter extends DefaultHandler - implements - LexicalHandler { + implements + LexicalHandler { + protected boolean isCDATA; - protected boolean isCDATA; + protected StringBuffer CDATABuffer; - protected StringBuffer CDATABuffer; + protected List namespaces; - protected List namespaces; + protected Locator docLocator; - protected Locator docLocator; + protected XMLReporter reporter; - protected XMLReporter reporter; + String xmlVersion = null, encoding = null; - public SAX2StAXBaseWriter() { - } + public SAX2StAXBaseWriter() { + } - public SAX2StAXBaseWriter(XMLReporter reporter) { - this.reporter = reporter; - } + public SAX2StAXBaseWriter(XMLReporter reporter) { + this.reporter = reporter; + } - public void setXMLReporter(XMLReporter reporter) { - this.reporter = reporter; - } + public void setXMLReporter(XMLReporter reporter) { + this.reporter = reporter; + } - public void setDocumentLocator(Locator locator) { - this.docLocator = locator; + public void setDocumentLocator(Locator locator) { + this.docLocator = locator; + } + + private void updateVersionAndEncoding() { + if (docLocator instanceof Locator2) { + Locator2 l2 = (Locator2) docLocator; + xmlVersion = l2.getXMLVersion(); + encoding = l2.getEncoding(); } + } + public void setXmlVersion(String version) { + this.xmlVersion = version; + } - public Location getCurrentLocation() { - if (docLocator != null) { - return new SAXLocation(docLocator); - } else { - return null; - } + public void setEncoding(String encoding) { + this.encoding = encoding; + } - } + void writeStartDocument() throws SAXException { + updateVersionAndEncoding(); + } - public void error(SAXParseException e) throws SAXException { - reportException("ERROR", e); + public Location getCurrentLocation() { + if (docLocator != null) { + return new SAXLocation(docLocator); + } else { + return null; } + } - public void fatalError(SAXParseException e) throws SAXException { - reportException("FATAL", e); - } + public void error(SAXParseException e) throws SAXException { + reportException("ERROR", e); + } - public void warning(SAXParseException e) throws SAXException { - reportException("WARNING", e); - } + public void fatalError(SAXParseException e) throws SAXException { + reportException("FATAL", e); + } - public void startDocument() throws SAXException { - namespaces = new ArrayList<>(2); - } + public void warning(SAXParseException e) throws SAXException { + reportException("WARNING", e); + } - public void endDocument() throws SAXException { - namespaces = null; - } + public void startDocument() throws SAXException { + namespaces = new ArrayList<>(2); + } - public void startElement(String uri, String localName, String qName, - Attributes attributes) throws SAXException { - namespaces = null; - } + public void endDocument() throws SAXException { + namespaces = null; + } - public void endElement(String uri, String localName, String qName) - throws SAXException { - namespaces = null; - } + public void startElement(String uri, String localName, String qName, + Attributes attributes) throws SAXException { + namespaces = null; + } - public void startPrefixMapping(String prefix, String uri) - throws SAXException { + public void endElement(String uri, String localName, String qName) + throws SAXException { + namespaces = null; + } - if (prefix == null) { - prefix = ""; - } else if (prefix.equals("xml")) { - return; - } + public void startPrefixMapping(String prefix, String uri) + throws SAXException { - if (namespaces == null) { - namespaces = new ArrayList<>(2); - } - namespaces.add(prefix); - namespaces.add(uri); + if (prefix == null) { + prefix = ""; + } else if (prefix.equals("xml")) { + return; } - - public void endPrefixMapping(String prefix) throws SAXException { + if (namespaces == null) { + namespaces = new ArrayList<>(2); } + namespaces.add(prefix); + namespaces.add(uri); + } - public void startCDATA() throws SAXException { - isCDATA = true; - if (CDATABuffer == null) { - CDATABuffer = new StringBuffer(); - } else { - CDATABuffer.setLength(0); - } - } + public void endPrefixMapping(String prefix) throws SAXException { + } - public void characters(char[] ch, int start, int length) - throws SAXException { - if (isCDATA) { - CDATABuffer.append(ch, start, length); - } + public void startCDATA() throws SAXException { + isCDATA = true; + if (CDATABuffer == null) { + CDATABuffer = new StringBuffer(); + } else { + CDATABuffer.setLength(0); } + } - public void endCDATA() throws SAXException { - isCDATA = false; - CDATABuffer.setLength(0); + public void characters(char[] ch, int start, int length) + throws SAXException { + if (isCDATA) { + CDATABuffer.append(ch, start, length); } + } - public void comment(char[] ch, int start, int length) throws SAXException { - } + public void endCDATA() throws SAXException { + isCDATA = false; + CDATABuffer.setLength(0); + } - public void endDTD() throws SAXException { - } + public void comment(char[] ch, int start, int length) throws SAXException { + } - public void endEntity(String name) throws SAXException { - } + public void endDTD() throws SAXException { + } - public void startDTD(String name, String publicId, String systemId) - throws SAXException { - } + public void endEntity(String name) throws SAXException { + } - public void startEntity(String name) throws SAXException { - } + public void startDTD(String name, String publicId, String systemId) + throws SAXException { + } - /** - * Used to report a {@link SAXException}to the {@link XMLReporter} - * registered with this handler. - */ - protected void reportException(String type, SAXException e) - throws SAXException { + public void startEntity(String name) throws SAXException { + } - if (reporter != null) { - try { - reporter.report(e.getMessage(), type, e, getCurrentLocation()); - } catch (XMLStreamException e1) { - throw new SAXException(e1); - } - } + /** + * Used to report a {@link SAXException}to the {@link XMLReporter} + * registered with this handler. + */ + protected void reportException(String type, SAXException e) + throws SAXException { + + if (reporter != null) { + try { + reporter.report(e.getMessage(), type, e, getCurrentLocation()); + } catch (XMLStreamException e1) { + throw new SAXException(e1); + } } + } - /** - * Parses an XML qualified name, and places the resulting prefix and local - * name in the provided String array. - * - * @param qName The qualified name to parse. - * @param results An array where parse results will be placed. The prefix - * will be placed at results[0], and the local - * part at results[1] - */ - public static final void parseQName(String qName, String[] results) { + /** + * Parses an XML qualified name, and places the resulting prefix and local + * name in the provided String array. + * + * @param qName The qualified name to parse. + * @param results An array where parse results will be placed. The prefix + * will be placed at results[0], and the local + * part at results[1] + */ + public static final void parseQName(String qName, String[] results) { - String prefix, local; - int idx = qName.indexOf(':'); - if (idx >= 0) { - prefix = qName.substring(0, idx); - local = qName.substring(idx + 1); - } else { - prefix = ""; - local = qName; - } - results[0] = prefix; - results[1] = local; + String prefix, local; + int idx = qName.indexOf(':'); + if (idx >= 0) { + prefix = qName.substring(0, idx); + local = qName.substring(idx + 1); + } else { + prefix = ""; + local = qName; } + results[0] = prefix; + results[1] = local; + } - /** - * {@Link Location}implementation used to expose details from a SAX - * {@link Locator}. - * - * @author christian - */ - private static final class SAXLocation implements Location { + /** + * {@Link Location}implementation used to expose details from a SAX + * {@link Locator}. + * + * @author christian + */ + private static final class SAXLocation implements Location { - private int lineNumber; - private int columnNumber; - private String publicId; - private String systemId; - private SAXLocation(Locator locator) { - lineNumber = locator.getLineNumber(); - columnNumber = locator.getColumnNumber(); - publicId = locator.getPublicId(); - systemId = locator.getSystemId(); - } + private int lineNumber; + private int columnNumber; + private String publicId; + private String systemId; + private SAXLocation(Locator locator) { + lineNumber = locator.getLineNumber(); + columnNumber = locator.getColumnNumber(); + publicId = locator.getPublicId(); + systemId = locator.getSystemId(); + } - public int getLineNumber() { - return lineNumber; - } + public int getLineNumber() { + return lineNumber; + } - public int getColumnNumber() { - return columnNumber; - } + public int getColumnNumber() { + return columnNumber; + } - public int getCharacterOffset() { - return -1; - } + public int getCharacterOffset() { + return -1; + } - public String getPublicId() { - return publicId; - } + public String getPublicId() { + return publicId; + } - public String getSystemId() { - return systemId; - } + public String getSystemId() { + return systemId; } + } }