You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by pi...@apache.org on 2005/09/07 03:19:22 UTC
svn commit: r279201 - in
/cocoon/branches/BRANCH_2_1_X/src/blocks/validation: conf/
java/org/apache/cocoon/components/validation/impl/
java/org/apache/cocoon/transformation/ samples/
Author: pier
Date: Tue Sep 6 18:19:11 2005
New Revision: 279201
URL: http://svn.apache.org/viewcvs?rev=279201&view=rev
Log:
Final fixes, including samples and root sitemap patches.
Added:
cocoon/branches/BRANCH_2_1_X/src/blocks/validation/conf/validation.xmap
cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/transformation/ValidatingTransformer.java
cocoon/branches/BRANCH_2_1_X/src/blocks/validation/samples/validation.xslt
Modified:
cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/CachingSchemaParser.java
cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/DefaultValidator.java
cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/JingContext.java
cocoon/branches/BRANCH_2_1_X/src/blocks/validation/samples/sitemap.xmap
cocoon/branches/BRANCH_2_1_X/src/blocks/validation/samples/welcome.xml
Added: cocoon/branches/BRANCH_2_1_X/src/blocks/validation/conf/validation.xmap
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/validation/conf/validation.xmap?rev=279201&view=auto
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/validation/conf/validation.xmap (added)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/validation/conf/validation.xmap Tue Sep 6 18:19:11 2005
@@ -0,0 +1,32 @@
+<?xml version="1.0"?>
+<!--
+ Copyright 1999-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.
+-->
+
+<xmap xmlns:map="http://apache.org/cocoon/sitemap/1.0"
+ xpath="/sitemap/components/transformers"
+ unless="transformer[@name='validate-relaxng']">
+
+ <map:transformer name="validate-relaxng"
+ logger="sitemap.transformer.validate"
+ src="org.apache.cocoon.transformation.ValidatingTransformer">
+ <!--+ The "language" is either one of the language constants specified
+ | by the org.apache.cocoon.components.validation.Validator class
+ | or the component name with wich the SchemaParser was registered
+ | in "cocoon.xconf" (for example "jing").
+ +-->
+ <language>http://relaxng.org/ns/structure/0.9</language>
+ </map:transformer>
+</xmap>
Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/CachingSchemaParser.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/CachingSchemaParser.java?rev=279201&r1=279200&r2=279201&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/CachingSchemaParser.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/CachingSchemaParser.java Tue Sep 6 18:19:11 2005
@@ -22,6 +22,7 @@
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
+import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.service.ServiceException;
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.avalon.framework.service.Serviceable;
@@ -41,7 +42,7 @@
*
* @author <a href="mailto:pier@betaversion.org">Pier Fumagalli</a>
*/
-public abstract class CachingSchemaParser
+public abstract class CachingSchemaParser extends AbstractLogEnabled
implements Serviceable, Initializable, Disposable, SchemaParser, Configurable {
/** <p>The {@link ServiceManager} configured for this instance.</p> */
@@ -62,7 +63,7 @@
throws ServiceException {
this.serviceManager = manager;
}
-
+
/**
* <p>Configure this instance.</p>
*
@@ -120,11 +121,14 @@
/* If the schema was found verify its validity and optionally clear */
if (schema != null) {
+ super.getLogger().debug("Accessing cached schema " + uri);
validity = schema.getValidity();
if (validity == null) {
+ super.getLogger().debug("Cached schema " + uri + " has no validity");
this.transientStore.remove(key);
schema = null;
} else if (validity.isValid() != SourceValidity.VALID) {
+ super.getLogger().debug("Cached schema " + uri + " is not valid");
this.transientStore.remove(key);
schema = null;
}
@@ -132,6 +136,7 @@
/* If the schema was not cached or was cleared, parse and cache it */
if (schema == null) {
+ super.getLogger().debug("Parsing schema " + uri);
schema = this.parseSchema(uri);
validity = schema.getValidity();
if ((validity != null) && (validity.isValid() == SourceValidity.VALID)) {
Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/DefaultValidator.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/DefaultValidator.java?rev=279201&r1=279200&r2=279201&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/DefaultValidator.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/DefaultValidator.java Tue Sep 6 18:19:11 2005
@@ -40,6 +40,7 @@
import org.apache.avalon.framework.service.ServiceException;
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.avalon.framework.service.Serviceable;
+import org.apache.avalon.framework.thread.ThreadSafe;
import org.apache.cocoon.components.validation.SchemaParser;
import org.apache.cocoon.components.validation.Validator;
@@ -48,7 +49,7 @@
*
* @author <a href="mailto:pier@betaversion.org">Pier Fumagalli</a>
*/
-public class DefaultValidator implements Validator, LogEnabled,
+public class DefaultValidator implements Validator, LogEnabled, ThreadSafe,
Contextualizable, Serviceable, Configurable, Initializable, Disposable {
/** <p>The default shorthand code to use in subcomponent configurations.</p> */
@@ -133,16 +134,23 @@
*/
public void initialize()
throws Exception {
+ this.logger.debug("Initializing " + this.getClass().getName());
+
if (this.logger == null) throw new IllegalStateException("Null logger");
if (this.context == null) throw new IllegalStateException("Null context");
if (this.manager == null) throw new IllegalStateException("Null manager");
if (this.conf == null) throw new IllegalStateException("Null configuration");
Configuration configurations[] = this.conf.getChildren(this.shorthand);
+ this.logger.debug("Configuring " + configurations.length + " schema parsers"
+ + " from " + this.conf.getLocation());
+
for (int x = 0; x < configurations.length; x++) try {
Configuration configuration = configurations[x];
String className = configuration.getAttribute("class");
String selectionKey = configuration.getAttribute("name");
+ this.logger.debug("Configuring schema parser " + selectionKey + " as "
+ + className + " from " + configuration.getLocation());
Class clazz;
try {
@@ -152,7 +160,7 @@
throw new ConfigurationException(message, configuration, exception);
}
- if (!clazz.isAssignableFrom(this.componentClass)) {
+ if (!this.componentClass.isAssignableFrom(clazz)) {
String message = "Class " + className + " does not represent a "
+ this.componentClass.getName();
throw new ConfigurationException(message, configuration);
@@ -168,6 +176,7 @@
this.components.add(this.setupComponent(component, configuration));
this.selections.put(selectionKey, component);
+ this.logger.debug("SchemaParser " + selectionKey + " class" + className);
if (component instanceof SchemaParser) {
SchemaParser parser = (SchemaParser) component;
String languages[] = parser.getSupportedLanguages();
@@ -175,6 +184,8 @@
for (int k = 0; x < languages.length; x++) {
if (this.selections.containsKey(languages[x])) continue;
this.selections.put(languages[x], component);
+ this.logger.debug("SchemaParser " + selectionKey
+ + "provides language " + languages[x]);
}
}
}
@@ -204,7 +215,7 @@
public Object select(Object key)
throws ServiceException {
if (this.isSelectable(key)) return this.selections.get(key);
- throw new ServiceException((String) key, "Non existing component" + key);
+ throw new ServiceException((String) key, "Schema parser not configured");
}
/**
@@ -268,7 +279,7 @@
if (component instanceof Startable)
((Startable) component).start();
started = true;
-
+
return component;
} catch (Exception exception) {
Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/JingContext.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/JingContext.java?rev=279201&r1=279200&r2=279201&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/JingContext.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/JingContext.java Tue Sep 6 18:19:11 2005
@@ -66,6 +66,7 @@
this.validatorProperties = builder.toPropertyMap();
this.sourceResolver = sourceResolver;
this.entityResolver = entityResolver;
+ this.parsedSourceStack.push(null);
}
/**
Added: cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/transformation/ValidatingTransformer.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/transformation/ValidatingTransformer.java?rev=279201&view=auto
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/transformation/ValidatingTransformer.java (added)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/transformation/ValidatingTransformer.java Tue Sep 6 18:19:11 2005
@@ -0,0 +1,162 @@
+/*
+ * Copyright 1999-2005 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 org.apache.cocoon.transformation;
+
+import java.io.IOException;
+import java.io.Serializable;
+import java.util.Map;
+
+import org.apache.avalon.framework.activity.Disposable;
+import org.apache.avalon.framework.configuration.Configurable;
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.ConfigurationException;
+import org.apache.avalon.framework.parameters.Parameters;
+import org.apache.avalon.framework.service.ServiceException;
+import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.avalon.framework.service.Serviceable;
+import org.apache.cocoon.ProcessingException;
+import org.apache.cocoon.caching.CacheableProcessingComponent;
+import org.apache.cocoon.components.validation.Schema;
+import org.apache.cocoon.components.validation.SchemaParser;
+import org.apache.cocoon.components.validation.Validator;
+import org.apache.cocoon.environment.SourceResolver;
+import org.apache.cocoon.xml.ContentHandlerWrapper;
+import org.apache.cocoon.xml.XMLConsumer;
+import org.apache.cocoon.xml.XMLMulticaster;
+import org.apache.excalibur.source.SourceValidity;
+import org.xml.sax.SAXException;
+
+/**
+ * <p>The {@link ValidatingTransformer} provides a very simple {@link Transformer}
+ * validating documents while being processed in a Cocoon pipeline.</p>
+ *
+ * <p>The only defined (but not required) configuration for this component is
+ * <code><language><i>...string...</i></language></code>
+ * indicating the language (or optionally the component name) for the
+ * {@link Validator} instance providing access to {@link Schema}s.</p>
+ *
+ * @author <a href="mailto:pier@betaversion.org">Pier Fumagalli</a>
+ */
+public class ValidatingTransformer extends AbstractTransformer
+implements Configurable, Serviceable, Disposable, CacheableProcessingComponent {
+
+ private ServiceManager serviceManager = null;
+ private Validator validator = null;
+ private SchemaParser schemaParser = null;
+ private Schema schema = null;
+ private String key = null;
+
+ /**
+ * <p>Contextualize this component instance specifying its associated
+ * {@link ServiceManager} instance.</p>
+ *
+ * @param manager the {@link ServiceManager} to associate with this component.
+ * @throws ServiceException if a dependancy of this could not be resolved.
+ */
+ public void service(ServiceManager manager)
+ throws ServiceException {
+ this.serviceManager = manager;
+ this.validator = (Validator) manager.lookup(Validator.ROLE);
+ }
+
+ /**
+ * <p>Configure this component instance.</p>
+ *
+ * <p>The only defined (but not required) configuration for this component is
+ * <code><language><i>...string...</i></language></code>
+ * indicating the language (or optionally the component name) for the
+ * {@link Validator} instance providing access to {@link Schema}s.</p>
+ *
+ * @param configuration a {@link Configuration} instance for this component.
+ * @throws ConfigurationException never thrown.
+ */
+ public void configure(Configuration configuration)
+ throws ConfigurationException {
+ String key = configuration.getChild("language").getValue();
+ try {
+ this.schemaParser = (SchemaParser) this.validator.select(key);
+ } catch (ServiceException exception) {
+ String message = "Language or instance \"" + key + "\" not recognized";
+ throw new ConfigurationException(message, configuration, exception);
+ }
+ }
+
+ /**
+ * <p>Dispose of this component instance releasing all previously acquired
+ * required instances back to the {@link ServiceManager}.</p>
+ */
+ public void dispose() {
+ this.validator.release(this.schemaParser);
+ this.serviceManager.release(this.validator);
+ }
+
+ /**
+ * <p>Contextualize this component in the scope of a pipeline when a request
+ * is processed.</p>
+ *
+ * @param resolver the {@link SourceResolver} contextualized in this request.
+ * @param objectModel unused.
+ * @param source the source URI of the schema to validate against.
+ * @param parameters unused.
+ */
+ public void setup(SourceResolver resolver, Map objectModel, String source,
+ Parameters parameters)
+ throws ProcessingException, SAXException, IOException {
+ this.schema = this.schemaParser.getSchema(source);
+ this.key = this.getClass().getName() + ":" +
+ this.schemaParser.getClass().getName() + ":" + source;
+ }
+
+ /**
+ * <p>Specify the {@link XMLConsumer} receiving SAX events emitted by this
+ * {@link Transformer} instance in the scope of a request.</p>
+ *
+ * @param consumer the {@link XMLConsumer} to send SAX events to.
+ */
+ public void setConsumer(XMLConsumer consumer) {
+ XMLConsumer handler = new ContentHandlerWrapper(this.schema.newValidator());
+ super.setConsumer(new XMLMulticaster(handler, consumer));
+ }
+
+ /**
+ * <p>Return the unique key to associated with the schema being processed in
+ * the scope of the request being processed for caching.</p>
+ *
+ * @return a non null {@link String} representing the unique key for the schema.
+ */
+ public Serializable getKey() {
+ return this.key;
+ }
+
+ /**
+ * <p>Return the {@link SourceValidity} associated with the schema currently
+ * being processed in the scope of the request being processed.</p>
+ *
+ * @return a non null {@link SourceValidity} instance.
+ */
+ public SourceValidity getValidity() {
+ return this.schema.getValidity();
+ }
+
+ /**
+ * <p>Recycle this component instance at the end of request processing.</p>
+ */
+ public void recycle() {
+ this.schema = null;
+ this.key = null;
+ super.recycle();
+ }
+}
Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/validation/samples/sitemap.xmap
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/validation/samples/sitemap.xmap?rev=279201&r1=279200&r2=279201&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/validation/samples/sitemap.xmap (original)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/validation/samples/sitemap.xmap Tue Sep 6 18:19:11 2005
@@ -38,22 +38,25 @@
<map:serialize/>
</map:match>
- <map:match pattern="jing-transformer-ok">
+ <map:match pattern="validation-ok">
<map:generate src="source-ok.xml"/>
- <map:transform type="jing" src="schema-ok.rng"/>
- <map:serialize type="xml"/>
+ <map:transform type="validate-relaxng" src="schema-ok.rng"/>
+ <map:transform src="validation.xslt"/>
+ <map:serialize/>
</map:match>
- <map:match pattern="jing-transformer-invalid">
+ <map:match pattern="validation-invalid">
<map:generate src="source-no.xml"/>
- <map:transform type="jing" src="schema-ok.rng"/>
- <map:serialize type="xml"/>
+ <map:transform type="validate-relaxng" src="schema-ok.rng"/>
+ <map:transform src="validation.xslt"/>
+ <map:serialize/>
</map:match>
- <map:match pattern="jing-transformer-noschema">
+ <map:match pattern="validation-noschema">
<map:generate src="source-ok.xml"/>
- <map:transform type="jing" src="schema-no.rng"/>
- <map:serialize type="xml"/>
+ <map:transform type="validate-relaxng" src="schema-no.rng"/>
+ <map:transform src="validation.xslt"/>
+ <map:serialize/>
</map:match>
</map:pipeline>
Added: cocoon/branches/BRANCH_2_1_X/src/blocks/validation/samples/validation.xslt
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/validation/samples/validation.xslt?rev=279201&view=auto
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/validation/samples/validation.xslt (added)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/validation/samples/validation.xslt Tue Sep 6 18:19:11 2005
@@ -0,0 +1,31 @@
+<?xml version="1.0"?>
+<!--
+ Copyright 1999-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.
+-->
+<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
+
+ <xsl:template match="/">
+ <html>
+ <head>
+ <title>Validation Successful</title>
+ </head>
+ <body>
+ <p>If you see this message, the in-pipeline validation was successful.</p>
+ <p><a href="welcome">Return to the samples</a></p>
+ </body>
+ </html>
+ </xsl:template>
+
+</xsl:stylesheet>
Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/validation/samples/welcome.xml
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/validation/samples/welcome.xml?rev=279201&r1=279200&r2=279201&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/validation/samples/welcome.xml (original)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/validation/samples/welcome.xml Tue Sep 6 18:19:11 2005
@@ -24,14 +24,14 @@
<sample name="Back" href="..">to Cocoon Blocks Samples main page</sample>
</group>
- <group name="JING Transformer">
- <sample name="Successful validation" href="jing-transformer-ok">
+ <group name="ValidatingTransformer Samples">
+ <sample name="Successful validation" href="validation-ok">
Shows the output of a pipeline with a valid document being processed.
</sample>
- <sample name="Validation failure" href="jing-transformer-invalid">
+ <sample name="Validation failure" href="validation-invalid">
Shows the output of a pipeline with an invalid document being processed.
</sample>
- <sample name="Schema parsing failure" href="jing-transformer-noschema">
+ <sample name="Schema parsing failure" href="validation-noschema">
Shows the output of a pipeline configured with a wrong schema.
</sample>
</group>
@@ -41,9 +41,9 @@
href="http://cocoon.zones.apache.org/daisy/documentation/blocks/validation.html">
On-line documentation for the Validation block.
</sample>
- <sample name="JING Transformer Documentation"
- href="http://cocoon.zones.apache.org/daisy/documentation/components/transformers/jingtransformer.html">
- On-line documentation for the JingTransformer.
+ <sample name="ValidatingTransformer Documentation"
+ href="http://cocoon.zones.apache.org/daisy/documentation/components/transformers/validatingtransformer.html">
+ On-line documentation for the ValidatingTransformer.
</sample>
</group>