View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
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, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package com.isenshi.util.extlib;
17  
18  import java.util.Iterator;
19  
20  import org.seasar.mayaa.engine.Page;
21  import org.seasar.mayaa.engine.processor.ProcessStatus;
22  import org.seasar.mayaa.engine.processor.ProcessorProperty;
23  import org.seasar.mayaa.engine.specification.NodeAttribute;
24  import org.seasar.mayaa.engine.specification.PrefixAwareName;
25  import org.seasar.mayaa.engine.specification.QName;
26  import org.seasar.mayaa.engine.specification.SpecificationNode;
27  import org.seasar.mayaa.impl.cycle.CycleUtil;
28  import org.seasar.mayaa.impl.engine.EngineUtil;
29  import org.seasar.mayaa.impl.engine.processor.AbstractAttributableProcessor;
30  import org.seasar.mayaa.impl.engine.processor.AttributeProcessor;
31  import org.seasar.mayaa.impl.engine.processor.ProcessorPropertyImpl;
32  
33  import com.isenshi.util.CharUtil;
34  import com.isenshi.util.PathUtils;
35  
36  /***
37   * 
38   * <tgw:replace name="" function="">
39   * 
40   * @author someda
41   */
42  public class TgwReplaceProcessor extends AttributeProcessor {
43  
44  	private static final long serialVersionUID = 145560631960429501L;
45  		
46  	private static final String FUNCTION_URLENCODE = "urlencode";
47  	
48  	
49  	private PrefixAwareName name;		
50  	
51  	private String function;			
52  						
53  	public PrefixAwareName getName() {
54  		return name;
55  	}
56  
57  	public void setName(PrefixAwareName name) {
58  		this.name = name;
59  	}
60  	
61  	public String getFunction() {
62  		return function;
63  	}
64  
65  	public void setFunction(String function) {
66  		this.function = function;
67  	}
68  
69  	public ProcessStatus doStartProcess(Page topLevelPage){		
70  		if(name == null){
71  			throw new IllegalStateException();
72  		}
73  
74          AbstractAttributableProcessor parent = findParentAttributable();
75  
76          QName currentQName = name.getQName();
77          String localName = currentQName.getLocalName();
78          String originalValue = null;
79  
80          SpecificationNode node =parent.getOriginalNode();
81          for(Iterator i =node.iterateAttribute();i.hasNext();){
82          	NodeAttribute attr = (NodeAttribute) i.next();
83          	String attrLocalName = attr.getQName().getLocalName();
84          	if(localName.equalsIgnoreCase(attrLocalName)){
85          		originalValue=attr.getValue();
86          		break;
87          	}        	
88          }
89                
90          String contextPath = CycleUtil.getRequestScope().getContextPath();
91          String sourcePath = EngineUtil.getSourcePath(getParentProcessor());        
92          String basePath = contextPath + sourcePath;
93           
94          String replaceValue = null;
95          if(FUNCTION_URLENCODE.equals(function)){
96              String sitePath = PathUtils.getRootPath(sourcePath);
97              replaceValue = contextPath + "/" + sitePath + "/" + CharUtil.urlEncode(originalValue);        	        
98          }else{
99          	replaceValue = originalValue;
100         }        
101         				
102         ProcessorProperty prop = new ProcessorPropertyImpl(name,replaceValue,String.class);
103 		parent.addProcesstimeProperty(new ProcessorPropertyWrapper(name,prop,basePath));		
104 		
105 		return ProcessStatus.SKIP_BODY;
106 	}
107 		
108 }