stephan 2003/04/25 01:51:12
Added: src/blocks/xmlform/java/org/apache/cocoon/components/validation
Schema.java SchemaFactory.java Validator.java
Violation.java
src/blocks/xmlform/java/org/apache/cocoon/components/validation/schematron
ActivePattern.java Assert.java Pattern.java
Phase.java Report.java Rule.java
SchematronFactory.java SchematronSchema.java
SchematronValidator.java ValidationResult.java
Log:
Forget to commit the validation classes.
Revision Changes Path
1.1
cocoon-2.1/src/blocks/xmlform/java/org/apache/cocoon/components/validation/Schema.java
Index: Schema.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, 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 Cocoon" 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
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", 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 (INCLU-
DING, 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 created by
Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.components.validation;
/**
*
* Created on Sat, April 6, 2002
*
* @author [EMAIL PROTECTED]
* @version CVS $Id: Schema.java,v 1.1 2003/04/25 08:51:11 stephan Exp $
*/
public interface Schema {
Validator newValidator() throws InstantiationException;
}
1.1
cocoon-2.1/src/blocks/xmlform/java/org/apache/cocoon/components/validation/SchemaFactory.java
Index: SchemaFactory.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, 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 Cocoon" 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
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", 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 (INCLU-
DING, 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 created by
Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.components.validation;
import org.xml.sax.InputSource;
/**
*
* Responsible for creating new instances of Schemas
* for different Schema languages
*
* @author [EMAIL PROTECTED]
* @version CVS $Id: SchemaFactory.java,v 1.1 2003/04/25 08:51:11 stephan Exp
$
*/
public abstract class SchemaFactory {
public static String NAMESPACE_SCHEMATRON =
"http://www.ascc.net/xml/schematron";
/** Creates a new instance of ValidatorFactory */
public SchemaFactory() {
}
/**
* This method creates an instance of a ValidatorFactory
* using the JDK 1.3 META-INF/services mechanism.
* The idea is borrowed from JARV
*
http://iso-relax.sourceforge.net/apiDoc/org/iso_relax/verifier/VerifierFactory.html
*
* @param ns the namespace of the schema language
* @return ValidatorFactory
* @throws InstantiationException when a factory could not be created
*/
public static SchemaFactory lookup(String ns)
throws InstantiationException {
// currently hardcoded implementation for Schematron
// until another schema validator is implemented
/* TODO: create SchematronValidatorFactory */
if (ns.equals(NAMESPACE_SCHEMATRON))
return new org
.apache
.cocoon
.components
.validation
.schematron
.SchematronFactory();
return null;
}
/**
* Loads and compiles a Schema instance
*
* @param InputSource the SAX input source containing the Schema
document
* @return Schema the compiled schema instance
* @throws InstantiationException when the Schema could not be loaded
or compiled
*/
public abstract Schema compileSchema(InputSource is)
throws InstantiationException;
}
1.1
cocoon-2.1/src/blocks/xmlform/java/org/apache/cocoon/components/validation/Validator.java
Index: Validator.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, 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 Cocoon" 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
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", 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 (INCLU-
DING, 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 created by
Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.components.validation;
import java.util.List;
/**
*
* Created on Sat, April 6, 2002
*
* @author [EMAIL PROTECTED]
* @version CVS $Id: Validator.java,v 1.1 2003/04/25 08:51:11 stephan Exp $
*/
public interface Validator {
/**
* Validates an instance against a schema and returns a set of errors.
*
* Validator is not thread safe and is not re-entrant.
*
* @param instance The instance can be either a DOM node or a JavaBean.
* @return SortedSet of ValidityViolation(s). The set is sorted by
* ValidityViolation.getPath()
*
* @throws RuntimeException if the validation process fails
* Should not happen in a normal environment.
*/
List validate(Object instance);
/**
* This property can be used for partial document validation.
* The concept is borrowed from the Schematron schema
* Not all schemas support partial validation
*/
String PROPERTY_PHASE = "http://xml.apache.org/cocoon/validator/phase";
/**
* @param property name
* @param value property value
* @throws IllegalArgumentException when the property is not supported
*/
void setProperty(String property, Object value)
throws IllegalArgumentException;
/**
* @param property name
* @return the property value
* @throws IllegalArgumentException when the property is not supported
*/
Object getProperty(String property)
throws IllegalArgumentException;
}
1.1
cocoon-2.1/src/blocks/xmlform/java/org/apache/cocoon/components/validation/Violation.java
Index: Violation.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, 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 Cocoon" 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
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", 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 (INCLU-
DING, 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 created by
Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.components.validation;
/**
* Encapsulates an error condition which was triggered
* by a violation of the document validity during
* validation
*
* @author [EMAIL PROTECTED]
* @version CVS $Id: Violation.java,v 1.1 2003/04/25 08:51:11 stephan Exp $
*/
public class Violation implements Comparable {
/**
* @return the XPath location of the Violation
*/
public String getPath() {
return xpath_;
}
/**
* set the XPath location of the Violation
*/
public void setPath(String xpath) {
xpath_ = xpath;
}
/**
* @return the error message
*/
public String getMessage() {
return message_;
}
/**
* set the error message
*/
public void setMessage(String message) {
message_ = message;
}
public boolean equals(Object obj) {
if (obj == null)
return false;
if (obj == this)
return true;
if (!(obj instanceof Violation))
throw new java.lang.IllegalArgumentException(
"Can only compare to a Violation object");
Violation v = (Violation) obj;
if (getPath().equals(v.getPath())
&& getMessage().equals(v.getMessage()))
return true;
else
return false;
}
public int hashCode() {
return (getPath().hashCode() ^ getMessage().hashCode());
}
public int compareTo(Object obj) {
if (obj == null)
return 1;
if (obj == this)
return 0;
if (!(obj instanceof Violation))
throw new java.lang.IllegalArgumentException(
"Can only compare to a Violation object");
Violation v = (Violation) obj;
int primaryResult = getPath().compareTo(v.getPath());
if (primaryResult != 0)
return primaryResult;
else {
if (getMessage() == null) {
if (v.getMessage() == null)
return 0;
else
return -1;
} else
return (getMessage().compareTo(v.getMessage()));
}
}
private String xpath_;
private String message_;
}
1.1
cocoon-2.1/src/blocks/xmlform/java/org/apache/cocoon/components/validation/schematron/ActivePattern.java
Index: ActivePattern.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, 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 Cocoon" 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
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", 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 (INCLU-
DING, 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 created by
Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.components.validation.schematron;
/**
* Represents a Schematron phase
* <active pattern="some"> element
*
* @author Ivelin Ivanov, [EMAIL PROTECTED], [EMAIL PROTECTED]
* @version CVS $Id: ActivePattern.java,v 1.1 2003/04/25 08:51:11 stephan Exp
$
*/
public class ActivePattern {
private String pattern_;
/**
* Returns the active pattern name
*/
public String getPattern() {
return pattern_;
}
/**
* Sets the active pattern name
*/
public void setPattern(String pattern) {
pattern_ = pattern;
}
}
1.1
cocoon-2.1/src/blocks/xmlform/java/org/apache/cocoon/components/validation/schematron/Assert.java
Index: Assert.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, 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 Cocoon" 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
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", 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 (INCLU-
DING, 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 created by
Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.components.validation.schematron;
/**
* Represents a Schematron assert element
*
* example:
* <assert test="count(ear)=2">A <name/> element should contain two
<emph>ear</emph> elements.</assert>
*
* @author Ivelin Ivanov, [EMAIL PROTECTED], [EMAIL PROTECTED]
* @version CVS $Id: Assert.java,v 1.1 2003/04/25 08:51:11 stephan Exp $
*/
public class Assert {
private String test_;
private String message_;
private String diagnostics_;
/**
* Returns the test attribute
*/
public String getTest() {
return test_;
}
/**
* Sets the test attribute
*/
public void setTest(String newTest) {
test_ = newTest;
}
/**
* Returns the message for to the element
*/
public String getMessage() {
return message_;
}
/**
* Sets the message for to the element
*/
public void setMessage(String newMessage) {
message_ = newMessage;
}
/**
* Returns the diagnostics list
*/
public String getDiagnostics() {
return diagnostics_;
}
/**
* Sets the diagnostics list
*/
public void setDiagnostics(String newDiagnostics) {
diagnostics_ = newDiagnostics;
}
}
1.1
cocoon-2.1/src/blocks/xmlform/java/org/apache/cocoon/components/validation/schematron/Pattern.java
Index: Pattern.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, 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 Cocoon" 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
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", 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 (INCLU-
DING, 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 created by
Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.components.validation.schematron;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* Represents a Schematron pattern
*
* @author Ivelin Ivanov, [EMAIL PROTECTED], [EMAIL PROTECTED]
* @version CVS $Id: Pattern.java,v 1.1 2003/04/25 08:51:11 stephan Exp $
*/
public class Pattern {
private String name_;
private String id_;
private ArrayList rules_ = new ArrayList();
/**
* Returns the id of the pattern
*/
public String getId() {
return id_;
}
/**
* Sets the id of the pattern
*/
public void setId(String newId) {
id_ = newId;
}
/**
* Returns the name of the pattern
*/
public String getName() {
return name_;
}
/**
* Sets the name of the pattern
*/
public void setName(String newName) {
name_ = newName;
}
/**
* Returns the list of rules
*/
public List getRule() {
return rules_;
}
/**
* Sets the list of rules
*/
public void setRule(Collection newRules) {
rules_ = new ArrayList();
rules_.addAll(newRules);
}
/**
* Add a rule to the list
*/
public void addRule(Rule r) {
rules_.add(r);
}
}
1.1
cocoon-2.1/src/blocks/xmlform/java/org/apache/cocoon/components/validation/schematron/Phase.java
Index: Phase.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, 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 Cocoon" 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
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", 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 (INCLU-
DING, 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 created by
Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.components.validation.schematron;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* Represents a Schematron phase element
*
* Example:
* <phase id="basicValidation">
* <active pattern="text" />
* <active pattern="tables" />
* <active pattern="attributePresence" />
* </phase>
*
*
* @author Ivelin Ivanov, [EMAIL PROTECTED], [EMAIL PROTECTED]
* @version CVS $Id: Phase.java,v 1.1 2003/04/25 08:51:11 stephan Exp $
*/
public class Phase {
private String id_;
private ArrayList active_ = new ArrayList();
/**
* Returns the id of the phase
*/
public String getId() {
return id_;
}
/**
* Sets the id of the phase
*/
public void setId(String newId) {
id_ = newId;
}
/**
* Returns the list of active patterns
*/
public List getActive() {
return active_;
}
/**
* Sets the list of active patterns
*/
public void setActive(Collection newActivePatterns) {
active_ = new ArrayList();
active_.addAll(newActivePatterns);
}
/**
* Add a pattern to the list of active patterns
*/
public void addActive(ActivePattern p) {
active_.add(p);
}
}
1.1
cocoon-2.1/src/blocks/xmlform/java/org/apache/cocoon/components/validation/schematron/Report.java
Index: Report.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, 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 Cocoon" 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
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", 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 (INCLU-
DING, 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 created by
Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.components.validation.schematron;
/**
* Represents a Schematron report element
*
* <report test="when">message</report>
* is equivalent to
* <assert test="not(when)">message</assert>
*
* example:
* <report test="bone">This dog has a bone.</report>
*
* @author Ivelin Ivanov, [EMAIL PROTECTED], [EMAIL PROTECTED]
* @version CVS $Id: Report.java,v 1.1 2003/04/25 08:51:11 stephan Exp $
*/
public class Report extends Assert {
}
1.1
cocoon-2.1/src/blocks/xmlform/java/org/apache/cocoon/components/validation/schematron/Rule.java
Index: Rule.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, 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 Cocoon" 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
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", 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 (INCLU-
DING, 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 created by
Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.components.validation.schematron;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* Represents a Schematron rule element
*
* From the Schematron specification:
*
* example:
* <rule context="dog">
* <assert test="count(ear) = 2">A 'dog' element should contain two 'ear'
elements.</assert>
* <report test="bone">This dog has a bone.</report>
* </rule>
*
* @author Ivelin Ivanov, [EMAIL PROTECTED], [EMAIL PROTECTED]
* @version CVS $Id: Rule.java,v 1.1 2003/04/25 08:51:11 stephan Exp $
*/
public class Rule {
private String context_;
private ArrayList asserts_ = new ArrayList();
private ArrayList reports_ = new ArrayList();
/**
* Returns the context of the pattern
*/
public String getContext() {
return context_;
}
/**
* Sets the context of the pattern
*/
public void setContext(String newContext) {
context_ = newContext;
}
/**
* Returns the list of the assertion rules
*/
public List getAssert() {
return asserts_;
}
/**
* Sets the the list of the assertion rules
*/
public void setAssert(Collection newAsserts) {
asserts_ = new ArrayList();
asserts_.addAll(newAsserts);
}
/**
* Add an assert rule
*/
public void addAssert(Assert a) {
asserts_.add(a);
}
/**
* Returns the list of the report rules
*/
public List getReport() {
return reports_;
}
/**
* Sets the list of the report rules
*/
public void setReport(Collection newReports) {
reports_ = new ArrayList();
reports_.addAll(newReports);
}
/**
* Add a report rule
*/
public void addReport(Report r) {
reports_.add(r);
}
}
1.1
cocoon-2.1/src/blocks/xmlform/java/org/apache/cocoon/components/validation/schematron/SchematronFactory.java
Index: SchematronFactory.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, 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 Cocoon" 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
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", 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 (INCLU-
DING, 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 created by
Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.components.validation.schematron;
import org.apache.avalon.framework.CascadingRuntimeException;
import org.apache.cocoon.components.validation.Schema;
import org.apache.cocoon.components.validation.SchemaFactory;
import org.apache.commons.jxpath.JXPathContext;
import org.apache.log.Hierarchy;
import org.apache.log.Logger;
import org.apache.log.Priority;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
/**
* A helper class which builds a SchematronSchema instance object
* from a DOM source
*
* @author Ivelin Ivanov, [EMAIL PROTECTED], [EMAIL PROTECTED]
* @author Michael Ratliff, [EMAIL PROTECTED] <[EMAIL PROTECTED]>, May 2002
* @version CVS $Id: SchematronFactory.java,v 1.1 2003/04/25 08:51:11 stephan
Exp $
*/
public class SchematronFactory extends SchemaFactory {
/**
* the schema name space prefix used in the schema document
*/
private String schemaPrefix_;
/**
* the default schema name space prefix
*/
private String defaultSchemaPrefix_ = "sch";
/**
* private logger
*/
private Logger logger = setupLogger();
//
// Constructors
//
/**
* initialize logger
*/
protected Logger setupLogger() {
Logger logger =
Hierarchy.getDefaultHierarchy().getLoggerFor("XmlForm");
logger.setPriority(Priority.ERROR);
return logger;
}
/**
* Builds a new Schema instance from
* the given XML InputSource
*
* @param schemaSrc
* the Schema document XML InputSource
*/
public Schema compileSchema(InputSource schemaSrc)
throws InstantiationException {
SchematronSchema schema = null;
try {
// load Schema file into a DOM document
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
DocumentBuilder dbld = dbf.newDocumentBuilder();
Document document = dbld.parse(schemaSrc);
schema = buildSchema(document);
} catch (Exception e) {
logger.error("!!! Failed loading Schematron schema", e);
throw new CascadingRuntimeException(
" !!! Failed loading Schematron schema",
e);
}
return schema;
} // build
/**
* Build Schematron schema object from a DOM document
* @param doc DOM document containing the schema
*
*/
protected SchematronSchema buildSchema(Document doc) {
SchematronSchema schema = new SchematronSchema();
doc.getNamespaceURI();
doc.getPrefix();
// Initialize the JXPath context
Element root = doc.createElement("root");
Element schemaElement = doc.getDocumentElement();
schemaPrefix_ = schemaElement.getPrefix();
root.appendChild(schemaElement);
JXPathContext jxpContext = JXPathContext.newContext(root);
jxpContext.setLenient(false);
// Bind sch:schema element
// schema title
String title =
(String) jxpContext.getValue("/schema/title",
String.class);
schema.setTitle(title);
logger.debug("Schema title: " + schema.getTitle());
bindPatterns(schema, jxpContext);
bindPhases(schema, jxpContext);
return schema;
}
/**
* populates the patterns elements from the dom tree
*
* @param schema the schema instance
* @param jxpContext
*/
protected void bindPatterns(
SchematronSchema schema,
JXPathContext jxpContext) {
// ensure that mandatory elements which are not found
// will result in Exception
jxpContext.setLenient(false);
// schema patterns
int ptCount =
((Integer) jxpContext
.getValue("count(/schema/pattern)",
Integer.class))
.intValue();
logger.debug("\nNumber of patterns: " + ptCount);
for (int i = 1; i <= ptCount; i++) {
logger.debug("Pattern# : " + i);
Pattern pattern = new Pattern();
String ptprefix = "/schema/pattern[" + i + "]";
String name =
(String) jxpContext.getValue(ptprefix +
"/@name", String.class);
pattern.setName(name);
logger.debug("Pattern name : " + pattern.getName());
String id =
(String) jxpContext.getValue(ptprefix + "/@id",
String.class);
pattern.setId(id);
logger.debug("Pattern id : " + pattern.getId());
bindRules(pattern, ptprefix, jxpContext);
schema.addPattern(pattern);
}
}
/**
* populates the rules elements for a pattern
* from the dom tree
*
* @param pattern
* @param pathPrefix pattern path prefix
* @param jxpContext JXPathContext
*/
protected void bindRules(
Pattern pattern,
String pathPrefix,
JXPathContext jxpContext) {
// ensure that mandatory elements which are not found
// will result in Exception
jxpContext.setLenient(false);
// schema rules
int ruleCount =
((Integer) jxpContext
.getValue("count(" + pathPrefix + "/rule)",
Integer.class))
.intValue();
logger.debug("\nNumber of rules: " + ruleCount);
for (int i = 1; i <= ruleCount; i++) {
logger.debug("Rule# : " + i);
Rule rule = new Rule();
String rulePrefix = pathPrefix + "/rule[" + i + "]";
String context =
(String) jxpContext.getValue(
rulePrefix + "/@context",
String.class);
rule.setContext(context);
logger.debug("Rule context : " + rule.getContext());
bindAsserts(rule, rulePrefix, jxpContext);
// Patch to make reports work in schematron
// Note change to name of bindRerports [sic] function
bindReports(rule, rulePrefix, jxpContext);
pattern.addRule(rule);
}
}
/**
* populates the assert elements for a rule
* from the dom tree
*
* @param rule
* @param pathPrefix rule path prefix
* @param jxpContext JXPathContext
*/
protected void bindAsserts(
Rule rule,
String pathPrefix,
JXPathContext jxpContext) {
// ensure that mandatory elements which are not found
// will result in Exception
jxpContext.setLenient(false);
// schema reports
int elementCount =
((Integer) jxpContext
.getValue("count(" + pathPrefix + "/assert)",
Integer.class))
.intValue();
logger.debug("\nNumber of asserts: " + elementCount);
for (int i = 1; i <= elementCount; i++) {
logger.debug("Assert# : " + i);
Assert assertion = new Assert();
String assertPrefix = pathPrefix + "/assert[" + i + "]";
String test =
(String) jxpContext.getValue(
assertPrefix + "/@test",
String.class);
assertion.setTest(test);
logger.debug("Assert test : " + assertion.getTest());
// since diagnostics is a non-mandatory element
// we will try to get its value in a lenient mode
jxpContext.setLenient(true);
String diagnostics =
(String) jxpContext.getValue(
assertPrefix + "/@diagnostics",
String.class);
assertion.setDiagnostics(diagnostics);
logger.debug("Assert diagnostics : " +
assertion.getDiagnostics());
jxpContext.setLenient(false);
// now read the report message
// TODO: The current implementation does not
// read xml tags used within the assert message.
// Solution is to use JXPath NodePointer to get
// to the DOM node and then convert it to a String.
// e.g.
// NodePointer nptr = (NodePointer)
jxpContext.locateValue( assertPrefix );
// Node msgNode = (Node) nptr.getNodeValue();
// convery DOMNode to String
String message =
(String) jxpContext.getValue(assertPrefix,
String.class);
assertion.setMessage(message);
logger.debug("Assert message : " +
assertion.getMessage());
rule.addAssert(assertion);
}
}
/**
* populates the report elements for a rule
* from the dom tree
*
* @param rule
* @param pathPrefix rule path prefix
* @param jxpContext JXPathContext
*/
protected void bindReports(
Rule rule,
String pathPrefix,
JXPathContext jxpContext) {
// ensure that mandatory elements which are not found
// will result in Exception
jxpContext.setLenient(false);
// schema reports
int elementCount =
((Integer) jxpContext
.getValue("count(" + pathPrefix + "/report)",
Integer.class))
.intValue();
logger.debug("\nNumber of reports: " + elementCount);
for (int i = 1; i <= elementCount; i++) {
logger.debug("Report# : " + i);
Report report = new Report();
String assertPrefix = pathPrefix + "/report[" + i + "]";
String test =
(String) jxpContext.getValue(
assertPrefix + "/@test",
String.class);
report.setTest(test);
logger.debug("Report test : " + report.getTest());
// since diagnostics is a non-mandatory element
// we will try to get its value in a lenient mode
jxpContext.setLenient(true);
String diagnostics =
(String) jxpContext.getValue(
assertPrefix + "/@diagnostics",
String.class);
report.setDiagnostics(diagnostics);
logger.debug("Report diagnostics : " +
report.getDiagnostics());
jxpContext.setLenient(false);
String message =
(String) jxpContext.getValue(assertPrefix,
String.class);
report.setMessage(message);
logger.debug("Report message : " +
report.getMessage());
rule.addReport(report);
}
}
/**
* populates the phases elements from the dom tree
*
* @param schema the schema instance
* @param jxpContext
*/
protected void bindPhases(
SchematronSchema schema,
JXPathContext jxpContext) {
// ensure that mandatory elements which are not found
// will result in Exception
jxpContext.setLenient(false);
// schema phases
int phaseCount =
((Integer) jxpContext
.getValue("count(/schema/phase)",
Integer.class))
.intValue();
logger.debug("\nNumber of phases: " + phaseCount);
for (int i = 1; i <= phaseCount; i++) {
logger.debug("phase# : " + i);
Phase phase = new Phase();
String phprefix = "/schema/phase[" + i + "]";
String id =
(String) jxpContext.getValue(phprefix + "/@id",
String.class);
phase.setId(id);
logger.debug("phase id : " + phase.getId());
bindPhaseActivePatterns(phase, phprefix, jxpContext);
schema.addPhase(phase);
}
}
protected void bindPhaseActivePatterns(
Phase phase,
String pathPrefix,
JXPathContext jxpContext) {
// ensure that mandatory elements which are not found
// will result in Exception
jxpContext.setLenient(false);
// phase active patterns
int elementCount =
((Integer) jxpContext
.getValue("count(" + pathPrefix + "/active)",
Integer.class))
.intValue();
logger.debug("Number of active patterns: " + elementCount);
for (int i = 1; i <= elementCount; i++) {
logger.debug("active pattern # : " + i);
ActivePattern activePattern = new ActivePattern();
String assertPrefix = pathPrefix + "/active[" + i + "]";
String pt =
(String) jxpContext.getValue(
assertPrefix + "/@pattern",
String.class);
activePattern.setPattern(pt);
logger.debug(
"Phase active pattern : " +
activePattern.getPattern());
phase.addActive(activePattern);
}
}
/**
* Replace all occurances of sch: with the actual Schema prefix used in
the document
*
* TODO: fix this implementaion. There are problems with DOM.
* Returns null instead of the actual namespace prefix (e.g. "sch") as
expected.
*/
protected String fixns(String path) {
// Ironicly, at the time I am writing this
// JDK 1.4 is offering String.replaceAll(regex, str)
// I don't use it however for backward compatibility
StringBuffer strbuf = new StringBuffer(path);
int i = 0;
int j = 0;
String dprefix = defaultSchemaPrefix_ + ":";
int dplen = dprefix.length();
while ((j = path.indexOf(dprefix, i)) >= 0) {
strbuf.append(path.substring(i, j));
strbuf.append(schemaPrefix_);
strbuf.append(':');
i = j + dplen;
}
strbuf.append(path.substring(i));
return strbuf.toString();
}
}
1.1
cocoon-2.1/src/blocks/xmlform/java/org/apache/cocoon/components/validation/schematron/SchematronSchema.java
Index: SchematronSchema.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, 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 Cocoon" 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
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", 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 (INCLU-
DING, 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 created by
Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.components.validation.schematron;
import org.apache.cocoon.components.validation.Schema;
import org.apache.cocoon.components.validation.Validator;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* Represents a Schematron Schema
*
* Specification:
* http://www.ascc.net/xml/resource/schematron/Schematron2000.html
*
* @author Ivelin Ivanov, [EMAIL PROTECTED], [EMAIL PROTECTED]
* @version CVS $Id: SchematronSchema.java,v 1.1 2003/04/25 08:51:11 stephan
Exp $
*/
public class SchematronSchema implements Schema {
private String title_;
private ArrayList patterns_ = new ArrayList();
private ArrayList phases_ = new ArrayList();
/**
* Returns the message for to the element
*/
public String getTitle() {
return title_;
}
/**
* Sets the message for to the element
*/
public void setTitle(String newTitle) {
title_ = newTitle;
}
/**
* Returns a list of the patterns which
* contain messages that failed during validation
*/
public List getPattern() {
return patterns_;
}
/**
* Sets the list of the patterns which
* contain messages that failed during validation
*/
public void setPattern(Collection newPatterns) {
patterns_ = new ArrayList();
patterns_.addAll(newPatterns);
}
/**
* Add a pattern to the list
*/
public void addPattern(Pattern p) {
patterns_.add(p);
}
/**
* Returns the list of schema phases
*/
public List getPhase() {
return phases_;
}
/**
* Sets the list of schema phases
*/
public void setPhase(Collection newPhases) {
phases_ = new ArrayList();
phases_.addAll(newPhases);
}
/**
* Add a pattern to the list
*/
public void addPhase(Phase p) {
phases_.add(p);
}
public Validator newValidator() throws InstantiationException {
return new SchematronValidator(this);
}
}
1.1
cocoon-2.1/src/blocks/xmlform/java/org/apache/cocoon/components/validation/schematron/SchematronValidator.java
Index: SchematronValidator.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, 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 Cocoon" 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
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", 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 (INCLU-
DING, 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 created by
Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.components.validation.schematron;
import org.apache.cocoon.components.validation.Validator;
import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.Pointer;
import org.apache.log.Hierarchy;
import org.apache.log.Logger;
import org.apache.log.Priority;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* An object representing a single Schematron schema, used to validate
* multiple XML instances.
*
* This implementation can validate JavaBeans and DOM documents.
* It is based exclusively on the JXPath library from the Jakarta Commons
project.
* See http://jakarta.apache.org/commons/
*
* @author Ivelin Ivanov, [EMAIL PROTECTED], [EMAIL PROTECTED]
* @version CVS $Id: SchematronValidator.java,v 1.1 2003/04/25 08:51:11
stephan Exp $
*/
public class SchematronValidator implements Validator {
/** The schema instance for this Validator
* It is initialized once when a new Validator instance
* is created and used multiple times for validating
* different JavaBeans/DOM objects against the schema
*/
private SchematronSchema schema_;
/**
* lookup map, with phase id keys.
* Used for efficiency when validating by phase
*/
private Map phaseMap_ = new HashMap();
/*
* Schematron Phase property
*/
private String phaseProperty_ = null;
/*
* private logger
*/
private Logger logger = setupLogger();
//
// Constructors
//
/**
* Constructs a new Validator object for a given Schematron schema.
*
* @param schema
* The Schematron schema
*/
public SchematronValidator(SchematronSchema schema) {
schema_ = schema;
preparePhaseMap();
}
//
// helper methods for the constructors
//
/**
* initialize logger
*/
protected Logger setupLogger() {
Logger logger =
Hierarchy.getDefaultHierarchy().getLoggerFor("XmlForm");
logger.setPriority(Priority.ERROR);
return logger;
}
protected void preparePhaseMap() {
Map patternMap = new HashMap();
Iterator ptiter = schema_.getPattern().iterator();
while (ptiter.hasNext()) {
Pattern pattern = (Pattern) ptiter.next();
patternMap.put(pattern.getId(), pattern);
}
Iterator phiter = schema_.getPhase().iterator();
while (phiter.hasNext()) {
Phase phase = (Phase) phiter.next();
List activePatterns = new ArrayList();
phaseMap_.put(phase.getId(), activePatterns);
Iterator activeIter = phase.getActive().iterator();
while (activeIter.hasNext()) {
ActivePattern active = (ActivePattern)
activeIter.next();
activePatterns.add(patternMap.get(active.getPattern()));
}
}
}
//
// public methods
//
/**
* Performs validation of the passed JavaBean or DOM object.
*
* This method tries to find the "phase" attribute
* and runs the active patterns for the phase.
* If phase not found, the method will try to match all patterns
*
*
* @param jbean The JavaBean or DOM object to be validated.
* @param props Properties which control different aspects of the
* validation process. This method only looks for the phase property.
* Another implementation may use other.
*
* @return A Result object which represents the result
* of the validation.
*/
public List validate(Object jbean) {
List patterns = null;
if (phaseProperty_ != null) {
patterns = getPatternsForPhase(phaseProperty_);
logger.debug(" Validating for phase: " +
phaseProperty_);
} else {
patterns = schema_.getPattern();
logger.debug(" Validating all patterns. No phase
provided ");
}
ValidationResult vres = new ValidationResult();
if (patterns != null) {
// create the JXPathContext
// which will be used to validate each rule
JXPathContext jxpContext =
JXPathContext.newContext(jbean);
Iterator iter = patterns.iterator();
while (iter.hasNext()) {
Pattern resultPattern =
evalPattern(jxpContext, (Pattern)
iter.next());
// if the resultPattern is null,
// then it passed successfully
if (resultPattern != null)
vres.addPattern(resultPattern);
}
}
return vres.toList();
}
/**
* return the list of patterns listed
* as <active/> elements of <phase/>
*
* @param phase name of the phase
* @return List of patterns
*/
protected List getPatternsForPhase(String phase) {
return (List) phaseMap_.get(phase);
}
/**
* Returns pattern with rules which failed during validation.
* The context attribute of each rule in the result pattern
* contains the exact location of the failed element
* unlike the context attribute of the original pattern which
* is an XSLT production pattern
*
* @param jxpContext The JXPathContext being validated
* @param pattern The production schema pattern to be evaluated
* @return pattern with rules wich failed during validation.
*/
protected Pattern evalPattern(JXPathContext jxpContext, Pattern
pattern) {
// copy attributes
Pattern resultPattern = new Pattern();
resultPattern.setName(pattern.getName());
resultPattern.setId(pattern.getId());
// evaluate rules
Iterator iter = pattern.getRule().iterator();
while (iter.hasNext()) {
List failedRules = evalRule(jxpContext, (Rule)
iter.next());
// if there were failed rules
// add them to the list of other failed rules
if (failedRules.size() > 0) {
failedRules.addAll(resultPattern.getRule());
resultPattern.setRule(failedRules);
}
}
// if there are no failed rules return null
if (resultPattern.getRule().size() == 0)
return null;
else
return resultPattern;
}
/**
* Returns rules with asserts or reports which failed during validation.
* The context attribute of each rule in the result pattern
* contains the exact location of the failed element
* unlike the context attribute of the original pattern which
* is an XSLT production pattern
*
* @param jxpContext The JXPath context being validated
* @param rule The original pattern rule to be evaluated
* @return pattern with rules wich failed during validation.
*/
protected List evalRule(JXPathContext jxpContext, Rule rule) {
List failedRules = new ArrayList();
Iterator pointerIter =
jxpContext.iteratePointers(rule.getContext());
while (pointerIter.hasNext()) {
Pointer ptr = (Pointer) pointerIter.next();
// prepare result Rule
Rule nextFailedRule = new Rule();
nextFailedRule.setContext(ptr.asPath());
// switch to the context of the rule
JXPathContext localJxpContext =
JXPathContext.newContext(jxpContext,
ptr.getValue());
// evaluate asserts
Iterator assertIter = rule.getAssert().iterator();
while (assertIter.hasNext()) {
Assert assertion = (Assert) assertIter.next();
// if an assert test fails, then it should be
added
// to the result
boolean passed = evalTest(localJxpContext,
assertion.getTest());
if (!passed) {
nextFailedRule.addAssert(assertion);
}
}
// evaluate reports
Iterator reportIter = rule.getReport().iterator();
while (reportIter.hasNext()) {
Report report = (Report) reportIter.next();
// if a report test passes, then it should be
added
// to the result
boolean passed = evalTest(localJxpContext,
report.getTest());
if (passed) {
nextFailedRule.addReport(report);
}
}
// if the nextFailedRule is non empty,
// then add it to the list of failed rules
if (nextFailedRule.getAssert().size() > 0
|| nextFailedRule.getReport().size() > 0) {
failedRules.add(nextFailedRule);
}
}
return failedRules;
}
/**
* Test an XPath expression in a context
*
* @param jxpContext The JXPath context being validated
* @param String The XPath expression
* @return boolean result of evaluation
*/
protected boolean evalTest(JXPathContext jxpContext, String test) {
Boolean passed = (Boolean) jxpContext.getValue(test,
Boolean.class);
return passed.booleanValue();
}
/**
* @param property name
* @return the property value
* @throws IllegalArgumentException when the property is not supported
*/
public Object getProperty(String property)
throws IllegalArgumentException {
if (property.equals(Validator.PROPERTY_PHASE))
return phaseProperty_;
else
throw new IllegalArgumentException(
" Property " + property + " is not supported");
}
/**
* @param property name
* @param value property value
* @throws IllegalArgumentException when the property is not supported
*/
public void setProperty(String property, Object value)
throws IllegalArgumentException {
if (property.equals(Validator.PROPERTY_PHASE)
&& (value == null || (value instanceof String))) {
phaseProperty_ = (String) value;
} else
throw new IllegalArgumentException(
" Property "
+ property
+ " is not supported or value is
invalid");
}
}
1.1
cocoon-2.1/src/blocks/xmlform/java/org/apache/cocoon/components/validation/schematron/ValidationResult.java
Index: ValidationResult.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, 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 Cocoon" 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
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", 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 (INCLU-
DING, 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 created by
Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.components.validation.schematron;
import org.apache.cocoon.components.validation.Violation;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
/**
* Represents the result of a Schematron validation process
*
* <validationResult>
* list of <pattern> elements with <rule> subelements
* </validationResult>
*
* @author Ivelin Ivanov, [EMAIL PROTECTED], [EMAIL PROTECTED]
* @version CVS $Id: ValidationResult.java,v 1.1 2003/04/25 08:51:11 stephan
Exp $
*/
public class ValidationResult {
private ArrayList patterns_ = new ArrayList();
/**
* Returns a list of the patterns which
* contain rules that failed during validation
*/
public List getPattern() {
return patterns_;
}
/**
* Sets the list of the patterns which
* contain rules that failed during validation
*/
public void setPattern(Collection newPatterns) {
patterns_ = new ArrayList();
patterns_.addAll(newPatterns);
}
/**
* Add a pattern to the list
*/
public void addPattern(Pattern p) {
patterns_.add(p);
}
public boolean isEmpty() {
return patterns_.isEmpty();
}
/**
* adds all errors to a sorted list
* Key is XPath of each error location
* @return SortedSet
*/
public List toList() {
if (isEmpty())
return null;
List violations = new LinkedList();
Iterator piter = getPattern().iterator();
while (piter.hasNext()) {
Pattern pattern = (Pattern) piter.next();
// System.out.println("Pattern name: " +
pattern.getName() + ", id: " + pattern.getId() );
Iterator ruleIter = pattern.getRule().iterator();
while (ruleIter.hasNext()) {
Rule rule = (Rule) ruleIter.next();
// System.out.println(" Rule name: " +
rule.getContext() );
Iterator assertIter =
rule.getAssert().iterator();
while (assertIter.hasNext()) {
Assert assertion = (Assert)
assertIter.next();
// add the next assert to the
violations set
Violation v = new Violation();
v.setPath(rule.getContext());
v.setMessage(assertion.getMessage());
violations.add(v);
// System.out.println(" Assert
test: " + assertion.getTest() + ", message: " + assertion.getMessage() );
}
Iterator reportIter =
rule.getReport().iterator();
while (reportIter.hasNext()) {
Report report = (Report)
reportIter.next();
// add the next report to the
violations set
Violation v = new Violation();
v.setPath(rule.getContext());
v.setMessage(report.getMessage());
violations.add(v);
// System.out.println(" Report
test: " + report.getTest() + ", message: " + report.getMessage() );
}
}
}
return violations;
}
}