Skip to main content
summaryrefslogtreecommitdiffstats
blob: 07a37ab5256a57d486cd23581cef68bb1451c073 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*******************************************************************************
 * Copyright (c) 2009 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.ua.tests.doc.internal.linkchecker;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;

import org.eclipse.help.internal.webapp.servlet.FilterUtils;
import org.eclipse.help.webapp.IFilter;
import org.eclipse.ua.tests.doc.internal.dialogs.SelectTocDialog;

public class OnLoadFilter implements IFilter {
	
	private static long uniqueId = 0;
	
    private class OutFilter extends OutputStream {
		
		private OutputStream out;
		
		private int state = 0;

		private String pathPrefix;
		
		public void updateState(int b) throws IOException {
			if (state == 0 && b == '<') {
				state = 1;
			} else if (state == 1 && (b == 'b' || b == 'B')) {
				state = 2;
			} else if (state == 2 && (b == 'o' || b == 'O')) {
				state = 3;
			} else if (state == 3 && (b == 'd' || b == 'D')) {
				state = 4;
		    } else if (state == 4 && (b == 'y' || b == 'Y')) {
			   state = 20;
			   out.write(b);
			   if (linkProvider.hasNext()) {
			       String location = (String)linkProvider.next(); 
			       String onload = getOnloadText(pathPrefix + location, testKind);
				   out.write(onload.getBytes());
				   //System.out.println("Onload = " + onload);
			   } else {
				   linkProvider = null;
				   String announceComplete = getCompletionText(testKind);
				   out.write(announceComplete.getBytes());
				   //System.out.println("announceComplete = " + announceComplete);
			   }
		    } else if (state == 20 && b == '>') {
			   state = 21;
		    } else if (state == 1 && (b == 'h' || b == 'H')) {
				state = 11;
		    } else if (state == 11 && (b == 'e' || b == 'e')) {
				state = 12;
		    } else if (state == 12 && (b == 'a' || b == 'a')) {
				state = 13;
		    } else if (state == 13 && (b == 'm' || b == 'M')) {
				state = 14;
		    } else if (state == 14 && (b == 'e' || b == 'E')) {
				state = 15;
		    } else if (state == 15 && (b == 's' || b == 'S')) {
				state = 16;
		    } else if (state == 16 && (b == 'e' || b == 'E')) {
				state = 17;
		    } else if (state == 17 && (b == 't' || b == 'T')) {
				state = 20;
		    } else if (state > 0 && state < 20) {
		    	state = 0;
		    }
		}

		private String getOnloadText(String location, int testKind) {
			String onload = " onload = \"";
			if (testKind == SelectTocDialog.FOLLOW_LINKS) {
				onload += "ua_test_doc_record_links();";
			}
			onload += " window.location = '";
			String uniqParam = "?uniq=" + ++uniqueId;
			int anchor = location.indexOf('#');
			if (anchor == -1) {
				onload = onload + location + uniqParam + "'";
			} else {
				onload = onload + location.substring(0, anchor) + uniqParam
						+ location.substring(anchor) + "'";
			}
			onload += '"';
			return onload;
		}
		
		private String getCompletionText(int testKind) {
			if (testKind == SelectTocDialog.FOLLOW_LINKS) {
                return " onload = \"ua_test_doc_check_links();\" ";
			} else {
				return " onload = \"ua_test_doc_complete();\" ";
			}
		}

		public OutFilter(OutputStream out, String prefix) {
			this.out = out;
			this.pathPrefix = prefix;
		}

		public void write(int b) throws IOException {
			updateState(b);
			if (state != 20) {
			    out.write(b);	
			}
		}
		
		public void close() throws IOException {
			out.close();
			super.close();
		}	
	}

	private int testKind;
    
    public OnLoadFilter(int testKind) {
    	this.testKind = testKind;
    }
    
    private static Iterator linkProvider;
	
	protected String getCommentText() {
		return "comment";
	}	

	public OutputStream filter(HttpServletRequest req, OutputStream out) {
		if (linkProvider == null) {
			return out;
		}
		String pathPrefix = FilterUtils.getRelativePathPrefix(req);
		if (pathPrefix.length() >= 4) {
		    return new OutFilter(out, pathPrefix.substring(0, pathPrefix.length() - 4));
		}
		return new OutFilter(out, "PLUGINS_ROOT");
	}

	public static void setLinkProvider(Iterator provider) {
		linkProvider = provider;
	}	

}

Back to the top