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