Search in sources :

Example 1 with DefaultEntityResolver

use of org.jaffa.util.DefaultEntityResolver in project jaffa-framework by jaffa-projects.

the class MenuNavigationForm method doValidate.

/**
 * This method should be invoked to copy the fields from the FormBean to the component after successful validation.
 * @return true of the values are copied successfully
 */
public boolean doValidate() throws FrameworkException, ApplicationExceptions {
    String value = null;
    ApplicationExceptions appExps = new ApplicationExceptions();
    value = getFileContentsWM().getValue();
    if (value == null || (value != null && value.trim().length() == 0)) {
        appExps.add(new MenuNavigationException(MenuNavigationException.PROP_XML_FILE_PARSE_ERROR, StringHelper.convertToHTML(MessageHelper.findMessage("label.Jaffa.Admin.MenuNavigation.NoContent", null))));
        throw appExps;
    }
    try {
        // Create a factory object for creating DOM parsers
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        // Specifies that the parser produced by this factory will validate documents as they are parsed.
        factory.setValidating(true);
        // Now use the factory to create a DOM parser
        DocumentBuilder parser = factory.newDocumentBuilder();
        // Specifies the EntityResolver onceto resolve DTD used in XML documents
        parser.setEntityResolver(new DefaultEntityResolver());
        // Specifies the ErrorHandler to handle warning/error/fatalError conditions
        parser.setErrorHandler(new DefaultErrorHandler());
        Document document = parser.parse(new InputSource(new StringReader(value)));
    } catch (ParserConfigurationException e) {
        appExps.add(new MenuNavigationException(MenuNavigationException.PROP_XML_FILE_PARSE_ERROR, StringHelper.convertToHTML(e.getMessage())));
        throw appExps;
    } catch (SAXException e) {
        appExps.add(new MenuNavigationException(MenuNavigationException.PROP_XML_FILE_PARSE_ERROR, StringHelper.convertToHTML(e.getMessage())));
        throw appExps;
    } catch (IOException e) {
        appExps.add(new MenuNavigationException(MenuNavigationException.PROP_XML_FILE_PARSE_ERROR, StringHelper.convertToHTML(e.getMessage())));
        throw appExps;
    }
    setFileContents(value);
    return true;
}
Also used : InputSource(org.xml.sax.InputSource) ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) MenuNavigationException(org.jaffa.applications.jaffa.modules.admin.components.menunavigation.ui.exceptions.MenuNavigationException) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) DefaultEntityResolver(org.jaffa.util.DefaultEntityResolver) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DefaultErrorHandler(org.jaffa.util.DefaultErrorHandler)

Example 2 with DefaultEntityResolver

use of org.jaffa.util.DefaultEntityResolver in project jaffa-framework by jaffa-projects.

the class ValidationRulesEditorForm method doValidate1.

/**
 * This method should be invoked to ensure a valid state of the FormBean. It will validate the data in the models and set the corresponding properties.
 * Errors will be raised in the FormBean, if any validation fails.
 * @param request The request stream
 * @return A true indicates validations went through successfully.
 */
public boolean doValidate1(HttpServletRequest request) throws FrameworkException, ApplicationExceptions {
    String value = null;
    ApplicationExceptions appExps = new ApplicationExceptions();
    value = getFileContentsWM().getValue();
    if (value != null && value.trim().length() == 0)
        value = null;
    try {
        // Create a factory object for creating DOM parsers
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        // Specifies that the parser produced by this factory will validate documents as they are parsed.
        factory.setValidating(true);
        // Now use the factory to create a DOM parser
        DocumentBuilder parser = factory.newDocumentBuilder();
        // Specifies the EntityResolver onceo resolve DTD used in XML documents
        parser.setEntityResolver(new DefaultEntityResolver());
        // Specifies the ErrorHandler to handle warning/error/fatalError conditions
        parser.setErrorHandler(new DefaultErrorHandler());
        Document document = parser.parse(new InputSource(new StringReader(value)));
    } catch (ParserConfigurationException e) {
        // Cannot pass e.toString() and pass as parameter as the the meesage contains quotes
        // which dows not work properly with displaying the messages
        appExps.add(new ConfigException(ConfigException.PROP_XML_FILE_PARSE_ERROR, StringHelper.convertToHTML(e.getMessage())));
        throw appExps;
    } catch (SAXException e) {
        appExps.add(new ConfigException(ConfigException.PROP_XML_FILE_PARSE_ERROR, StringHelper.convertToHTML(e.getMessage())));
        throw appExps;
    } catch (IOException e) {
        appExps.add(new ConfigException(ConfigException.PROP_XML_FILE_PARSE_ERROR, StringHelper.convertToHTML(e.getMessage())));
        throw appExps;
    }
    setFileContents(value);
    return true;
}
Also used : InputSource(org.xml.sax.InputSource) ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) ConfigException(org.jaffa.applications.jaffa.modules.admin.exceptions.ConfigException) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) DefaultEntityResolver(org.jaffa.util.DefaultEntityResolver) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DefaultErrorHandler(org.jaffa.util.DefaultErrorHandler)

Example 3 with DefaultEntityResolver

use of org.jaffa.util.DefaultEntityResolver in project jaffa-framework by jaffa-projects.

the class RulesMetaDataService method find.

/**
 * Parse the rules-xml file for the given named className.
 */
private static void find(String className, ICache cache, String variation) throws SAXException, IOException {
    synchronized (cache) {
        if (cache.containsKey(className))
            return;
        CustomHandler handler = new CustomHandler(className);
        try {
            String xmlFile;
            if (VariationContext.DEFAULT_VARIATION.equals(variation))
                xmlFile = (String) Config.getProperty(Config.PROP_RULES_ENGINE_CORE_RULES_URL, DEFAULT_XML_FILE);
            else {
                String variationRulesDirectory = (String) Config.getProperty(Config.PROP_RULES_ENGINE_VARIATIONS_DIR, null);
                if (variationRulesDirectory == null || variationRulesDirectory.length() == 0) {
                    // No directory specified. Hence no variations exist. Just return.
                    return;
                } else {
                    xmlFile = variationRulesDirectory + '/' + variation + VARIATION_FILE_SUFFIX;
                }
            }
            URL url = URLHelper.newExtendedURL(xmlFile);
            if (url == null)
                throw new FileNotFoundException("File not found - " + xmlFile);
            XMLReader reader = XMLReaderFactory.createXMLReader(PARSER_NAME);
            reader.setContentHandler(handler);
            reader.setEntityResolver(new DefaultEntityResolver());
            reader.setErrorHandler(new DefaultErrorHandler());
            reader.parse(new InputSource(url.openStream()));
            cache.put(className, handler.getClassMetaData());
        } catch (SAXException e) {
            // Its alrite if the SUCCESS_MESSAGE is returned
            if (SUCCESS_MESSAGE.equals(e.getMessage()))
                cache.put(className, handler.getClassMetaData());
            else
                throw e;
        } catch (IOException e) {
            throw e;
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) DefaultEntityResolver(org.jaffa.util.DefaultEntityResolver) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) DefaultErrorHandler(org.jaffa.util.DefaultErrorHandler) URL(java.net.URL) XMLReader(org.xml.sax.XMLReader) SAXException(org.xml.sax.SAXException)

Example 4 with DefaultEntityResolver

use of org.jaffa.util.DefaultEntityResolver in project jaffa-framework by jaffa-projects.

the class ValidatorMetaDataService method find.

/**
 * Parse the validator-xml file for the given named validator.
 */
private static synchronized void find(String name) throws SAXException, IOException {
    if (c_cache.containsKey(name))
        return;
    CustomHandler handler = null;
    URL url = null;
    String validatorFiles = (String) Config.getProperty(Config.PROP_RULES_ENGINE_VALIDATORS_URL_LIST, XML_FILE);
    for (StringTokenizer strtknzr = new StringTokenizer(validatorFiles, ","); strtknzr.hasMoreTokens(); ) {
        String validatorFile = strtknzr.nextToken();
        try {
            url = URLHelper.newExtendedURL(validatorFile);
        } catch (MalformedURLException e) {
            url = null;
        }
        if (url == null)
            throw new FileNotFoundException("File not found - " + validatorFile);
        try {
            handler = new CustomHandler(name);
            XMLReader reader = XMLReaderFactory.createXMLReader(PARSER_NAME);
            reader.setContentHandler(handler);
            reader.setEntityResolver(new DefaultEntityResolver());
            reader.setErrorHandler(new DefaultErrorHandler());
            reader.parse(new InputSource(url.openStream()));
        } catch (SAXException e) {
            // Its alrite if the SUCCESS_MESSAGE is returned
            if (SUCCESS_MESSAGE.equals(e.getMessage()) && handler != null)
                break;
            else
                throw e;
        } catch (IOException e) {
            throw e;
        }
        if (handler.getFieldValidatorMetaData() != null)
            break;
    }
    c_cache.put(name, handler != null ? handler.getFieldValidatorMetaData() : null);
}
Also used : StringTokenizer(java.util.StringTokenizer) MalformedURLException(java.net.MalformedURLException) InputSource(org.xml.sax.InputSource) DefaultEntityResolver(org.jaffa.util.DefaultEntityResolver) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) DefaultErrorHandler(org.jaffa.util.DefaultErrorHandler) URL(java.net.URL) XMLReader(org.xml.sax.XMLReader) SAXException(org.xml.sax.SAXException)

Example 5 with DefaultEntityResolver

use of org.jaffa.util.DefaultEntityResolver in project jaffa-framework by jaffa-projects.

the class ConfigurationService method parseNew.

private ClassMetaData parseNew(String classname) {
    if (log.isDebugEnabled()) {
        log.debug("Locating the mapping file for the Persistent class " + classname);
    }
    String shortname = classname.substring(classname.lastIndexOf('.') + 1);
    for (Iterator itr = m_locations.iterator(); itr.hasNext(); ) {
        DirLoc dirLoc = (DirLoc) itr.next();
        String packageName = dirLoc.getDir();
        StringBuffer urlNameBuf = new StringBuffer(packageName);
        if (!packageName.endsWith("/") && !packageName.endsWith("\\")) {
            urlNameBuf.append('/');
        }
        urlNameBuf.append(shortname).append(MAPPING_FILE_PREFIX);
        String urlName = urlNameBuf.toString();
        if (log.isDebugEnabled()) {
            log.debug("Looking for the mapping file " + urlName);
        }
        URL url = null;
        try {
            url = URLHelper.newExtendedURL(urlName);
        } catch (MalformedURLException e) {
            if (log.isDebugEnabled()) {
                log.debug("Could not find the mapping file " + urlName + ". " + e.getMessage());
            }
        }
        if (url != null) {
            DefaultHandler handler = new MappingParser();
            InputStream stream = null;
            try {
                XMLReader reader = XMLReaderFactory.createXMLReader(PARSER_NAME);
                reader.setContentHandler(handler);
                reader.setEntityResolver(new DefaultEntityResolver());
                reader.setErrorHandler(new DefaultErrorHandler());
                stream = url.openStream();
                reader.parse(new InputSource(stream));
            } catch (Exception e) {
                if (log.isDebugEnabled()) {
                    log.debug("Error in parsing the mapping file " + urlName + ". Will try and look at another location", e);
                }
                // try another location !!!
                continue;
            } finally {
                try {
                    if (stream != null) {
                        stream.close();
                    }
                } catch (IOException e) {
                // do nothing
                }
            }
            ClassMetaData classMetaData = ((MappingParser) handler).getMetaData();
            if (classMetaData != null && classMetaData.getClassName().equals(classname)) {
                if (log.isDebugEnabled()) {
                    log.debug("Validating the ClassMetaData object for the mapping file " + urlName);
                }
                classMetaData.setXmlFileUrl(url);
                classMetaData.validate();
                synchronized (m_metaCache) {
                    // one final check before inserting
                    if (m_metaCache.containsKey(classname)) {
                        classMetaData = (ClassMetaData) m_metaCache.get(classname);
                    } else {
                        m_metaCache.put(classname, classMetaData);
                    }
                }
                return classMetaData;
            } else {
                if (log.isDebugEnabled()) {
                    log.debug("The classname in the mapping file " + urlName + ", does not match the required value " + classname + ". Will try and look at another location");
                }
            }
        }
    }
    String str = "Could not find/parse the mapping file for the class " + classname;
    log.error(str);
    throw new ConfigurationServiceRuntimeException(str);
}
Also used : MalformedURLException(java.net.MalformedURLException) InputSource(org.xml.sax.InputSource) InputStream(java.io.InputStream) IOException(java.io.IOException) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) ConfigurationServiceRuntimeException(org.jaffa.persistence.engines.jdbcengine.configservice.exceptions.ConfigurationServiceRuntimeException) IOException(java.io.IOException) InitFileNotFoundRuntimeException(org.jaffa.persistence.engines.jdbcengine.configservice.exceptions.InitFileNotFoundRuntimeException) DefaultHandler(org.xml.sax.helpers.DefaultHandler) ConfigurationServiceRuntimeException(org.jaffa.persistence.engines.jdbcengine.configservice.exceptions.ConfigurationServiceRuntimeException) DefaultEntityResolver(org.jaffa.util.DefaultEntityResolver) DirLoc(org.jaffa.persistence.engines.jdbcengine.configservice.initdomain.DirLoc) DefaultErrorHandler(org.jaffa.util.DefaultErrorHandler) XMLReader(org.xml.sax.XMLReader)

Aggregations

DefaultEntityResolver (org.jaffa.util.DefaultEntityResolver)12 DefaultErrorHandler (org.jaffa.util.DefaultErrorHandler)12 DocumentBuilder (javax.xml.parsers.DocumentBuilder)8 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)8 IOException (java.io.IOException)6 InputSource (org.xml.sax.InputSource)6 SAXException (org.xml.sax.SAXException)6 Document (org.w3c.dom.Document)5 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4 StringReader (java.io.StringReader)3 MalformedURLException (java.net.MalformedURLException)3 URL (java.net.URL)3 ApplicationExceptions (org.jaffa.exceptions.ApplicationExceptions)3 XMLReader (org.xml.sax.XMLReader)3 FileNotFoundException (java.io.FileNotFoundException)2 Node (org.w3c.dom.Node)2 NodeList (org.w3c.dom.NodeList)2 InputStream (java.io.InputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 StringTokenizer (java.util.StringTokenizer)1