• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hazendaz / sitemesh2 / 59

22 Mar 2026 02:30AM UTC coverage: 40.347%. Remained the same
59

push

github

hazendaz
[mvn] Update maven wrapper

698 of 1891 branches covered (36.91%)

Branch coverage included in aggregate %.

1555 of 3693 relevant lines covered (42.11%)

0.42 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main/java/com/opensymphony/module/sitemesh/factory/BaseFactory.java
1
/*
2
 * SPDX-License-Identifier: Apache-2.0
3
 * Copyright 2011-2026 Hazendaz
4
 */
5
/*
6
 * Title:        BaseFactory
7
 * Description:
8
 *
9
 * This software is published under the terms of the OpenSymphony Software
10
 * License version 1.1, of which a copy has been included with this
11
 * distribution in the LICENSE.txt file.
12
 */
13

14
package com.opensymphony.module.sitemesh.factory;
15

16
import com.opensymphony.module.sitemesh.Config;
17
import com.opensymphony.module.sitemesh.DecoratorMapper;
18
import com.opensymphony.module.sitemesh.Factory;
19
import com.opensymphony.module.sitemesh.PageParser;
20
import com.opensymphony.module.sitemesh.mapper.PathMapper;
21
import com.opensymphony.module.sitemesh.util.ClassLoaderUtil;
22

23
import java.lang.reflect.InvocationTargetException;
24
import java.util.HashMap;
25
import java.util.Map;
26
import java.util.Properties;
27

28
/**
29
 * Base Factory implementation. Provides utility methods for implementation.
30
 *
31
 * @author <a href="mailto:joe@truemesh.com">Joe Walnes</a>
32
 */
33
public abstract class BaseFactory extends Factory {
34

35
    /** The config is of type ServletConfig or FilterConfig. */
36
    protected Config config;
37

38
    /**
39
     * Instance of {@link com.opensymphony.module.sitemesh.DecoratorMapper}. Because it is thread-safe, it can be shared
40
     * by multiple clients. This is only the last DecoratorMapper in the chain, and all parents will be automatically
41
     * delegated to it.
42
     */
43
    protected DecoratorMapper decoratorMapper;
44

45
    /** Map that associates content-types with PageParser instances. */
46
    protected Map<Object, Object> pageParsers;
47

48
    /** A map of paths that are excluded from decoration. */
49
    protected PathMapper excludeUrls;
50

51
    /**
52
     * Constructor for default implementation of Factory. Should never be called by client. Singleton instance should be
53
     * obtained instead.
54
     *
55
     * @param config
56
     *            the config
57
     *
58
     * @see #getInstance(com.opensymphony.module.sitemesh.Config config)
59
     */
60
    protected BaseFactory(Config config) {
×
61
        this.config = config;
×
62
        clearDecoratorMappers();
×
63
        clearParserMappings();
×
64
        clearExcludeUrls();
×
65
    }
×
66

67
    /** Return instance of DecoratorMapper. */
68
    @Override
69
    public DecoratorMapper getDecoratorMapper() {
70
        return decoratorMapper;
×
71
    }
72

73
    /**
74
     * Create a PageParser suitable for the given content-type.
75
     * <p>
76
     * For example, if the supplied parameter is <code>text/html</code> a parser shall be returned that can parse HTML
77
     * accordingly. Returns null if no parser can be found for the supplied content type.
78
     * </p>
79
     *
80
     * @param contentType
81
     *            The MIME content-type of the data to be parsed
82
     *
83
     * @return Appropriate <code>PageParser</code> for reading data, or <code>null</code> if no suitable parser was
84
     *         found.
85
     */
86
    @Override
87
    public PageParser getPageParser(String contentType) {
88
        return (PageParser) pageParsers.get(contentType);
×
89
    }
90

91
    /**
92
     * Determine whether a Page of given content-type should be parsed or not.
93
     */
94
    @Override
95
    public boolean shouldParsePage(String contentType) {
96
        return pageParsers.containsKey(contentType);
×
97
    }
98

99
    /**
100
     * Returns <code>true</code> if the supplied path matches one of the exclude URLs specified in sitemesh.xml,
101
     * otherwise returns <code>false</code>.
102
     *
103
     * @param path
104
     *            The path.
105
     *
106
     * @return whether the path is excluded
107
     */
108
    @Override
109
    public boolean isPathExcluded(String path) {
110
        return excludeUrls.get(path) != null;
×
111
    }
112

113
    /**
114
     * Clear all current DecoratorMappers.
115
     */
116
    protected void clearDecoratorMappers() {
117
        decoratorMapper = null;
×
118
    }
×
119

120
    /**
121
     * Push new DecoratorMapper onto end of chain.
122
     *
123
     * @param className
124
     *            the class name
125
     * @param properties
126
     *            the properties
127
     */
128
    protected void pushDecoratorMapper(String className, Properties properties) {
129
        try {
130
            Class<?> decoratorMapperClass = ClassLoaderUtil.loadClass(className, getClass());
×
131
            DecoratorMapper newMapper = getDecoratorMapper(decoratorMapperClass);
×
132
            newMapper.init(config, properties, decoratorMapper);
×
133
            decoratorMapper = newMapper;
×
134
        } catch (ClassNotFoundException e) {
×
135
            throw new FactoryException("Could not load DecoratorMapper class : " + className, e);
×
136
        } catch (Exception e) {
×
137
            throw new FactoryException("Could not initialize DecoratorMapper : " + className, e);
×
138
        }
×
139
    }
×
140

141
    /**
142
     * Gets the decorator mapper.
143
     *
144
     * @param decoratorMapperClass
145
     *            the decorator mapper class
146
     *
147
     * @return the decorator mapper
148
     *
149
     * @throws InstantiationException
150
     *             the instantiation exception
151
     * @throws IllegalAccessException
152
     *             the illegal access exception
153
     * @throws InvocationTargetException
154
     *             the invocation target exception
155
     * @throws NoSuchMethodException
156
     *             the no such method exception
157
     */
158
    protected DecoratorMapper getDecoratorMapper(Class<?> decoratorMapperClass)
159
            throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
160
        return (DecoratorMapper) decoratorMapperClass.getDeclaredConstructor().newInstance();
×
161
    }
