View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.internal.config;
21  
22  import org.apache.commons.digester.Digester;
23  import org.apache.commons.io.IOUtils;
24  import org.apache.myfaces.tobago.context.ThemeImpl;
25  import org.apache.myfaces.tobago.context.ThemeResources;
26  import org.apache.myfaces.tobago.context.ThemeScript;
27  import org.apache.myfaces.tobago.context.ThemeStyle;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  import org.xml.sax.SAXException;
31  
32  import javax.faces.FacesException;
33  import java.io.IOException;
34  import java.io.InputStream;
35  import java.net.URL;
36  
37  /**
38   * @since 1.0.7
39   * @deprecated since 1.5.0. The grammar is now part of the TobagoConfigParser
40   */
41  public class ThemeParser {
42  
43    private static final Logger LOG = LoggerFactory.getLogger(ThemeParser.class);
44  
45    private Digester digester;
46  
47    public ThemeParser() {
48      configure();
49    }
50  
51    private void configure() {
52      digester = new Digester();
53      digester.setUseContextClassLoader(true);
54      // todo   digester.setValidating(true);
55      digester.setValidating(false);
56      digester.addCallMethod("tobago-theme/name", "setName", 0);
57      digester.addCallMethod("tobago-theme/deprecated-name", "setDeprecatedName", 0);
58      digester.addCallMethod("tobago-theme/display-name", "setDisplayName", 0);
59      digester.addCallMethod("tobago-theme/resource-path", "setResourcePath", 0);
60      digester.addCallMethod("tobago-theme/fallback", "setFallbackName", 0);
61      digester.addObjectCreate("tobago-theme/renderers", RenderersConfigImpl.class);
62      digester.addSetNext("tobago-theme/renderers", "setRenderersConfig");
63      digester.addObjectCreate("tobago-theme/renderers/renderer", RendererConfig.class);
64      digester.addSetNext("tobago-theme/renderers/renderer", "addRenderer");
65      digester.addCallMethod("tobago-theme/renderers/renderer/name", "setName", 0);
66      digester.addCallMethod("tobago-theme/renderers/renderer/supported-markup/markup", "addSupportedMarkup", 0);
67      digester.addObjectCreate("tobago-theme/resources", ThemeResources.class);
68      digester.addSetProperties("tobago-theme/resources");
69      digester.addSetNext("tobago-theme/resources", "addResources");
70      digester.addObjectCreate("tobago-theme/resources/script", ThemeScript.class);
71      digester.addSetProperties("tobago-theme/resources/script");
72      digester.addSetNext("tobago-theme/resources/script", "addScript");
73      digester.addObjectCreate("tobago-theme/resources/style", ThemeStyle.class);
74      digester.addSetProperties("tobago-theme/resources/style");
75      digester.addSetNext("tobago-theme/resources/style", "addStyle");
76  
77  
78    }
79  
80    public ThemeImpl parse(final URL url)
81        throws IOException, SAXException, FacesException {
82  
83      InputStream inputStream = null;
84      try {
85        inputStream = url.openStream();
86        ThemeImpl theme = new ThemeImpl();
87        digester.push(theme);
88        digester.parse(inputStream);
89        if (LOG.isInfoEnabled()) {
90          LOG.info("Found theme: '" + theme.getName() + "'");
91        }
92        if (theme.getDisplayName() == null) {
93          LOG.warn("No display name set for theme: '" + theme.getName() + "'");
94          theme.setDisplayName(theme.getName());
95        }
96        return theme;
97      } finally {
98        IOUtils.closeQuietly(inputStream);
99      }
100   }
101 
102 }