KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > transformation > ValidatingTransformer


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.transformation;
17
18 import java.io.IOException JavaDoc;
19 import java.io.Serializable JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.apache.avalon.framework.activity.Disposable;
23 import org.apache.avalon.framework.configuration.Configurable;
24 import org.apache.avalon.framework.configuration.Configuration;
25 import org.apache.avalon.framework.configuration.ConfigurationException;
26 import org.apache.avalon.framework.parameters.Parameters;
27 import org.apache.avalon.framework.service.ServiceException;
28 import org.apache.avalon.framework.service.ServiceManager;
29 import org.apache.avalon.framework.service.Serviceable;
30 import org.apache.cocoon.ProcessingException;
31 import org.apache.cocoon.caching.CacheableProcessingComponent;
32 import org.apache.cocoon.components.validation.ValidationHandler;
33 import org.apache.cocoon.components.validation.Validator;
34 import org.apache.cocoon.environment.SourceResolver;
35 import org.apache.cocoon.xml.ContentHandlerWrapper;
36 import org.apache.cocoon.xml.XMLConsumer;
37 import org.apache.cocoon.xml.XMLMulticaster;
38 import org.apache.excalibur.source.Source;
39 import org.apache.excalibur.source.SourceValidity;
40 import org.xml.sax.SAXException JavaDoc;
41
42 /**
43  * <p>The {@link ValidatingTransformer} provides a very simple {@link Transformer}
44  * validating documents while being processed in a Cocoon pipeline.</p>
45  *
46  * <p>The only defined (but not required) configuration for this component is
47  * <code>&lt;grammar&gt;<i>...string...</i>&lt;/grammar&gt;</code>
48  * indicating the default grammar language of the schemas to use.</p>
49  *
50  * <p>This configuration parameter can be overridden by specifying the
51  * <code>grammar</code> parameter when using this {@link Transformer} in a
52  * pipeline.</p>
53  *
54  * <p>If no grammar is specified (either as a configuration, or a parameter) this
55  * transformer will instruct the {@link Validator} to try and guess the grammar
56  * of the schema being parsed.</p>
57  *
58  * @author <a HREF="mailto:pier@betaversion.org">Pier Fumagalli</a>
59  */

60 public class ValidatingTransformer extends AbstractTransformer
61 implements Configurable, Serviceable, Disposable, CacheableProcessingComponent {
62
63     /** <p>The configured {@link ServiceManager} instance.</p> */
64     private ServiceManager serviceManager = null;
65     /** <p>The configured {@link Validator} instance.</p> */
66     private Validator validator = null;
67     /** <p>The configured default grammar language.</p> */
68     private String JavaDoc grammar = null;
69
70     /** <p>The {@link ValidationHandler} to use in this transformation.</p> */
71     private ValidationHandler handler = null;
72     /** <p>A unique key identifying the schema source for caching.</p> */
73     private String JavaDoc key = null;
74
75     /**
76      * <p>Create a new {@link ValidatingTransformer} instance.</p>
77      */

78     public ValidatingTransformer() {
79         super();
80     }
81
82     /**
83      * <p>Contextualize this component instance specifying its associated
84      * {@link ServiceManager} instance.</p>
85      *
86      * @param manager the {@link ServiceManager} to associate with this component.
87      * @throws ServiceException if a dependancy of this could not be resolved.
88      */

89     public void service(ServiceManager manager)
90     throws ServiceException {
91         this.serviceManager = manager;
92         this.validator = (Validator) manager.lookup(Validator.ROLE);
93     }
94
95     /**
96      * <p>Configure this component instance.</p>
97      *
98      * <p>The only defined (but not required) configuration for this component is
99      * <code>&lt;grammar&gt;<i>...string...</i>&lt;/grammar&gt;</code>
100      * indicating the default grammar used by this transformer used for parsing
101      * schemas.</p>
102      *
103      * @param configuration a {@link Configuration} instance for this component.
104      * @throws ConfigurationException never thrown.
105      */

106     public void configure(Configuration configuration)
107     throws ConfigurationException {
108         this.grammar = configuration.getChild("grammar").getValue(null);
109     }
110
111     /**
112      * <p>Dispose of this component instance releasing all previously acquired
113      * required instances back to the {@link ServiceManager}.</p>
114      */

115     public void dispose() {
116         this.serviceManager.release(this.validator);
117     }
118
119     /**
120      * <p>Contextualize this component in the scope of a pipeline when a request
121      * is processed.</p>
122      *
123      * @param resolver the {@link SourceResolver} contextualized in this request.
124      * @param objectModel unused.
125      * @param source the source URI of the schema to validate against.
126      * @param parameters unused.
127      */

128     public void setup(SourceResolver resolver, Map JavaDoc objectModel, String JavaDoc source,
129                       Parameters parameters)
130     throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
131         Source s = null;
132         try {
133             s = resolver.resolveURI(source);
134             String JavaDoc g = parameters.getParameter("grammar", this.grammar);
135             if (g == null) {
136                 this.handler = this.validator.getValidationHandler(s);
137             } else{
138                 this.handler = this.validator.getValidationHandler(s, g);
139             }
140         } finally {
141             if (source != null) resolver.release(s);
142         }
143     }
144
145     /**
146      * <p>Specify the {@link XMLConsumer} receiving SAX events emitted by this
147      * {@link Transformer} instance in the scope of a request.</p>
148      *
149      * @param consumer the {@link XMLConsumer} to send SAX events to.
150      */

151     public void setConsumer(XMLConsumer consumer) {
152         XMLConsumer handler = new ContentHandlerWrapper(this.handler, this.handler);
153         super.setConsumer(new XMLMulticaster(handler, consumer));
154     }
155
156     /**
157      * <p>Return the unique key to associated with the schema being processed in
158      * the scope of the request being processed for caching.</p>
159      *
160      * @return a non null {@link String} representing the unique key for the schema.
161      */

162     public Serializable JavaDoc getKey() {
163         return this.key;
164     }
165
166     /**
167      * <p>Return the {@link SourceValidity} associated with the schema currently
168      * being processed in the scope of the request being processed.</p>
169      *
170      * @return a non null {@link SourceValidity} instance.
171      */

172     public SourceValidity getValidity() {
173         return this.handler.getValidity();
174     }
175
176     /**
177      * <p>Recycle this component instance at the end of request processing.</p>
178      */

179     public void recycle() {
180         this.handler = null;
181         this.key = null;
182         super.recycle();
183     }
184 }
185
Popular Tags