|
| 1 | +/* |
| 2 | + * Copyright 2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.integration.xml; |
| 18 | + |
| 19 | +import javax.xml.XMLConstants; |
| 20 | +import javax.xml.transform.TransformerFactory; |
| 21 | +import javax.xml.transform.TransformerFactoryConfigurationError; |
| 22 | + |
| 23 | +import org.apache.commons.logging.Log; |
| 24 | +import org.apache.commons.logging.LogFactory; |
| 25 | + |
| 26 | +/** |
| 27 | + * The {@link TransformerFactory} configuration wrapper with some common settings. |
| 28 | + * Copy of {@link org.springframework.xml.transform.TransformerFactoryUtils}, but with |
| 29 | + * catching exception for unknown properties. |
| 30 | + * |
| 31 | + * @author Greg Turnquist |
| 32 | + * @author Artem Bilan |
| 33 | + * |
| 34 | + * @since 4.3.21 |
| 35 | + */ |
| 36 | + |
| 37 | +public final class TransformerFactoryUtils { |
| 38 | + |
| 39 | + private static final Log LOG = LogFactory.getLog(TransformerFactoryUtils.class); |
| 40 | + |
| 41 | + /** |
| 42 | + * Build a new {@link TransformerFactory} using the default constructor. |
| 43 | + */ |
| 44 | + public static TransformerFactory newInstance() { |
| 45 | + return defaultSettings(TransformerFactory.newInstance()); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Build an {@link TransformerFactory} and prevent external entities from accessing. |
| 50 | + * @see TransformerFactory#newInstance() |
| 51 | + */ |
| 52 | + public static TransformerFactory newInstance(Class<? extends TransformerFactory> transformerFactoryClass) { |
| 53 | + try { |
| 54 | + return defaultSettings(transformerFactoryClass.newInstance()); |
| 55 | + } |
| 56 | + catch (InstantiationException e) { |
| 57 | + throw new TransformerFactoryConfigurationError(e, |
| 58 | + "Could not instantiate TransformerFactory [" + transformerFactoryClass + "]"); |
| 59 | + } |
| 60 | + catch (IllegalAccessException e) { |
| 61 | + throw new TransformerFactoryConfigurationError(e, |
| 62 | + "Could not instantiate TransformerFactory [" + transformerFactoryClass + "]"); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Prevent external entities from accessing. |
| 68 | + */ |
| 69 | + private static TransformerFactory defaultSettings(TransformerFactory factory) { |
| 70 | + try { |
| 71 | + factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, ""); |
| 72 | + } |
| 73 | + catch (IllegalArgumentException e) { |
| 74 | + if (LOG.isWarnEnabled()) { |
| 75 | + LOG.warn(XMLConstants.ACCESS_EXTERNAL_DTD + " property not supported by " + |
| 76 | + factory.getClass().getCanonicalName()); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + try { |
| 81 | + factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, ""); |
| 82 | + } |
| 83 | + catch (IllegalArgumentException e) { |
| 84 | + if (LOG.isWarnEnabled()) { |
| 85 | + LOG.warn(XMLConstants.ACCESS_EXTERNAL_STYLESHEET + " property not supported by " + |
| 86 | + factory.getClass().getCanonicalName()); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return factory; |
| 91 | + } |
| 92 | + |
| 93 | + private TransformerFactoryUtils() { |
| 94 | + } |
| 95 | + |
| 96 | +} |
0 commit comments