StaxUtilities { public static String replaceWhiteSpace(String string) { if(string == null) { return null; } StringBuilder result = new StringBuilder(string); for(int i = 0; i < result.length(); i++) { char c = result.charAt(i); if(c == TAB || c == CARRIAGE_RETURN || c == LINE_FEED) { result.setCharAt(i, SPACE); } } return result.toString(); } private  StaxUtilities(); static String readSimpleTextNodeIfAvailable(XMLStreamReader reader, String namespaceURI, String nodeName); static String readSimpleTextNodeIfAvailable(XMLStreamReader reader, String namespaceURI, String nodeName, WhiteSpaceHandling whiteSpace); static String readText(XMLStreamReader reader); static String readText(XMLStreamReader reader, WhiteSpaceHandling whiteSpace); static String processWhiteSpace(String string, WhiteSpaceHandling whiteSpace); static String collapseWhiteSpace(String string); static String replaceWhiteSpace(String string); static String readAttributeValue(XMLStreamReader reader, String namespaceURI, String name); static String readAttributeValue(XMLStreamReader reader, String namespaceURI, String name, WhiteSpaceHandling whiteSpace); static void writeNamespace(XMLStreamWriter writer, String prefix, String namespaceURI); static NamespaceInfo setNamespace(XMLStreamWriter writer, String prefix, String namespaceURI, String defaultPrefix); static void writeStartElement(XMLStreamWriter writer, String prefix, String namespaceURI, String nodeName); static void writeEmptyElement(XMLStreamWriter writer, String prefix, String namespaceURI, String nodeName); static void writeAttribute(XMLStreamWriter writer, boolean qualified, String prefix, String namespaceURI, String name, String value); static void writeAttribute(XMLStreamWriter writer, boolean qualified, String prefix, String namespaceURI, String name, String value, WhiteSpaceHandling whiteSpace); static void writeAttributeIfNotNull(XMLStreamWriter writer, boolean qualified, String prefix, String namespaceURI, String name, String value); static void writeAttributeIfNotNull(XMLStreamWriter writer, boolean qualified, String prefix, String namespaceURI, String name, String value, WhiteSpaceHandling whiteSpace); @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") static String normalizeNewlines(String input); static void writeSimpleTextNode(XMLStreamWriter writer, String prefix, String namespaceURI, String nodeName, String text, WhiteSpaceHandling whiteSpace, boolean tryUsingCData); static void writeSimpleTextNode(XMLStreamWriter writer, String prefix, String namespaceURI, String nodeName, String text, WhiteSpaceHandling whiteSpace); static void writeSimpleTextNode(XMLStreamWriter writer, String prefix, String namespaceURI, String nodeName, String text); static void writeSimpleTextNodeIfNotNull(XMLStreamWriter writer, String prefix, String namespaceURI, String nodeName, String text, WhiteSpaceHandling whiteSpace, boolean tryUsingCData); static void writeSimpleTextNodeIfNotNull(XMLStreamWriter writer, String prefix, String namespaceURI, String nodeName, String text, WhiteSpaceHandling whiteSpace); static void writeSimpleTextNodeIfNotNull(XMLStreamWriter writer, String prefix, String namespaceURI, String nodeName, String text); static void writeText(XMLStreamWriter writer, String text, WhiteSpaceHandling whiteSpace, boolean tryUsingCData); static void writeText(XMLStreamWriter writer, String text); static String getEventTypeString(int eventType); static String writerStatus(String msg, XMLStreamWriter writer, String namespaceURI); static void logWriter(Logger logger, String msg, XMLStreamWriter writer, String namespaceURI); static String readerStatus(String msg, XMLStreamReader reader); static void logReader(Logger logger, String msg, XMLStreamReader reader); static final String CDATA_END; static final String XML_SCHEMA_INSTANCE_NAMESPACE_URI; static final String XML_SCHEMA_INSTANCE_PREFIX; static final String XML_SCHEMA_INSTANCE_SCHEMA_LOCATION_ATTRIBUTE; static final String NO_PREFIX; }
@Test public void testReplaceWhiteSpace() { String input; String result; String expectedResult; input = null; expectedResult = null; result = StaxUtilities.replaceWhiteSpace(input); assertEquals(expectedResult, result); input = ""; expectedResult = ""; result = StaxUtilities.replaceWhiteSpace(input); assertEquals(expectedResult, result); input = "foobar"; expectedResult = "foobar"; result = StaxUtilities.replaceWhiteSpace(input); assertEquals(expectedResult, result); input = "foo\nbar\nfoobar"; expectedResult = "foo bar foobar"; result = StaxUtilities.replaceWhiteSpace(input); assertEquals(expectedResult, result); input = "foo\r\nbar\r\nfoobar"; expectedResult = "foo bar foobar"; result = StaxUtilities.replaceWhiteSpace(input); assertEquals(expectedResult, result); input = "foo\r\n\r\nbar\r\nfoobar"; expectedResult = "foo bar foobar"; result = StaxUtilities.replaceWhiteSpace(input); assertEquals(expectedResult, result); input = "foo\n\rbar\n\rfoobar"; expectedResult = "foo bar foobar"; result = StaxUtilities.replaceWhiteSpace(input); assertEquals(expectedResult, result); input = "foo\rbar\rfoobar"; expectedResult = "foo bar foobar"; result = StaxUtilities.replaceWhiteSpace(input); assertEquals(expectedResult, result); input = "foo\r\rbar\rfoobar"; expectedResult = "foo bar foobar"; result = StaxUtilities.replaceWhiteSpace(input); assertEquals(expectedResult, result); input = "foo\r\n\r\rbar\rfoobar\r\r\r"; expectedResult = "foo bar foobar "; result = StaxUtilities.replaceWhiteSpace(input); assertEquals(expectedResult, result); input = " foo\r\nb a r\r\nfoo bar \r\n "; expectedResult = " foo b a r foo bar "; result = StaxUtilities.replaceWhiteSpace(input); assertEquals(expectedResult, result); }