1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.contract; |
17 | |
|
18 | |
import java.io.BufferedReader; |
19 | |
import java.io.ByteArrayInputStream; |
20 | |
import java.io.ByteArrayOutputStream; |
21 | |
import java.io.File; |
22 | |
import java.io.FileNotFoundException; |
23 | |
import java.io.FileReader; |
24 | |
import java.io.IOException; |
25 | |
import java.io.InputStream; |
26 | |
import java.io.InputStreamReader; |
27 | |
import java.io.StringReader; |
28 | |
import java.io.UnsupportedEncodingException; |
29 | |
import java.net.URL; |
30 | |
import java.net.URLConnection; |
31 | |
import java.util.ArrayList; |
32 | |
import java.util.Collection; |
33 | |
|
34 | |
import javax.xml.parsers.DocumentBuilder; |
35 | |
import javax.xml.parsers.DocumentBuilderFactory; |
36 | |
import javax.xml.parsers.ParserConfigurationException; |
37 | |
import javax.xml.transform.stream.StreamSource; |
38 | |
|
39 | |
import org.w3c.dom.Document; |
40 | |
import org.xml.sax.SAXException; |
41 | |
|
42 | |
public class ContractReader { |
43 | |
|
44 | |
private String contractText; |
45 | |
private String contractPath; |
46 | |
private URL url; |
47 | |
private String jsessionId; |
48 | |
private boolean nested; |
49 | |
|
50 | |
|
51 | |
public ContractReader(URL url, String jsessionId) throws IOException { |
52 | 0 | this(url, jsessionId, false); |
53 | 0 | return; |
54 | |
} |
55 | |
|
56 | |
|
57 | 0 | public ContractReader(URL url, String jsessionId, boolean nested) throws IOException { |
58 | 0 | this.nested = nested; |
59 | 0 | this.contractPath = url.toString(); |
60 | 0 | this.url = url; |
61 | 0 | this.jsessionId = jsessionId; |
62 | |
|
63 | 0 | URLConnection connection = url.openConnection(); |
64 | 0 | connection.setRequestProperty("Cookie", "JSESSIONID=" + jsessionId); |
65 | |
|
66 | 0 | InputStreamReader myReader = new InputStreamReader(connection.getInputStream()); |
67 | 0 | BufferedReader reader = new BufferedReader(myReader); |
68 | |
|
69 | 0 | contractText = trimContract(reader); |
70 | 0 | return; |
71 | |
} |
72 | |
|
73 | |
|
74 | 0 | public ContractReader(File file) throws FileNotFoundException, IOException { |
75 | 0 | contractPath = file.getCanonicalPath(); |
76 | 0 | FileReader fileReader = new FileReader(file); |
77 | 0 | BufferedReader reader = new BufferedReader(fileReader); |
78 | |
|
79 | 0 | contractText = trimContract(reader); |
80 | 0 | } |
81 | |
|
82 | |
|
83 | |
public Document getDocument() throws ParserConfigurationException, UnsupportedEncodingException, IOException, SAXException { |
84 | 0 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
85 | 0 | DocumentBuilder builder = factory.newDocumentBuilder(); |
86 | |
|
87 | 0 | return builder.parse(new ByteArrayInputStream(contractText.getBytes("UTF-8"))); |
88 | |
} |
89 | |
|
90 | |
|
91 | |
public StreamSource getStreamSource() { |
92 | 0 | StringReader stringReader = new StringReader(contractText); |
93 | 0 | return new StreamSource(stringReader); |
94 | |
} |
95 | |
|
96 | |
|
97 | |
public String getText() { |
98 | 0 | return (this.contractText); |
99 | |
} |
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
protected String trimContract(BufferedReader reader) throws IOException { |
117 | |
|
118 | 0 | StringBuilder builder = new StringBuilder(); |
119 | |
String line; |
120 | 0 | boolean inContract = false; |
121 | 0 | boolean linkPage = false; |
122 | 0 | Collection <String>linkedPages = new ArrayList<String>(); |
123 | 0 | boolean p = false; |
124 | |
|
125 | 0 | if (!this.nested) { |
126 | 0 | builder.append("<contents>\n"); |
127 | |
} |
128 | |
|
129 | 0 | while ((line = reader.readLine()) != null) { |
130 | 0 | if (line.contains("ServiceOperationStart")) { |
131 | 0 | inContract = true; |
132 | 0 | continue; |
133 | |
} |
134 | |
|
135 | 0 | if (line.contains("ServiceOperationStop")) { |
136 | 0 | inContract = false; |
137 | 0 | continue; |
138 | |
} |
139 | |
|
140 | 0 | if (linkPage) { |
141 | 0 | if (line.contains("href=")) { |
142 | 0 | linkedPages.add(parsePageLink(line)); |
143 | |
} |
144 | |
|
145 | 0 | if (line.contains("</div>")) { |
146 | 0 | linkPage = false; |
147 | |
} |
148 | |
|
149 | |
continue; |
150 | |
} |
151 | |
|
152 | |
|
153 | 0 | if (line.contains("</a>Linked Operations</h3>")) { |
154 | 0 | linkPage = true; |
155 | 0 | continue; |
156 | |
} |
157 | |
|
158 | 0 | if (!inContract) { |
159 | 0 | continue; |
160 | |
} |
161 | |
|
162 | 0 | builder.append(line + "\n"); |
163 | |
} |
164 | |
|
165 | 0 | for (String page : linkedPages) { |
166 | 0 | ContractReader cr = new ContractReader(new URL(this.url.getProtocol(), |
167 | |
this.url.getHost(), |
168 | |
this.url.getPort(), |
169 | |
page), |
170 | |
this.jsessionId, true); |
171 | 0 | builder.append(cr.getText()); |
172 | 0 | } |
173 | |
|
174 | 0 | if (!this.nested) { |
175 | 0 | builder.append("</contents>\n"); |
176 | |
} |
177 | |
|
178 | 0 | return (builder.toString()); |
179 | |
} |
180 | |
|
181 | |
|
182 | |
private String parsePageLink(String line) { |
183 | 0 | String[] tok = line.split("href="); |
184 | 0 | if (tok.length < 2) { |
185 | 0 | return (""); |
186 | |
} |
187 | |
|
188 | 0 | tok = tok[1].split("\""); |
189 | 0 | return (tok[1]); |
190 | |
} |
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
public String getContractText() { |
198 | 0 | return contractText; |
199 | |
} |
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
public String getContractPath() { |
207 | 0 | return contractPath; |
208 | |
} |
209 | |
} |