/* * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. */ /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.qetest.trax; import com.sun.org.apache.xml.internal.utils.SystemIDResolver; import java.util.Arrays; import javax.xml.transform.Source; import javax.xml.transform.TransformerException; import javax.xml.transform.URIResolver; import javax.xml.transform.stream.StreamSource; import static org.testng.Assert.fail; /** * Implementation of URIResolver that logs all calls. Currently just provides * default service; returns null. * */ public class CheckingURIResolver implements URIResolver { /** * Expected value(s) for URIs we may resolve. */ private final String[] expected; /** * Default constructor for simple verification test. */ public CheckingURIResolver() { this.expected = new String[0]; } /** * Initiator by given URIs that we expected. * * @param expected expected URI string that will be verified when resolve. */ public CheckingURIResolver(final String[] expected) { this.expected = expected; } /** * This will be called by the processor when it encounters an xsl:include, * xsl:import, or document() function. * * @param href An href attribute, which may be relative or absolute. * @param base The base URI in effect when the href attribute was * encountered. * @return A non-null Source object. * @throws TransformerException if an error occurs when trying to resolve * the URI. */ @Override public Source resolve(String href, String base) throws TransformerException { // Note that we don't currently have a default URIResolver, // so the LoggingURIResolver class will just attempt // to use the SystemIDResolver class instead String sysId = SystemIDResolver.getAbsoluteURI(href, base); Source resolvedSource = new StreamSource(sysId); // verifiy sysId and base. if(!Arrays.stream(expected).anyMatch(expectedStr -> expectedStr.equals("{" + base + "}" + href))) fail("Unexpected resolve called href:" + href + ";base:" + base); return resolvedSource; } }