xml-xmlbeans-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dutta Satadip <s-du...@sbcglobal.net>
Subject Better messages from the validator
Date Tue, 13 Jan 2004 07:08:42 GMT
Hello 
I made some changes to validator to accomplish the objective of better messages. 

The changes included in this version of the validator include more verbose messages during
validation for

- Expected Elements ( message contains the name of the expected element to fix the problem)

- Unexpected Elements 

Some of the future changes that might be useful (probably based on some community feedback)
would be verbose messages for missing/wrong attributes, elements that need to match certain
patterns and the like.

The changes in the validator would require the changes in the SchemaTypeVisitorImpl that I
have emailed earlier

In any case I am attaching 2 files

1. Validator

2. SchemaTypeVistorImpl 

The diff for validator.java is included at the end of this email.

Feedback is greatly appreciated.

With Regards

Dutta:)

 

Diff of the changes to validator.java

130a131

> private boolean _testValidity = false;

290a292

> 

315c317,325

< commit();

---

> 

> if ( ! _testValidity )

> {

> commit();

> }

> else

> {

> rollback();

> }

353a364

> 

357c368

< if (!_top._curPart.canStartWithElement(eltName))

---

> if (!_top._curPart.canStartWithElement(eltName))

360c371

< return notValid();

---

> return notValid();

379c390

< return notValid();

---

> return notValid();

447a459,466

> public boolean testValid(QName eltName)

> {

> _testValidity = true;

> boolean retVal = visit(eltName);

> _testValidity = false;

> return retVal;

> }

> 

480c499

< 

---

> 

489c508

< return _matchedParticle;

---

> return _matchedParticle;

*****CVS exited normally with code 1*****

cvs update Validator.java (in directory D:\OSD\xml-xmlbeans\v2\src\typeimpl\org\apache\xmlbeans\impl\validator\)

M Validator.java

*****CVS exited normally with code 0*****

cvs diff Validator.java (in directory D:\OSD\xml-xmlbeans\v2\src\typeimpl\org\apache\xmlbeans\impl\validator\)

Index: Validator.java

===================================================================

RCS file: /home/cvspublic/xml-xmlbeans/v2/src/typeimpl/org/apache/xmlbeans/impl/validator/Validator.java,v

retrieving revision 1.1

diff -r1.1 Validator.java

5c5

< * Copyright (c) 2003 The Apache Software Foundation. All rights 

---

> * Copyright (c) 2003 The Apache Software Foundation. All rights

13c13

< * notice, this list of conditions and the following disclaimer. 

---

> * notice, this list of conditions and the following disclaimer.

21c21

< * if any, must include the following acknowledgment: 

---

> * if any, must include the following acknowledgment:

27c27

< * 4. The names "Apache" and "Apache Software Foundation" must 

---

> * 4. The names "Apache" and "Apache Software Foundation" must

29c29

< * software without prior written permission. For written 

---

> * software without prior written permission. For written

32,33c32,33

< * 5. Products derived from this software may not be called "Apache 

< * XMLBeans", nor may "Apache" appear in their name, without prior 

---

> * 5. Products derived from this software may not be called "Apache

> * XMLBeans", nor may "Apache" appear in their name, without prior

52c52

< * originally based on software copyright (c) 2000-2003 BEA Systems 

---

> * originally based on software copyright (c) 2000-2003 BEA Systems

99a100,102

> import org.apache.xmlbeans.*;

> 

> import java.util.*;

233a237

> String message = null;

262c266,275

< emitFieldError( event, "Element not allowed:", name );

---

> message = findDetailedErrorBegin(state , name.toString());

> if (message != null)

> {

> emitFieldError(event, message);

> message = null;

> }

> else

> {

> emitFieldError(event, "Element not allowed:", name);

> }

263a277

> 

275c289,291

< emitFieldError( event, "Element not allowed:", name );

---

> // Additional processing may be needed to generate more

> // descriptive messages

> emitFieldError( event, "Element not allowed:", name );

276a293

> 

317c334

< emitFieldError(event, 

---

> emitFieldError(event,

525c542

< 

---

> 

666c683

< emitFieldError(

---

> emitFieldError(

667a685

> 

681,683c699,701

< event, 

< "Default QName values are unsupported for attribute: " + 

< QNameHelper.pretty(sla.getName()), 

---

> event,

> "Default QName values are unsupported for attribute: " +

> QNameHelper.pretty(sla.getName()),

687c705

< else 

---

> else

703c721,723

< State state = topState();

---

> String message = null;

> State state = topState();

> 

708c728,740

< emitFieldError( event, "Expected element(s)" );

---

> {

> 

> message = findDetailedErrorEnd(state);

> 

> if (message != null)

> {

> emitFieldError(event, message);

> }

> else

> {

> emitFieldError(event, "Expected element(s)");

> }

> }

758c790

< if (!emptyContent && !state._canHaveMixedContent && 

---

> if (!emptyContent && !state._canHaveMixedContent &&

761c793

< if (field instanceof SchemaLocalElement) 

---

> if (field instanceof SchemaLocalElement)

773a806,895

> private String findDetailedErrorBegin(State state, String name)

> {

> String elementBefore = null;

> String message = null;

> SchemaType elmtType = state._type;

> List allSeenTypes = new ArrayList();

> 

> allSeenTypes.addAll(Arrays.asList(elmtType.getElementProperties()));

> for (int ii = 0; ii < allSeenTypes.size(); ii++)

> {

> //Get the element from the schema

> SchemaProperty sProp = (SchemaProperty) allSeenTypes.get(ii);

> // if it is a attribute ignore

> if (sProp.isAttribute())

> continue;

> 

> // test if the element is valid

> if (!state.test(sProp.getName()))

> {

> 

> // For scenarios in which element is invalid in the begining of the complex type

> if ( elementBefore != null)

> {

> message = "Element " + name + " is not valid in " +

> _stateStack._field.getName() + " : expected " +

> elementBefore;

> break;

> 

> }

> else if ( ii == allSeenTypes.size()-1 )

> {

> // case in which an unexpected element is present at the end

> message = "Unexpected Element " + name + " after " + sProp.getName();

> break;

> }

> 

> }

> else

> {

> elementBefore =sProp.getName().toString();

> 

> if ( ii == allSeenTypes.size()-1 )

> {

> // case in which an unexpected element is present at the end

> message = "Element " + name + " is not valid in " +

> _stateStack._field.getName() + " : expected " +

> sProp.getName();

> break;

> }

> 

> }

> }

> return message;

> }

> 

> private String findDetailedErrorEnd(State state)

> {

> String elementAfter = null;

> String message = null;

> 

> SchemaType elmtType = state._type;

> 

> 

> List allSeenTypes = new ArrayList();

> allSeenTypes.addAll(Arrays.asList(elmtType.getElementProperties()));

> for (int ii = 0; ii < allSeenTypes.size(); ii++) {

> SchemaProperty sProp = (SchemaProperty) allSeenTypes.get(ii);

> 

> if (sProp.isAttribute())

> continue;

> 

> if (!state.test(sProp.getName()))

> {

> elementAfter = sProp.getName().toString();

> }

> else

> {

> if ( ii == allSeenTypes.size()-1 && elementAfter != null)

> {

> 

> message = _stateStack._field.getName()+ " is invalid after "+

> elementAfter +": expected " + sProp.getName().toString() ;

> }

> }

> }

> 

> return message;

> }

> 

> 

780a903,907

> boolean test( QName name )

> {

> return _canHaveElements && _visitor.testValid( name );

> }

> 

1153c1280


Mime
View raw message