Mercurial > public > html2wiki
annotate src/org/nwoca/ssdt/tools/html2wiki/ReplaceTransformer.java @ 11:8f23347884c0
Deal with double dashes surrounded by words. ReplaceTransformer can now replace specific group.
author | smith@nwoca.org |
---|---|
date | Fri, 28 Jan 2011 13:57:25 -0500 |
parents | 22ed6d93442c |
children |
rev | line source |
---|---|
0
f8b1ea49d065
Initial version of crude HTML to WikiText converter. Customized for converting HTML files from DEC Document into Wiki markup.
smith@nwoca.org
parents:
diff
changeset
|
1 package org.nwoca.ssdt.tools.html2wiki; |
f8b1ea49d065
Initial version of crude HTML to WikiText converter. Customized for converting HTML files from DEC Document into Wiki markup.
smith@nwoca.org
parents:
diff
changeset
|
2 |
f8b1ea49d065
Initial version of crude HTML to WikiText converter. Customized for converting HTML files from DEC Document into Wiki markup.
smith@nwoca.org
parents:
diff
changeset
|
3 import java.util.regex.*; |
f8b1ea49d065
Initial version of crude HTML to WikiText converter. Customized for converting HTML files from DEC Document into Wiki markup.
smith@nwoca.org
parents:
diff
changeset
|
4 |
f8b1ea49d065
Initial version of crude HTML to WikiText converter. Customized for converting HTML files from DEC Document into Wiki markup.
smith@nwoca.org
parents:
diff
changeset
|
5 class ReplaceTransformer implements Transformer { |
f8b1ea49d065
Initial version of crude HTML to WikiText converter. Customized for converting HTML files from DEC Document into Wiki markup.
smith@nwoca.org
parents:
diff
changeset
|
6 |
f8b1ea49d065
Initial version of crude HTML to WikiText converter. Customized for converting HTML files from DEC Document into Wiki markup.
smith@nwoca.org
parents:
diff
changeset
|
7 private Pattern replace; |
f8b1ea49d065
Initial version of crude HTML to WikiText converter. Customized for converting HTML files from DEC Document into Wiki markup.
smith@nwoca.org
parents:
diff
changeset
|
8 private String replacement; |
11
8f23347884c0
Deal with double dashes surrounded by words. ReplaceTransformer can now replace specific group.
smith@nwoca.org
parents:
4
diff
changeset
|
9 private int replaceGroup; |
8f23347884c0
Deal with double dashes surrounded by words. ReplaceTransformer can now replace specific group.
smith@nwoca.org
parents:
4
diff
changeset
|
10 |
8f23347884c0
Deal with double dashes surrounded by words. ReplaceTransformer can now replace specific group.
smith@nwoca.org
parents:
4
diff
changeset
|
11 /** |
8f23347884c0
Deal with double dashes surrounded by words. ReplaceTransformer can now replace specific group.
smith@nwoca.org
parents:
4
diff
changeset
|
12 * ReplaceTransform to replace the entire regex. |
8f23347884c0
Deal with double dashes surrounded by words. ReplaceTransformer can now replace specific group.
smith@nwoca.org
parents:
4
diff
changeset
|
13 * |
8f23347884c0
Deal with double dashes surrounded by words. ReplaceTransformer can now replace specific group.
smith@nwoca.org
parents:
4
diff
changeset
|
14 * @param regex |
8f23347884c0
Deal with double dashes surrounded by words. ReplaceTransformer can now replace specific group.
smith@nwoca.org
parents:
4
diff
changeset
|
15 * @param replacement |
8f23347884c0
Deal with double dashes surrounded by words. ReplaceTransformer can now replace specific group.
smith@nwoca.org
parents:
4
diff
changeset
|
16 */ |
0
f8b1ea49d065
Initial version of crude HTML to WikiText converter. Customized for converting HTML files from DEC Document into Wiki markup.
smith@nwoca.org
parents:
diff
changeset
|
17 public ReplaceTransformer(String regex, String replacement) { |
11
8f23347884c0
Deal with double dashes surrounded by words. ReplaceTransformer can now replace specific group.
smith@nwoca.org
parents:
4
diff
changeset
|
18 this(regex,replacement,0); |
8f23347884c0
Deal with double dashes surrounded by words. ReplaceTransformer can now replace specific group.
smith@nwoca.org
parents:
4
diff
changeset
|
19 } |
8f23347884c0
Deal with double dashes surrounded by words. ReplaceTransformer can now replace specific group.
smith@nwoca.org
parents:
4
diff
changeset
|
20 /** |
8f23347884c0
Deal with double dashes surrounded by words. ReplaceTransformer can now replace specific group.
smith@nwoca.org
parents:
4
diff
changeset
|
21 * ReplaceTransform to replace a specific group in the Regex. |
8f23347884c0
Deal with double dashes surrounded by words. ReplaceTransformer can now replace specific group.
smith@nwoca.org
parents:
4
diff
changeset
|
22 * |
8f23347884c0
Deal with double dashes surrounded by words. ReplaceTransformer can now replace specific group.
smith@nwoca.org
parents:
4
diff
changeset
|
23 * @param regex |
8f23347884c0
Deal with double dashes surrounded by words. ReplaceTransformer can now replace specific group.
smith@nwoca.org
parents:
4
diff
changeset
|
24 * @param replacement |
8f23347884c0
Deal with double dashes surrounded by words. ReplaceTransformer can now replace specific group.
smith@nwoca.org
parents:
4
diff
changeset
|
25 * @param replaceGroup group to replace |
8f23347884c0
Deal with double dashes surrounded by words. ReplaceTransformer can now replace specific group.
smith@nwoca.org
parents:
4
diff
changeset
|
26 */ |
8f23347884c0
Deal with double dashes surrounded by words. ReplaceTransformer can now replace specific group.
smith@nwoca.org
parents:
4
diff
changeset
|
27 |
8f23347884c0
Deal with double dashes surrounded by words. ReplaceTransformer can now replace specific group.
smith@nwoca.org
parents:
4
diff
changeset
|
28 public ReplaceTransformer(String regex, String replacement, int replaceGroup) { |
0
f8b1ea49d065
Initial version of crude HTML to WikiText converter. Customized for converting HTML files from DEC Document into Wiki markup.
smith@nwoca.org
parents:
diff
changeset
|
29 replace = Pattern.compile(regex); |
f8b1ea49d065
Initial version of crude HTML to WikiText converter. Customized for converting HTML files from DEC Document into Wiki markup.
smith@nwoca.org
parents:
diff
changeset
|
30 this.replacement = replacement; |
11
8f23347884c0
Deal with double dashes surrounded by words. ReplaceTransformer can now replace specific group.
smith@nwoca.org
parents:
4
diff
changeset
|
31 this.replaceGroup = replaceGroup; |
0
f8b1ea49d065
Initial version of crude HTML to WikiText converter. Customized for converting HTML files from DEC Document into Wiki markup.
smith@nwoca.org
parents:
diff
changeset
|
32 } |
11
8f23347884c0
Deal with double dashes surrounded by words. ReplaceTransformer can now replace specific group.
smith@nwoca.org
parents:
4
diff
changeset
|
33 |
0
f8b1ea49d065
Initial version of crude HTML to WikiText converter. Customized for converting HTML files from DEC Document into Wiki markup.
smith@nwoca.org
parents:
diff
changeset
|
34 public void apply(StringBuffer buffer) { |
f8b1ea49d065
Initial version of crude HTML to WikiText converter. Customized for converting HTML files from DEC Document into Wiki markup.
smith@nwoca.org
parents:
diff
changeset
|
35 Matcher matcher = replace.matcher(buffer); |
f8b1ea49d065
Initial version of crude HTML to WikiText converter. Customized for converting HTML files from DEC Document into Wiki markup.
smith@nwoca.org
parents:
diff
changeset
|
36 |
11
8f23347884c0
Deal with double dashes surrounded by words. ReplaceTransformer can now replace specific group.
smith@nwoca.org
parents:
4
diff
changeset
|
37 int start = 0; |
8f23347884c0
Deal with double dashes surrounded by words. ReplaceTransformer can now replace specific group.
smith@nwoca.org
parents:
4
diff
changeset
|
38 while (matcher.find(start)) { |
8f23347884c0
Deal with double dashes surrounded by words. ReplaceTransformer can now replace specific group.
smith@nwoca.org
parents:
4
diff
changeset
|
39 if (replaceGroup == 0) { |
8f23347884c0
Deal with double dashes surrounded by words. ReplaceTransformer can now replace specific group.
smith@nwoca.org
parents:
4
diff
changeset
|
40 buffer.replace(matcher.start(),matcher.end(),replacement); |
8f23347884c0
Deal with double dashes surrounded by words. ReplaceTransformer can now replace specific group.
smith@nwoca.org
parents:
4
diff
changeset
|
41 start = matcher.start() + replacement.length(); |
8f23347884c0
Deal with double dashes surrounded by words. ReplaceTransformer can now replace specific group.
smith@nwoca.org
parents:
4
diff
changeset
|
42 } else { |
8f23347884c0
Deal with double dashes surrounded by words. ReplaceTransformer can now replace specific group.
smith@nwoca.org
parents:
4
diff
changeset
|
43 String t = ""; |
8f23347884c0
Deal with double dashes surrounded by words. ReplaceTransformer can now replace specific group.
smith@nwoca.org
parents:
4
diff
changeset
|
44 for (int i = 1; i < matcher.groupCount() + 1 ; i++) { |
8f23347884c0
Deal with double dashes surrounded by words. ReplaceTransformer can now replace specific group.
smith@nwoca.org
parents:
4
diff
changeset
|
45 if (i != replaceGroup) { |
8f23347884c0
Deal with double dashes surrounded by words. ReplaceTransformer can now replace specific group.
smith@nwoca.org
parents:
4
diff
changeset
|
46 t += matcher.group(i); |
8f23347884c0
Deal with double dashes surrounded by words. ReplaceTransformer can now replace specific group.
smith@nwoca.org
parents:
4
diff
changeset
|
47 } else { |
8f23347884c0
Deal with double dashes surrounded by words. ReplaceTransformer can now replace specific group.
smith@nwoca.org
parents:
4
diff
changeset
|
48 t += replacement; |
8f23347884c0
Deal with double dashes surrounded by words. ReplaceTransformer can now replace specific group.
smith@nwoca.org
parents:
4
diff
changeset
|
49 } |
8f23347884c0
Deal with double dashes surrounded by words. ReplaceTransformer can now replace specific group.
smith@nwoca.org
parents:
4
diff
changeset
|
50 } |
8f23347884c0
Deal with double dashes surrounded by words. ReplaceTransformer can now replace specific group.
smith@nwoca.org
parents:
4
diff
changeset
|
51 buffer.replace(matcher.start(),matcher.end(),t); |
8f23347884c0
Deal with double dashes surrounded by words. ReplaceTransformer can now replace specific group.
smith@nwoca.org
parents:
4
diff
changeset
|
52 start = matcher.start() + t.length(); |
8f23347884c0
Deal with double dashes surrounded by words. ReplaceTransformer can now replace specific group.
smith@nwoca.org
parents:
4
diff
changeset
|
53 } |
4
22ed6d93442c
Start modifying transformers to Confluence wiki syntax
smith@nwoca.org
parents:
0
diff
changeset
|
54 } |
0
f8b1ea49d065
Initial version of crude HTML to WikiText converter. Customized for converting HTML files from DEC Document into Wiki markup.
smith@nwoca.org
parents:
diff
changeset
|
55 } |
f8b1ea49d065
Initial version of crude HTML to WikiText converter. Customized for converting HTML files from DEC Document into Wiki markup.
smith@nwoca.org
parents:
diff
changeset
|
56 |
f8b1ea49d065
Initial version of crude HTML to WikiText converter. Customized for converting HTML files from DEC Document into Wiki markup.
smith@nwoca.org
parents:
diff
changeset
|
57 public String toString() { |
f8b1ea49d065
Initial version of crude HTML to WikiText converter. Customized for converting HTML files from DEC Document into Wiki markup.
smith@nwoca.org
parents:
diff
changeset
|
58 return "Replace: " + replace.pattern() + " with " +replacement; |
f8b1ea49d065
Initial version of crude HTML to WikiText converter. Customized for converting HTML files from DEC Document into Wiki markup.
smith@nwoca.org
parents:
diff
changeset
|
59 } |
f8b1ea49d065
Initial version of crude HTML to WikiText converter. Customized for converting HTML files from DEC Document into Wiki markup.
smith@nwoca.org
parents:
diff
changeset
|
60 |
f8b1ea49d065
Initial version of crude HTML to WikiText converter. Customized for converting HTML files from DEC Document into Wiki markup.
smith@nwoca.org
parents:
diff
changeset
|
61 |
f8b1ea49d065
Initial version of crude HTML to WikiText converter. Customized for converting HTML files from DEC Document into Wiki markup.
smith@nwoca.org
parents:
diff
changeset
|
62 } |