1/*
2 * Copyright (C) 2007 The Android Open Source Project
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 *     http://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
17package tests.api.org.xml.sax.support;
18
19import java.io.IOException;
20import java.util.HashMap;
21import java.util.HashSet;
22import java.util.Map;
23import java.util.Set;
24
25import org.xml.sax.ContentHandler;
26import org.xml.sax.DTDHandler;
27import org.xml.sax.EntityResolver;
28import org.xml.sax.ErrorHandler;
29import org.xml.sax.InputSource;
30import org.xml.sax.SAXException;
31import org.xml.sax.SAXNotRecognizedException;
32import org.xml.sax.SAXNotSupportedException;
33import org.xml.sax.XMLReader;
34
35/**
36 * A helper class that implements the SAX XMLReader interface and logs method
37 * calls.
38 */
39public class MockReader implements XMLReader {
40
41    private MethodLogger logger;
42
43    private ContentHandler contentHandler;
44
45    private DTDHandler dtdHandler;
46
47    private EntityResolver resolver;
48
49    private ErrorHandler errorHandler;
50
51    private Set<String> features = new HashSet<String>();
52
53    private Map<String, Object> properties = new HashMap<String, Object>();
54
55    public MockReader(MethodLogger logger) {
56        super();
57        this.logger = logger;
58    }
59
60
61    public ContentHandler getContentHandler() {
62        return contentHandler;
63    }
64
65    public DTDHandler getDTDHandler() {
66        return dtdHandler;
67    }
68
69    public EntityResolver getEntityResolver() {
70        return resolver;
71    }
72
73    public ErrorHandler getErrorHandler() {
74        return errorHandler;
75    }
76
77    public boolean getFeature(String name) throws SAXNotRecognizedException,
78            SAXNotSupportedException {
79        return features.contains(name);
80    }
81
82    public Object getProperty(String name) throws SAXNotRecognizedException,
83            SAXNotSupportedException {
84        return properties.get(name);
85    }
86
87    public void parse(InputSource input) throws IOException, SAXException {
88        logger.add("parse", input);
89    }
90
91    public void parse(String systemId) throws IOException, SAXException {
92        logger.add("parse", systemId);
93    }
94
95    public void setContentHandler(ContentHandler handler) {
96        this.contentHandler = handler;
97    }
98
99    public void setDTDHandler(DTDHandler handler) {
100        this.dtdHandler = handler;
101    }
102
103    public void setEntityResolver(EntityResolver resolver) {
104        this.resolver = resolver;
105    }
106
107    public void setErrorHandler(ErrorHandler handler) {
108        this.errorHandler = handler;
109    }
110
111    public void setFeature(String name, boolean value) {
112        if (value) {
113            features.add(name);
114        } else {
115            features.remove(name);
116        }
117    }
118
119    public void setProperty(String name, Object value) throws SAXNotRecognizedException,
120            SAXNotSupportedException {
121        if (value == null) {
122            properties.remove(name);
123        } else {
124            properties.put(name, value);
125        }
126    }
127
128}
129