org.apache.shale.tiger.config.FacesConfigParser Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.shale.tiger.config;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import org.apache.commons.digester.Digester;
import org.apache.commons.digester.RuleSet;
import org.apache.shale.tiger.managed.rules.ManagedBeansRuleSet;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
/**
* Parser utility for processing faces-config.xml
resources.
* Information parsed by each call to the parse()
method is
* merged with any previous information stored in the specified
* {@link FacesConfigConfig} bean.
*
* To use this utility, instantiate a new instance and set the
* facesConfig
, resource
, and validating
* properties. Then, call the parse()
method. You can parse
* more than one resource by resetting the resource
property
* and calling parse()
again.
*/
public class FacesConfigParser {
// -------------------------------------------------------- Static Constants
/**
* Registration information for the DTD used to validate the specified
* configuration resources.
*/
private static final String[] REGISTRATIONS =
{ "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN",
"/org/apache/shale/tiger/resources/web-facesconfig_1_0.dtd",
"-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN",
"/org/apache/shale/tiger/resources/web-facesconfig_1_1.dtd",
};
// -------------------------------------------------------------- Properties
/**
* Configuration resource summarizing all of the configuration metadata
* from resources that have been parsed so far.
*/
private FacesConfigConfig facesConfig = null;
/**
* Return the {@link FacesConfigConfig} bean summarizing all of the
* configuration metadata from resources that have been parsed so far.
*/
public FacesConfigConfig getFacesConfig() {
return this.facesConfig;
}
/**
* Set the {@link FacesConfigConfig} bean that will summarize all of the
* configuration metadata from parsed resources.
*
* @param facesConfig New {@link FacesConfigConfig} instance to be populated
*/
public void setFacesConfig(FacesConfigConfig facesConfig) {
this.facesConfig = facesConfig;
}
/**
* The URL of the configuration resource to be parsed.
*/
private URL resource = null;
/**
* Return the URL of the configuration resource to be parsed.
*/
public URL getResource() {
return this.resource;
}
/**
* Set the URL of the configuration resource to be parsed.
*
* @param resource The new configuration resource URL
*/
public void setResource(URL resource) {
this.resource = resource;
}
/**
* Flag indicating whether we should do a validating parse.
*/
private boolean validating = true;
/**
* Return the validating parse flag.
*/
public boolean isValidating() {
return this.validating;
}
/**
* Set the validating parse flag.
*
* @param validating The new validating parse flag
*/
public void setValidating(boolean validating) {
this.validating = validating;
}
// ---------------------------------------------------------- Public Methods
/**
* Parse the configuration resource specified by the resource
* property, storing resulting information in the configuration bean
* specified by the facesConfig
property.
*
* @exception IOException if an input/output error occurs
* @exception SAXException if an XML parsing error occurs
*/
public void parse() throws IOException, SAXException {
Digester digester = digester();
digester.clear();
digester.push(getFacesConfig());
InputSource source = new InputSource(getResource().toExternalForm());
InputStream stream = null;
try {
stream = getResource().openStream();
source.setByteStream(stream);
digester.parse(source);
} catch (IOException e) {
throw e;
} catch (SAXException e) {
throw e;
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
; // Fall through
}
}
}
}
// --------------------------------------------------------- Private Methods
/**
* Configured Digester
instance to be used (lazily
* instantiated.
*/
private Digester digester = null;
/**
* Return a fully configured Digester
instance.
*/
private Digester digester() {
// Return any previously created instance
if (digester != null) {
return digester;
}
// Create and configure a new Digester instance
digester = new Digester();
digester.setNamespaceAware(false);
digester.setUseContextClassLoader(true);
digester.setValidating(isValidating());
// Register our local copy of DTDs we recognize
for (int i = 0; i < REGISTRATIONS.length; i += 2) {
URL url = this.getClass().getResource(REGISTRATIONS[i + 1]);
digester.register(REGISTRATIONS[i], url.toString());
}
// Configure processing rules
RuleSet ruleSet = null;
ruleSet = new ManagedBeansRuleSet();
ruleSet.addRuleInstances(digester);
// Return the configured instance
return digester;
}
}