View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.xml.secure;
19  
20  import java.io.IOException;
21  import java.util.Objects;
22  
23  import org.xml.sax.ContentHandler;
24  import org.xml.sax.DTDHandler;
25  import org.xml.sax.EntityResolver;
26  import org.xml.sax.ErrorHandler;
27  import org.xml.sax.InputSource;
28  import org.xml.sax.SAXException;
29  import org.xml.sax.SAXNotRecognizedException;
30  import org.xml.sax.SAXNotSupportedException;
31  import org.xml.sax.XMLReader;
32  
33  /**
34   * {@link XMLReader} wrapper that keeps a {@link FallbackIgnoreEntityResolver2} floor as the reader's entity resolver, non-overridable by the caller.
35   *
36   * <p>The floor is installed once and stays the reader's entity resolver for the wrapper's lifetime; {@link #setEntityResolver(EntityResolver)} routes the
37   * caller's resolver through {@link FallbackIgnoreEntityResolver2#setDelegate} instead of replacing it. This includes the {@code DefaultHandler} that
38   * {@link javax.xml.parsers.SAXParser#parse(org.xml.sax.InputSource, org.xml.sax.helpers.DefaultHandler) SAXParser.parse(source, handler)} installs as the
39   * reader's entity resolver, which would otherwise silently replace the floor. {@link #getEntityResolver()} reports the caller's resolver unwrapped.</p>
40   *
41   * <p>Every other method forwards to the wrapped delegate; subclasses (e.g. {@code SecureExpatXMLReader}) add per-implementation fixups on top of the floor.</p>
42   */
43  class SecureXMLReader implements XMLReader {
44  
45      private final XMLReader delegate;
46  
47      private final FallbackIgnoreEntityResolver2 floor;
48  
49      /**
50       * Constructs a new instance.
51       *
52       * @param delegate the delegate to wrap; must not be {@code null}.
53       * @throws NullPointerException if {@code delegate} is {@code null}.
54       */
55      SecureXMLReader(final XMLReader delegate) {
56          this.delegate = Objects.requireNonNull(delegate, "delegate");
57          this.floor = new FallbackIgnoreEntityResolver2(null);
58          delegate.setEntityResolver(floor);
59      }
60  
61      @Override
62      public ContentHandler getContentHandler() {
63          return delegate.getContentHandler();
64      }
65  
66      /**
67       * Gets the wrapped reader, so tests can observe which parser implementation a rewrite picked.
68       *
69       * @return The wrapped reader.
70       */
71      XMLReader getDelegate() {
72          return delegate;
73      }
74  
75      @Override
76      public DTDHandler getDTDHandler() {
77          return delegate.getDTDHandler();
78      }
79  
80      @Override
81      public EntityResolver getEntityResolver() {
82          return floor.getDelegate();
83      }
84  
85      @Override
86      public ErrorHandler getErrorHandler() {
87          return delegate.getErrorHandler();
88      }
89  
90      @Override
91      public boolean getFeature(final String name) throws SAXNotRecognizedException, SAXNotSupportedException {
92          return delegate.getFeature(name);
93      }
94  
95      @Override
96      public Object getProperty(final String name) throws SAXNotRecognizedException, SAXNotSupportedException {
97          return delegate.getProperty(name);
98      }
99  
100     @Override
101     public void parse(final InputSource input) throws IOException, SAXException {
102         delegate.parse(input);
103     }
104 
105     @Override
106     public void parse(final String systemId) throws IOException, SAXException {
107         delegate.parse(systemId);
108     }
109 
110     @Override
111     public void setContentHandler(final ContentHandler handler) {
112         delegate.setContentHandler(handler);
113     }
114 
115     @Override
116     public void setDTDHandler(final DTDHandler handler) {
117         delegate.setDTDHandler(handler);
118     }
119 
120     @Override
121     public void setEntityResolver(final EntityResolver resolver) {
122         floor.setDelegate(resolver);
123     }
124 
125     @Override
126     public void setErrorHandler(final ErrorHandler handler) {
127         delegate.setErrorHandler(handler);
128     }
129 
130     @Override
131     public void setFeature(final String name, final boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
132         delegate.setFeature(name, value);
133     }
134 
135     @Override
136     public void setProperty(final String name, final Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
137         delegate.setProperty(name, value);
138     }
139 
140 }