1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.exolab.castor.xml;
15
16 import java.text.MessageFormat;
17 import java.util.Locale;
18 import java.util.ResourceBundle;
19
20 import org.apache.commons.lang3.ArrayUtils;
21 import org.apache.commons.lang3.StringUtils;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.xml.sax.ContentHandler;
25 import org.xml.sax.SAXException;
26
27
28
29
30
31
32
33 public class CharactersProcessor {
34
35
36
37
38 private static final Log LOG = LogFactory.getLog(CharactersProcessor.class);
39
40
41
42
43 protected static ResourceBundle resourceBundle;
44
45
46
47
48 private final UnmarshalHandler _unmarshalHandler;
49
50 static {
51 resourceBundle = ResourceBundle.getBundle("UnmarshalHandlerMessages", Locale.getDefault());
52 }
53
54
55
56
57
58
59
60
61 public CharactersProcessor(final UnmarshalHandler unmarshalHandler) {
62 _unmarshalHandler = unmarshalHandler;
63 }
64
65 public void compute(char[] ch, int start, int length) throws SAXException {
66 String string = new String(ch, start, length);
67 if (LOG.isTraceEnabled()) {
68 String trace = MessageFormat.format(
69 resourceBundle.getString("unmarshalHandler.log.trace.characters"), new Object[] {string});
70 LOG.trace(trace);
71 }
72
73
74
75 if (_unmarshalHandler.getStrictElementHandler().skipElement()) {
76 return;
77 }
78
79 if (_unmarshalHandler.getStateStack().isEmpty()) {
80 return;
81 }
82
83 if (_unmarshalHandler.getAnyNodeHandler().hasAnyUnmarshaller()) {
84 _unmarshalHandler.getAnyNodeHandler().characters(ch, start, length);
85 return;
86 }
87
88 UnmarshalState state = _unmarshalHandler.getStateStack().getLastState();
89
90 boolean removedTrailingWhitespace = false;
91 boolean removedLeadingWhitespace = false;
92 if (!state.isWhitespacePreserving() && !ArrayUtils.isEmpty(ch)) {
93 removedTrailingWhitespace = Character.isWhitespace(ch[start + length - 1]);
94 removedLeadingWhitespace = Character.isWhitespace(ch[start]);
95 string = string.trim();
96 }
97
98 if (state.getBuffer() == null) {
99 state.setBuffer(new StringBuffer());
100 } else {
101 if (state.isWhitespacePreserving()) {
102 state.setTrailingWhitespaceRemoved(false);
103 state.getBuffer().append(string);
104 return;
105 } else if (StringUtils.isEmpty(string)) {
106 state.setTrailingWhitespaceRemoved(removedTrailingWhitespace);
107 return;
108 } else if (state.isTrailingWhitespaceRemoved() || removedLeadingWhitespace) {
109 state.getBuffer().append(' ');
110 }
111 }
112 state.setTrailingWhitespaceRemoved(removedTrailingWhitespace);
113 state.getBuffer().append(string);
114 }
115 }