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 da...@apache.org on 2004/01/16 23:30:35 UTC
cvs commit: xml-xmlbeans/v2/src/xmlpublic/org/apache/xmlbeans SchemaTypeElementSequencer.java SchemaType.java
davidbau 2004/01/16 14:30:35
Modified: v2/src/typeimpl/org/apache/xmlbeans/impl/schema
SchemaTypeImpl.java SchemaTypeVisitorImpl.java
v2/src/xmlpublic/org/apache/xmlbeans SchemaType.java
Added: v2/src/xmlpublic/org/apache/xmlbeans
SchemaTypeElementSequencer.java
Log:
Exposing some of the XMLSchema validator functionality in the public interfaces.
Includes code contributed by Dutta Satadip.
Revision Changes Path
1.5 +37 -0 xml-xmlbeans/v2/src/typeimpl/org/apache/xmlbeans/impl/schema/SchemaTypeImpl.java
Index: SchemaTypeImpl.java
===================================================================
RCS file: /home/cvs/xml-xmlbeans/v2/src/typeimpl/org/apache/xmlbeans/impl/schema/SchemaTypeImpl.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- SchemaTypeImpl.java 11 Dec 2003 19:32:54 -0000 1.4
+++ SchemaTypeImpl.java 16 Jan 2004 22:30:35 -0000 1.5
@@ -68,6 +68,7 @@
import org.apache.xmlbeans.SchemaParticle;
import org.apache.xmlbeans.SchemaGlobalAttribute;
import org.apache.xmlbeans.SchemaAttributeModel;
+import org.apache.xmlbeans.SchemaTypeElementSequencer;
import org.apache.xmlbeans.SchemaTypeSystem;
import org.apache.xmlbeans.SchemaProperty;
import org.apache.xmlbeans.QNameSet;
@@ -437,6 +438,14 @@
public void setComplexTypeVariety(int complexTypeVariety)
{ assertResolving(); _complexTypeVariety = complexTypeVariety; }
+ public SchemaTypeElementSequencer getElementSequencer()
+ {
+ if (_complexTypeVariety == NOT_COMPLEX_TYPE)
+ return new SequencerImpl(null);
+
+ return new SequencerImpl(new SchemaTypeVisitorImpl(_contentModel));
+ }
+
/** Set the abstract and final flags for a complex type */
void setAbstractFinal(
boolean abs, boolean finalExt, boolean finalRest, boolean finalList, boolean finalUnion)
@@ -2011,4 +2020,32 @@
public SchemaComponent.Ref getComponentRef()
{ return getRef(); }
+ /**
+ * Gives access to the internals of element validation
+ */
+ private static class SequencerImpl implements SchemaTypeElementSequencer
+ {
+ private SchemaTypeVisitorImpl _visitor;
+
+ private SequencerImpl(SchemaTypeVisitorImpl visitor)
+ {
+ _visitor = visitor;
+ }
+
+ public boolean next(QName elementName)
+ {
+ if (_visitor == null)
+ return false;
+
+ return _visitor.visit(elementName);
+ }
+
+ public boolean peek(QName elementName)
+ {
+ if (_visitor == null)
+ return false;
+
+ return _visitor.testValid(elementName);
+ }
+ }
}
1.2 +39 -6 xml-xmlbeans/v2/src/typeimpl/org/apache/xmlbeans/impl/schema/SchemaTypeVisitorImpl.java
Index: SchemaTypeVisitorImpl.java
===================================================================
RCS file: /home/cvs/xml-xmlbeans/v2/src/typeimpl/org/apache/xmlbeans/impl/schema/SchemaTypeVisitorImpl.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- SchemaTypeVisitorImpl.java 26 Sep 2003 21:23:28 -0000 1.1
+++ SchemaTypeVisitorImpl.java 16 Jan 2004 22:30:35 -0000 1.2
@@ -119,6 +119,8 @@
}
+ final static boolean PROBE_VALIDITY = true;
+ final static boolean CHECK_VALIDITY = false;
private VisitorState[] _stack;
private VisitorState[] _rollback;
@@ -309,10 +311,17 @@
* is returned. It commits the changed state machine state,
* stores the matched element state, and returns true.
*/
- boolean ok(SchemaParticle part)
+ boolean ok(SchemaParticle part, boolean testValidity)
{
- _matchedParticle = part;
- commit();
+ if ( ! testValidity )
+ {
+ _matchedParticle = part;
+ commit();
+ }
+ else
+ {
+ rollback();
+ }
return true;
}
@@ -324,9 +333,28 @@
*
* Call visit(null) once at the end if you're checking for
* complete validity of the sequence of elements.
+ *
+ * This is a wrapper for the actual visit implementation.
*/
public boolean visit(QName eltName)
{
+ return visit(eltName, CHECK_VALIDITY);
+ }
+
+ /**
+ * The actual implementation that
+ * traverses a deterministic content model, checking for
+ * validity at any given point.
+ *
+ * When testValidity is false then this method will change states
+ * if the current state is valid
+ *
+ * When testValidity is true then this method will not change states
+ * and will return if a particular state is valid or invalid
+ */
+
+ public boolean visit(QName eltName ,boolean testValidity)
+ {
if (!prepare())
return notValid();
@@ -351,7 +379,7 @@
break minmax;
}
_top._curCount++;
- return ok(_top._curPart);
+ return ok(_top._curPart, testValidity);
case SchemaParticle.ELEMENT:
if (!_top._curPart.canStartWithElement(eltName))
@@ -361,7 +389,7 @@
break minmax;
}
_top._curCount++;
- return ok(_top._curPart);
+ return ok(_top._curPart, testValidity);
case SchemaParticle.SEQUENCE:
for (int i = _top._processedChildCount; i < _top._childCount; i++)
@@ -439,10 +467,15 @@
// we've completed the outermost loop
if (eltName == null)
- return ok(null);
+ return ok(null, testValidity);
// this means we have extra elements at the end
return notValid();
+ }
+
+ public boolean testValid(QName eltName)
+ {
+ return visit(eltName,PROBE_VALIDITY);
}
/**
1.3 +8 -0 xml-xmlbeans/v2/src/xmlpublic/org/apache/xmlbeans/SchemaType.java
Index: SchemaType.java
===================================================================
RCS file: /home/cvs/xml-xmlbeans/v2/src/xmlpublic/org/apache/xmlbeans/SchemaType.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- SchemaType.java 13 Nov 2003 01:03:01 -0000 1.2
+++ SchemaType.java 16 Jan 2004 22:30:35 -0000 1.3
@@ -499,6 +499,14 @@
/**
+ * Returns a {@link SchemaTypeElementSequencer} object, which can then
+ * be used to validate complex content inside this element. This is useful
+ * for example for trying out different names and see which one would be
+ * valid as a child of this element.
+ */
+ SchemaTypeElementSequencer getElementSequencer();
+
+ /**
* The array of inner (anonymous) types defined
* within this type.
*/
1.1 xml-xmlbeans/v2/src/xmlpublic/org/apache/xmlbeans/SchemaTypeElementSequencer.java
Index: SchemaTypeElementSequencer.java
===================================================================
/*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache
* XMLBeans", nor may "Apache" appear in their name, without prior
* written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation and was
* originally based on software copyright (c) 2000-2003 BEA Systems
* Inc., <http://www.bea.com/>. For more information on the Apache Software
* Foundation, please see <http://www.apache.org/>.
*/
package org.apache.xmlbeans;
import javax.xml.namespace.QName;
/**
* This class is used to programatically validate the contents of an
* XML element.Call to both {@link #next} and {@link #peek}
* will return true if the element
* with the provided name is allowed at the current position in the element
* content, the difference being that {@link #next} will advance
* the current position, while {@link #peek} won't.
*
* @see SchemaType#getElementSequencer
*/
public interface SchemaTypeElementSequencer
{
/**
* Returns true if the element with the given name is valid at the
* current position. Advances the current position.
*/
boolean next(QName elementName);
/**
* Return true if the element with the given name is valid at the
* current position. Does not advance the current position.
*/
boolean peek(QName elementName);
}
---------------------------------------------------------------------
To unsubscribe, e-mail: xmlbeans-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xmlbeans-cvs-help@xml.apache.org