Hi Luca,
" the problem with the large sections is that we cannot point to the
exact paragraph in witch the error occurs"
The issue is some sections can be as large as 5-6 pages ... just
indicating an error generically in a section is not really helping the
user.
I took a look at the source for what you had done, for e.g. you have
used regular expression matching to determine the section name from
the error message ... :
<code>
....
//check what type of text the exception launch
if(exceptionMessage.matches("(.*)Attribute '(.*)' must
appear on element '(.*)'."))
{
//compile the regex
Pattern p = Pattern.compile("(.*)Attribute
'(.*)' must appear on element '(.*)'");
//set the input
Matcher m = p.matcher(exceptionMessage);
//the attribute name
String attribute = "";
//the elemet name
....
</code>
This seems very fragile and liable to break if we switch jdk versions
or try to use it for different locales / regions ...
Have you looked at ValidationEventLocator and ValidationEventCollector
class that allows resolving Validatin / SaxException to xml source ?
It returns the dom node having the error and the column and line
number with the error ...
Ashok
This seems very fragile and liable to break if we switch jdk versions
or try to use it for different locales / regions ...
Have you looked at ValidationEventLocator and ValidationEventCollector
class that allows resolving Validatin / SaxException to xml source ?
It returns the dom node having the error and the column and line
number with the error ...
Actually I wasnt suggesting using Saxon for this part...
You are validating the output document using an XML parser (Xerces)
and then trapping the exception. I am suggesting exactly the same,
except that when you trap the exception you use a source locator to
identify the XML node responsible for the problem. Once we identify
the problematic node it is easy to walk the parent tree and identify
the originating paragraph and section. We know the mapping between
text:section/@name and AN container elements and also between text:p
and the AN paragraph representation... so AN and ODF containers are
relatively straightforward to map to each other.
> And, as you know, the method that retrieves the line and the column works on
> the XML version not on ODF original document. But we have to tell to the
> user where is the problem in the ODF and not in the XLM produced version :-(
> And last but not least, the method getCurrentElementNode in the validator
> (both SAX and JAXP) points to the first parent of the element in witch the
> error occurs.
No no.. i am not suggesting using getCurrentElementNode() since that
doesnt provide the information we need...
Here is some sample code to explain what i am suggesting ....
public static void main(String[] args} {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(true);
///set the schema somewhere
SAXParser parser = factory.newSAXParser();
parser.parse("sample_an.xml", new AnXmlLocator());
}
.....
public class AnXmlLocator extends DefaultHandler {
Locator locator;
public void setDocumentLocator(Locator locator) {
this.locator = locator;
}
//for testing we allow only 'clause'
//ideally we should be trapping saxexception and doing the source
location stuff there
public void startElement(String uri, String localName,
String qName, Attributes at) throws SAXException {
if (qName.equals("clause")) {
// ... do nothing
} else {
//for every other element raise an exception and identify
the source of the error
//do mapping staff between AN xml error and ODF error
String location = "";
if (locator != null) {
location = locator.getSystemId(); //name of xml doc
location += " line " + locator.getLineNumber();
location += ", column " + locator.getColumnNumber();
location += ": ";
}
throw new SAXException(location + "Illegal element");
}
}
Ashok
Are you getting a validation failure or an entirely different exception ... ?
Also did you set the sax driver explicitly to use Apache xerces ... ?
I have had a lot of errors in the past if this is not set explicitly
as the jaxp saxparserfactory tends to pick up the jdk's own sax parser
(which is also xerces, but a different version)....
I ll also checkout the code you have committed and test it at my end....
Ashok
Luca,
Can you send me the document that you are testing with (the one you
are using to test the error handler ) ...
If I have it i can try and make a small demo of how it could work ....
Ashok