refactory: added UI removed from core plugin.
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / wizards / html / StringDivider.java
1 /*
2  * $Id: StringDivider.java,v 1.2 2006-10-21 23:18:43 pombredanne Exp $
3  * Copyright Narushima Hironori. All rights reserved.
4  */
5 package net.sourceforge.phpeclipse.wizards.html;
6
7 import java.util.Arrays;
8 import java.util.regex.Pattern;
9
10 public class StringDivider {
11
12         static Pattern tagNameChoosePattern = Pattern
13                         .compile("<[\\s/]*(\\w+)\\s*.*>");
14
15         String[] splitRegexpCandidates = { "\t", ",", "\\s", "\\s+", };
16
17         public StringDivider() {
18         }
19
20         public String[][] divide(String content) {
21                 return divide(content, getDivideSuitedRegexp(content));
22         }
23
24         public String[][] divide(String content, String regexp) {
25                 String[] lines = content.split("\n");
26                 int len = lines.length;
27                 String[][] dist = new String[len][];
28
29                 int max = Integer.MIN_VALUE;
30                 for (int i = 0; i < len; i++) {
31                         String line = lines[i];
32                         String[] cells = line.split(regexp);
33                         dist[i] = cells;
34                         if (max < cells.length) {
35                                 max = cells.length;
36                         }
37                 }
38                 for (int i = 0; i < len; i++) {
39                         String[] newArray = new String[max];
40                         Arrays.fill(newArray, "");
41                         System.arraycopy(dist[i], 0, newArray, 0, dist[i].length);
42                         dist[i] = newArray;
43                 }
44                 return dist;
45         }
46
47         public String getDivideSuitedRegexp(String content) {
48                 String[] lines = content.split("\n");
49
50                 String resultRegexp = null;
51                 int score = Integer.MAX_VALUE, cellCount = Integer.MIN_VALUE;
52
53                 for (int i = 0; i < splitRegexpCandidates.length; i++) {
54                         String regexp = splitRegexpCandidates[i];
55                         int max = Integer.MIN_VALUE, min = Integer.MAX_VALUE;
56                         for (int j = 0; j < lines.length; j++) {
57                                 String[] vals = lines[j].split(regexp);
58                                 if (max < vals.length) {
59                                         max = vals.length;
60                                 }
61                                 if (min > vals.length) {
62                                         min = vals.length;
63                                 }
64                         }
65                         int s = max - min;
66                         if (score > s || (score == s && max > cellCount)) {
67                                 cellCount = max;
68                                 score = s;
69                                 resultRegexp = regexp;
70                         }
71                 }
72                 return resultRegexp;
73         }
74
75 }