KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.avalon.framework.configuration.Configuration;
19 import org.apache.avalon.framework.configuration.ConfigurationException;
20 import org.apache.cocoon.components.treeprocessor.AbstractProcessingNodeBuilder;
21 import org.apache.cocoon.components.treeprocessor.CategoryNode;
22 import org.apache.cocoon.components.treeprocessor.CategoryNodeBuilder;
23 import org.apache.cocoon.components.treeprocessor.LinkedProcessingNodeBuilder;
24 import org.apache.cocoon.components.treeprocessor.ProcessingNode;
25 import org.apache.cocoon.components.treeprocessor.variables.VariableResolverFactory;
26
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29
30 /**
31  *
32  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
33  * @version CVS $Id: RedirectToNodeBuilder.java 30932 2004-07-29 17:35:38Z vgritsenko $
34  */

35
36 public class RedirectToNodeBuilder extends AbstractProcessingNodeBuilder
37   implements LinkedProcessingNodeBuilder {
38
39     private CallNode callNode;
40     private String JavaDoc resourceName;
41
42     /** This builder has no parameters -- return <code>false</code> */
43     protected boolean hasParameters() {
44         return false;
45     }
46
47     public ProcessingNode buildNode(Configuration config) throws Exception JavaDoc {
48         
49         if (((SitemapLanguage)this.treeBuilder).isBuildingErrorHandler()) {
50             throw new ConfigurationException("'map:redirect' is forbidden inside a 'map:handle-errors', at "
51             + config.getLocation());
52         }
53         
54         // Is it a redirect to resource ?
55
this.resourceName = config.getAttribute("resource", null);
56         if (this.resourceName != null) {
57             
58             getLogger().warn("Redirect to resource is deprecated. Use map:call instead at " +
59                 config.getLocation());
60
61             this.callNode = new CallNode();
62             this.treeBuilder.setupNode(this.callNode, config);
63
64             String JavaDoc target = config.getAttribute("target", null);
65             if (target != null) {
66                 Map JavaDoc params = new HashMap JavaDoc(1);
67                 params.put("target", VariableResolverFactory.getResolver(target, this.manager));
68                 this.callNode.setParameters(params);
69             }
70             return this.callNode;
71             
72         } else {
73             ProcessingNode URINode = new RedirectToURINode(
74                 VariableResolverFactory.getResolver(config.getAttribute("uri"), this.manager),
75                 config.getAttributeAsBoolean("session", false),
76                 config.getAttributeAsBoolean("global", false),
77                 config.getAttributeAsBoolean("permanent", false)
78             );
79             return this.treeBuilder.setupNode(URINode, config);
80
81         }
82     }
83
84     public void linkNode() throws Exception JavaDoc {
85
86         if (this.callNode != null) {
87             CategoryNode resources = CategoryNodeBuilder.getCategoryNode(this.treeBuilder, "resources");
88
89             if (resources == null) {
90                 String JavaDoc msg = "This sitemap contains no resources. Cannot redirect at " +
91                     this.callNode.getLocation();
92                 throw new ConfigurationException(msg);
93             }
94
95             this.callNode.setResource(
96                 resources,
97                 this.resourceName
98             );
99         }
100     }
101 }
102
Popular Tags