KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > treeprocessor > sitemap > SerializeNode


1 /*
2  * Copyright 1999-2004 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.components.treeprocessor.sitemap;
17
18 import java.util.Map JavaDoc;
19
20 import org.apache.avalon.framework.parameters.Parameters;
21 import org.apache.cocoon.Constants;
22 import org.apache.cocoon.components.pipeline.ProcessingPipeline;
23 import org.apache.cocoon.components.treeprocessor.InvokeContext;
24 import org.apache.cocoon.components.treeprocessor.ParameterizableProcessingNode;
25 import org.apache.cocoon.components.treeprocessor.PipelineEventComponentProcessingNode;
26 import org.apache.cocoon.components.treeprocessor.ProcessingNode;
27 import org.apache.cocoon.components.treeprocessor.variables.VariableResolver;
28 import org.apache.cocoon.environment.Environment;
29 import org.apache.cocoon.sitemap.PatternException;
30 /**
31  *
32  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
33  * @author <a HREF="mailto:uv@upaya.co.uk">Upayavira</a>
34  * @version CVS $Id: SerializeNode.java 232400 2005-08-12 22:14:26Z sylvain $
35  */

36 public class SerializeNode extends PipelineEventComponentProcessingNode implements ParameterizableProcessingNode {
37
38     private String JavaDoc serializerName;
39
40     private VariableResolver source;
41
42     private VariableResolver mimeType;
43
44     private int statusCode;
45
46     private Map JavaDoc parameters;
47
48
49     /**
50      * Build a <code>SerializerNode</code> having a name, a mime-type and a status code (HTTP codes).
51      *
52      * @param name the name of the serializer to use.
53      * @param mimeType the mime-type, or <code>null</code> not specified.
54      * @param statusCode the HTTP response status code, or <code>-1</code> if not specified.
55      */

56     public SerializeNode(String JavaDoc name, VariableResolver source, VariableResolver mimeType, int statusCode) throws PatternException {
57         this.serializerName = name;
58         this.source = source;
59         this.mimeType = mimeType;
60         this.statusCode = statusCode;
61     }
62
63     public void setParameters(Map JavaDoc parameterMap) {
64         this.parameters = parameterMap;
65     }
66
67     public final boolean invoke(Environment env, InvokeContext context)
68     throws Exception JavaDoc {
69
70         // Check view
71
if (this.views != null) {
72        
73             //inform the pipeline that we have a branch point
74
context.getProcessingPipeline().informBranchPoint();
75
76             String JavaDoc cocoonView = env.getView();
77             if (cocoonView != null) {
78
79                 // Get view node
80
ProcessingNode viewNode = (ProcessingNode)this.views.get(cocoonView);
81
82                 if (viewNode != null) {
83                     if (getLogger().isInfoEnabled()) {
84                         getLogger().info("Jumping to view " + cocoonView + " from serializer at " + this.getLocation());
85                     }
86                     return viewNode.invoke(env, context);
87                 }
88             }
89         }
90         
91         Map JavaDoc objectModel = env.getObjectModel();
92         ProcessingPipeline pipeline = context.getProcessingPipeline();
93
94         // Perform link translation if requested
95
if (objectModel.containsKey(Constants.LINK_OBJECT)) {
96             pipeline.addTransformer("<translator>", null, Parameters.EMPTY_PARAMETERS, Parameters.EMPTY_PARAMETERS);
97         }
98         
99         if (objectModel.containsKey(Constants.LINK_COLLECTION_OBJECT) && env.isExternal()) {
100             pipeline.addTransformer("<gatherer>", null, Parameters.EMPTY_PARAMETERS, Parameters.EMPTY_PARAMETERS);
101         }
102
103         pipeline.setSerializer(
104             this.serializerName,
105             source.resolve(context, objectModel),
106             VariableResolver.buildParameters(this.parameters, context, objectModel),
107             this.pipelineHints == null
108                 ? Parameters.EMPTY_PARAMETERS
109                 : VariableResolver.buildParameters(this.pipelineHints, context, objectModel),
110             this.mimeType.resolve(context, env.getObjectModel())
111         );
112
113         // Set status code if there is one
114
if (this.statusCode >= 0) {
115             env.setStatus(this.statusCode);
116         }
117
118         if (! context.isBuildingPipelineOnly()) {
119             // Process pipeline
120
return pipeline.process(env);
121
122         } else {
123             // Return true : pipeline is finished.
124
return true;
125         }
126     }
127 }
128
Popular Tags