1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 */ 4 /* 5 * Licensed to the Apache Software Foundation (ASF) under one or more 6 * contributor license agreements. See the NOTICE file distributed with 7 * this work for additional information regarding copyright ownership. 8 * The ASF licenses this file to You under the Apache License, Version 2.0 9 * (the "License"); you may not use this file except in compliance with 10 * the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 package org.apache.qetest.trax; 21 22 import com.sun.org.apache.xml.internal.utils.SystemIDResolver; 23 import java.util.Arrays; 24 import javax.xml.transform.Source; 25 import javax.xml.transform.TransformerException; 26 import javax.xml.transform.URIResolver; 27 import javax.xml.transform.stream.StreamSource; 28 import static org.testng.Assert.fail; 29 30 /** 31 * Implementation of URIResolver that logs all calls. Currently just provides 32 * default service; returns null. 33 * 34 */ 35 public class CheckingURIResolver implements URIResolver { 36 37 /** 38 * Expected value(s) for URIs we may resolve. 39 */ 40 private final String[] expected; 41 42 /** 43 * Default constructor for simple verification test. 44 */ 45 public CheckingURIResolver() { 46 this.expected = new String[0]; 47 } 48 49 /** 50 * Initiator by given URIs that we expected. 51 * 52 * @param expected expected URI string that will be verified when resolve. 53 */ 54 public CheckingURIResolver(final String[] expected) { 55 this.expected = expected; 56 } 57 58 /** 59 * This will be called by the processor when it encounters an xsl:include, 60 * xsl:import, or document() function. 61 * 62 * @param href An href attribute, which may be relative or absolute. 63 * @param base The base URI in effect when the href attribute was 64 * encountered. 65 * @return A non-null Source object. 66 * @throws TransformerException if an error occurs when trying to resolve 67 * the URI. 68 */ 69 @Override 70 public Source resolve(String href, String base) throws TransformerException { 71 // Note that we don't currently have a default URIResolver, 72 // so the LoggingURIResolver class will just attempt 73 // to use the SystemIDResolver class instead 74 String sysId = SystemIDResolver.getAbsoluteURI(href, base); 75 Source resolvedSource = new StreamSource(sysId); 76 77 // verifiy sysId and base. 78 if(!Arrays.stream(expected).anyMatch(expectedStr -> 79 expectedStr.equals("{" + base + "}" + href))) 80 fail("Unexpected resolve called href:" + href + ";base:" + base); 81 return resolvedSource; 82 } 83 }