Search in sources :

Example 1 with XmlParser

use of org.apache.struts.tiles.xmlDefinition.XmlParser in project jaffa-framework by jaffa-projects.

the class JaffaI18nFactorySet method parseXmlFile.

/**
 * Parse specified xml file and find definitions from with in classpath by searching all jars
 * and add definition to specified definitions set.
 * This method is used to load several description files in one instances list.
 * If filename exists and definition set is <code>null</code>, create a new set. Otherwise, return
 * passed definition set (can be <code>null</code>).
 * @param servletContext Current servlet context. Used to open file.
 * @param xmlDefinitions Definitions set to which definitions will be added. If null, a definitions
 * set is created on request.
 * @return XmlDefinitionsSet The definitions set created or passed as parameter.
 * @throws DefinitionsFactoryException On errors parsing file.
 */
protected XmlDefinitionsSet parseXmlFile(ServletContext servletContext, XmlDefinitionsSet xmlDefinitions) throws DefinitionsFactoryException {
    if (log.isDebugEnabled()) {
        log.debug("Parsing tiles definition files from classpath");
    }
    try {
        List<Resource> resourceList = new ArrayList<>();
        for (String filename : filenames) {
            PathMatchingResourcePatternResolver resolver = OrderedPathMatchingResourcePatternResolver.getInstance();
            resourceList.addAll(Arrays.asList(resolver.getResources(filename)));
        }
        // if( xmlParser == null )
        if (true) {
            xmlParser = new XmlParser();
            xmlParser.setValidating(isValidatingParser);
        }
        // Check if definition set already exist.
        if (xmlDefinitions == null) {
            xmlDefinitions = new XmlDefinitionsSet();
        }
        if (resourceList != null && resourceList.size() > 0) {
            for (Resource resource : resourceList) {
                if (log.isDebugEnabled()) {
                    log.debug("Parsing tiles definition: " + resource.getFilename());
                }
                xmlParser.parse(resource.getInputStream(), xmlDefinitions);
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Can't open files '" + filenames + "'");
            }
            return xmlDefinitions;
        }
    } catch (SAXException | IOException ex) {
        if (log.isDebugEnabled()) {
            log.debug("Error while parsing files '" + filenames + "'.");
            ex.printStackTrace();
        }
        throw new DefinitionsFactoryException("Error while parsing files '" + filenames + "'. " + ex.getMessage(), ex);
    }
    return xmlDefinitions;
}
Also used : XmlParser(org.apache.struts.tiles.xmlDefinition.XmlParser) DefinitionsFactoryException(org.apache.struts.tiles.DefinitionsFactoryException) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) OrderedPathMatchingResourcePatternResolver(org.jaffa.util.OrderedPathMatchingResourcePatternResolver) XmlDefinitionsSet(org.apache.struts.tiles.xmlDefinition.XmlDefinitionsSet) SAXException(org.xml.sax.SAXException)

Aggregations

IOException (java.io.IOException)1 DefinitionsFactoryException (org.apache.struts.tiles.DefinitionsFactoryException)1 XmlDefinitionsSet (org.apache.struts.tiles.xmlDefinition.XmlDefinitionsSet)1 XmlParser (org.apache.struts.tiles.xmlDefinition.XmlParser)1 OrderedPathMatchingResourcePatternResolver (org.jaffa.util.OrderedPathMatchingResourcePatternResolver)1 Resource (org.springframework.core.io.Resource)1 PathMatchingResourcePatternResolver (org.springframework.core.io.support.PathMatchingResourcePatternResolver)1 SAXException (org.xml.sax.SAXException)1