1
|
|
/*
|
2
|
|
* JBind
|
3
|
|
*
|
4
|
|
* Copyright (c) by Stefan Wachter. All rights reserved.
|
5
|
|
*
|
6
|
|
* Usage, modification, and redistribution is subject to license terms that are
|
7
|
|
* available at 'http://www.jbind.org'. The JBind license is like the
|
8
|
|
* 'Apache Software License V 1.1'.
|
9
|
|
*/
|
10
|
|
package org.jbind.xml.base;
|
11
|
|
|
12
|
|
import org.jbind.util.other.StrBuffer;
|
13
|
|
|
14
|
|
public class WhiteSpaceProcessing {
|
15
|
|
|
16
|
|
public static WhiteSpaceProcessing NONE = new WhiteSpaceProcessing(0, "none");
|
17
|
|
|
18
|
|
/**
|
19
|
|
* The first level.
|
20
|
|
*/
|
21
|
|
public static WhiteSpaceProcessing PRESERVE = new WhiteSpaceProcessing(1, "preserve");
|
22
|
|
/**
|
23
|
|
* The second level.
|
24
|
|
*/
|
25
|
|
public static WhiteSpaceProcessing REPLACE = new WhiteSpaceProcessing(2, "replace");
|
26
|
|
/**
|
27
|
|
* The third level.
|
28
|
|
*/
|
29
|
|
public static WhiteSpaceProcessing COLLAPSE = new WhiteSpaceProcessing(3, "collapse");
|
30
|
|
|
31
|
|
private int myLevel;
|
32
|
|
private String myName = null;
|
33
|
|
|
34
|
12
|
private WhiteSpaceProcessing(int aLevel, String aName) {
|
35
|
12
|
myLevel = aLevel;
|
36
|
12
|
myName = aName;
|
37
|
|
}
|
38
|
|
|
39
|
|
/**
|
40
|
|
* Checks if this white space processing is after the specified white space processing.
|
41
|
|
*
|
42
|
|
* @param aWSP <i>(required)</i>.
|
43
|
|
*/
|
44
|
0
|
public boolean isAfter(WhiteSpaceProcessing aWSP) {
|
45
|
0
|
return myLevel > aWSP.myLevel;
|
46
|
|
}
|
47
|
|
|
48
|
|
/**
|
49
|
|
* Checks if this white space processing is before the specified white space processing.
|
50
|
|
*
|
51
|
|
* @param aWSP <i>(required)</i>.
|
52
|
|
*/
|
53
|
303
|
public boolean isBefore(WhiteSpaceProcessing aWSP) {
|
54
|
303
|
return myLevel < aWSP.myLevel;
|
55
|
|
}
|
56
|
|
|
57
|
14642
|
public String process(String aString) {
|
58
|
14642
|
String res = null;
|
59
|
14642
|
if (myLevel <= 1) {
|
60
|
1289
|
res = aString;
|
61
|
|
} else {
|
62
|
13353
|
int length = aString.length();
|
63
|
13353
|
StrBuffer sb = new StrBuffer(length);
|
64
|
|
// Suppress leading space if necessary
|
65
|
13353
|
boolean spaceInserted = true;
|
66
|
13353
|
for (int i = 0; i < length; i++) {
|
67
|
4483465
|
char c = aString.charAt(i);
|
68
|
4483465
|
if ((c == '\t') || (c == '\n') || (c == '\r')) {
|
69
|
2184972
|
c = ' ';
|
70
|
|
}
|
71
|
4483465
|
if ((c != ' ') || (myLevel == 2) || !spaceInserted) {
|
72
|
34545
|
sb.append(c);
|
73
|
34545
|
spaceInserted = c == ' ';
|
74
|
|
}
|
75
|
|
}
|
76
|
|
// Remove trailing space if necessary
|
77
|
13353
|
if (myLevel == 3) {
|
78
|
13349
|
int l = sb.length();
|
79
|
13349
|
if ((l > 0) && (sb.charAt(l - 1) == ' ')) {
|
80
|
79
|
sb.deleteCharAt(l - 1);
|
81
|
|
}
|
82
|
|
}
|
83
|
13353
|
res = sb.toString();
|
84
|
|
}
|
85
|
14642
|
return res;
|
86
|
|
}
|
87
|
|
|
88
|
|
/**
|
89
|
|
* Checks if a string satisfies the white space processing.
|
90
|
|
*
|
91
|
|
* @param aString <i>(required)</i>.
|
92
|
|
* @return Returns <code>true</code> iff the string satisfies the white space
|
93
|
|
* processing.
|
94
|
|
*/
|
95
|
6
|
public boolean check(String aString) {
|
96
|
6
|
boolean res = true;
|
97
|
6
|
if (myLevel == 1) {}
|
98
|
|
else {
|
99
|
0
|
boolean hadSpace = true;
|
100
|
0
|
for (int i = aString.length(); res && (--i >= 0); ) {
|
101
|
0
|
char c = aString.charAt(i);
|
102
|
0
|
res = ((c != '\t') && (c != '\n') && (c != '\r'));
|
103
|
0
|
if (res) {
|
104
|
0
|
res = (myLevel != 3) || (c != ' ') || !hadSpace;
|
105
|
|
}
|
106
|
0
|
hadSpace = c == ' ';
|
107
|
|
}
|
108
|
0
|
if (res) {
|
109
|
0
|
res = (myLevel != 3) || (aString.length() == 0) || (aString.charAt(0) != ' ');
|
110
|
|
}
|
111
|
|
}
|
112
|
6
|
return res;
|
113
|
|
}
|
114
|
|
|
115
|
0
|
public String toString() {
|
116
|
0
|
return myName;
|
117
|
|
}
|
118
|
|
}
|
119
|
|
|