You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xmlbeans-cvs@xml.apache.org by ce...@apache.org on 2004/04/09 23:53:44 UTC
cvs commit: xml-xmlbeans/v2/test/src/drt/drtcases ValidatorTests.java SmokeTests.java StandaloneTests.java ValidationTests.java
cezar 2004/04/09 14:53:44
Modified: v2/src/typeimpl/org/apache/xmlbeans/impl/validator
Validator.java
v2/src/xmlpublic/org/apache/xmlbeans XmlError.java
v2/test/src/drt/drtcases SmokeTests.java
StandaloneTests.java ValidationTests.java
Added: v2/src/xmlpublic/org/apache/xmlbeans XmlValidationError.java
v2/test/src/drt/drtcases ValidatorTests.java
Log:
Functionality to retrieve xml validation errors programatically - patch from Dutta Satadip.
DRT: passes
Revision Changes Path
1.10 +200 -156 xml-xmlbeans/v2/src/typeimpl/org/apache/xmlbeans/impl/validator/Validator.java
Index: Validator.java
===================================================================
RCS file: /home/cvs/xml-xmlbeans/v2/src/typeimpl/org/apache/xmlbeans/impl/validator/Validator.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- Validator.java 23 Mar 2004 02:42:45 -0000 1.9
+++ Validator.java 9 Apr 2004 21:53:44 -0000 1.10
@@ -53,6 +53,7 @@
import org.apache.xmlbeans.SchemaType;
import org.apache.xmlbeans.SchemaTypeLoader;
import org.apache.xmlbeans.XmlError;
+import org.apache.xmlbeans.XmlValidationError;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;
import org.apache.xmlbeans.SimpleValue;
@@ -93,7 +94,11 @@
{
public void invalid ( String message )
{
- Validator.this.emitError( _event, message );
+ // TODO (dutta) Addtional Attributes for validation error have limited information
+ //at this time but will be a part of the second round of refactoring
+
+ Validator.this.emitError(_event, message, null, null, null,
+ XmlValidationError.ATTRIBUTE_TYPE_INVALID, null);
}
Event _event;
@@ -104,12 +109,17 @@
return !_invalid && _constraintEngine.isValid();
}
- private void emitError ( Event event, String msg )
+ private void emitError ( Event event, String msg, QName offendingQName,
+ SchemaType expectedSchemaType, List expectedQNames,
+ int errorType, SchemaType badSchemaType)
{
- emitError(event, msg, XmlError.SEVERITY_ERROR);
+ emitError(event, msg, XmlError.SEVERITY_ERROR, offendingQName , expectedSchemaType,
+ expectedQNames , errorType, badSchemaType);
}
- private void emitError ( Event event, String msg, int severity )
+ private void emitError ( Event event, String msg, int severity, QName offendingQName,
+ SchemaType expectedSchemaType, List expectedQNames,
+ int errorType, SchemaType badSchemaType )
{
_errorState++;
@@ -121,31 +131,33 @@
if (_errorListener != null)
{
assert event != null;
-
- _errorListener.add(IdentityConstraint.errorForEvent(msg, severity, event));
+ _errorListener.add(
+ XmlValidationError.forCursorWithDetails( msg, severity, event.getLocationAsCursor(),
+ offendingQName, expectedSchemaType, expectedQNames,
+ errorType, badSchemaType));
}
}
}
- private void emitFieldError ( Event event, String msg, QName name )
+ private void emitFieldError ( Event event, String msg, QName offendingQName,
+ SchemaType expectedSchemaType, List expectedQNames,
+ int errorType, SchemaType badSchemaType )
{
- emitFieldError( event, msg + " " + QNameHelper.pretty( name ) );
+ emitFieldError(event, msg, XmlError.SEVERITY_ERROR, offendingQName,
+ expectedSchemaType, expectedQNames , errorType, badSchemaType);
}
- private void emitFieldError ( Event event, String msg )
- {
- emitFieldError(event, msg, XmlError.SEVERITY_ERROR);
- }
-
- private void emitFieldError ( Event event, String msg, int severity )
+ private void emitFieldError ( Event event, String msg, int severity, QName offendingQName,
+ SchemaType expectedSchemaType, List expectedQNames,
+ int errorType, SchemaType badSchemaType )
{
if (_stateStack != null && _stateStack._field != null)
{
- msg +=
- " in element " + QNameHelper.pretty( _stateStack._field.getName() );
+ msg += " in element " + QNameHelper.pretty( _stateStack._field.getName() );
}
- Validator.this.emitError( event, msg, severity );
+ Validator.this.emitError( event, msg, severity, offendingQName , expectedSchemaType,
+ expectedQNames , errorType, badSchemaType);
}
// For XmlEventListener.error
@@ -196,7 +208,6 @@
{
_localElement = null;
_wildcardElement = null;
- String message = null;
State state = topState();
SchemaType elementType = null;
@@ -218,23 +229,18 @@
if (state._isNil)
{
- emitFieldError(event, "Nil element cannot have element content");
+ emitFieldError(event, "Nil element cannot have element content",
+ state._field.getName(), state._type, null,
+ XmlValidationError.NIL_ELEMENT, state._type);
+
_eatContent = 1;
return;
}
if (!state.visit( name ))
{
- message = findDetailedErrorBegin(state , name);
- if (message != null)
- {
- emitFieldError(event, message);
- message = null;
- }
- else
- {
- emitFieldError(event, "Element not allowed:", name);
- }
+ findDetailedErrorBegin(event ,state , name);
+
_eatContent = 1;
return;
@@ -252,7 +258,11 @@
{
// Additional processing may be needed to generate more
// descriptive messages
- emitFieldError( event, "Element not allowed:", name );
+ emitFieldError(event,
+ "Element not allowed: " + QNameHelper.pretty(name) ,
+ name, null, null,
+ XmlValidationError.ELEMENT_NOT_ALLOWED, state._type);
+
_eatContent = 1;
return;
@@ -273,8 +283,10 @@
{
if (wildcardProcess == SchemaParticle.STRICT)
{
- emitFieldError(
- event, "Element not allowed (strict wildcard, and no definition found):", name );
+ emitFieldError( event,
+ "Element not allowed (strict wildcard, and no definition found): " + QNameHelper.pretty(name),
+ name, state._type, null,
+ XmlValidationError.ELEMENT_NOT_ALLOWED, state._type);
}
_eatContent = 1;
@@ -284,8 +296,7 @@
}
else
{
- assert
- currentParticle.getParticleType() == SchemaParticle.ELEMENT;
+ assert currentParticle.getParticleType() == SchemaParticle.ELEMENT;
// If the current element particle name does not match the name
// of the event, then the current element is a substitute for
@@ -296,8 +307,11 @@
{
if (((SchemaLocalElement)currentParticle).blockSubstitution())
{
- emitFieldError(event,
- "Element substitution not allowed when group head has block='substitution'", name);
+ emitFieldError( event,
+ "Element substitution not allowed when group head has block='substitution'" + QNameHelper.pretty( name),
+ name, state._type, null,
+ XmlValidationError.ELEMENT_NOT_ALLOWED, state._type);
+
_eatContent = 1;
return;
}
@@ -329,7 +343,9 @@
if (elementType.isNoType())
{
- emitFieldError(event, "Invalid type.");
+ emitFieldError( event, "Invalid type.", event.getName(), null, null,
+ XmlValidationError.ELEMENT_TYPE_INVALID, null);
+
_eatContent = 1;
}
@@ -355,10 +371,7 @@
{
_vc._event = null;
- xsiType =
- _globalTypes.findType(
- XmlQNameImpl.validateLexical( value, _vc, event ) );
-
+ xsiType = _globalTypes.findType( XmlQNameImpl.validateLexical( value, _vc, event ) );
}
catch ( Throwable t )
{
@@ -371,8 +384,10 @@
if (originalErrorState != _errorState)
{
- emitFieldError(
- event, "Invalid xsi:type qname: '" + value + "'" );
+ // not sure how to extract this one
+ emitFieldError( event, "Invalid xsi:type qname: '" + value + "'",
+ event.getName(), xsiType, null,
+ XmlValidationError.ELEMENT_TYPE_INVALID, state._type);
_eatContent = 1;
@@ -380,7 +395,10 @@
}
else if (xsiType == null)
{
- emitError(event, "Could not find xsi:type: '" + value + "'");
+ // NOT SURE errorAttributes._expectedSchemaType = xsiType;
+ emitError(event, "Could not find xsi:type: '" + value + "'",
+ event.getName(), null, null,
+ XmlValidationError.ELEMENT_TYPE_INVALID, null);
_eatContent = 1;
@@ -392,10 +410,10 @@
{
if (!elementType.isAssignableFrom(xsiType))
{
- emitFieldError(
- event,
- "Type '" + xsiType +
- "' is not derived from '" + elementType + "'" );
+ emitFieldError( event, "Type '" + xsiType +
+ "' is not derived from '" + elementType + "'",
+ event.getName(), elementType, null,
+ XmlValidationError.ELEMENT_TYPE_INVALID, state._type);
_eatContent = 1;
@@ -409,11 +427,11 @@
{
if (t.getDerivationType() == SchemaType.DT_EXTENSION)
{
- emitFieldError(
- event,
- "Extension type: '" + xsiType +
- "' may not be substituted for: '" +
- elementType + "'" );
+ emitFieldError( event, "Extension type: '" + xsiType +
+ "' may not be substituted for: '" + elementType + "'",
+ event.getName(), elementType, null,
+ XmlValidationError.ELEMENT_TYPE_INVALID,
+ state._type);
_eatContent = 1;
@@ -429,11 +447,10 @@
{
if (t.getDerivationType() == SchemaType.DT_RESTRICTION)
{
- emitFieldError(
- event,
- "Restriction type: '" + xsiType +
- "' may not be substituted for: '" +
- elementType + "'" );
+ emitFieldError( event, "Restriction type: '" + xsiType +
+ "' may not be substituted for: '" + elementType + "'",
+ event.getName(), elementType, null,
+ XmlValidationError.ELEMENT_TYPE_INVALID, state._type);
_eatContent = 1;
@@ -455,11 +472,10 @@
if ((t.getDerivationType() == SchemaType.DT_RESTRICTION && sle.blockRestriction()) ||
(t.getDerivationType() == SchemaType.DT_EXTENSION && sle.blockExtension()))
{
- emitError(
- event,
- "Derived type: '" + xsiType +
- "' may not be substituted for element '" +
- QNameHelper.pretty(sle.getName()) + "'" );
+ //need to find a way to get the right type
+ emitError( event, "Derived type: '" + xsiType +
+ "' may not be substituted for element '" + QNameHelper.pretty(sle.getName()) + "'" ,
+ sle.getName(), null, null, XmlValidationError.ELEMENT_TYPE_INVALID, null);
_eatContent = 1;
@@ -467,7 +483,6 @@
}
}
}
-
}
elementType = xsiType;
@@ -480,8 +495,11 @@
if (sle.isAbstract())
{
- emitError(event, "Element '" + QNameHelper.pretty(sle.getName()) +
- "' is abstract and cannot be used in an instance.");
+ //todo (dutta) need to find a way to get the right type
+ emitError(event, "Element '" + QNameHelper.pretty(sle.getName()) +
+ "' is abstract and cannot be used in an instance.",
+ sle.getName(), null, null, XmlValidationError.ELEMENT_TYPE_INVALID, null);
+
_eatContent = 1;
return;
}
@@ -489,10 +507,8 @@
if (elementType != null && elementType.isAbstract())
{
- emitFieldError(
- event,
- "Abstract type: " + elementType +
- " cannot be used in an instance" );
+ emitFieldError( event, "Abstract type: " + elementType + " cannot be used in an instance",
+ event.getName(), elementType, null, XmlValidationError.ELEMENT_TYPE_INVALID, state._type);
_eatContent = 1;
@@ -512,7 +528,9 @@
// note in schema spec 3.3.4, you're not even allowed to say xsi:nil="false" if you're not nillable!
if (hasNil && !elementField.isNillable())
{
- emitFieldError(event, "Element has xsi:nil attribute but is not nillable");
+ emitFieldError( event, "Element has xsi:nil attribute but is not nillable",
+ elementField.getName(), elementField.getType(), null,
+ XmlValidationError.ELEMENT_TYPE_INVALID, state._type);
_eatContent = 1;
return;
@@ -542,9 +560,9 @@
if (state._attrs.contains( attrName ))
{
- emitFieldError(
- event,
- "Duplicate attribute: " + QNameHelper.pretty( attrName ) );
+ // todo (dutta) need additional logic to determine the expectedSchemaType
+ emitFieldError( event, "Duplicate attribute: " + QNameHelper.pretty( attrName ),
+ attrName, null, null, XmlValidationError.INCORRECT_ATTRIBUTE, state._type );
return;
}
@@ -553,7 +571,9 @@
if (!state._canHaveAttrs)
{
- emitFieldError( event, "Can't have attributes" );
+ // todo (dutta) need additional logic to determine the expectedSchemaType
+ emitFieldError( event, "Can't have attributes", attrName, null, null,
+ XmlValidationError.INCORRECT_ATTRIBUTE, state._type);
return;
}
@@ -568,10 +588,9 @@
if (attrSchema.getUse() == SchemaLocalAttribute.PROHIBITED)
{
- emitFieldError(
- event,
- "Attribute is prohibited: "
- + QNameHelper.pretty( attrName ) );
+ // todo (dutta) need additional logic to determine the expectedSchemaType
+ emitFieldError( event, "Attribute is prohibited: " + QNameHelper.pretty( attrName ),
+ attrName, null, null, XmlValidationError.INCORRECT_ATTRIBUTE, state._type );
return;
}
@@ -590,10 +609,10 @@
if (wildcardProcess == SchemaAttributeModel.NONE)
{
- emitFieldError(
- event,
- "Attribute not allowed (no wildcards allowed): "
- + QNameHelper.pretty( attrName ) );
+ // todo (dutta) need additional logic to determine the expectedSchemaType
+ emitFieldError( event,
+ "Attribute not allowed (no wildcards allowed): " + QNameHelper.pretty( attrName ),
+ attrName, null, null, XmlValidationError.INCORRECT_ATTRIBUTE, state._type);
return;
}
@@ -602,14 +621,13 @@
if (!attrWildcardSet.contains( attrName ))
{
- emitFieldError(
- event,
- "Attribute not allowed: " + QNameHelper.pretty( attrName ) );
+ // todo (dutta) need additional logic to determine the expectedSchemaType
+ emitFieldError( event, "Attribute not allowed: " + QNameHelper.pretty( attrName ),
+ attrName, null, null, XmlValidationError.INCORRECT_ATTRIBUTE, state._type);
return;
}
-
if (wildcardProcess == SchemaAttributeModel.SKIP)
return;
@@ -623,9 +641,10 @@
assert wildcardProcess == SchemaAttributeModel.STRICT;
- emitFieldError(
- event,
- "Attribute not allowed (strict wildcard, and no definition found): " + QNameHelper.pretty( attrName ) );
+ // todo (dutta) need additional logic to determine the expectedSchemaType
+ emitFieldError( event,
+ "Attribute not allowed (strict wildcard, and no definition found): " + QNameHelper.pretty( attrName ),
+ attrName, null, null, XmlValidationError.INCORRECT_ATTRIBUTE, state._type);
return;
}
@@ -654,8 +673,8 @@
{
if (sla.getUse() == SchemaLocalAttribute.REQUIRED)
{
- emitFieldError(
- event, "Expected attribute: ", sla.getName() );
+ emitFieldError( event, "Expected attribute: " + QNameHelper.pretty (sla.getName()),
+ sla.getName(), null, null, XmlValidationError.INCORRECT_ATTRIBUTE, state._type);
}
else if (sla.isDefault() || sla.isFixed())
{
@@ -700,17 +719,7 @@
{
if (!state.end())
{
-
- message = findDetailedErrorEnd(state);
-
- if (message != null)
- {
- emitFieldError(event, message);
- }
- else
- {
- emitFieldError(event, "Expected element(s)");
- }
+ findDetailedErrorEnd(event,state);
}
// This end event has no text, use this fact to pass no text to
@@ -730,7 +739,11 @@
State state = topState();
if (state._isNil)
- emitFieldError(event, "Nil element cannot have simple content");
+ {
+ emitFieldError( event, "Nil element cannot have simple content",
+ state._field.getName(), state._type, null,
+ XmlValidationError.NIL_ELEMENT, state._type );
+ }
else
handleText( event, false, state._field );
@@ -767,21 +780,27 @@
if (field instanceof SchemaLocalElement)
{
SchemaLocalElement e = (SchemaLocalElement)field;
- emitError(event, "Element: '" + QNameHelper.pretty(e.getName()) + "' cannot have mixed content.");
+
+ emitError(event, "Element: '" + QNameHelper.pretty(e.getName()) +
+ "' cannot have mixed content.", e.getName(), field.getType(),
+ null, XmlValidationError.ELEMENT_TYPE_INVALID, null);
}
else
- emitError( event, "Can't have mixed content" );
+ {
+ // todo (dutta) offendingQName = not sure how to get this(event.getName()??);
+ emitError(event, "Can't have mixed content", event.getName(),
+ state._type, null, XmlValidationError.ELEMENT_TYPE_INVALID, null);
+ }
}
if (!emptyContent)
state._sawText = true;
}
- private String findDetailedErrorBegin(State state, QName qName)
+ private void findDetailedErrorBegin(Event event, State state, QName qName)
{
String message = null;
SchemaProperty[] eltProperties = state._type.getElementProperties();
-
for (int ii = 0; ii < eltProperties.length; ii++)
{
//Get the element from the schema
@@ -791,13 +810,23 @@
if (state.test(sProp.getName()))
{
message = "Expected element " + QNameHelper.pretty(sProp.getName()) + " instead of " + QNameHelper.pretty(qName) + " here";
+ ArrayList expectedNames = new ArrayList();
+ expectedNames.add(sProp.getName());
+
+ emitFieldError( event, message, qName, sProp.getType(),
+ expectedNames, XmlValidationError.INCORRECT_ELEMENT, state._type);
+
break;
}
}
- return message;
+ if (message == null)
+ {
+ emitFieldError( event, "Element not allowed: " + QNameHelper.pretty( qName),
+ qName, null, null, XmlValidationError.INCORRECT_ELEMENT, state._type);
+ }
}
- private String findDetailedErrorEnd(State state)
+ private void findDetailedErrorEnd(Event event, State state)
{
SchemaProperty[] eltProperties = state._type.getElementProperties();
String message = null;
@@ -812,10 +841,22 @@
{
message = "Expected element " + QNameHelper.pretty(sProp.getName()) +
" at the end of the content";
+
+ ArrayList expectedNames = new ArrayList();
+ expectedNames.add(sProp.getName());
+
+ emitFieldError (event, message, null, sProp.getType(), expectedNames,
+ XmlValidationError.INCORRECT_ELEMENT, state._type);
+
break;
}
}
- return message;
+
+ if (message == null)
+ {
+ emitFieldError( event, "Expected element(s)", null, null, null,
+ XmlValidationError.ELEMENT_NOT_ALLOWED, state._type);
+ }
}
@@ -981,7 +1022,9 @@
if (type.isNoType())
{
- emitError(event, "Invalid type.");
+ emitError( event, "Invalid type.", field.getName(), type, null,
+ XmlValidationError.ELEMENT_TYPE_INVALID, null);
+
return null;
}
@@ -1003,11 +1046,10 @@
{
if (XmlQName.type.isAssignableFrom(type))
{
- emitError(
- event,
- "Default QName values are unsupported for " +
- QNameHelper.readable(type) + " - ignoring.",
- XmlError.SEVERITY_INFO);
+ emitError( event, "Default QName values are unsupported for " +
+ QNameHelper.readable(type) + " - ignoring.",
+ XmlError.SEVERITY_INFO, field.getName(), type, null,
+ XmlValidationError.ELEMENT_TYPE_INVALID, null);
return null;
}
@@ -1041,11 +1083,10 @@
if (!val.valueEquals( def ))
{
- // BUGBUG - make this more verbose
-
- emitError(
- event,
- "Value not equal to fixed value. " + value );
+ // TODO (dutta) - make this more verbose
+ emitError( event, "Value not equal to fixed value. " + value,
+ field.getName(), field.getType(), null,
+ XmlValidationError.ELEMENT_TYPE_INVALID, null);
return null;
}
@@ -1224,14 +1265,13 @@
if (!type.matchPatternFacet( value ))
{
- emitError(
- event,
- "List '" + value + "' does not match pattern for " + QNameHelper.readable(type) );
+ emitError( event,
+ "List '" + value + "' does not match pattern for " + QNameHelper.readable(type),
+ null, type, null, XmlValidationError.LIST_INVALID, null);
}
String[] items = XmlListImpl.split_list(value);
-
int i;
XmlObject o;
@@ -1239,10 +1279,10 @@
{
if ((i = ((SimpleValue)o).getIntValue()) != items.length)
{
- emitError(
- event,
- "List (" + value + ") does not have " + i +
- " items per length facet for " + QNameHelper.readable(type));
+ //offending Qname not valid
+ emitError( event, "List (" + value + ") does not have " + i +
+ " items per length facet for " + QNameHelper.readable(type),
+ null, type, null, XmlValidationError.LIST_INVALID, null);
}
}
@@ -1250,10 +1290,10 @@
{
if ((i = ((SimpleValue)o).getIntValue()) > items.length)
{
- emitError(
- event,
- "List (" + value + ") has only " + items.length +
- " items, fewer than min length facet (" + i + ") for " + QNameHelper.readable(type) );
+ //offending Qname not valid
+ emitError( event, "List (" + value + ") has only " + items.length +
+ " items, fewer than min length facet (" + i + ") for " + QNameHelper.readable(type),
+ null, type, null, XmlValidationError.LIST_INVALID, null);
}
}
@@ -1261,10 +1301,10 @@
{
if ((i = ((SimpleValue)o).getIntValue()) < items.length)
{
- emitError(
- event,
- "List (" + value + ") has " + items.length +
- " items, more than max length facet (" + i + ") for " + QNameHelper.readable(type) );
+ //offending Qname not valid
+ emitError( event, "List (" + value + ") has " + items.length +
+ " items, more than max length facet (" + i + ") for " + QNameHelper.readable(type),
+ null, type, null, XmlValidationError.LIST_INVALID, null);
}
}
@@ -1297,10 +1337,10 @@
}
catch (XmlValueOutOfRangeException e)
{
- emitError(
- event,
- "List value (" + value +
- ") is not a valid enumeration value for " + QNameHelper.readable(type));
+ //offending Qname not valid ??
+ emitError( event, "List value (" + value +
+ ") is not a valid enumeration value for " + QNameHelper.readable(type),
+ null, type, null, XmlValidationError.LIST_INVALID, null);
}
finally
{
@@ -1318,9 +1358,10 @@
if (!type.matchPatternFacet( value ))
{
- emitError(
- event,
- "Union '" + value + "' does not match pattern for " + QNameHelper.readable(type));
+ //offending Qname not valid ??
+ emitError( event,
+ "Union '" + value + "' does not match pattern for " + QNameHelper.readable(type),
+ null, type, null, XmlValidationError.UNION_INVALID, null);
}
int currentWsr = SchemaType.WS_PRESERVE;
@@ -1368,9 +1409,10 @@
if (i >= types.length)
{
- emitError(
- event,
- "Union '" + value + "' does not match any members of " + QNameHelper.readable(type) );
+ //offending Qname not valid ??
+ emitError( event,
+ "Union '" + value + "' does not match any members of " + QNameHelper.readable(type),
+ null, type, null, XmlValidationError.UNION_INVALID, null);
}
else
{
@@ -1394,19 +1436,21 @@
if (i >= unionEnumvals.length)
{
- emitError(
- event,
- "Union '" + value +
- "' is not a valid enumeration value for " + QNameHelper.readable(type) );
+
+ //offending Qname not valid ??
+ emitError( event, "Union '" + value +
+ "' is not a valid enumeration value for " + QNameHelper.readable(type),
+ null, type, null, XmlValidationError.UNION_INVALID, null);
}
}
catch (XmlValueOutOfRangeException e)
{
// actually, the current union code always ends up here when invalid
- emitError(
- event,
- "Union '" + value +
- "' is not a valid enumeration value for " + QNameHelper.readable(type) );
+
+ //offending Qname not valid ??
+ emitError( event, "Union '" + value +
+ "' is not a valid enumeration value for " + QNameHelper.readable(type),
+ null, type, null, XmlValidationError.UNION_INVALID, null );
}
finally
{
1.4 +44 -30 xml-xmlbeans/v2/src/xmlpublic/org/apache/xmlbeans/XmlError.java
Index: XmlError.java
===================================================================
RCS file: /home/cvs/xml-xmlbeans/v2/src/xmlpublic/org/apache/xmlbeans/XmlError.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- XmlError.java 27 Mar 2004 00:22:55 -0000 1.3
+++ XmlError.java 9 Apr 2004 21:53:44 -0000 1.4
@@ -76,6 +76,49 @@
}
/**
+ * The static factory methods should be used instead of
+ * this constructor.
+ */
+ protected XmlError(String message, int severity, XmlCursor cursor)
+ {
+ String source = null;
+ int line = -1;
+ int column = -1;
+ int offset = -1;
+
+ if (cursor != null)
+ {
+ // Hunt down the line/column/offset
+ source = cursor.documentProperties().getSourceName();
+
+ XmlCursor c = cursor.newCursor();
+
+ XmlLineNumber ln =
+ (XmlLineNumber) c.getBookmark( XmlLineNumber.class );
+
+ if (ln == null)
+ ln = (XmlLineNumber) c.toPrevBookmark( XmlLineNumber.class );
+
+ if (ln != null)
+ {
+ line = ln.getLine();
+ column = ln.getColumn();
+ offset = ln.getOffset();
+ }
+
+ c.dispose();
+ }
+
+ _message = message;
+ _severity = severity;
+ _source = source;
+ _line = line;
+ _column = column;
+ _offset = offset;
+ _cursor = cursor;
+ }
+
+ /**
* Returns an XmlError for the given message, with no location and {@link #SEVERITY_ERROR}.
* @param message the error message
*/
@@ -203,36 +246,7 @@
*/
public static XmlError forCursor(String message, int severity, XmlCursor cursor)
{
- if (cursor == null)
- return forMessage(message, severity);
-
- // Hunt down the line/column/offset
-
- int line = -1;
- int column = -1;
- int offset = -1;
- String source = null;
-
- source = cursor.documentProperties().getSourceName();
-
- XmlCursor c = cursor.newCursor();
-
- XmlLineNumber ln =
- (XmlLineNumber) c.getBookmark( XmlLineNumber.class );
-
- if (ln == null)
- ln = (XmlLineNumber) c.toPrevBookmark( XmlLineNumber.class );
-
- if (ln != null)
- {
- line = ln.getLine();
- column = ln.getColumn();
- offset = ln.getOffset();
- }
-
- c.dispose();
-
- return new XmlError(message, severity, source, line, column, offset, cursor);
+ return new XmlError(message, severity, cursor);
}
/**
1.1 xml-xmlbeans/v2/src/xmlpublic/org/apache/xmlbeans/XmlValidationError.java
Index: XmlValidationError.java
===================================================================
/* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Contributed by Dutta Satadip for adding functionality to retrieve xml validation errors
* programatically.
*/
package org.apache.xmlbeans;
import java.util.List;
import org.apache.xmlbeans.XmlError;
import javax.xml.namespace.QName;
/**
* The XmlValidationError class extends the {@link XmlError }XMLError class. The XML Validator contains
* extra attributes that can be used to construct the XML validation error programatically.
* <p>
* To extract the validation error cast the errors to XmlValidationError instead of
* XmlError for example:
* <br/>
* <pre>
* xobj.validate(new XmlOptions().setErrorListener(errors))
* for (Iterator it = errors.iterator(); it.hasNext(); )
* {
* XmlError err = (XmlError)it.next());
* if (err instanceof XmlValidationError)
* {
* XmlValidationError validationError = (XmlValidationError) err;
* }
* }
*</pre>
*
*
* Whenever an XmlValidationError is retrieved it will always be populated with
* the following information:
* <ul>
* <li>Message</li>
* <li>Severity</li>
* <li>Error Type </li>
* </ul>
*
* <p>
* The error type is very important because the other attributes of the
* XMLValidationError are populated based on the error type.
* </p>
* if errortype == INCORRECT_ELEMENT then
* <br/>
* offendingQName, badSchemaType will always be present, however expectedSchemaType and
* expectedQNames are available only if it is possible to determine them during vaildation.
*<br/>
*
* <p>
* if errortype == ELEMENT_NOT_ALLOWED then
* <br/>
* badSchemaType will always be present, however expectedSchemaType and
* offendingQName are available only if it is possible to determine them during vaildation.
* <br/>
*
* <p>
* if errortype == INCORRECT_ATTRIBUTE then
* <br/>
* offendingQName, badSchemaType will always be present
* <br/>
*
* <p>
* if errortype == ATTRIBUTE_TYPE_INVALID then
* <br/>
* no other addtional attributes are populated
* <br/>
*
* <p>
* if errortype == LIST_INVALID then
* <br/>
* expectedSchemaType will always be present
* <br/>
*
* <p>
* if errortype == UNION_INVALID then
* <br/>
* expectedSchemaType will always be present
* <br/>
*
*
* <p>
* if errortype == NIL_ELEMENT then
* <br/>
* offendingQName, expectedSchemaType and badSchemaType will always be present
* <br/>
*
* <p>
* if errortype == ELEMENT_TYPE_INVALID then
* <br/>
* offendingQName will always be present, other attributes may be available
* <br/>
*/
public class XmlValidationError extends XmlError
{
public static final int INCORRECT_ELEMENT = 1;
public static final int ELEMENT_NOT_ALLOWED = 2;
public static final int ELEMENT_TYPE_INVALID = 3;
public static final int NIL_ELEMENT = 4;
public static final int INCORRECT_ATTRIBUTE = 1000;
public static final int ATTRIBUTE_TYPE_INVALID = 1001;
public static final int LIST_INVALID = 2000;
public static final int UNION_INVALID = 3000;
public static final int UNDEFINED = 10000;
private QName _offendingQName;
private SchemaType _expectedSchemaType;
private List _expectedQNames;
private int _errorType;
private SchemaType _badSchemaType;
/**
* The static factory methods should be used instead of
* this constructor.
*/
private XmlValidationError(String msg, int severity,
XmlCursor cursor, QName offendingQname, SchemaType expectedSchemaType,
List expectedQNames, int errorType, SchemaType badSchemaType)
{
super(msg, severity, cursor);
setOffendingQName(offendingQname);
setExpectedSchemaType(expectedSchemaType);
setExpectedQNames(expectedQNames);
setErrorType(errorType);
setBadSchemaType(badSchemaType);
}
public static XmlValidationError forCursorWithDetails( String msg, int severity,
XmlCursor cursor, QName offendingQname, SchemaType expectedSchemaType,
List expectedQNames, int errorType, SchemaType badSchemaType)
{
return new XmlValidationError(msg, severity, cursor, offendingQname,
expectedSchemaType, expectedQNames, errorType, badSchemaType);
}
public SchemaType getBadSchemaType()
{
return _badSchemaType;
}
public void setBadSchemaType(SchemaType _badSchemaType)
{
this._badSchemaType = _badSchemaType;
}
public int getErrorType()
{
return _errorType;
}
public void setErrorType(int _errorType)
{
this._errorType = _errorType;
}
public List getExpectedQNames()
{
return _expectedQNames;
}
public void setExpectedQNames(List _expectedQNames)
{
this._expectedQNames = _expectedQNames;
}
public QName getOffendingQName()
{
return _offendingQName;
}
public void setOffendingQName(QName _offendingQName)
{
this._offendingQName = _offendingQName;
}
public SchemaType getExpectedSchemaType()
{
return _expectedSchemaType;
}
public void setExpectedSchemaType(SchemaType _expectedSchemaType)
{
this._expectedSchemaType = _expectedSchemaType;
}
}
1.11 +1 -0 xml-xmlbeans/v2/test/src/drt/drtcases/SmokeTests.java
Index: SmokeTests.java
===================================================================
RCS file: /home/cvs/xml-xmlbeans/v2/test/src/drt/drtcases/SmokeTests.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- SmokeTests.java 5 Apr 2004 17:26:07 -0000 1.10
+++ SmokeTests.java 9 Apr 2004 21:53:44 -0000 1.11
@@ -51,6 +51,7 @@
suite.addTest(NumeralsTests.suite());
suite.addTest(ValidatingXMLStreamReaderTests.suite());
suite.addTest(SelectChldAttTests.suite());
+ suite.addTest(ValidatorTests.suite());
return suite;
}
}
1.3 +1 -0 xml-xmlbeans/v2/test/src/drt/drtcases/StandaloneTests.java
Index: StandaloneTests.java
===================================================================
RCS file: /home/cvs/xml-xmlbeans/v2/test/src/drt/drtcases/StandaloneTests.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- StandaloneTests.java 12 Feb 2004 20:06:31 -0000 1.2
+++ StandaloneTests.java 9 Apr 2004 21:53:44 -0000 1.3
@@ -44,6 +44,7 @@
suite.addTest(DomTests.suite());
suite.addTest(GDateTests.suite());
suite.addTest(SubstGroupTests.suite());
+ suite.addTest(ValidatorTests.suite());
return suite;
}
}
1.6 +0 -1 xml-xmlbeans/v2/test/src/drt/drtcases/ValidationTests.java
Index: ValidationTests.java
===================================================================
RCS file: /home/cvs/xml-xmlbeans/v2/test/src/drt/drtcases/ValidationTests.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- ValidationTests.java 12 Feb 2004 20:06:31 -0000 1.5
+++ ValidationTests.java 9 Apr 2004 21:53:44 -0000 1.6
@@ -27,7 +27,6 @@
import org.apache.xmlbeans.XMLStreamValidationException;
import org.apache.xmlbeans.XmlDecimal;
import org.apache.xmlbeans.impl.values.XmlValueOutOfRangeException;
-import org.openuri.testNumerals.DocDocument;
import java.io.File;
import java.util.ArrayList;
1.1 xml-xmlbeans/v2/test/src/drt/drtcases/ValidatorTests.java
Index: ValidatorTests.java
===================================================================
/* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package drtcases;
import junit.framework.Assert;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.xmlbeans.SchemaType;
import org.apache.xmlbeans.SchemaTypeLoader;
import org.apache.xmlbeans.XmlBeans;
import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlError;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;
import org.apache.xmlbeans.XmlValidationError;
import org.apache.xmlbeans.XmlException;
import javax.xml.namespace.QName;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ValidatorTests extends TestCase
{
public ValidatorTests(String name)
{ super(name); }
public static Test suite()
{ return new TestSuite(ValidatorTests.class); }
static String[] _args;
static String _test;
public static File getCaseFile(String theCase)
{
return TestEnv.xbeanCase("store/" + theCase);
}
static XmlCursor loadCase(String theCase) throws Exception
{
return XmlObject.Factory.parse(getCaseFile(theCase)).newCursor();
}
public SchemaTypeLoader makeSchemaTypeLoader(String[] schemas) throws XmlException
{
XmlObject[] schemaDocs = new XmlObject[schemas.length];
for (int i = 0; i < schemas.length; i++)
{
schemaDocs[i] = XmlObject.Factory.parse(schemas[i]);
}
return XmlBeans.loadXsd(schemaDocs);
}
public SchemaTypeLoader makeSchemaTypeLoader(File[] schemas) throws XmlException, IOException
{
XmlObject[] schemaDocs = new XmlObject[schemas.length];
for (int i = 0; i < schemas.length; i++)
{
schemaDocs[i] = XmlObject.Factory.parse(schemas[i], new XmlOptions().setLoadLineNumbers().setLoadMessageDigest());
}
return XmlBeans.loadXsd(schemaDocs);
}
public List performValidation(String[] schemas, QName docType, String instances,
boolean startOnDocument) throws XmlException
{
SchemaTypeLoader stl = makeSchemaTypeLoader(schemas);
XmlOptions options = new XmlOptions();
if (docType != null)
{
SchemaType docSchema = stl.findDocumentType(docType);
Assert.assertTrue(docSchema != null);
options.setDocumentType(docSchema);
}
XmlObject x = stl.parse( instances, null, options);
if (!startOnDocument)
{
XmlCursor c = x.newCursor();
c.toFirstChild();
x = c.getObject();
c.dispose();
}
List xel = new ArrayList();
options.setErrorListener(xel);
x.validate(options);
return xel;
}
public void testValidationElementError() throws XmlException
{
String bobSchema = "<xs:schema\n" + " xmlns:xs='http://www.w3.org/2001/XMLSchema'\n" + " xmlns:bob='http://openuri.org/bobschema'\n" + " targetNamespace='http://openuri.org/bobschema'\n" + " elementFormDefault='qualified'>\n" + "\n" + " <xs:complexType name='biff'>\n" + " <xs:complexContent>\n" + " <xs:extension base='bob:foo'>\n" + " <xs:sequence>\n" + " <xs:element name='a' minOccurs='0' maxOccurs='unbounded'/>\n" + " </xs:sequence>\n" + " </xs:extension>\n" + " </xs:complexContent>\n" + " </xs:complexType>\n" + "" + " <xs:complexType name='foo'>\n" + " </xs:complexType>\n" + "" + " <xs:element name='foo' type='bob:foo'>\n" + " </xs:element>\n" + "" + "</xs:schema>\n";
String invalid = "<bob:foo xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:bob='http://openuri.org/bobschema' " + "xsi:type='bob:biff'><bob:q/></bob:foo>";
String[] schemas = {bobSchema};
List errors = null;
errors = performValidation(schemas, null, invalid, true);
Assert.assertTrue(errors != null);
Assert.assertTrue(errors.size()>0);
for (Iterator it = errors.iterator(); it.hasNext();)
{
XmlValidationError xmlValError = (XmlValidationError) it.next();
Assert.assertEquals(xmlValError.getErrorType(), XmlValidationError.INCORRECT_ELEMENT);
Assert.assertEquals(xmlValError.getBadSchemaType().getName().getLocalPart(), "biff");
Assert.assertEquals(xmlValError.getOffendingQName().getLocalPart(), "q");
Assert.assertEquals(xmlValError.getMessage(), "Expected element a@http://openuri.org/bobschema instead of q@http://openuri.org/bobschema here in element foo@http://openuri.org/bobschema");
}
}
public void testValidationAttributeError() throws XmlException
{
StringBuffer empSchema = new StringBuffer();
empSchema.append("<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' elementFormDefault='qualified'>\n");
empSchema.append("<xs:element name='age'>\n");
empSchema.append("<xs:simpleType>\n");
empSchema.append("<xs:restriction base='xs:integer'>\n");
empSchema.append("<xs:minInclusive value='0'/>\n");
empSchema.append("<xs:maxInclusive value='100'/>\n");
empSchema.append("</xs:restriction>\n");
empSchema.append("</xs:simpleType>\n");
empSchema.append("</xs:element>\n");
empSchema.append("<xs:element name='empRecords'>\n");
empSchema.append("<xs:complexType>\n");
empSchema.append("<xs:sequence>\n");
empSchema.append("<xs:element name='person' type='personType' maxOccurs='unbounded'/>\n");
empSchema.append("</xs:sequence>\n");
empSchema.append("</xs:complexType>\n");
empSchema.append("</xs:element>\n");
empSchema.append("<xs:element name='name' type='xs:string'/>\n");
empSchema.append("<xs:complexType name='personType'>\n");
empSchema.append("<xs:sequence>\n");
empSchema.append("<xs:element ref='name'/>\n");
empSchema.append("<xs:element ref='age'/>\n");
empSchema.append("</xs:sequence>\n");
empSchema.append("<xs:attribute name='employee' use='required'>\n");
empSchema.append("<xs:simpleType>\n");
empSchema.append("<xs:restriction base='xs:NMTOKEN'>\n");
empSchema.append("<xs:enumeration value='current'/>\n");
empSchema.append("<xs:enumeration value='past'/>\n");
empSchema.append("</xs:restriction>\n");
empSchema.append("</xs:simpleType>\n");
empSchema.append("</xs:attribute>\n");
empSchema.append("</xs:complexType>\n");
empSchema.append("</xs:schema>\n");
StringBuffer xmlInstance = new StringBuffer();
xmlInstance.append("<empRecords xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' >");
xmlInstance.append("<person employee='past'>");
xmlInstance.append("<name>joe blow</name>");
xmlInstance.append("<age>31</age>");
xmlInstance.append("</person>");
xmlInstance.append("<person>");
xmlInstance.append("<name>test user</name>");
xmlInstance.append("<age>29</age>");
xmlInstance.append("</person>");
xmlInstance.append("</empRecords>");
String[] schemas = {empSchema.toString()};
List errors = null;
errors = performValidation(schemas, null, xmlInstance.toString(), true);
Assert.assertTrue(errors != null);
Assert.assertTrue(errors.size()>0);
for (Iterator it = errors.iterator(); it.hasNext();)
{
XmlValidationError xmlValError = (XmlValidationError) it.next();
Assert.assertEquals(xmlValError.getErrorType(), XmlValidationError.INCORRECT_ATTRIBUTE);
Assert.assertEquals(xmlValError.getBadSchemaType().getName().getLocalPart(), "personType");
Assert.assertEquals(xmlValError.getOffendingQName().getLocalPart(), "employee");
Assert.assertEquals(xmlValError.getMessage(), "Expected attribute: employee in element person");
}
}
public void testValidationIncorrectElementError() throws XmlException
{
StringBuffer empSchema = new StringBuffer();
empSchema.append("<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' elementFormDefault='qualified'>\n");
empSchema.append("<xs:element name='age'>\n");
empSchema.append("<xs:simpleType>\n");
empSchema.append("<xs:restriction base='xs:integer'>\n");
empSchema.append("<xs:minInclusive value='0'/>\n");
empSchema.append("<xs:maxInclusive value='100'/>\n");
empSchema.append("</xs:restriction>\n");
empSchema.append("</xs:simpleType>\n");
empSchema.append("</xs:element>\n");
empSchema.append("<xs:element name='empRecords'>\n");
empSchema.append("<xs:complexType>\n");
empSchema.append("<xs:sequence>\n");
empSchema.append("<xs:element name='person' type='personType' maxOccurs='unbounded'/>\n");
empSchema.append("</xs:sequence>\n");
empSchema.append("</xs:complexType>\n");
empSchema.append("</xs:element>\n");
empSchema.append("<xs:element name='name' type='xs:string'/>\n");
empSchema.append("<xs:complexType name='personType'>\n");
empSchema.append("<xs:sequence>\n");
empSchema.append("<xs:element ref='name'/>\n");
empSchema.append("<xs:element ref='age'/>\n");
empSchema.append("</xs:sequence>\n");
empSchema.append("<xs:attribute name='employee' use='required'>\n");
empSchema.append("<xs:simpleType>\n");
empSchema.append("<xs:restriction base='xs:NMTOKEN'>\n");
empSchema.append("<xs:enumeration value='current'/>\n");
empSchema.append("<xs:enumeration value='past'/>\n");
empSchema.append("</xs:restriction>\n");
empSchema.append("</xs:simpleType>\n");
empSchema.append("</xs:attribute>\n");
empSchema.append("</xs:complexType>\n");
empSchema.append("</xs:schema>\n");
StringBuffer xmlInstance = new StringBuffer();
xmlInstance.append("<empRecords xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' >");
xmlInstance.append("<person employee='past'>");
//xmlInstance.append("<name>joe blow</name>");
xmlInstance.append("<age>31</age>");
xmlInstance.append("</person>");
xmlInstance.append("<person employee='current'>");
xmlInstance.append("<name>test user</name>");
xmlInstance.append("<age>29</age>");
xmlInstance.append("</person>");
xmlInstance.append("</empRecords>");
String[] schemas = {empSchema.toString()};
List errors = null;
errors = performValidation(schemas, null, xmlInstance.toString(), true);
Assert.assertTrue(errors != null);
Assert.assertTrue(errors.size()>0);
Iterator it = errors.iterator();
Assert.assertTrue(it.hasNext());
XmlValidationError xmlValError = (XmlValidationError) it.next();
Assert.assertEquals(XmlValidationError.INCORRECT_ELEMENT, xmlValError.getErrorType());
Assert.assertEquals("personType", xmlValError.getBadSchemaType().getName().getLocalPart());
// todo debug this Assert.assertEquals(xmlValError.getOffendingQName().getLocalPart(), "age");
Assert.assertEquals("Expected element name instead of age here in element person", xmlValError.getMessage());
Assert.assertTrue(it.hasNext());
xmlValError = (XmlValidationError) it.next();
Assert.assertEquals(XmlValidationError.INCORRECT_ELEMENT, xmlValError.getErrorType());
Assert.assertEquals("personType", xmlValError.getBadSchemaType().getName().getLocalPart());
// todo debug this Assert.assertEquals(xmlValError.getOffendingQName().getLocalPart(), "age");
Assert.assertEquals("Expected element name at the end of the content in element person", xmlValError.getMessage());
}
public void testValidationElementNotAllowedError() throws XmlException
{
StringBuffer empSchema = new StringBuffer();
empSchema.append("<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' elementFormDefault='qualified'>\n");
empSchema.append("<xs:element name='age'>\n");
empSchema.append("<xs:simpleType>\n");
empSchema.append("<xs:restriction base='xs:integer'>\n");
empSchema.append("<xs:minInclusive value='0'/>\n");
empSchema.append("<xs:maxInclusive value='100'/>\n");
empSchema.append("</xs:restriction>\n");
empSchema.append("</xs:simpleType>\n");
empSchema.append("</xs:element>\n");
empSchema.append("<xs:element name='empRecords'>\n");
empSchema.append("<xs:complexType>\n");
empSchema.append("<xs:sequence>\n");
empSchema.append("<xs:element name='person' type='personType' maxOccurs='unbounded'/>\n");
empSchema.append("</xs:sequence>\n");
empSchema.append("</xs:complexType>\n");
empSchema.append("</xs:element>\n");
empSchema.append("<xs:element name='name' type='xs:string'/>\n");
empSchema.append("<xs:complexType name='personType'>\n");
empSchema.append("<xs:sequence>\n");
empSchema.append("<xs:element ref='name'/>\n");
empSchema.append("<xs:element ref='age'/>\n");
empSchema.append("</xs:sequence>\n");
empSchema.append("<xs:attribute name='employee' use='required'>\n");
empSchema.append("<xs:simpleType>\n");
empSchema.append("<xs:restriction base='xs:NMTOKEN'>\n");
empSchema.append("<xs:enumeration value='current'/>\n");
empSchema.append("<xs:enumeration value='past'/>\n");
empSchema.append("</xs:restriction>\n");
empSchema.append("</xs:simpleType>\n");
empSchema.append("</xs:attribute>\n");
empSchema.append("</xs:complexType>\n");
empSchema.append("</xs:schema>\n");
StringBuffer xmlInstance = new StringBuffer();
xmlInstance.append("<empRecords xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' >");
xmlInstance.append("<person employee='past'>");
xmlInstance.append("<name>joe blow</name>");
xmlInstance.append("<age>31</age>");
xmlInstance.append("</person>");
xmlInstance.append("<person employee='current'>");
xmlInstance.append("<name>test user</name>");
xmlInstance.append("<age>29</age>");
xmlInstance.append("</person>");
xmlInstance.append("</empRecords>");
String[] schemas = {empSchema.toString()};
List errors = null;
errors = performValidation(schemas, null, xmlInstance.toString(), true);
Assert.assertTrue(errors != null);
// todo: enable this assert Assert.assertTrue(errors.size()>0);
for (Iterator it = errors.iterator(); it.hasNext();)
{
XmlValidationError xmlValError = (XmlValidationError) it.next();
Assert.assertEquals(xmlValError.getErrorType(), XmlValidationError.ELEMENT_NOT_ALLOWED);
Assert.assertEquals(xmlValError.getBadSchemaType().getName().getLocalPart(), "personType");
Assert.assertEquals(xmlValError.getMessage(), "Expected element(s)");
}
}
public void testValidationAttributeTypeError() throws XmlException
{
StringBuffer empSchema = new StringBuffer();
empSchema.append("<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' elementFormDefault='qualified'>\n");
empSchema.append("<xs:element name='age'>\n");
empSchema.append("<xs:simpleType>\n");
empSchema.append("<xs:restriction base='xs:integer'>\n");
empSchema.append("<xs:minInclusive value='0'/>\n");
empSchema.append("<xs:maxInclusive value='100'/>\n");
empSchema.append("</xs:restriction>\n");
empSchema.append("</xs:simpleType>\n");
empSchema.append("</xs:element>\n");
empSchema.append("<xs:element name='empRecords'>\n");
empSchema.append("<xs:complexType>\n");
empSchema.append("<xs:sequence>\n");
empSchema.append("<xs:element name='person' type='personType' maxOccurs='unbounded'/>\n");
empSchema.append("</xs:sequence>\n");
empSchema.append("</xs:complexType>\n");
empSchema.append("</xs:element>\n");
empSchema.append("<xs:element name='name' type='xs:string'/>\n");
empSchema.append("<xs:complexType name='personType'>\n");
empSchema.append("<xs:sequence>\n");
empSchema.append("<xs:element ref='name'/>\n");
empSchema.append("<xs:element ref='age'/>\n");
empSchema.append("</xs:sequence>\n");
empSchema.append("<xs:attribute name='employee' use='required'>\n");
empSchema.append("<xs:simpleType>\n");
empSchema.append("<xs:restriction base='xs:NMTOKEN'>\n");
empSchema.append("<xs:enumeration value='current'/>\n");
empSchema.append("<xs:enumeration value='past'/>\n");
empSchema.append("</xs:restriction>\n");
empSchema.append("</xs:simpleType>\n");
empSchema.append("</xs:attribute>\n");
empSchema.append("</xs:complexType>\n");
empSchema.append("</xs:schema>\n");
StringBuffer xmlInstance = new StringBuffer();
xmlInstance.append("<empRecords xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' >");
xmlInstance.append("<person employee='past'>");
xmlInstance.append("<name>joe blow</name>");
xmlInstance.append("<age>31</age>");
xmlInstance.append("</person>");
xmlInstance.append("<person employee='current'>");
xmlInstance.append("<name>test user</name>");
xmlInstance.append("<age>junk</age>");
xmlInstance.append("</person>");
xmlInstance.append("</empRecords>");
String[] schemas = {empSchema.toString()};
List errors = null;
errors = performValidation(schemas, null, xmlInstance.toString(), true);
Assert.assertTrue(errors != null);
Assert.assertTrue(errors.size()>0);
for (Iterator it = errors.iterator(); it.hasNext();)
{
XmlValidationError xmlValError = (XmlValidationError) it.next();
Assert.assertEquals(xmlValError.getErrorType(), XmlValidationError.ATTRIBUTE_TYPE_INVALID);
Assert.assertEquals(xmlValError.getMessage(), "Illegal decimal, unexpected char: 106");
}
}
public void testElementError() throws XmlException
{
String bobSchema = "<xs:schema\n" + " xmlns:xs='http://www.w3.org/2001/XMLSchema'\n" +
" xmlns:bob='http://openuri.org/bobschema'\n" +
" targetNamespace='http://openuri.org/bobschema'\n" +
" elementFormDefault='qualified'>\n" +
"\n" +
" <xs:complexType name='biff'>\n" +
" <xs:complexContent>\n" +
" <xs:extension base='bob:foo'>\n" +
" <xs:sequence>\n" +
" <xs:element name='a' minOccurs='0' maxOccurs='unbounded'/>\n" +
" </xs:sequence>\n" +
" </xs:extension>\n" +
" </xs:complexContent>\n" +
" </xs:complexType>\n" + "" +
" <xs:complexType name='foo'>\n" +
" </xs:complexType>\n" + "" +
" <xs:element name='foo' type='bob:foo'>\n" +
" </xs:element>\n" + "" +
"</xs:schema>\n";
String invalid = "<bob:foo xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " +
"xmlns:bob='http://openuri.org/bobschema' " +
"xsi:type='bob:biff'><bob:q/></bob:foo>";
String[] schemas = {bobSchema};
List errors = performValidation(schemas, null, invalid, true);
Assert.assertTrue(errors != null);
Assert.assertTrue(errors.size()>0);
for (Iterator it = errors.iterator(); it.hasNext();)
{
XmlError xmlError = (XmlError) it.next();
Assert.assertEquals(xmlError.getMessage(), "Expected element a@http://openuri.org/bobschema instead of q@http://openuri.org/bobschema here in element foo@http://openuri.org/bobschema");
// todo check XmlValidationError
}
}
public void testAttributeError() throws XmlException
{
StringBuffer empSchema = new StringBuffer();
empSchema.append("<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' elementFormDefault='qualified'>\n");
empSchema.append("<xs:element name='age'>\n");
empSchema.append("<xs:simpleType>\n");
empSchema.append("<xs:restriction base='xs:integer'>\n");
empSchema.append("<xs:minInclusive value='0'/>\n");
empSchema.append("<xs:maxInclusive value='100'/>\n");
empSchema.append("</xs:restriction>\n");
empSchema.append("</xs:simpleType>\n");
empSchema.append("</xs:element>\n");
empSchema.append("<xs:element name='empRecords'>\n");
empSchema.append("<xs:complexType>\n");
empSchema.append("<xs:sequence>\n");
empSchema.append("<xs:element name='person' type='personType' maxOccurs='unbounded'/>\n");
empSchema.append("</xs:sequence>\n");
empSchema.append("</xs:complexType>\n");
empSchema.append("</xs:element>\n");
empSchema.append("<xs:element name='name' type='xs:string'/>\n");
empSchema.append("<xs:complexType name='personType'>\n");
empSchema.append("<xs:sequence>\n");
empSchema.append("<xs:element ref='name'/>\n");
empSchema.append("<xs:element ref='age'/>\n");
empSchema.append("</xs:sequence>\n");
empSchema.append("<xs:attribute name='employee' use='required'>\n");
empSchema.append("<xs:simpleType>\n");
empSchema.append("<xs:restriction base='xs:NMTOKEN'>\n");
empSchema.append("<xs:enumeration value='current'/>\n");
empSchema.append("<xs:enumeration value='past'/>\n");
empSchema.append("</xs:restriction>\n");
empSchema.append("</xs:simpleType>\n");
empSchema.append("</xs:attribute>\n");
empSchema.append("</xs:complexType>\n");
empSchema.append("</xs:schema>\n");
StringBuffer xmlInstance = new StringBuffer();
xmlInstance.append("<empRecords xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' >");
xmlInstance.append("<person employee='past'>");
xmlInstance.append("<name>joe blow</name>");
xmlInstance.append("<age>31</age>");
xmlInstance.append("</person>");
xmlInstance.append("<person>");
xmlInstance.append("<name>test user</name>");
xmlInstance.append("<age>29</age>");
xmlInstance.append("</person>");
xmlInstance.append("</empRecords>");
String[] schemas = {empSchema.toString()};
List errors = null;
errors = performValidation(schemas, null, xmlInstance.toString(), true);
Assert.assertTrue(errors != null);
Assert.assertTrue(errors.size()>0);
for (Iterator it = errors.iterator(); it.hasNext();)
{
XmlError xmlError = (XmlError) it.next();
Assert.assertEquals(xmlError.getMessage(), "Expected attribute: employee in element person");
// todo check XmlValidationError
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: xmlbeans-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xmlbeans-cvs-help@xml.apache.org