|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, 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 | +package common.catalog; |
| 24 | + |
| 25 | +import org.w3c.dom.ls.LSInput; |
| 26 | +import org.w3c.dom.ls.LSResourceResolver; |
| 27 | +import org.xml.sax.SAXException; |
| 28 | + |
| 29 | +import javax.xml.catalog.CatalogFeatures; |
| 30 | +import javax.xml.transform.Source; |
| 31 | +import javax.xml.transform.stream.StreamSource; |
| 32 | +import javax.xml.validation.SchemaFactory; |
| 33 | +import java.io.IOException; |
| 34 | +import java.io.InputStream; |
| 35 | +import java.io.InputStreamReader; |
| 36 | +import java.io.Reader; |
| 37 | +import java.io.StringReader; |
| 38 | +import java.util.Collections; |
| 39 | +import java.util.HashMap; |
| 40 | +import java.util.Map; |
| 41 | + |
| 42 | +import static java.util.Objects.requireNonNull; |
| 43 | +import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI; |
| 44 | +import static javax.xml.catalog.CatalogManager.catalogResolver; |
| 45 | +import javax.xml.catalog.CatalogResolver; |
| 46 | +import javax.xml.validation.Validator; |
| 47 | +import org.testng.annotations.Test; |
| 48 | + |
| 49 | +/* |
| 50 | + * @test |
| 51 | + * @bug 8323571 |
| 52 | + * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest |
| 53 | + * @run testng common.catalog.NullIdTest |
| 54 | + * @summary Verifies null values are handled properly in the source resolution |
| 55 | + * process. |
| 56 | + */ |
| 57 | +public class NullIdTest { |
| 58 | + private static final Map<String, String> SCHEMAS; |
| 59 | + // Source Level JDK 8 |
| 60 | + static { |
| 61 | + Map<String, String> map = new HashMap<>(); |
| 62 | + map.put("https://schemas.opentest4j.org/reporting/events/0.1.0", "events.xsd"); |
| 63 | + map.put("https://schemas.opentest4j.org/reporting/core/0.1.0", "core.xsd"); |
| 64 | + SCHEMAS = Collections.unmodifiableMap(map); |
| 65 | + } |
| 66 | + |
| 67 | + /* |
| 68 | + * Verifies that the source resolution process recognizes the custom InputSource |
| 69 | + * correctly even though the public and system IDs are null. |
| 70 | + */ |
| 71 | + @Test |
| 72 | + public void test() throws Exception { |
| 73 | + String xml = "<events xmlns=\"https://schemas.opentest4j.org/reporting/events/0.1.0\"/>"; |
| 74 | + validate(new StreamSource(new StringReader(xml))); |
| 75 | + System.out.println("Successfully validated"); |
| 76 | + } |
| 77 | + |
| 78 | + private static void validate(Source source) throws SAXException, IOException { |
| 79 | + SchemaFactory schemaFactory = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI); |
| 80 | + Validator validator = schemaFactory.newSchema().newValidator(); |
| 81 | + validator.setResourceResolver(createResourceResolver()); |
| 82 | + validator.validate(source); |
| 83 | + } |
| 84 | + |
| 85 | + private static LSResourceResolver createResourceResolver() { |
| 86 | + return (type, namespaceURI, publicId, systemId, baseURI) -> { |
| 87 | + if (namespaceURI != null) { |
| 88 | + if (SCHEMAS.containsKey(namespaceURI)) { |
| 89 | + CustomLSInputImpl input = new CustomLSInputImpl(); |
| 90 | + input.setPublicId(publicId); |
| 91 | + String schema = SCHEMAS.get(namespaceURI); |
| 92 | + input.setSystemId(requireNonNull(NullIdTest.class.getResource(schema)).toExternalForm()); |
| 93 | + input.setBaseURI(baseURI); |
| 94 | + InputStream stream = NullIdTest.class.getResourceAsStream(schema); |
| 95 | + input.setCharacterStream(new InputStreamReader(requireNonNull(stream))); |
| 96 | + return input; |
| 97 | + } |
| 98 | + } |
| 99 | + if (systemId != null) { |
| 100 | + CatalogFeatures features = CatalogFeatures.builder() |
| 101 | + .with(CatalogFeatures.Feature.RESOLVE, "continue") |
| 102 | + .build(); |
| 103 | + CatalogResolver catalogResolver = catalogResolver(features); |
| 104 | + return catalogResolver.resolveResource(type, namespaceURI, publicId, systemId, baseURI); |
| 105 | + } |
| 106 | + return null; |
| 107 | + }; |
| 108 | + } |
| 109 | + |
| 110 | + static class CustomLSInputImpl implements LSInput { |
| 111 | + |
| 112 | + private Reader characterStream; |
| 113 | + private InputStream byteStream; |
| 114 | + private String stringData; |
| 115 | + private String systemId; |
| 116 | + private String publicId; |
| 117 | + private String baseURI; |
| 118 | + private String encoding; |
| 119 | + private boolean certifiedText; |
| 120 | + |
| 121 | + @Override |
| 122 | + public Reader getCharacterStream() { |
| 123 | + return characterStream; |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public void setCharacterStream(Reader characterStream) { |
| 128 | + this.characterStream = characterStream; |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public InputStream getByteStream() { |
| 133 | + return byteStream; |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public void setByteStream(InputStream byteStream) { |
| 138 | + this.byteStream = byteStream; |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public String getStringData() { |
| 143 | + return stringData; |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public void setStringData(String stringData) { |
| 148 | + this.stringData = stringData; |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public String getSystemId() { |
| 153 | + return systemId; |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public void setSystemId(String systemId) { |
| 158 | + this.systemId = systemId; |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public String getPublicId() { |
| 163 | + return publicId; |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public void setPublicId(String publicId) { |
| 168 | + this.publicId = publicId; |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public String getBaseURI() { |
| 173 | + return baseURI; |
| 174 | + } |
| 175 | + |
| 176 | + @Override |
| 177 | + public void setBaseURI(String baseURI) { |
| 178 | + this.baseURI = baseURI; |
| 179 | + } |
| 180 | + |
| 181 | + @Override |
| 182 | + public String getEncoding() { |
| 183 | + return encoding; |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + public void setEncoding(String encoding) { |
| 188 | + this.encoding = encoding; |
| 189 | + } |
| 190 | + |
| 191 | + @Override |
| 192 | + public boolean getCertifiedText() { |
| 193 | + return certifiedText; |
| 194 | + } |
| 195 | + |
| 196 | + @Override |
| 197 | + public void setCertifiedText(boolean certifiedText) { |
| 198 | + this.certifiedText = certifiedText; |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments