Details
-
Improvement
-
Status: Closed
-
Minor
-
Resolution: Fixed
-
Nightly Builds
-
None
-
None
-
Operating System: Windows XP
Platform: PC
-
27671
Description
I have implemented my own function to validate various text fields for correct
html validation.
I have this function in TestValidator.
// import org.w3c.tidy.Tidy; import this package
/**
- Checks if the field is required.
* - @param value The value validation is being performed on.
- @return boolean/Object If the field isn't <code>null</code> and
- has a length greater than zero,
- <code>true</code> is returned.
- Otherwise <code>false</code>.
*/
public static Object validateHtml(Object bean, Field field) {
String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
//creates a new Tidy
Tidy tidy = new Tidy();
URL configurationFile =
Thread.currentThread().getContextClassLoader().getResource("tidy_default.cfg");
try
{ // if configuration file exists load and set it Properties testProperties = new Properties(); testProperties.load(configurationFile.openStream()); tidy.setConfigurationFromProps(testProperties); }catch ( IOException ioe)
{ // not found should not happen } // out
ByteArrayInputStream in = new ByteArrayInputStream(value.getBytes());
OutputStream out = new ByteArrayOutputStream();
// go!
StringWriter errorLog = new StringWriter();
tidy.setErrout(new PrintWriter(errorLog));
// parse the text
tidy.parse( in, out);
// Put all the errors into a map and also the tidy's
// corrected suggestion to fix the errors
Map aResultMap = new HashMap();
aResultMap.put( "errors", (Object) new String( ""+tidy.getParseErrors()));
aResultMap.put( "warnings", (Object) new String(
""+tidy.getParseWarnings()));
aResultMap.put( "error_report", (Object) (
errorLog.getBuffer().toString()) );
aResultMap.put( "tidy_report", (Object) ( out.toString()) );
try
{ in.close(); out.close(); } catch (IOException ie)
{}
return (Object) aResultMap;
}
By sending a Hashmap(or any object) I get more details about the errors which I
can send it to the users for corrective action. The problem I am having is not
able to retrieve the object added to the ValidatorResult.ResultStatus object.
If you can add this function(similar to isValid()) in ValidatorResult that would
be great as we can add more customized errors and parse and display to the user.
public Object getResultObject(String validatorName)
{ Object o = hAction.get(validatorName); ResultStatus aStatus = null; if (o != null) aStatus = (ResultStatus) o; return aStatus.getResult(); }
As In my case I have to have the tidy's suggestions to display to the user for a
given text.
Thanks
Manish