2 * $Id: StringDivider.java,v 1.2 2006-10-21 23:18:43 pombredanne Exp $
3 * Copyright Narushima Hironori. All rights reserved.
5 package net.sourceforge.phpeclipse.wizards.html;
7 import java.util.Arrays;
8 import java.util.regex.Pattern;
10 public class StringDivider {
12 static Pattern tagNameChoosePattern = Pattern
13 .compile("<[\\s/]*(\\w+)\\s*.*>");
15 String[] splitRegexpCandidates = { "\t", ",", "\\s", "\\s+", };
17 public StringDivider() {
20 public String[][] divide(String content) {
21 return divide(content, getDivideSuitedRegexp(content));
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][];
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);
34 if (max < cells.length) {
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);
47 public String getDivideSuitedRegexp(String content) {
48 String[] lines = content.split("\n");
50 String resultRegexp = null;
51 int score = Integer.MAX_VALUE, cellCount = Integer.MIN_VALUE;
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) {
61 if (min > vals.length) {
66 if (score > s || (score == s && max > cellCount)) {
69 resultRegexp = regexp;