162

163
    /** Clear all PageParser mappings. */
164
    protected void clearParserMappings() {
165
        pageParsers = new HashMap<>();
×
166
    }
×
167

168
    /**
169
     * Map new PageParser to given content-type. contentType = null signifies default PageParser for unknown
170
     * content-types.
171
     *
172
     * @param contentType
173
     *            the content type
174
     * @param className
175
     *            the class name
176
     */
177
    protected void mapParser(String contentType, String className) {
178
        if (className.endsWith(".DefaultPageParser")) {
×
179
            return; // Backwards compatability - this can safely be ignored.
×
180
        }
181
        try {
182
            PageParser pp = (PageParser) ClassLoaderUtil.loadClass(className, getClass()).getDeclaredConstructor()
×
183
                    .newInstance();
×
184
            // Store the parser even if the content type is NULL. [This
185
            // is most probably the legacy default page parser which
186
            // we no longer have a use for]
187
            pageParsers.put(contentType, pp);
×
188
        } catch (ClassNotFoundException e) {
×
189
            throw new FactoryException("Could not load PageParser class : " + className, e);
×
190
        } catch (Exception e) {
×
191
            throw new FactoryException("Could not instantiate PageParser : " + className, e);
×
192
        }
×
193
    }
×
194

195
    /**
196
     * Adds the exclude url.
197
     *
198
     * @param path
199
     *            the path
200
     */
201
    protected void addExcludeUrl(String path) {
202
        excludeUrls.put("", path);
×
203
    }
×
204

205
    /**
206
     * Clears all exclude URLs.
207
     */
208
    protected void clearExcludeUrls() {
209
        excludeUrls = new PathMapper();
×
210
    }
×
211

212
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc