use of org.jaffa.util.DefaultErrorHandler 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;
}
use of org.jaffa.util.DefaultErrorHandler 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;
}
use of org.jaffa.util.DefaultErrorHandler 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;
}
}
}
use of org.jaffa.util.DefaultErrorHandler 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);
}
use of org.jaffa.util.DefaultErrorHandler 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);
}
Aggregations