View Javadoc

1   package org.cateproject.view.mock;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.util.ArrayList;
6   import java.util.List;
7   
8   import javax.servlet.http.HttpServletResponse;
9   
10  import org.apache.commons.logging.Log;
11  import org.apache.commons.logging.LogFactory;
12  import org.apache.velocity.tools.view.tools.LinkTool;
13  import org.cateproject.view.tools.CATELinkTool;
14  
15  import eu.etaxonomy.cdm.model.common.CdmBase;
16  import eu.etaxonomy.cdm.model.common.ICdmBase;
17  import eu.etaxonomy.cdm.model.common.User;
18  
19  public class MockLinkTool extends CATELinkTool {
20  	private static Log log = LogFactory.getLog(MockLinkTool.class);
21  	private File root;
22  	
23  	public void setRoot(File root) {
24  		this.root = root;
25  	}
26  	
27  	public void setResponse(HttpServletResponse response) {
28  		this.response = response;
29  	}
30  	
31  	private String replaceDoWithHtml(String original) {
32  		if(original.endsWith("do")) {
33  			return original.substring(0, original.length() - 2) + "html";
34  		} else {
35  			return original;
36  		}
37  	}
38  	@Override
39  	public LinkTool setSublist(String resource, ICdmBase cdmBase, String subList) {
40  		StringBuffer stringBuffer = new StringBuffer();
41  		stringBuffer.append(resource);
42  		stringBuffer.append("/" + subList + ".html");
43  		return addAuditEvent(setRelative(stringBuffer.toString()),null);
44  	}
45  	
46  	@Override
47  	public LinkTool setResource(String resource, ICdmBase cdmBase) {
48  		StringBuffer stringBuffer = new StringBuffer();
49  		stringBuffer.append(resource);
50  		stringBuffer.append("/index.html");
51  		return addAuditEvent(setRelative(stringBuffer.toString()),null);
52  	}
53  	
54  	@Override
55  	public LinkTool setList(String resource) {
56  		StringBuffer stringBuffer = new StringBuffer();
57  		stringBuffer.append(resource + "/list.html");
58  		return addAuditEvent(setRelative(stringBuffer.toString()),null);
59  	}
60  	
61  	@Override
62  	public String toString() {
63  		String string = super.toString();
64  		
65  		if(string.startsWith("/")) {
66  			return string.substring(1);
67  		} else {
68  			return string;
69  		}
70  	}
71  	
72  	@Override
73  	public LinkTool setURI(String uri) {
74  		return super.setURI(replaceDoWithHtml(uri));
75  	}
76  	
77  	@Override
78      public LinkTool setRelative(String uri)
79      {
80  		try {
81  	      if(uri.startsWith("http://")) return (LinkTool)super.absolute(uri);
82  	      
83            String resource = "target/generated-sources/mock" + uri;
84            File resourceFile = new File(replaceDoWithHtml(resource));
85            return (LinkTool)super.uri(getRelativePath(root,resourceFile));
86  		} catch(Exception e) {
87  			return null;
88  		}
89      }
90      
91      public String getContextURL() {
92      	return ".";
93      }
94      
95      public static String getRelativePath(File home,File f){
96  		File r;
97  		List<String> homelist;
98  		List<String> filelist;
99  		String s;
100         try {
101 		    homelist = getPathList(home);
102 		    filelist = getPathList(f);
103 		    s = matchPathLists(homelist,filelist);
104         } catch(IOException ioe) {
105         	return f.getName();
106         }
107 
108 		return s;
109 	}
110 	
111 	private static List<String> getPathList(File f) throws IOException {
112 		List<String> l = new ArrayList<String>();
113 		File r;
114 		
115 		r = f.getCanonicalFile();
116 		while(r != null) {
117 			l.add(r.getName());
118 			r = r.getParentFile();
119 		}
120 		
121 		return l;
122 	}
123 	
124 	private static String matchPathLists(List<String> r,List<String> f) {
125 		int i;
126 		int j;
127 		String s;
128 		// start at the beginning of the lists
129 		// iterate while both lists are equal
130 		s = "";
131 		i = r.size()-1;
132 		j = f.size()-1;
133 
134 		// first eliminate common root
135 		while((i >= 0)&&(j >= 0)&&(r.get(i).equals(f.get(j)))) {
136 			i--;
137 			j--;
138 		}
139 
140 		// for each remaining level in the home path, add a ..
141 		for(;i>=0;i--) {
142 			s += ".." + "/";
143 		}
144 
145 		// for each level in the file path, add the path
146 		for(;j>=1;j--) {
147 			s += f.get(j) + "/";
148 		}
149 
150 		// file name
151 		s += f.get(j);
152 		return s;
153 	}
154 }